| 1 | #!/usr/bin/env perl | ||
| 2 | use Net::Docker; | ||
| 3 | use Data::Dumper; | ||
| 4 | |||
| 5 | my $api = Net::Docker->new; | ||
| 6 | |||
| 7 | if ($ARGV[0] eq 'ps') { | ||
| 8 | my %args; | ||
| 9 | if ($ARGV[1] eq '-a') { | ||
| 10 | $args{all}=1; | ||
| 11 | } | ||
| 12 | print "ID IMAGE COMMAND CREATED STATUS PORTS\n"; | ||
| 13 | for my $row (@{ $api->ps(%args) }) { | ||
| 14 | my $id = substr $_->{Id}, 0, 12; | ||
| 15 | for (qw/Id Image Command Created Status Ports/) { | ||
| 16 | my $len = $_ eq 'Command' ? 23 : 20; | ||
| 17 | my $printlen = $_ eq 'Id' ? 12 : 19; | ||
| 18 | my $val = substr $row->{$_}, 0, $printlen; | ||
| 19 | printf("%-${len}s", $val); | ||
| 20 | } | ||
| 21 | print "\n"; | ||
| 22 | } | ||
| 23 | } | ||
| 24 | elsif ($ARGV[0] eq 'run') { | ||
| 25 | my ($cmd, $image, @client_cmd) = @ARGV; | ||
| 26 | |||
| 27 | my $id = $api->create(Image => $image, Cmd => \@client_cmd, AttachStdin => \1, OpenStdin => \1); | ||
| 28 | $api->start($id); | ||
| 29 | |||
| 30 | my $cv = $api->streaming_logs($id, | ||
| 31 | stream => 1, | ||
| 32 | logs => 1, | ||
| 33 | stdin => 1, stderr => 1, stdout => 1, | ||
| 34 | in_fh => \*STDIN, | ||
| 35 | out_fh => \*STDOUT | ||
| 36 | ); | ||
| 37 | $cv->recv; | ||
| 38 | } | ||
| 39 | elsif ($ARGV[0] eq 'attach') { | ||
| 40 | my ($cmd, $id) = @ARGV; | ||
| 41 | my $cv = $api->streaming_logs($id, | ||
| 42 | stream => 1, | ||
| 43 | logs => 1, | ||
| 44 | stdin => 1, stderr => 1, stdout => 1, | ||
| 45 | in_fh => \*STDIN, | ||
| 46 | out_fh => \*STDOUT | ||
| 47 | ); | ||
| 48 | $cv->recv; | ||
| 49 | } | ||
| 50 |