lib/Net/Docker.pm

filter: Statementstmt 37.2% 60 / 161 Branchbran 28.5% 4 / 14 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 37.7% 17 / 45 Podpod 0.0% 0 / 20 totaltotal 31.7% 81 / 255 CC 15 SCAR 21.9
CC
15
Cov
35%
create
56.0
_parse
12.0
remove_image
6.0
CRAP 9.0
↑ Summary script/docker.pl »
1 package Net::Docker;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 our $VERSION = '0.002005';
5  
6 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use AnyEvent::Socket 'tcp_connect';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use AnyEvent::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶has address => (is => 'ro', default => sub { $ENV{DOCKER_HOST} || 'http:var/run/docker.sock/' });
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"DOCKER_HOST"} or "http:var/run/docker.sock/" 0 1 1
17 2 has ua => (is => 'lazy');
18  
19 1 ▶sub _build_ua {
Subroutine _build_ua: called CC=2 SCAR=7.0
20 1 my $self = shift;
21 1 ▶ if ( $self->address !~ m!http://! ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->address =~ m[http://]) 1 0 1
22 1 require LWP::Protocol::http::SocketUnixAlt;
23 1 LWP::Protocol::implementor( http => 'LWP::Protocol::http::SocketUnixAlt' );
24 }
25 1 my $ua = LWP::UserAgent->new;
26 1 return $ua;
27 }
28  
29 1 ▶sub _uri { my $self = shift; return $self->uri(@_); }
Subroutine _uri: called CC=1 SCAR=0.0
30  
31 1 ▶sub uri {
Subroutine uri: called CC=1 SCAR=0.0
Pod undocumented
32 1 my ($self, $rel, %options) = @_;
33 1 my $uri = URI->new($self->address . $rel);
34 1 $uri->query_form(%options);
35 1 return $uri;
36 }
37  
38 0 ▶sub _parse {
Subroutine _parse: not called CC=3 SCAR=24.8
39 0 my ($self, $uri, %options) = @_;
40 0 my $res = $self->ua->get($self->_uri($uri, %options));
41 0 ▶ if ($res->content_type eq 'application/json') {
Branch: 2 branches
BranchTrueFalseTotal
if ($res->content_type eq 'application/json') { } 0 0 0
elsif ($res->content_type eq 'text/plain') { } 0 0 0
42 0 return decode_json($res->decoded_content);
43 }
44 elsif ($res->content_type eq 'text/plain') {
45 0 return eval { decode_json($res->decoded_content) };
46 }
47 0 $res->dump;
48 }
49  
50 1 ▶sub _parse_request {
Subroutine _parse_request: called CC=2 SCAR=7.6
51 1 my ($self, $res) = @_;
52 1 ▶ if ($res->content_type eq 'application/json') {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->content_type eq "application/json") 0 1 1
53 0 my $json = JSON::XS->new;
54 0 return $json->incr_parse($res->decoded_content);
55 }
56 1 my $message = $res->decoded_content;
57 1 $message =~ s/\r?\n$//;
58 1 croak $message;
59 }
60  
61 0 ▶sub create {
Subroutine create: not called CC=7 SCAR=40.3
Pod undocumented
62 0 my ($self, %options) = @_;
63 0 ▶ $options{AttachStderr} //= \1;
Condition0%0/2
$options{"AttachStderr"} //= \1
l!l
00
MC/DC0%0/1
$options{"AttachStderr"} //= \1
$options{"AttachStderr"}
Truth table0%0/2
A
A= $options{"AttachStderr"}
Aresult
11
00
64 0 ▶ $options{AttachStdout} //= \1;
Condition0%0/2
$options{"AttachStdout"} //= \1
l!l
00
MC/DC0%0/1
$options{"AttachStdout"} //= \1
$options{"AttachStdout"}
Truth table0%0/2
A
A= $options{"AttachStdout"}
Aresult
11
00
65 0 ▶ $options{AttachStdin} //= \0;
Condition0%0/2
$options{"AttachStdin"} //= \0
l!l
00
MC/DC0%0/1
$options{"AttachStdin"} //= \0
$options{"AttachStdin"}
Truth table0%0/2
A
A= $options{"AttachStdin"}
Aresult
11
00
66 0 ▶ $options{OpenStdin} //= \0;
Condition0%0/2
$options{"OpenStdin"} //= \0
l!l
00
MC/DC0%0/1
$options{"OpenStdin"} //= \0
$options{"OpenStdin"}
Truth table0%0/2
A
A= $options{"OpenStdin"}
Aresult
11
00
67 0 ▶ $options{Tty} //= \1;
Condition0%0/2
$options{"Tty"} //= \1
l!l
00
MC/DC0%0/1
$options{"Tty"} //= \1
$options{"Tty"}
Truth table0%0/2
A
A= $options{"Tty"}
Aresult
11
00
68  
69 ## workaround for an odd API implementation of
70 ## container naming
71 0 my %query;
72 0 ▶ if (my $name = delete $options{Name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = delete $options{"Name"}) 0 0 0
73 0 $query{name} = $name;
74 }
75  
76 0 my $input = encode_json(\%options);
77  
78 0 my $res = $self->ua->post($self->uri('/containers/create', %query), 'Content-Type' => 'application/json', Content => $input);
79  
80 0 my $json = JSON::XS->new;
81 0 my $out = $json->incr_parse($res->decoded_content);
82 0 return $out->{Id};
83 }
84  
85 0 ▶sub ps {
Subroutine ps: not called CC=1 SCAR=6.9
Pod undocumented
86 0 my ($self, %options) = @_;
87 0 return $self->_parse('/containers/ps', %options);
88 }
89  
90 0 ▶sub images {
Subroutine images: not called CC=1 SCAR=6.9
Pod undocumented
91 0 my ($self, %options) = @_;
92 0 return $self->_parse('/images/json', %options);
93 }
94  
95 0 ▶sub images_viz {
Subroutine images_viz: not called CC=1 SCAR=6.9
Pod undocumented
96 0 my ($self, %options) = @_;
97 0 return $self->_parse('/images/viz', %options);
98 }
99  
100 0 ▶sub search {
Subroutine search: not called CC=1 SCAR=6.9
Pod undocumented
101 0 my ($self, %options) = @_;
102 0 return $self->_parse('/images/search', %options);
103 }
104  
105 0 ▶sub history {
Subroutine history: not called CC=1 SCAR=6.9
Pod undocumented
106 0 my ($self, $image, %options) = @_;
107 0 return $self->_parse('/images/'.$image.'/history', %options);
108 }
109  
110 0 ▶sub inspect {
Subroutine inspect: not called CC=1 SCAR=6.9
Pod undocumented
111 0 my ($self, $image, %options) = @_;
112 0 return $self->_parse('/images/'.$image.'/json', %options);
113 }
114  
115 0 ▶sub version {
Subroutine version: not called CC=1 SCAR=6.9
Pod undocumented
116 0 my ($self, %options) = @_;
117 0 return $self->_parse('/version', %options);
118 }
119  
120 0 ▶sub info {
Subroutine info: not called CC=1 SCAR=6.9
Pod undocumented
121 0 my ($self, %options) = @_;
122 0 return $self->_parse('/info', %options);
123 }
124  
125 0 ▶sub inspect_container {
Subroutine inspect_container: not called CC=1 SCAR=6.9
Pod undocumented
126 0 my ($self, $name, %options) = @_;
127 0 return $self->_parse('/containers/'.$name.'/json', %options);
128 }
129  
130 0 ▶sub export {
Subroutine export: not called CC=1 SCAR=6.9
Pod undocumented
131 0 my ($self, $name, %options) = @_;
132 0 return $self->_parse('/containers/'.$name.'/export', %options);
133 }
134  
135 0 ▶sub diff {
Subroutine diff: not called CC=1 SCAR=6.9
Pod undocumented
136 0 my ($self, $name, %options) = @_;
137 0 return $self->_parse('/containers/'.$name.'/changes', %options);
138 }
139  
140 0 ▶sub remove_image {
Subroutine remove_image: not called CC=2 SCAR=17.9
Pod undocumented
141 0 my ($self, @names) = @_;
142 0 for my $image (@names) {
143 0 $self->ua->request(HTTP::Request->new('DELETE', $self->_uri('/images/'.$image)));
144 }
145 0 return;
146 }
147  
148 0 ▶sub remove_container {
Subroutine remove_container: not called CC=2 SCAR=17.9
Pod undocumented
149 0 my ($self, @names) = @_;
150 0 for my $container (@names) {
151 0 $self->ua->request(HTTP::Request->new('DELETE', $self->_uri('/containers/'.$container)));
152 }
153 0 return;
154 }
155  
156 1 ▶sub pull {
Subroutine pull: called CC=2 SCAR=7.1
Pod undocumented
157 1 my ($self, $repository, $tag, $registry) = @_;
158  
159 1 ▶ if ($repository =~ m/:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($repository =~ /:/) 0 1 1
160 0 ($repository, $tag) = split/:/, $repository;
161 }
162 1 my %options = (
163 fromImage => $repository,
164 tag => $tag,
165 registry => $registry,
166 );
167 1 my $uri = '/images/create';
168 1 my $res = $self->ua->post($self->_uri($uri, %options));
169 1 return $self->_parse_request($res);
170 }
171  
172 0 ▶sub start {
Subroutine start: not called CC=1 SCAR=6.9
Pod undocumented
173 0 my ($self, $name, %options) = @_;
174 0 $self->ua->post($self->_uri('/containers/'.$name.'/start'));
175 0 return;
176 }
177  
178 0 ▶sub stop {
Subroutine stop: not called CC=1 SCAR=6.9
Pod undocumented
179 0 my ($self, $name, %options) = @_;
180 0 $self->ua->post($self->_uri('/containers/'.$name.'/stop'));
181 0 return;
182 }
183  
184 0 ▶sub logs {
Subroutine logs: not called CC=1 SCAR=6.9
Pod undocumented
185 0 my ($self, $container) = @_;
186 0 my %params = (
187 logs => 1,
188 stdout => 1,
189 stderr => 1,
190 );
191 0 my $url = $self->_uri('/containers/'.$container.'/attach');
192 0 my $res = $self->ua->post($url, \%params);
193 0 return $res->content;
194 }
195  
196 0 ▶sub streaming_logs {
Subroutine streaming_logs: not called CC=1 SCAR=6.9
Pod undocumented
197 0 my ($self, $container, %options) = @_;
198  
199 0 *STDOUT->autoflush(1);
200  
201 0 my $input = delete $options{in_fh};
202 0 my $output = delete $options{out_fh};
203  
204 0 my $cv = AnyEvent->condvar;
205  
206 0 my $in_hndl;
207 my $out_hndl;
208  
209 0 ▶ my $callback; $callback = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
210 0 my ($fh, $headers) = @_;
211  
212 0 ▶ $fh->on_error(sub {$cv->send});
Subroutine __ANON__: not called CC=1 SCAR=6.9
213 0 ▶ $fh->on_eof(sub {$cv->send});
Subroutine __ANON__: not called CC=1 SCAR=6.9
214  
215 0 $out_hndl = AnyEvent::Handle->new(fh => $output);
216  
217 0 ▶ $fh->on_read(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
218 0 my ($handle) = @_;
219 0 ▶ $handle->unshift_read(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
220 0 my ($h) = @_;
221 0 my $length = length $h->{rbuf};
222 0 $out_hndl->push_write($h->{rbuf});
223 0 substr $h->{rbuf}, 0, $length, '';
224 0 });
225 0 });
226  
227 0 $in_hndl = AnyEvent::Handle->new(fh => $input);
228 0 ▶ $in_hndl->on_read(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
229 0 my ($h) = @_;
230 0 ▶ $h->push_read(line => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
231 0 my ($h2, $line, $eol) = @_;
232 0 $fh->push_write($line . $eol);
233 0 });
234 0 });
235 0 ▶ $in_hndl->on_eof(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
236 0 $cv->send;
237 0 });
238 0 };
239  
240 my %post_opt = (
241 want_body_handle => 1,
242 0 ▶ tcp_connect => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
243 0 my ($host, $port, $connect_cb, $prepare_cb) = @_;
244 0 return tcp_connect('unix/', '/var/run/docker.sock', $connect_cb, $prepare_cb);
245 },
246 0 );
247  
248 0 my $uri = URI->new('http://localhost/v1.7/containers/'.$container.'/attach');
249 0 $uri->query_form(%options);
250  
251 0 http_request(POST => $uri->as_string, %post_opt, $callback);
252  
253 0 return $cv;
254 }
255  
256 2 1;
257  
258 =head1 NAME
259  
260 Net::Docker - Interface to the Docker API
261  
262 =head1 SYNOPSIS
263  
264 use Net::Docker;
265  
266 my $api = Net::Docker->new;
267  
268 my $id = $api->create(
269 Image => 'ubuntu',
270 Cmd => ['/bin/bash'],
271 AttachStdin => \1,
272 OpenStdin => \1,
273 Name => 'my-container',
274 );
275  
276 say $id;
277 $api->start($id);
278  
279 my $cv = $api->streaming_logs($id,
280 stream => 1, logs => 1,
281 stdin => 1, stderr => 1, stdout => 1,
282 in_fh => \*STDIN, out_fh => \*STDOUT,
283 );
284 $cv->recv;
285  
286 =head1 DESCRIPTION
287  
288 Perl module for using the Docker Remote API.
289  
290 =head1 AUTHOR
291  
292 Peter Stuifzand E<lt>peter@stuifzand.euE<gt>
293  
294 =head1 COPYRIGHT
295  
296 Copyright 2013 - Peter Stuifzand
297  
298 =head1 LICENSE
299  
300 This library is free software; you can redistribute it and/or modify
301 it under the same terms as Perl itself.
302  
303 =head1 SEE ALSO
304  
305 L<http://docker.io>
306  
307 =cut
↑ Summary script/docker.pl »