App-Actions
view release on metacpan or search on metacpan
lib/App/Actions/Docker.pm view on Meta::CPAN
);
return undef if $rc != 0;
$output = '' unless defined $output;
$output =~ s/\s+//g;
return $output =~ /\A\d+\z/ ? $output + 0 : undef;
}
sub pull {
my ($self, $image) = @_;
my @args = ('pull');
push @args, $self->platform_args;
push @args, $image;
my $rc = $self->run(@args);
# Advice for the reader, not a place to point at source lines.
throw($self, 'runner.image.pull_failed', { image => $image, status => $rc, location => 0 }) if $rc != 0;
return 1;
}
# Pull several images at once and report the exit status of each. Docker's
# own output goes straight to the terminal, so a slow image is still visible
# while the others finish. The result is keyed by image name, which lets the
# caller say which images failed and what needed them.
sub pull_many {
my ($self, $images, %options) = @_;
croak 'No images given' unless ref $images eq 'ARRAY' && @$images;
# A caller that is building a machine-readable report asks for silence:
# docker's own progress lines would not be part of any document.
my $quiet = $options{quiet} ? 1 : 0;
if ($self->{dry_run}) {
for my $image (@$images) {
print "[dry-run] $self->{bin} ",
$self->_display('pull', $self->platform_args, $image), "\n"
unless $self->{quiet} || $quiet || $self->{explain};
}
return { map { $_ => 0 } @$images };
}
# The command lines are printed before anything starts, because the
# children then write to the same terminal in no particular order.
if ($self->{verbose} && !$self->{quiet} && !$quiet) {
for my $image (@$images) {
print "+ $self->{bin} ",
$self->_display('pull', $self->platform_args, $image), "\n";
}
}
my @commands = map {
[ $self->_command('pull', $self->platform_args, $_) ]
} @$images;
my %redirect;
if ($quiet) {
my $devnull = File::Spec->devnull;
%redirect = (stdout => $devnull, stderr => $devnull);
}
my $codes = App::Actions::Process::run_parallel(
\@commands,
concurrency => $options{concurrency} || 4,
%redirect,
);
my %results;
for my $index (0 .. $#$images) {
$results{ $images->[$index] } = $codes->[$index];
}
return \%results;
}
sub image_remove {
my ($self, $image) = @_;
return $self->run_silent('image', 'rm', '-f', $image);
}
sub network_create {
my ($self, $name) = @_;
return $self->run_silent('network', 'create', $name);
}
sub network_remove {
my ($self, $name) = @_;
return $self->run_silent('network', 'rm', $name);
}
sub container_start {
my ($self, @args) = @_;
return $self->run_silent('run', '-d', @args);
}
# The same start, with what the daemon said kept rather than discarded. A
# start that fails explains itself on standard error - the image it cannot
# use, the entrypoint the image does not have, the port already taken - and
# that text is the only account of the failure; the exit status alone leaves a
# reader comparing the command with the number. Returns the output and the
# status, in the order capture uses.
sub container_start_captured {
my ($self, @args) = @_;
my ($output, $rc) = $self->capture_merged('run', '-d', @args);
return (defined $output ? $output : '', $rc);
}
sub container_exec {
my ($self, @args) = @_;
return $self->run('exec', @args);
}
sub container_running {
my ($self, $name) = @_;
return 1 if $self->{dry_run};
lib/App/Actions/Docker.pm view on Meta::CPAN
$seen{ lc $_->name }++ for grep { blessed($_) && $_->isa('App::Actions::Docker::Variable') } @args;
my (@command, %hidden);
for my $arg (@args) {
if (!(blessed($arg) && $arg->isa('App::Actions::Docker::Variable'))) {
push @command, $arg;
next;
}
my ($name, $value) = ($arg->name, $arg->value);
my $hideable = $name =~ $VARIABLE_NAME && $name !~ $CLIENT_VARIABLE
&& $seen{ lc $name } == 1 && index($value, "\0") < 0;
push @command, $hideable ? $name : "$name=$value";
$hidden{$name} = $value if $hideable;
}
return (\@command, \%hidden);
}
# _announce
# Purpose: Print the command about to run, the way the mode asks for.
# Entry: @args - the Docker arguments.
# Exit status: Returns 1 when the command should be run, 0 in a dry run.
# Side effects: Prints "[dry-run] ..." in a dry run (unless quiet or
# explaining) and "+ ..." when verbose (unless quiet). Secrets
# are redacted from what is printed, never from what is run.
sub _announce {
my ($self, @args) = @_;
my $display = $self->_display(@args);
if ($self->{dry_run}) {
print "[dry-run] $self->{bin} $display\n"
unless $self->{quiet} || $self->{explain};
return 0;
}
print "+ $self->{bin} $display\n" if $self->{verbose} && !$self->{quiet};
return 1;
}
sub capture {
my ($self, @args) = @_;
return $self->_capture(0, 0, @args);
}
sub capture_silent {
my ($self, @args) = @_;
return $self->_capture(1, 0, @args);
}
# Capture both of the command's streams, as if it had been run with 2>&1.
sub capture_merged {
my ($self, @args) = @_;
return $self->_capture(1, 1, @args);
}
sub _capture {
my ($self, $quiet_stderr, $merge_stderr, @args) = @_;
return ('', 0) unless $self->_announce(@args);
my ($command, $hidden) = $self->_hide_variables(@args);
local @ENV{ keys %{$hidden} } = values %{$hidden};
return App::Actions::Process::capture(
[ $self->_command(@{$command}) ],
quiet_stderr => $quiet_stderr,
merge_stderr => $merge_stderr,
);
}
sub redact {
my ($self, $text) = @_;
return $text unless defined $text;
# Longest first: when one secret contains another, replacing the shorter
# one first would leave the rest of the longer one in the text ("abc" and
# "abcdef" would print "***def").
for my $secret (sort { length $b <=> length $a || $a cmp $b } keys %{ $self->{secret_values} }) {
$text =~ s/\Q$secret\E/***/g;
}
return $text;
}
sub _display {
my ($self, @args) = @_;
my @copy = @args;
for my $arg (@copy) {
next unless defined $arg;
if ($arg =~ /^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/s
&& $self->{secret_names}{$1}) {
$arg = "$1=***";
next;
}
$arg = $self->redact($arg);
}
return join ' ', map { _quote($_) } @copy;
}
sub _quote {
my ($argument) = @_;
# Empty becomes '', a safe word stays bare, and anything else is single
# quoted with each embedded quote closed, escaped and reopened.
my $copy = defined $argument ? $argument : '';
return !length $copy ? q{''}
: $copy =~ /\A[A-Za-z0-9_@%+=:,.\/-]+\z/ ? $copy
: "'" . ($copy =~ s/'/'\\''/gr) . "'";
}
1;
__END__
=encoding utf-8
=head1 NAME
App::Actions::Docker - run Docker commands for the workflow runner
=head1 SYNOPSIS
Check that Docker works, then pull an image:
( run in 1.266 second using v1.01-cache-2.11-cpan-60884a8df76 )