| 1 | # -*- perl -*- | ||
| 2 | |||
| 3 | # | ||
| 4 | # Author: Slaven Rezic | ||
| 5 | # | ||
| 6 | # Copyright (C) 2017,2018 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::Docker; | ||
| 15 | |||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | our $VERSION = '0.012'; | ||
| 19 | |||
| 20 | sub new { bless {}, shift } | ||
| 21 | sub functions { qw(docker_connect) } | ||
| 22 | |||
| 23 | sub docker_connect { | ||
| 24 | my($self, %opts) = @_; | ||
| 25 | my $docker_connection = Doit::Docker::RPC->do_connect(dry_run => $self->is_dry_run, components => $self->{components}, %opts); | ||
| 26 | $docker_connection; | ||
| 27 | } | ||
| 28 | |||
| 29 | { | ||
| 30 | package Doit::Docker::RPC; | ||
| 31 | |||
| 32 | our @ISA = ('Doit::_AnyRPCImpl'); | ||
| 33 | |||
| 34 | use Doit::Log; | ||
| 35 | |||
| 36 | my $socket_count = 0; | ||
| 37 | |||
| 38 | sub do_connect { | ||
| 39 | my($class, %opts) = @_; | ||
| 40 | my $container = delete $opts{container}; | ||
| 41 | if (!defined $container) { error "The container option is mandatory" } | ||
| 42 | my $dry_run = delete $opts{dry_run}; | ||
| 43 | my $debug = delete $opts{debug}; | ||
| 44 | my @components = @{ delete $opts{components} || [] }; | ||
| 45 | my $perl = delete $opts{perl} || 'perl'; | ||
| 46 | error "Unhandled options: " . join(" ", %opts) if %opts; | ||
| 47 | |||
| 48 | my $self = bless { }, $class; | ||
| 49 | |||
| 50 | require File::Basename; | ||
| 51 | require IPC::Open2; | ||
| 52 | require FindBin; | ||
| 53 | |||
| 54 | my $copy_to_container = sub ($$) { | ||
| 55 | my($src, $dest) = @_; | ||
| 56 | my @docker_cmd = (qw(docker exec -i), $container, qw(sh -c), "cat > $dest"); # XXX use simply docker cp if docker is new enough (>=1.8 or so) | ||
| 57 | info "Copying '$src' to docker container '$container' as '$dest'" if $debug; | ||
| 58 | open my $ifh, '<', $src | ||
| 59 | or error "Error opening $src: $!"; | ||
| 60 | open my $ofh, '|-', @docker_cmd # XXX shell quoting! XXX | ||
| 61 | or error "Error running @docker_cmd: $!"; | ||
| 62 | local $/ = \4096; | ||
| 63 | while(<$ifh>) { | ||
| 64 | print $ofh $_; | ||
| 65 | } | ||
| 66 | close $ofh | ||
| 67 | or error "Error running @docker_cmd: $!"; | ||
| 68 | }; | ||
| 69 | |||
| 70 | my $exec_in_container = sub { | ||
| 71 | my @cmd = @_; | ||
| 72 | my @docker_cmd = (qw(docker exec), $container, @cmd); | ||
| 73 | info "Running '@cmd' in docker container '$container'" if $debug; | ||
| 74 | system @docker_cmd; | ||
| 75 | if ($? != 0) { | ||
| 76 | error "Error running '@cmd' in docker container '$container'"; | ||
| 77 | } | ||
| 78 | }; | ||
| 79 | |||
| 80 | # XXX too much duplication with Doit::SSH | ||
| 81 | { | ||
| 82 | my $remote_cmd = "if [ ! -d .doit/lib ] ; then mkdir -p .doit/lib; fi"; | ||
| 83 | $exec_in_container->('sh', '-c', $remote_cmd); | ||
| 84 | } | ||
| 85 | if ($FindBin::RealScript ne '-e') { | ||
| 86 | no warnings 'once'; | ||
| 87 | $copy_to_container->("$FindBin::RealBin/$FindBin::RealScript", ".doit/$FindBin::RealScript"); | ||
| 88 | } | ||
| 89 | $copy_to_container->($INC{"Doit.pm"}, ".doit/lib/Doit.pm"); | ||
| 90 | { | ||
| 91 | my %seen_dir; | ||
| 92 | for my $component (@components) { | ||
| 93 | my $from = $component->{path}; | ||
| 94 | my $to = $component->{relpath}; | ||
| 95 | my $full_target = ".doit/lib/$to"; | ||
| 96 | my $target_dir = File::Basename::dirname($full_target); | ||
| 97 | if (!$seen_dir{$target_dir}) { | ||
| 98 | $exec_in_container->('sh', '-c', "if [ ! -d $target_dir ] ; then mkdir -p $target_dir; fi"); | ||
| 99 | $seen_dir{$target_dir} = 1; | ||
| 100 | } | ||
| 101 | $copy_to_container->($from, $full_target); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | # Socket pathname, make it possible to find out | ||
| 106 | # old outdated sockets easily by including a | ||
| 107 | # timestamp. Also need to maintain a $socket_count, | ||
| 108 | # if the same script opens multiple sockets quickly. | ||
| 109 | my $sock_path = do { | ||
| 110 | require POSIX; | ||
| 111 | "/tmp/." . join(".", "doit", "docker", POSIX::strftime("%Y%m%d_%H%M%S", gmtime), $<, $$, (++$socket_count)) . ".sock"; | ||
| 112 | }; | ||
| 113 | |||
| 114 | # On linux use Linux Abstract Namespace Sockets --- | ||
| 115 | # invisible and automatically cleaned up. See man 7 unix. | ||
| 116 | my $LANS_PREFIX = $class->_can_LANS; | ||
| 117 | |||
| 118 | # Run the server | ||
| 119 | my @cmd_worker = | ||
| 120 | ( | ||
| 121 | $perl, "-I.doit", "-I.doit/lib", "-e", | ||
| 122 | Doit::_ScriptTools::self_require($FindBin::RealScript) . | ||
| 123 | q{my $d = Doit->init; } . | ||
| 124 | Doit::_ScriptTools::add_components(@components) . | ||
| 125 | q<sub _server_cleanup { unlink "> . $sock_path . q<" }> . | ||
| 126 | q<$SIG{PIPE} = \&_server_cleanup; > . | ||
| 127 | q<END { _server_cleanup() } > . | ||
| 128 | q{Doit::RPC::Server->new($d, "} . $sock_path . q{", excl => 1, debug => } . ($debug?1:0).q{)->run();}, | ||
| 129 | "--", ($dry_run? "--dry-run" : ()) | ||
| 130 | ); | ||
| 131 | my $worker_pid = fork; | ||
| 132 | if (!defined $worker_pid) { | ||
| 133 | error "fork failed: $!"; | ||
| 134 | } elsif ($worker_pid == 0) { | ||
| 135 | $exec_in_container->(@cmd_worker); | ||
| 136 | if ($? != 0) { | ||
| 137 | warning "Failed to run '@cmd_worker'"; # XXX actually an error(), but I want to use CORE::exit | ||
| 138 | CORE::exit(1); # XXX necessary to use CORE::exit? | ||
| 139 | } else { | ||
| 140 | CORE::exit(0); # XXX necessary to use CORE::exit? | ||
| 141 | } | ||
| 142 | } | ||
| 143 | $self->{worker_pid} = $worker_pid; | ||
| 144 | |||
| 145 | my @cmd_comm = | ||
| 146 | ( | ||
| 147 | $perl, "-I.doit/lib", "-MDoit", "-e", | ||
| 148 | q{Doit::Comm->comm_to_sock("} . $sock_path . q{", debug => shift);}, | ||
| 149 | !!$debug, | ||
| 150 | ); | ||
| 151 | |||
| 152 | info "comm perl cmd: @cmd_comm\n" if $debug; | ||
| 153 | my($out, $in); | ||
| 154 | my $comm_pid = IPC::Open2::open2($out, $in, | ||
| 155 | 'docker', 'exec', '-i', $container, @cmd_comm, | ||
| 156 | ); | ||
| 157 | $self->{comm_pid} = $comm_pid; | ||
| 158 | $self->{rpc} = Doit::RPC::Client->new($out, $in, label => "docker:$container", debug => $debug); | ||
| 159 | |||
| 160 | $self; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub DESTROY { | ||
| 164 | my $self = shift; | ||
| 165 | kill 9 => $self->{comm_pid} if $self->{comm_pid}; | ||
| 166 | kill 9 => $self->{worker_pid} if $self->{worker_pid}; | ||
| 167 | waitpid $self->{comm_pid}, 0 if $self->{comm_pid}; | ||
| 168 | waitpid $self->{worker_pid}, 0 if $self->{worker_pid}; | ||
| 169 | } | ||
| 170 | |||
| 171 | } | ||
| 172 | |||
| 173 | 1; | ||
| 174 | |||
| 175 | __END__ |