-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnimbusapp.pl
More file actions
executable file
·765 lines (612 loc) · 24.1 KB
/
nimbusapp.pl
File metadata and controls
executable file
·765 lines (612 loc) · 24.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
#!/usr/bin/env perl
use constant START_LINE => __LINE__ - 2;
use 5.020;
use strict;
use warnings;
use File::Temp qw();
use File::Copy qw(move);
use File::Slurp qw(read_file write_file);
use File::Spec::Functions qw(catfile);
use File::Path qw(make_path);
use File::Basename qw(basename dirname);
use JSON::Tiny qw(encode_json decode_json); # HTTP responses, docker + nimbus config
use YAML::Tiny; # Docker compose
use HTTP::Tiny; # Download update, tags
use Template::Tiny; # Text content
use Scalar::Util qw(reftype);
use Getopt::Long;
use Time::Piece;
use Sort::Versions;
use Term::ANSIColor;
use if $^O eq 'MSWin32', 'Win32::Console::ANSI';
use if $^O eq 'MSWin32', qw'Win32::ShellQuote quote_system';
use experimental qw(postderef lexical_subs signatures);
use constant {
RELEASE_VERSION => "CHANGEME_RELEASE",
RELEASE_DATE => "CHANGEME_DATE",
COMPOSE_FILE => 'docker-compose.yml',
DEFAULT_TAG_COUNT => 10,
STDOUT_FLAG => '__OUTPUT_TO_STDOUT__'
};
# Add handlers for die/warn when distributed via FatPack
if (START_LINE != 0) {
$SIG{__DIE__} = sub {
die $_[0] =~ s/line (\d+).$/"$& (Source:" . ($1 - START_LINE) . ')'/re;
};
$SIG{__WARN__} = sub {
warn $_[0] =~ s/line (\d+).$/"$& (Source:" . ($1 - START_LINE) . ')'/re;
};
}
my %config = do {
my $isWin32 = $^O eq 'MSWin32';
my $userHome = $ENV{HOME} // $ENV{USERPROFILE};
my $nimbusHome = $ENV{NIMBUS_HOME} // $ENV{NIMBUS_BASEDIR} //
catfile(
$userHome // fatal("Could not determine home directory.\n"),
".nimbusapp"
);
make_path($nimbusHome) unless -d $nimbusHome;
(
WINDOWS => $isWin32,
DEFAULT_ORG => $ENV{NIMBUS_DEFAULT_ORG} // "admpresales",
APPS_CONFIG => $ENV{NIMBUS_CONFIG} // catfile($nimbusHome, 'apps.json'),
APPS_OLD_CONFIG => $ENV{NIMBUS_OLD_CONFIG} // catfile($nimbusHome, 'apps.config'),
CACHE => $ENV{NIMBUS_CACHE} // catfile($nimbusHome, 'cache'),
DEBUG => $ENV{NIMBUS_DEBUG} // $ENV{NIMBUS_TRACE} // 0, # Be verbose
TRACE => $ENV{NIMBUS_TRACE} // 0, # Be too verbose
FORCE => $ENV{NIMBUS_FORCE} // 0, # Skip prompts
QUIET => $ENV{NIMBUS_QUIET} // 0, # Be quiet
DAEMON_CONFIG => $isWin32 ? 'C:\ProgramData\Docker\config\daemon.json' : '/etc/docker/daemon.json',
LOG_FILE => $ENV{NIMBUS_LOG} // catfile($nimbusHome, 'nimbusapp.log'),
INSTALL => $ENV{NIMBUS_INSTALL_DIR} // dirname($0),
DOWNLOAD => $ENV{NIMBUS_DOWNLOAD_URL} // 'https://github.com/admpresales/nimbusapp/releases/latest/download/nimbusapp' . ($isWin32 ? '.zip' : '.tar.gz'),
HUB_API_BASE => $ENV{NIMBUS_HUB_API_BASE} // "https://hub.docker.com/v2",
INTELLIJ_MOUNT_HOME => $ENV{NIMBUS_INTELLIJ_HOME} // catfile( $userHome, 'IdeaProjects_docker' ),
INTELLIJ_MOUNT_M2 => $ENV{NIMBUS_INTELLIJ_MAVEN} // catfile( $userHome, '.m2' ),
NL => $isWin32 ? "\r\n" : "\n",
SAVE_APPS_CONFIG => $ENV{NIMBUS_SAVE_CONFIG} // 1,
COMPOSE_COMMAND => $ENV{NIMBUS_COMPOSE} ? [ split ' ', $ENV{NIMBUS_COMPOSE} ] : [ 'docker-compose' ]
);
};
my %dispatch = (
help => sub { usage(); },
version => \&display_version,
update => sub {
display_version();
prompt('CONFIRM_UPDATE');
update_version();
},
up => prompt_first('CONFIRM_RECREATE', \&docker_app_compose),
down => prompt_first('CONFIRM_DELETE', \&docker_compose),
render => \&docker_app,
inspect => \&docker_app,
tags => sub {
print join $config{NL}, list_tags(@_);
print $config{NL};
},
cache => sub($cmd, $params, $args) {
print $params->{tag}, $config{NL};
},
delete => \&delete_image,
purge => \&purge_images,
);
# Compose pass-through
$dispatch{$_} = \&docker_compose for qw( pull start stop restart rm ps logs exec kill port events run top );
# Output functions
# Everything except docker-{compose,app} output goes to STDERR
# This allows that output to be easily recorded or used in scripts
use subs qw(info debug warning error fatal usage);
sub _log {
my $logLevel = shift;
my $t = localtime;
open(my $fh, '>>', $config{LOG_FILE});
printf $fh "%s %6s %s%s", $t->datetime, $logLevel, join('', @_), $config{NL};
close($fh);
}
sub _output { print STDERR @_, $config{NL}; }
sub trace { return unless $config{TRACE}; _log 'TRACE', @_; _output @_; }
sub debug { _log 'DEBUG', @_; _output @_ if $config{DEBUG}; }
sub info { _log 'INFO', @_; _output @_ unless $config{QUIET}; }
sub warning { _log 'WARN', @_; _output text_block('LABEL_WARN'), @_; }
sub error { _log 'ERROR', @_; _output text_block('LABEL_ERROR'), @_; }
sub fatal { _log 'FATAL', @_; die text_block('LABEL_ERROR'), @_, $config{NL}; }
sub usage { fatal @_ ? (text_block('LABEL_ERROR'), @_, $config{NL}) : '', text_block('USAGE'); }
# Load some text from the configuration document at the bottom of this file
# This keeps huge blocks of text out of the code, which may or may not be useful
#
# As this sub is used to handle error text, any errors should result in a 'die'
# instead of using the program's standard 'fatal'
sub text_block($name, $params = {}) {
state $text = load_text();
state $template = Template::Tiny->new();
state $formatting = {
red => color('bold red'),
yellow => color('bold yellow'),
bold => color('bold'),
reset => color('reset')
};
die "Unknown message template: ${name}.\n" unless $text->{$name};
$params->@{keys %$formatting} = values %$formatting;
$template->process(\$text->{$name}, $params, \my $output) || die sprintf "Template error (%s): %s\n", $name, $template->error;
return $output;
}
sub run_command($cmd, $output = undef) {
$cmd = [ quote_system(@$cmd) ] if $config{WINDOWS};
debug("Running: @$cmd");
# Special case, for docker-compose up|pull to properly display progress text
if (!$config{debug} && defined $output && $output eq STDOUT_FLAG) {
trace("Using system() to execute", @$cmd);
system(@$cmd) == 0 or fatal "system: Could not run $cmd->[0]: $! ($?)";
return $?;
}
trace("Using open() to execute", @$cmd);
open(my $fh, '-|', @$cmd) or fatal "Could not run $cmd->[0]: $! ($?)";
$fh->autoflush;
my $mode = reftype $output || 0;
while (defined(my $line = <$fh>)) {
if ($config{DEBUG}) {
chomp(my $ln = $line);
_log 'OUT', $ln;
}
if ($mode eq 'ARRAY') {
chomp $line;
push @$output, $line;
}
elsif ($mode eq 'SCALAR') {
$$output .= $line;
}
elsif ($mode eq 'GLOB') {
print $output $line;
}
else {
print $line;
}
}
close($fh) or fatal "Error running $cmd->[0]: $! ($?)";
return $?;
}
sub download($url, $context = 'Download error') {
my $res = HTTP::Tiny->new->get($url);
if (! $res->{success}) {
fatal $context, $config{NL},
"\t", join("\t", $url, $res->{status}, $res->{reason})
}
$res->{content};
}
sub display_version {
info("Release Version: ", RELEASE_VERSION);
info("Release Date: ", RELEASE_DATE);
0;
}
sub update_version {
my $archive = do {
my $content = download $config{DOWNLOAD};
my $temp = File::Temp->new(UNLINK => 0, suffix => $config{WINDOWS} ? '.zip' : '.tar.gz');
print $temp $content;
close($temp);
$temp->filename;
};
debug("Temporary download location: ", $archive);
debug("Extracting to: ", $config{INSTALL});
my $nimbus_exe = catfile($config{INSTALL}, 'nimbusapp');
my @extract = $config{WINDOWS}
? ( 'powershell', '-c', qq(Expand-Archive -Force -Path "$archive" -DestinationPath "$config{INSTALL}") )
: ( (! -w $nimbus_exe || ! -w $config{INSTALL} ? 'sudo' : ()), 'tar', 'xzf', $archive, '--no-same-owner', '-C', $config{INSTALL} );
run_command([ @extract ]);
fatal "Failed to extract '$archive'. Status: $?" if $?;
unlink($archive) if -f $archive;
run_command([ $nimbus_exe, '--version' ]);
}
sub prompt($label, $params = {}) {
return if $config{FORCE} || $params->{cmd} && $params->{args} && $params->{cmd} eq 'up' && grep { /--force-recreate/ } $params->{args}->@*;
print STDERR text_block($label, $params) =~ s/[\n\r]+$//r, " [y/N] ";
my $result = <STDIN>;
chomp($result);
_log 'PROMPT', $label, " = ", $result;
exit if $result !~ /^y(es)?$/i;
}
sub prompt_first($label, $sub) {
return sub {
my $params = $_[1];
my $check = '';
docker_compose('ps', $params, [ '-q' ], \$check);
debug("Check result: $check");
if ($check ne '') {
local $params->{containers} = [];
docker_compose('ps', $params, [ qw(--services --all) ], $params->{containers});
prompt($label, $params);
}
$sub->(@_);
};
}
sub apply_mounts($params) {
return unless $params->{intellij_home} || $params->{intellij_m2};
fatal "Mounts are currently only available for Intellij" unless $params->{image} eq 'intellij';
my $compose = YAML::Tiny->read( $params->{composeFile} );
if ($params->{intellij_home}) {
push $compose->[0]->{services}->{intellij}->{volumes}->@*,
{
type => 'bind',
source => $config{INTELLIJ_MOUNT_HOME},
target => '/home/demo/IdeaProjects'
};
}
if ($params->{intellij_m2}) {
push $compose->[0]->{services}->{intellij}->{volumes}->@*,
{
type => 'bind',
source => $config{INTELLIJ_MOUNT_M2},
target => '/home/demo/.m2'
};
}
my $yaml = $compose->write_string( $params->{composeFile} );
$yaml =~ s/'(true|false|null)'/$1/g;
write_file( $params->{composeFile}, $yaml );
}
sub docker_app($cmd, $params, $args) {
if ($cmd eq 'inspect') {
run_command([ 'docker-app', 'inspect', $params->{fullImage} ]);
}
else { # Render
my @settings = map {
('-s', sprintf '%s="%s"', $_, $params->{set}{$_})
} keys $params->{set}->%*;
make_path($params->{composeDir}) unless -d $params->{composeDir};
my $temp = File::Temp->new(UNLINK => 0);
eval {
local $SIG{__DIE__};
run_command([ 'docker-app', 'render', @settings, $params->{fullImage} ], $temp);
};
close($temp);
if ($@) {
unlink($temp->filename);
if ($params->{tag} =~ /^(.*?)_/) {
warning "Image name contains an underscore which is not used by nimbusapp. ",
sprintf "Try using %s/%s:%s instead.", $params->{org}, $params->{image}, $1;
}
fatal "Could not render. $@";
}
move $temp->filename, $params->{composeFile} or fatal "Error moving compose file: $!";
apply_mounts($params);
if ($cmd eq 'render') {
print read_file $params->{composeFile};
}
}
return 0;
}
sub docker_compose($cmd, $params, $args, $output = undef) {
if (! -f $params->{composeFile}) {
make_path($params->{composeDir}) unless -d $params->{composeDir};
my $oldComposeFile = catfile($config{CACHE}, $params->@{qw(project org image tag)}, $params->{image} . '.yml');
if (-f $oldComposeFile) {
info "Importing configuration from previous version of nimbusapp";
move($oldComposeFile, $params->{composeFile}) or warning "Could not import old config: $!";
}
if (! -f $params->{composeFile}) {
my $rc = docker_app($cmd, $params, $args);
return $rc if $rc;
}
}
if ($cmd eq 'up') {
# Start in background by default (exclusive with --no-start)
unshift @$args, '-d' unless grep { $_ eq '--no-start' || $_ eq '-d' } @$args;
# Re-initialize anonymous volumes
unshift @$args, '-V' unless $params->{preserve_volumes} || grep { $_ eq '-V' } @$args;
# Remove orphan containers
unshift @$args, '--remove-orphans' unless grep { $_ eq '--remove-orphans' } @$args;
}
if ($cmd eq 'down') {
# Remove named an anonymous volumes with the container
unshift @$args, '-v' unless $params->{preserve_volumes} || grep { $_ eq '-v' } @$args;
# Remove orphan containers
unshift @$args, '--remove-orphans' unless grep { $_ eq '--remove-orphans' } @$args;
}
$output = STDOUT_FLAG if not defined $output and ($cmd eq 'up' or $cmd eq 'pull');
return run_command([ $config{COMPOSE_COMMAND}->@*, '-f', $params->{composeFile}, '-p', $params->{project}, $cmd, @$args ], $output);
}
sub delete_image($cmd, $params, $args) {
$config{SAVE_APPS_CONFIG} = 0;
$params->{composeFile} = catfile( dirname( $params->{composeFile} ), 'delete.yml');
docker_app($cmd, $params, $args);
my $compose = YAML::Tiny->read( $params->{composeFile} );
my @images =
grep { qx(docker images -q $_) }
map { $_->{image} }
values $compose->[0]{services}->%*;
unless (@images) {
info "Nothing to delete.";
return 0;
}
local $params->{images} = [ @images ];
prompt('CONFIRM_IMAGE_DELETE', $params);
return run_command(['docker', 'rmi', @images])
}
sub purge_images($cmd, $params, $args) {
state sub split_image($i) { $i =~ qr/(.*?):(.*$)/ };
my $compose = YAML::Tiny->read( $params->{composeFile} );
my @images = map { $_->{image} } values $compose->[0]{services}->%*;
my %keep = ();
my @all = ();
for my $image (@images) {
my ($base, $tag) = split_image $image;
$keep{$image} = 1;
run_command([ qw(docker image ls --format {{.Repository}}:{{.Tag}}), $base ], \@all);
}
my @remove =
map { $_->[0] }
sort { $a->[1] cmp $b->[1] || versioncmp($a->[2], $b->[2]) }
map { [ $_, split_image($_) ] }
grep { state $seen = {}; $_ && !$keep{$_} && !$seen->{$_}++ }
@all;
unless (@remove) {
info "Nothing to delete.";
return 0;
}
$params->{images} = [@remove];
prompt('CONFIRM_IMAGE_DELETE', $params);
run_command([ qw(docker rmi), @remove ]);
}
sub docker_app_compose {
my $rc = docker_app(@_);
return $rc if $rc;
return docker_compose(@_);
}
sub list_tags($, $params, $) {
my $url = sprintf("%s/repositories/%s/%s.dockerapp/tags", $config{HUB_API_BASE}, $params->{org}, $params->{originalImage});
my $n = $params->{latest} // $params->{number} // DEFAULT_TAG_COUNT;
my $data = decode_json(download $url)->{results};
return
grep { $n-- > 0 }
sort { versioncmp($b, $a) }
map { $_->{name} }
grep { $params->{all} or $_->{name} !~ /-dev$/ } @$data;
}
sub read_app_config_json {
if (-f $config{APPS_CONFIG}) {
return decode_json(scalar read_file( $config{APPS_CONFIG} ));
}
return undef;
}
sub read_app_config_old {
return undef unless -f $config{APPS_OLD_CONFIG};
return {
map {
my %app;
@app{qw(org image tag)} = $_->@[1..3];
$_->[0] => \%app;
}
grep { @$_ == 4 }
map { [ split qr/[\s:\/]/ ] }
read_file( $config{APPS_OLD_CONFIG} )
};
}
sub read_app_config {
if (-f $config{APPS_CONFIG}) {
read_app_config_json
}
elsif (-f $config{APPS_OLD_CONFIG}) {
read_app_config_old
}
else {
{};
}
}
sub load_app_config(%params) {
my $apps = read_app_config;
if (defined(my $app = $apps->{$params{project}})) {
$params{$_} ||= $app->{$_} for qw(image tag org);
$params{image} = $app->{image} if $params{project} eq $params{image};
}
return %params;
}
sub save_app_config(%params) {
my $apps = read_app_config;
$apps->{$params{project}} = {
map { $_ => $params{$_} } qw(image org tag)
};
write_file $config{APPS_CONFIG}, encode_json($apps);
}
sub get_dns_servers {
grep { state %seen; !$seen{$_}++ } map { /DNS.*?:\s*(\d.*)/ } qx{netsh interface ip show config};
}
# States:
# 1 STOPPED
# 4 RUNNING
sub wait_for_docker($state) {
while (1) {
qx{sc.exe query docker} =~ /^\s* STATE \s* : \s* \d+ \s* (\w+)/imx;
last if $1 eq $state;
sleep 1;
}
}
sub restart_docker {
qx{sc.exe stop docker};
wait_for_docker('STOPPED');
qx{sc.exe start docker};
wait_for_docker('RUNNING');
}
# Ensure docker networking is set up
# { "dns": [ "192.168.61.2" ] }
if ($config{WINDOWS} && ! -d dirname $config{DAEMON_CONFIG}) {
warning "Could not find Docker configuration directory to check DNS settings.";
}
elsif ($config{WINDOWS}) {
my $daemon = -f $config{DAEMON_CONFIG} ? decode_json( scalar read_file($config{DAEMON_CONFIG}) ) : {};
if (!defined $daemon->{dns} || @{$daemon->{dns}} == 0) {
info text_block('DNS_START', {});
my @dns = get_dns_servers();
if (@dns) {
$daemon->{dns} = [ @dns ];
write_file( $config{DAEMON_CONFIG}, encode_json($daemon) );
info text_block('DNS_COMPLETE', { servers => [ @dns ], file => $config{DAEMON_CONFIG} });
restart_docker();
} else {
warning text_block('MISSING_DNS', { file => $config{DAEMON_CONFIG} });
}
}
}
my $image_re = qr{
^(?<originalImage>
(?: (?<org> [a-z0-9]{4,30} ) \/ )? # Optional org (lowercase + numbers)
(?<image> [a-z0-9][a-z0-9_.-]+ ) # Image (lowercase + numbers + limited special)
(?: : (?<tag> [a-zA-Z0-9][a-zA-Z0-9_.-]+ ) )? # Optional tag (lower/upper + numbers + limited special)
)$
}xx;
my %params = (
debug => \$config{DEBUG},
force => \$config{FORCE},
quiet => \$config{QUIET},
org => $config{DEFAULT_ORG},
set => {}
);
usage unless @ARGV;
_log 'CMD', join(' ', @ARGV);
my $command;
{
local $SIG{__WARN__} = \&usage;
my sub dispatch_exit { exit ($dispatch{$_[0]}->(@_) // 0); }
GetOptions( \%params,
'project|p=s',
'intellij_home|v', 'intellij_m2|m', 'preserve_volumes',
'debug|d', 'quiet|q', 'force|f', 'all|a',
'number|n=i', 'latest',
'set|s=s%',
( map { $_ => \&dispatch_exit } qw(help update version) ),
'<>' => sub {
state $image;
if (!defined $image) {
$image = shift;
if ($image =~ /^(help|version|update)$/) {
dispatch_exit($1);
}
elsif ($image =~ $image_re) {
@params{keys %+} = values %+;
}
else {
usage "Invalid image format: $image";
}
}
else {
$command = shift;
unshift @ARGV, '--'; # die('!FINISH');
}
}
) or usage "Error parsing command line options options.";
}
if (not defined $params{project}) {
if (not defined $params{image}) {
usage("No image or project specified.");
}
$params{project} = $params{image};
}
%params = load_app_config(%params);
usage("No image found.") if not defined $params{image};
$params{originalImage} ||= $params{image};
$params{composeDir} = catfile($config{CACHE}, $params{project});
$params{composeFile} = catfile($params{composeDir}, COMPOSE_FILE);
$params{cmd} = $command;
$params{args} = [ @ARGV ];
if (!defined $command) {
usage "Please specify a command.";
}
elsif (!defined $dispatch{$command}) {
usage "Unknown command: $command";
}
elsif (!defined($params{tag}) && $command ne 'tags') {
if (defined $params{latest} && $params{latest}) {
($params{tag}) = list_tags(undef, \%params, undef);
}
else {
fatal text_block('MISSING_VERSION', \%params);
}
}
if ($command ne 'tags') {
$params{fullImage} = sprintf "%s/%s.dockerapp:%s", @params{qw(org image tag)};
info 'Using: ', $params{fullImage} unless $command eq 'tags' || $command eq 'render';
}
my $rc = $dispatch{$command}->($command, \%params, \@ARGV);
save_app_config(%params) if $config{SAVE_APPS_CONFIG} && $rc == 0;
exit $rc;
sub load_text {
return {
LABEL_WARN => '[% yellow %]WARNING:[% reset %] ',
LABEL_ERROR => '[% red %]ERROR:[% reset %] ',
DNS_START => 'No Docker DNS configuration found, attempting to configure DNS servers.',
DNS_COMPLETE => q{
Docker DNS servers have been configured to use the following addresses:
[% FOREACH server IN servers %]
[% server %]
[% END %]
Docker DNS settings can be reviewed in [% file %]
},
MISSING_DNS => q{No DNS servers found, Docker containers may not be able to communicate with other servers.
Docker DNS settings can be found in [% file %]
},
MISSING_VERSION => q{No version number specified!
If this is your first time using [% originalImage %], please specify a version number:
nimbusapp [% originalImage %]:<version_number> [% cmd %]
The version number you choose will be remembered for future commands.
},
CONFIRM_UPDATE => '[% red %]Do you want to update your nimbusapp version?[% reset %]',
CONFIRM_DELETE => q{
[% bold %]This action will [% red %]DELETE[% reset %][% bold %] your containers and is [% red %]IRREVERSIBLE[% reset %]!
[% bold %]You may wish to use [% reset %]`nimbusapp [% originalImage %] stop'[% bold %] to shut down your containers without deleting them.[% reset %]
[% bold %]The following containers will be deleted:[% reset %]
[% FOREACH item IN containers -%]
- [% item %]
[% END -%]
[% red %]Do you wish to DELETE these containers?[% reset %]
},
CONFIRM_RECREATE => q{
[% bold %]This action may cause one or more of your containers to be [% red %]DELETED[% reset %][% bold %] and [% red %]RECREATED[% reset %].
[% bold %]Recreating containers is normal when changing their configuration, such as image, tag or ports.[% reset %]
[% bold %]You may wish to use [% reset %]`nimbusapp [% originalImage %] start'[% bold %] to start your existing containers.[% reset %]
[% bold %]The following containers may be recreated:[% reset %]
[% FOREACH item IN containers -%]
- [% item %]
[% END -%]
[% red %]Some or all containers may be recreated, do you wish to continue?[% reset %]
},
CONFIRM_IMAGE_DELETE => q{
[% bold %]This action will attempt to [% red %]DELETE[% reset %][% bold %] your images and is [% red %]IRREVERSIBLE[% reset %]!
[% bold %]Any images that are currently in use will not be deleted, use [% reset %]`nimbusapp <image> down`[% bold %] to delete containers before removing images.[% reset %]
[% bold %]The following images will be deleted:[% reset %]
[% FOREACH img IN images -%]
- [% img %]
[% END -%]
[% red %]Do you wish to DELETE these images?[% reset %]
},
USAGE => q{
Usage: nimbusapp <IMAGE>[:<VERSION>] [OPTIONS] COMMAND [CMD OPTIONS]
Options:
IMAGE The Docker App file you wish to run. If no repository is provided, admpresales is assumed.
VERSION The version of the Docker App file you wish to run.
Only required the first time a container is created, and will be cached for future use.
-d, --debug Enable debugging output (use twice for verbose bash commands)
-f, --force Skip all prompts - Use with caution, this option will happily delete your data without warning
-s, --set Enables you to set(override) default arguments
--version Print the version of nimbusapp and exit
Commands:
down Stop and remove containers
help Prints this help message
inspect Shows metadata and settings for a given application
logs Shows logs for containers
ps Lists containers
pull Pull service images
render Render the Compose file for the application
rm Remove stopped containers
restart Restart containers
start Start existing containers
stop Stop existing containers
up Creates and start containers
delete Deletes containers from a specific application version
purge Deletes old containers from the application
cache Prints the cached application version
version Prints version information
Command Options:
up --no-start Create containers without starting them
--force-recreate Force all containers to be re-created
--no-recreate Do not allow any containers to be re-created
}
}
}