Skip to content

Instantly share code, notes, and snippets.

@melezhik
Created October 17, 2017 14:41
Show Gist options
  • Save melezhik/b30f1bf76a7037a8a166c07956e7a336 to your computer and use it in GitHub Desktop.
Save melezhik/b30f1bf76a7037a8a166c07956e7a336 to your computer and use it in GitHub Desktop.
Cannot invoke this object (REPR: Null; VMNull)
$ sparrowdo --module_run=Prometheus --docker=quizzical_feynman --no_sudo --ssh_user=root running sparrow tasks on 127.0.0.1 ...
target OS is - centos7
enter module <Prometheus> ...
push [task] create directory /var/data/prometheus OK
enter module <RemoteFile> ...
Cannot invoke this object (REPR: Null; VMNull)
in sub tasks at /home/melezhik/.rakudobrew/moar-2017.06/install/share/perl6/site/sources/4249182EA83216A8B16D87044D142E563B672B56 (Sparrowdo::RemoteFile) line 9
in sub module_run at /home/melezhik/.rakudobrew/moar-2017.06/install/share/perl6/site/sources/94D7D3A22199E0D6B8AC951EC962D6A6E20F6FEC (Sparrowdo) line 150
in sub tasks at /home/melezhik/.rakudobrew/moar-2017.06/install/share/perl6/site/sources/63FE40470DDAB720D50B03055CCB56C85A657410 (Sparrowdo::Prometheus) line 18
in sub module_run at /home/melezhik/.rakudobrew/moar-2017.06/install/share/perl6/site/sources/94D7D3A22199E0D6B8AC951EC962D6A6E20F6FEC (Sparrowdo) line 150
in sub MAIN at /home/melezhik/.rakudobrew/moar-2017.06/install/share/perl6/site/resources/1E73B622436233052D2F1D1E8F12A267A3205EFB line 143
in block <unit> at /home/melezhik/.rakudobrew/moar-2017.06/install/share/perl6/site/resources/1E73B622436233052D2F1D1E8F12A267A3205EFB line 366
1 use v6;
2
3 unit module Sparrowdo::RemoteFile;
4
5 use Sparrowdo;
6
7 our sub tasks (%args?) {
8
9 say "OK";
10 task_run %(
11 task => 'install curl',
12 plugin => 'package-generic',
13 parameters => %( list => 'curl' )
14 );
15
16 task_run %(
17 task => "create directory for downloads",
18 plugin => "directory",
19 parameters => %( path => %args<location>.IO.dirname )
20 );
21
22 my $cmd = 'curl ' ~ %args<url> ~ ' -w \'%{url_effective} ==> <%{http_code}> \''
23 ~ ' -L -s -k -f -o ' ~ %args<location>;
24
25 $cmd ~= ' -u' ~ %args<user> if %args<user>.defined;
26 $cmd ~= ':' ~ %args<password> if %args<password>.defined;
27
28 $cmd ~= ' && echo && ls -lh ' ~ %args<location>;
29
30 task_run %(
31 task => "download remote file",
32 plugin => "bash",
33 parameters => %( command => $cmd, debug => 0 );
34 );
35
36 }
37
1 use v6;
2
3 unit module Sparrowdo::Prometheus;
4
5 use Sparrowdo;
6 use Sparrowdo::RemoteFile;
7 use Sparrowdo::Archive;
8 use Sparrowdo::Core::DSL::Directory;
9 use Sparrowdo::Core::DSL::Systemd;
10 use Sparrowdo::Core::DSL::Service;
11
12 our sub tasks (%args) {
13
14 my $distro-url = 'https://github.com/prometheus/prometheus/releases/download/v1.8.0/prometheus-1.8.0.linux-amd64.tar.gz';
15
16 directory '/var/data/prometheus';
17
18 module_run 'RemoteFile', %(
19 url => $distro-url,
20 location => '/var/data/prometheus/prometheus.tar.gz'
21 );
22
23 module_run 'Archive', %(
24 source => '/var/data/prometheus/prometheus.tar.gz',
25 target => '/var/data/prometheus/',
26 );
27
28 #systemd-service "prometheus", %(
29 # user => "root",
30 # workdir => "/var/data/prometheus/prometheus-1.8.0.linux-amd64",
31 # command => "/var/data/prometheus/prometheus-1.8.0.linux-amd64/prometheus"
32 #);
33
34 #service-start "prometheus";
35
36 }
37
1 use v6;
2
3 unit module Sparrowdo;
4
5 use Terminal::ANSIColor;
6 use Data::Dump;
7
8 my %input_params = Hash.new;
9 my $target_os; my $target_hostname;
10 my @tasks = Array.new;
11 my @plugins = Array.new;
12 my @spl = Array.new;
13 my %config = Hash.new;
14
15 sub push_task (%data){
16
17 @tasks.push: %data;
18
19 say input_params('NoColor') ??
20 ( 'push [task] ' ~ %data<task> ~ ' OK' ) !!
21 colored('push [task] ' ~ %data<task> ~ ' OK', 'bold green on_black');
22
23 }
24
25 sub push_spl ($item){
26
27 @spl.push: $item;
28
29 say input_params('NoColor') ??
30 ( 'push ' ~ $item ~ ' into SPL - OK' ) !!
31 colored('push ' ~ $item ~ ' into SPL - OK', 'bold yellow on_cyan');
32
33 }
34
35 sub get_tasks () is export {
36 say Dump(@tasks) if %*ENV<SPARROWDO-DEBUG>;
37 @tasks;
38 }
39
40 sub get_spl () is export {
41 @spl
42 }
43
44 sub set_target_os ($os) is export {
45 $target_os = $os
46 }
47
48 sub target_os () is export {
49 return $target_os;
50 }
51
52 sub set_target_hostname ($hostname) is export {
53 $target_hostname = $hostname
54 }
55
56 sub target_hostname () is export {
57 return $target_hostname;
58 }
59
60 sub set_input_params (%args) is export {
61
62 for %args.kv -> $name, $value {
63 %input_params.push($name => $value);
64 }
65
66 }
67
68 sub input_params ($name) is export {
69
70 %input_params{$name};
71
72 }
73
74 sub set_spl(%args) is export {
75 for %args.kv -> $plg, $source {
76 push_spl($plg ~ ' ' ~ $source);
77 }
78 }
79
80
81 multi sub task_run($task_desc, $plugin_name, %parameters?) is export {
82 task_run %(
83 task => "$task_desc" ~ " [plg] " ~ $plugin_name,
84 plugin => $plugin_name,
85 parameters => %parameters
86 );
87 }
88
89 multi sub task_run(%args) is export {
90
91 my %task_data = %(
92 task => %args<task>,
93 plugin => %args<plugin>,
94 data => %args<parameters>
95 );
96
97 push_task %task_data;
98
99 }
100
101 multi sub task-run(%args) is export {
102 task_run %args
103 }
104
105 multi sub task-run($task_desc, $plugin_name, %parameters?) is export {
106 task_run $task_desc, $plugin_name, %parameters
107 }
108
109 sub plg-list() is export {
110 @plugins;
111 }
112
113 multi sub plg-run($plg) is export {
114 plg-run([$plg])
115 }
116
117 multi sub plg-run(@plg-list) is export {
118
119 for @plg-list -> $p {
120 if $p ~~ /(\S+)\@(.*)/ {
121 my $name = $0; my $params = $1;
122 my @args = split(/\,/,$params);
123 @plugins.push: [ $name, @args ];
124 say input_params('NoColor') ??
125 ( 'push [plugin] ' ~ $name ~ ~ ' ' ~ @args ~ ' OK' ) !!
126 colored('push [plugin] ' ~ $name ~ ~ ' ' ~ @args ~ ' OK', 'bold green on_black');
127 } else {
128 @plugins.push: [ $p ];
129 say input_params('NoColor') ??
130 ( 'push [plugin] ' ~ $p ~ ' OK' ) !!
131 colored('push [plugin] ' ~ $p ~ ' OK', 'bold green on_black');
132 }
133 }
134 }
135
136 sub module_run($name, %args = %()) is export {
137
138 say input_params('NoColor') ??
139 ( 'enter module <' ~ $name ~ '> ... ' ) !!
140 colored('enter module <' ~ $name ~ '> ... ', 'bold cyan on_black');
141
142 if ( $name ~~ /(\S+)\@(.*)/ ) {
143 my $mod-name = $0; my $params = $1;
144 my %mod-args;
145 for split(/\,/,$params) -> $p { %mod-args{$0.Str} = $1.Str if $p ~~ /(\S+?)\=(.*)/ };
146 require ::('Sparrowdo::' ~ $mod-name);
147 ::('Sparrowdo::' ~ $mod-name ~ '::&tasks')(%mod-args);
148 } else {
149 require ::('Sparrowdo::' ~ $name);
150 ::('Sparrowdo::' ~ $name ~ '::&tasks')(%args);
151 }
152
153
154 }
155
156 sub config() is export {
157 %config
158 }
159
160 sub config_set( %data = %()) is export {
161 %config = %data
162 }
$ perl -n -e 'print ++$i,$_' /home/melezhik/.rakudobrew/moar-2017.06/install/share/perl6/site/resources/1E73B622436233052D2F1D1E8F12A267A3205EFB | cat
1use Terminal::ANSIColor;
2use JSON::Tiny;
3use Sparrowdo;
4use Sparrowdo::Core::DSL::User;
5use Sparrowdo::Core::DSL::Group;
6use Sparrowdo::Core::DSL::File;
7use Sparrowdo::Core::DSL::Directory;
8use Sparrowdo::Core::DSL::Template;
9use Sparrowdo::Core::DSL::Systemd;
10use Sparrowdo::Core::DSL::Package;
11use Sparrowdo::Core::DSL::CPAN::Package;
12use Sparrowdo::Core::DSL::Zef;
13use Sparrowdo::Core::DSL::Git;
14use Sparrowdo::Core::DSL::Service;
15use Sparrowdo::Core::DSL::Bash;
16use Sparrowdo::Core::DSL::Ssh;
17use Sparrowdo::Core::DSL::Assert;
18use Config::Simple;
19
20
21sub MAIN (
22
23 Str :$host = '127.0.0.1',
24 Str :$sparrowfile,
25 Str :$http_proxy,
26 Str :$https_proxy,
27 Str :$ssh_user,
28 Str :$ssh_private_key,
29 Int :$ssh_port = 22,
30 Bool :$verbose = False,
31 Bool :$bootstrap = False,
32 Bool :$check_syntax = False,
33 Str :$module_run,
34 :$task_run,
35 Bool :$no_sudo = False,
36 Bool :$no_color = False,
37 Bool :$no_index_update = False,
38 Str :$sparrow_root = '/opt/sparrow',
39 Str :$repo,
40 Bool :$local_mode = False,
41 Str :$password,
42 Str :$docker,
43 Str :$cwd,
44 Str :$format,
45)
46
47{
48
49 # read config if exists
50
51 my $sparrowdo-cache = %*ENV<USER> ?? '/home/' ~ %*ENV<USER> ~ '/.sparrowdo/' ~ $sparrow_root !! '/.sparrowdo';
52
53 shell "rm -rf $sparrowdo-cache/";
54
55 mkdir "$sparrowdo-cache/plugins";
56
57 my $conf-ini-file = %*ENV<USER> ?? '/home/' ~ %*ENV<USER> ~ '/sparrowdo.ini' !! ( $sparrow_root ~ '/sparrowdo.ini' );
58
59 my $conf-ini = Hash.new;
60
61 if $conf-ini-file.IO ~~ :e {
62 Config::Simple.read($conf-ini-file,:f("ini"))
63 }
64
65 my $verbose_val = $verbose;
66
67 $verbose_val = True if $conf-ini<sparrowdo><verbose> && ! $verbose_val;
68
69 my $format_val = $conf-ini<sparrowdo><format> ?? $conf-ini<sparrowdo><format> !! %*ENV<OUTTHENTIC_FORMAT> || 'default';
70
71 my $no_index_update_val = $no_index_update;
72
73 $no_index_update_val = True if $conf-ini<sparrowdo><no_index_update> && ! $no_index_update_val;
74
75 my $repo_val = $repo;
76
77 $repo_val = $conf-ini<sparrowdo><repo> if $conf-ini<sparrowdo><repo> && ! $repo_val;
78
79
80 set_input_params %(
81 Host => $host,
82 Sparrowfile => $sparrowfile,
83 Docker => $docker,
84 LocalMode => $local_mode,
85 Cwd => $cwd,
86 HttpProxy => $http_proxy,
87 HttpsProxy => $https_proxy,
88 SshPort => $ssh_port,
89 SshUser => $ssh_user,
90 SshPrivateKey => $ssh_private_key,
91 Verbose => $verbose_val,
92 Format => $format || $format_val,
93 NoSudo => $no_sudo,
94 NoColor => $no_color,
95 NoIndexUpdate => $no_index_update_val,
96 SparrowRoot => $sparrow_root,
97 Repo => $repo_val,
98 SparrowhubApi => $conf-ini<sparrowdo><sparrowhub_api>,
99 Password => $password
100 );
101
102 ssh_shell "rm -rf $sparrow_root/sparrowdo-cache && mkdir -m 777 -p $sparrow_root/sparrowdo-cache";
103
104 bootstrap($sparrow_root,$sparrowdo-cache) if $bootstrap;
105
106 ssh_shell "rm -rf $sparrow_root/sparrow-cache && \\
107 mkdir -m 777 -p $sparrow_root/sparrow-cache && \\
108 mkdir -m 777 -p $sparrow_root/sparrow-cache/plugins && \\
109 mkdir -m 777 -p $sparrow_root/sparrow-cache/files && \\
110 echo print os | perl -MOutthentic > $sparrow_root/sparrow-cache/os.txt && \\
111 uname -n > $sparrow_root/sparrow-cache/hostname.txt";
112
113
114 say $no_color ??
115 'running sparrow tasks on ' ~ $host ~ ' ... ' !!
116 colored( 'running sparrow tasks on ' ~ $host ~ ' ... ', 'bold black on_yellow');
117
118 if $docker {
119
120 _scp "$sparrow_root/sparrow-cache/os.txt", "$sparrowdo-cache/", 1; # copy back a target host data back to master host
121 _scp "$sparrow_root/sparrow-cache/hostname.txt", "$sparrowdo-cache/", 1; # copy back a target host data back to master host
122
123 } else {
124 _scp "$sparrow_root/sparrow-cache/*.txt", "$sparrowdo-cache/", 1, 1; # copy back a target host data back to master host
125 }
126
127 set_target_os slurp "$sparrowdo-cache/os.txt";
128 set_target_hostname slurp "$sparrowdo-cache/hostname.txt";
129
130 say $no_color ??
131 ('target OS is - '~ target_os ) !!
132 colored('target OS is - '~ target_os, 'black on_white');
133
134 if 'config.pl6'.IO.e {
135 say $no_color ??
136 'load configuration from config.pl6 ...' !!
137 colored('load configuration from config.pl6 ...', 'blue on_green');
138 config_set(EVALFILE 'config.pl6');
139 }
140
141
142 if $module_run {
143 module_run $module_run;
144 } elsif $task_run {
145 plg-run($task_run);
146 } else {
147 EVALFILE $sparrowfile||'sparrowfile';
148 }
149
150 exit if $check_syntax;
151
152 if get_spl() {
153
154 if input_params('Verbose') {
155 say input_params('NoColor') ??
156 'populating SPL file' !!
157 colored( 'populating SPL file', 'bold yellow on_cyan' );
158 }
159
160 spurt "$sparrowdo-cache/sparrow.list", get_spl().join: "\n";
161 _scp "$sparrowdo-cache/sparrow.list", "$sparrow_root/sparrow-cache/";
162 ssh_shell "mkdir -p $sparrow_root && mv $sparrow_root/sparrow-cache/sparrow.list $sparrow_root";
163 say input_params('NoColor') ??
164 "copied SPL file as $sparrow_root/sparrow.list - OK" !!
165 colored("copied SPL file as $sparrow_root/sparrow.list - OK", 'bold green on_black');
166
167 } else {
168
169 ssh_shell "mkdir -p $sparrow_root && touch $sparrow_root/sparrow.list";
170 say input_params('NoColor') ??
171 "SPL file $sparrow_root/sparrow.list is empty" !!
172 colored("SPL file $sparrow_root/sparrow.list is empty", 'bold green on_black');
173
174 }
175
176 if $repo_val {
177 ssh_shell "echo repo: $repo_val > $sparrow_root/sparrow-cache/sparrow.yaml";
178 say input_params('NoColor') ??
179 "set custom repo to $repo_val - OK" !!
180 colored("set custom repo to $repo_val - OK", 'bold green on_black');
181 }
182
183 ssh_shell 'sparrow index update' unless input_params('NoIndexUpdate');
184
185 # plugins mode
186 if ($task_run) {
187 for plg-list() -> @p {
188 ssh_shell "sparrow plg install " ~ @p[0];
189 if @p[1] {
190 spurt "$sparrowdo-cache/plugins/@p[0].args", @p[1].join: "\n";
191 _scp "$sparrowdo-cache/plugins/@p[0].args", "$sparrow_root/sparrow-cache/plugins";
192 ssh_shell "sparrow plg run " ~ @p[0] ~ ' --args-file ' ~ "$sparrow_root/sparrow-cache/plugins/" ~ "@p[0].args";
193 } else {
194 ssh_shell "sparrow plg run " ~ @p[0];
195 }
196 }
197 # task box mode
198 } else {
199 spurt "$sparrowdo-cache/task-box.json", (to-json get_tasks());
200 _scp "$sparrowdo-cache/task-box.json", "$sparrow_root/sparrow-cache";
201 say input_params('NoColor') ??
202 "set up task box file - $sparrowdo-cache/task-box.json - OK" !!
203 colored("set up task box file - $sparrowdo-cache/task-box.json - OK", 'bold green on_black');
204 ssh_shell "sparrow box run $sparrow_root/sparrow-cache/task-box.json --mode quiet --purge-cache";
205 }
206
207}
208
209sub ssh_shell ( $cmd ) {
210
211
212 #my @bash_commands = ( 'export LC_ALL=en_US.UTF-8' );
213 my @bash_commands = ( 'export LC_ALL=C' );
214 my $sparrow_root = input_params('SparrowRoot');
215
216 @bash_commands.push: 'export http_proxy=' ~ input_params('HttpProxy') if input_params('HttpProxy');
217 @bash_commands.push: 'export https_proxy=' ~ input_params('HttpsProxy') if input_params('HttpsProxy');
218 @bash_commands.push: 'export GIT_PROTOCOL=https';
219 @bash_commands.push: 'export PERL_USE_UNSAFE_INC=1';
220 @bash_commands.push: 'export PATH=/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/local/bin:/usr/sbin/:/sbin/:/bin/:$PATH';
221 @bash_commands.push: 'export SPARROW_ROOT=' ~ input_params('SparrowRoot');
222 @bash_commands.push: 'export SPARROW_NO_COLOR=1' if input_params('NoColor');
223 @bash_commands.push: "export SPARROW_CONF_PATH=$sparrow_root/sparrow-cache/sparrow.yaml";
224 @bash_commands.push: 'export OUTTHENTIC_CWD=' ~ input_params('Cwd') if input_params('Cwd');
225 @bash_commands.push: 'export sparrow_hub_api_url=' ~ input_params('SparrowhubApi') if input_params('SparrowhubApi');
226 @bash_commands.push: 'export OUTTHENTIC_FORMAT=' ~ input_params('Format');
227
228 @bash_commands.push: $cmd;
229
230 my $ssh_cmd;
231
232 if input_params('LocalMode') {
233
234 $ssh_cmd = ( input_params('NoSudo') ) ?? "sh -c '" !! "sudo sh -c '";
235 $ssh_cmd ~= ~ ( join ' ; ', @bash_commands ) ~ "'";
236
237 } elsif input_params('Docker') {
238
239 $ssh_cmd = "docker exec -it " ~ input_params('Docker') ~ ' ';
240
241 if input_params('NoSudo') {
242 $ssh_cmd ~= "sh -c '" ~ ( join ' ; ', @bash_commands ) ~ "'";
243 } else {
244 $ssh_cmd ~= "sudo sh -c '" ~ ( join ' ; ', @bash_commands ) ~ "'";
245 }
246
247 } else {
248
249 if input_params('Password') {
250 $ssh_cmd = 'sshpass -p ' ~ input_params('Password');
251 $ssh_cmd ~= ' ssh -o ConnectionAttempts=1 -o ConnectTimeout=5';
252 } elsif %*ENV<SSHPASS> {
253 $ssh_cmd = 'sshpass -e ssh -o ConnectionAttempts=1 -o ConnectTimeout=5';
254 } else {
255 $ssh_cmd = 'ssh -o ConnectionAttempts=1 -o ConnectTimeout=5';
256 }
257
258 $ssh_cmd ~= ' -p ' ~ input_params('SshPort') if input_params('SshPort');
259
260 $ssh_cmd ~= ' -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -tt';
261
262 $ssh_cmd ~= ' -q' unless input_params('Verbose');
263
264 $ssh_cmd ~= ' -i ' ~ input_params('SshPrivateKey') if input_params('SshPrivateKey');
265
266 if input_params('SshUser') {
267 $ssh_cmd ~= ' ' ~ input_params('SshUser') ~ '@' ~ input_params('Host') ~ ' ';
268 } else {
269 $ssh_cmd ~= ' ' ~ input_params('Host') ~ ' ';
270 }
271
272 $ssh_cmd ~= ( input_params('NoSudo') ) ?? " \"sh -c '" !! " \"sudo sh -c '";
273
274 $ssh_cmd ~= ~ ( join ' ; ', @bash_commands ) ~ "'\"";
275
276 $ssh_cmd ~= ' 2>/dev/null' unless input_params('Verbose');
277
278 }
279
280 say input_params('NoColor') ??
281 $ssh_cmd !!
282 colored($ssh_cmd, 'bold green') if input_params('Verbose');
283
284 shell $ssh_cmd;
285
286}
287
288our sub _copy-local-file ( $file, $dest ) {
289
290 say input_params('NoColor') ??
291 "copy local file $file to remote $dest" !!
292 colored("copy local file $file to remote $dest", 'bold yellow');
293 _scp($file,$dest);
294
295}
296
297
298our sub _scp ( $file, $dest, $reverse = 0, $recursive = 0 ) {
299
300 my $ssh_host_term;
301
302 if input_params('SshUser') {
303 $ssh_host_term = input_params('SshUser') ~ '@' ~ input_params('Host');
304 } else {
305 $ssh_host_term = input_params('Host');
306 }
307
308 my $scp_command;
309
310 if ( input_params('LocalMode') ) {
311
312 $scp_command = 'cp';
313
314 if $reverse {
315 $scp_command ~= ' ' ~ $file ~ ' ' ~ $dest ;
316 } else {
317 $scp_command ~= ' ' ~ $file ~ ' ' ~ $dest;
318 }
319
320 } elsif ( input_params('Docker') ) {
321
322 $scp_command = "docker cp ";
323
324 if $reverse {
325 $scp_command ~= input_params('Docker') ~ ':' ~ $file ~ ' ' ~ $dest;
326 } else {
327 $scp_command ~= $file ~ ' ' ~ input_params('Docker') ~ ':' ~ $dest;
328 }
329
330 } else {
331
332 my $scp_params = ' -P ' ~ input_params('SshPort');
333
334 $scp_params ~= ' -i ' ~ input_params('SshPrivateKey') if input_params('SshPrivateKey');
335
336 $scp_params ~= ' -q' unless input_params('Verbose');
337
338 $scp_params ~= ' -r ' if $recursive;
339
340
341 if input_params('Password') {
342 $scp_command = 'sshpass -p ' ~ input_params('Password') ~ ' ';
343 $scp_command ~= 'scp -o ConnectionAttempts=1 -o ConnectTimeout=5 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ' ~ $scp_params;
344 } elsif %*ENV<SSHPASS> {
345 $scp_command = 'sshpass -e';
346 $scp_command ~= ' scp -o ConnectionAttempts=1 -o ConnectTimeout=5 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ' ~ $scp_params;
347 } else {
348 $scp_command = 'scp -o ConnectionAttempts=1 -o ConnectTimeout=5 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ' ~ $scp_params;
349 }
350
351 if $reverse {
352 $scp_command ~= ' ' ~ $ssh_host_term ~ ':' ~ $file ~ ' ' ~ $dest ;
353 } else {
354 $scp_command ~= ' ' ~ $file ~ ' ' ~ $ssh_host_term ~ ':' ~ $dest;
355 }
356 }
357
358 say ( input_params('NoColor') ??
359 $scp_command !!
360 colored($scp_command, 'bold green') ) if input_params('Verbose');
361
362 shell $scp_command;
363
364}
365
366sub bootstrap ( $sparrow_root, $sparrowdo-cache ) {
367
368 my $host = input_params('Host');
369 say input_params('NoColor') ??
370 'running sparrow bootstrap for host: ' ~ $host ~ ' ... ' !!
371 colored( 'running sparrow bootstrap for host: ' ~ $host ~ ' ... ', 'bold black on_yellow');
372
373 my $bootstrap = q:to/HERE/;
374#! /usr/bin/env sh
375
376# Find out the target OS
377if [ -s /etc/os-release ]; then
378 # freedesktop.org and systemd
379 . /etc/os-release
380 OS=$NAME
381 VER=$VERSION_ID
382elif lsb_release -h >/dev/null 2>&1; then
383 # linuxbase.org
384 OS=$(lsb_release -si)
385 VER=$(lsb_release -sr)
386elif [ -s /etc/lsb-release ]; then
387 # For some versions of Debian/Ubuntu without lsb_release command
388 . /etc/lsb-release
389 OS=$DISTRIB_ID
390 VER=$DISTRIB_RELEASE
391elif [ -s /etc/debian_version ]; then
392 # Older Debian/Ubuntu/etc.
393 OS=Debian
394 VER=$(cat /etc/debian_version)
395elif [ -s /etc/SuSe-release ]; then
396 # Older SuSE/etc.
397 printf "TODO\n"
398elif [ -s /etc/redhat-release ]; then
399 # Older Red Hat, CentOS, etc.
400 OS=$(cat /etc/redhat-release| head -n 1)
401else
402 RELEASE_INFO=$(cat /etc/*-release 2>/dev/null | head -n 1)
403
404 if [ ! -z "$RELEASE_INFO" ]; then
405 OS=$(printf -- "$RELEASE_INFO" | awk '{ print $1 }')
406 VER=$(printf -- "$RELEASE_INFO" | awk '{ print $NF }')
407 else
408 # Fall back to uname, e.g. "Linux <version>", also works for BSD, etc.
409 OS=$(uname -s)
410 VER=$(uname -r)
411 fi
412fi
413
414# Convert OS name to lowercase to remove inconsistencies
415OS=$(printf -- "$OS" | awk '{ print tolower($1) }')
416
417printf -- "Bootstrap for %s\n" "$OS"
418
419# Install native dependencies
420case "$OS" in
421 alpine)
422 apk update --wait 120
423 apk add --wait 120 curl perl git bash build-base gcc perl-dev wget
424 ;;
425 amazon|centos|red)
426 yum -q -y install make curl perl gcc perl-Digest-MD5 perl-Test-Harness perl-Data-Dumper perl-ExtUtils-MakeMaker perl-Hash-Merge
427 ;;
428 arch|archlinux)
429 pacman -Syy
430 pacman -S --needed --noconfirm -q curl cpanminus guile perl gcc make perl-clone perl-config-tiny perl-yaml perl-test-yaml perl-test-base perl-spiffy perl-algorithm-diff perl-text-diff perl-json perl-try-tiny perl-capture-tiny perl-file-sharedir perl-file-sharedir-install perl-module-build-tiny perl-extutils-installpaths perl-extutils-config perl-extutils-helpers perl-class-inspector
431 ;;
432 debian|ubuntu)
433 DEBIAN_FRONTEND=noninteractive
434
435 apt-get update
436 apt-get install -y -qq build-essential curl perl
437 ;;
438 fedora)
439 dnf -y install curl perl gcc perl-open perl-Test perl-Test-Simple perl-Storable perl-Digest-MD5 perl-Test-Harness perl-Data-Dumper perl-ExtUtils-MakeMaker perl-Hash-Merge
440 ;;
441 funtoo)
442 echo
443 ;;
444 minoca)
445 opkg -f /etc/opkg/opkg.conf update
446 opkg install curl perl make gcc tar gzip
447 ;;
448 ubuntu)
449 ;;
450 *)
451 printf -- "Your OS (%s) is not supported\n" "$OS"
452 exit 1
453esac
454
455# Install cpanm
456if ! which cpanm 2>/dev/null; then
457 printf -- "installing cpanm ...\n"
458 curl -s -kL http://cpanmin.us/ -o /bin/cpanm
459 chmod a+x /bin/cpanm
460fi
461
462# Install sparrow
463if ! which sparrow 2>/dev/null; then
464 cpanm -q Outthentic Sparrow Test::More || cpanm -q Outthentic Sparrow Test::More
465fi
466
467# Forcefully upgrade Outthentic and Sparrow
468cpanm -q --notest Outthentic Sparrow || cpanm --notest -q Outthentic Sparrow
469
470# Update sparrow
471sparrow index update
472
473HERE
474
475
476 spurt "$sparrowdo-cache/bootstrap.sh", $bootstrap;
477
478 _scp "$sparrowdo-cache/bootstrap.sh", "$sparrow_root/sparrowdo-cache";
479
480 ssh_shell "sh $sparrow_root/sparrowdo-cache/bootstrap.sh";
481
482}
483
484# vim: ft=perl6 et sw=2 ts=2
@melezhik
Copy link
Author

melezhik commented Oct 17, 2017

extra comment to the previous one:

  1. First code chunk is for:

in sub MAIN at /home/melezhik/.rakudobrew/moar-2017.06/install/share/perl6/site/resources/1E73B622436233052D2F1D1E8F12A267A3205EFB line 143
in block at /home/melezhik/.rakudobrew/moar-2017.06/install/share/perl6/site/resources/1E73B622436233052D2F1D1E8F12A267A3205EFB line 366

  1. Other chunks could be identified by module's names (Sparrowdo, Sparrowdo::RemoteFile, Sparrowdo::Prometheus )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment