| 1 | # -*- perl -*- | ||
| 2 | |||
| 3 | # | ||
| 4 | # Author: Slaven Rezic | ||
| 5 | # | ||
| 6 | # Copyright (C) 2019 Slaven Rezic. All rights reserved. | ||
| 7 | # This package is free software; you can redistribute it and/or | ||
| 8 | # modify it under the same terms as Perl itself. | ||
| 9 | # | ||
| 10 | # Mail: slaven@rezic.de | ||
| 11 | # WWW: http://www.rezic.de/eserte/ | ||
| 12 | # | ||
| 13 | |||
| 14 | package Doit::Bootstrap; | ||
| 15 | |||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | our $VERSION = '0.01'; | ||
| 19 | |||
| 20 | use Doit::Log; | ||
| 21 | |||
| 22 | sub _bootstrap_perl { | ||
| 23 | my($doit_ssh, %opts) = @_; | ||
| 24 | my $dry_run = delete $opts{dry_run}; | ||
| 25 | die 'Unhandled options: ' . join(' ', %opts) if %opts; | ||
| 26 | |||
| 27 | my $ssh = $doit_ssh->ssh; | ||
| 28 | |||
| 29 | if ($ssh->test('which perl > /dev/null')) { | ||
| 30 | return 1; | ||
| 31 | } | ||
| 32 | error "Running remote command 'which perl' failed: " . $ssh->error | ||
| 33 | if $ssh->error; | ||
| 34 | |||
| 35 | chomp(my $uname = $ssh->capture('uname')); | ||
| 36 | |||
| 37 | my $installer; | ||
| 38 | if ($uname eq 'Linux') { | ||
| 39 | my @distro_tokens; | ||
| 40 | for my $line ($ssh->capture('cat /etc/os-release')) { | ||
| 41 | if ($line =~ m{^ID(?:_LIKE)=\"(.*)\"}) { | ||
| 42 | push @distro_tokens, split /\s+/, $1; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | for my $distro_token (@distro_tokens) { | ||
| 46 | if ($distro_token =~ m{^(centos|rhel|fedora)$}) { | ||
| 47 | $installer = 'yum'; | ||
| 48 | } elsif ($distro_token =~ m{^(debian|ubuntu)$}) { | ||
| 49 | $installer = 'apt-get'; | ||
| 50 | } | ||
| 51 | last if $installer; | ||
| 52 | } | ||
| 53 | } else { | ||
| 54 | if (($uname||'') eq '') { | ||
| 55 | error "Cannot detect remote operating system --- no 'uname' installed?"; | ||
| 56 | } else { | ||
| 57 | error "No bootstrapping implementation for '$uname' --- please install perl manually"; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | if (!$installer) { | ||
| 62 | error "Cannot find a suitable installer --- please install perl manually"; | ||
| 63 | } | ||
| 64 | |||
| 65 | my @cmds; | ||
| 66 | if ($installer eq 'yum') { | ||
| 67 | push @cmds, ['yum', 'install', '-y', 'perl']; | ||
| 68 | } elsif ($installer eq 'apt-get') { | ||
| 69 | push @cmds, ( | ||
| 70 | ['apt-get', 'update', '-y'], | ||
| 71 | ['apt-get', 'install', '-y', 'perl'], | ||
| 72 | ); | ||
| 73 | } else { | ||
| 74 | error "No support for installer '$installer'"; | ||
| 75 | } | ||
| 76 | |||
| 77 | for my $cmd (@cmds) { | ||
| 78 | if ($dry_run) { | ||
| 79 | info "@$cmd (dry-run)"; | ||
| 80 | } else { | ||
| 81 | info "@$cmd"; | ||
| 82 | $ssh->system(@$cmd); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | if ($dry_run) { | ||
| 87 | info "perl not installed (dry-run), subsequent execution will likely fail"; | ||
| 88 | } else { | ||
| 89 | if ($ssh->test('which perl > /dev/null')) { | ||
| 90 | return 1; | ||
| 91 | } | ||
| 92 | error "perl installation failed"; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | 1; | ||
| 97 | |||
| 98 | __END__ |