Marathon.pm

filter: Statementstmt 46.9% 61 / 130 Branchbran 20.5% 7 / 34 Conditioncond 9.0% 3 / 33 MC/DCmcdc 6.6% 1 / 15 Subroutinesub 47.0% 16 / 34 Podpod 93.3% 14 / 15 totaltotal 39.0% 102 / 261 CC 31 SCAR 28.0
CC
31
Cov
36%
get_tasks
72.0
kill_tasks
30.0
kill_deployment
30.0
CRAP 16.5
« Marathon/Deployment.pm ↑ Summary Marathon/Group.pm »
1 package Marathon;
2  
3 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Marathon::App;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Marathon::Group;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Marathon::Events;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Marathon::Deployment;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 Marathon - An object-oriented Mapper for the Marathon REST API
16  
17 =head1 VERSION
18  
19 Version 0.9
20  
21 =cut
22  
23 4 our $VERSION = '0.9';
24 4 our $verbose = 0;
25  
26  
27 =head1 SYNOPSIS
28  
29 This module is a wrapper around the [Marathon REST API](http://mesosphere.github.io/marathon/docs/rest-api.html), so it can be used without having to write JSON by hand.
30  
31 For the most common tasks, there is a helper method in the main module. Some additional metods are found in the Marathon::App etc. submodules.
32  
33 To start, create a marathon object:
34  
35 my $m = Marathon->new( url => 'http://my.marathon.here:8080' );
36  
37 my $app = $m->get_app('hello-marathon');
38
39 $app->instances( 23 );
40 $app->update();
41 print STDERR Dumper( $app->deployments );
42
43 sleep 10;
44
45 $app->instances( 1 );
46 $app->update( {force => 'true'} ); # should work even if the scaling up is not done yet.
47  
48 Please report [issues on github](https://github.com/geidies/perl-Marathon)
49  
50 =head1 SUBROUTINES/METHODS
51  
52 =head2 new
53  
54 Creates a Marathon object. You can pass in the URL to the marathon REST interface:
55
56 use Marathon;
57 my $marathon = Marathon->new( url => 'http://169.254.47.11:8080', verbose => 0 );
58  
59 The "verbose" parameter makes the module more chatty on STDERR.
60  
61 =cut
62  
63 4 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
64 4 my ($class, %conf) = @_;
65 4 ▶ my $url = delete $conf{url} || 'http://localhost:8080/';
Condition100%2/2
delete $conf{'url'} || 'http://localhost:8080/'
l!l
31
MC/DC100%1/1
delete $conf{'url'} || 'http://localhost:8080/'
delete $conf{'url'}
Truth table100%2/2
A
A= delete $conf{'url'}
Aresult
11
00
66 4 ▶ $Marathon::verbose = delete $conf{verbose} || 0;
Condition50%1/2
delete $conf{'verbose'} || 0
l!l
04
MC/DC0%0/1
delete $conf{'verbose'} || 0
delete $conf{'verbose'}
Truth table50%1/2
A
A= delete $conf{'verbose'}
Aresult
11
00
67 4 my $ua = LWP::UserAgent->new;
68 4 my $self = bless {
69 _ua => $ua,
70 };
71 4 $self->_set_url($url);
72 4 return $self;
73 }
74  
75 4 ▶sub _set_url { # void
Subroutine _set_url: called CC=3 SCAR=11.0
76 4 my ($self, $url) = @_;
77 4 ▶ unless ( $url =~ m,^https?\://, ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url =~ m[^https?\://]) 1 3 4
78 1 $url = 'http://' . $url;
79 }
80 4 ▶ unless ( $url =~ m,/$, ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url =~ m[/$]) 1 3 4
81 1 $url .= '/';
82 }
83 4 $self->{_url} = $url;
84 }
85  
86 =head2 get_app( $id )
87  
88 Returns a Marathon::App as identified by the single argument "id". In case there is no such app, will return undef.
89  
90 my $app = $marathon->get_app('such-1');
91 print $app->id . "\n";
92  
93 =cut
94  
95 0 ▶sub get_app { # Marathon::App
Subroutine get_app: not called CC=2 SCAR=17.9
Pod documented
96 0 my ( $self, $id ) = @_;
97 0 my $api_response = $self->_get_obj('/v2/apps/' . $id);
98 0 ▶ return undef unless defined $api_response;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $api_response 0 0 0
99 0 return Marathon::App->new( $api_response->{app}, $self );
100 }
101  
102 =head2 new_app( $config )
103  
104 Returns a new Marathon::App as described in the $config hash. Example:
105  
106 my $app = $marathon->new_app({ id => 'very-1', mem => 4, cpus => 0.1, cmd => "while [ 1 ]; do echo 'wow.'; done" });
107  
108 This will not (!) start the app in marathon. To do so, call create() on the returned object:
109  
110 $app->create();
111  
112 =cut
113  
114 0 ▶sub new_app {
Subroutine new_app: not called CC=1 SCAR=6.9
Pod documented
115 0 my ($self, $config) = @_;
116 0 return Marathon::App->new( $config, $self );
117 }
118  
119 =head2 get_group( $id )
120  
121 Works like get_app, just for groups.
122  
123 =cut
124  
125 0 ▶sub get_group { # Marathon::App
Subroutine get_group: not called CC=1 SCAR=6.9
Pod documented
126 0 my ( $self, $id ) = @_;
127 0 return Marathon::Group->get( $id, $self );
128 }
129  
130 =head2 new_group( $config )
131  
132 Creates a new group. You can either specify the apps in-line:
133  
134 my $group = $marathon->new_group( { id => 'very-1', apps: [{ id => "such-2", cmd => ... }, { id => "such-3", cmd => ... }] } );
135  
136 Or add them to the created group later:
137  
138 my $group = $marathon->new_group( { id => 'very-1' } );
139 $group->add( $marathon->new_app( { id => "such-2", cmd => ... } );
140 $group->add( $marathon->new_app( { id => "such-3", cmd => ... } );
141  
142 In any case, new_group will just return a Marathon::Group object, it will not commit to marathon until you call create() on the returned object:
143  
144 $group->create();
145  
146 =cut
147  
148 0 ▶sub new_group {
Subroutine new_group: not called CC=1 SCAR=6.9
Pod documented
149 0 my ($self, $config) = @_;
150 0 return Marathon::Group->new( $config, $self );
151 }
152  
153 =head2 events()
154  
155 Returns a Marathon::Events objects. You can register callbacks on it and start listening to the events stream.
156  
157 =cut
158  
159 0 ▶sub events {
Subroutine events: not called CC=1 SCAR=6.9
Pod documented
160 0 my $self = shift;
161 0 return Marathon::Events->new( $self );
162 }
163  
164 =head2 get_tasks( $status )
165  
166 Returns an array of currently running tasks. If $status is "running" or "staging", will filter and return only those tasks.
167  
168 =cut
169  
170 0 ▶sub get_tasks {
Subroutine get_tasks: not called CC=8 SCAR=42.8
Pod documented
171 0 my ($self, $status) = @_;
172 0 ▶ $status = '' unless $status && $status =~ m/^running|staging$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $status and $status =~ /^running|staging$/ 0 0 0
Condition0%0/3
$status and $status =~ /^running|staging$/
!ll&&!rl&&r
000
MC/DC0%0/2
$status and $status =~ /^running|staging$/
$status $status =~ /^running|staging$/
Truth table0%0/3
A and B
A= $statusB= $status =~ /^running|staging$/
ABresult
0X0
100
111
173 0 ▶ if ( $status ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status) 0 0 0
174 0 $status = '?status='.$status;
175 }
176 0 my $task_obj = $self->_get_obj_from_json('/v2/tasks'.$status);
177 0 ▶ my $task_arrayref = ( defined $task_obj && exists $task_obj->{tasks} && $task_obj->{tasks} ) || [];
Condition0%0/8
defined $task_obj && exists $task_obj->{'tasks'} && $task_obj->{'tasks'} || []
l!l
00
defined $task_obj and exists $task_obj->{"tasks"} and $task_obj->{"tasks"}
!ll&&!rl&&r
000
defined $task_obj and exists $task_obj->{"tasks"}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $task_obj && exists $task_obj->{'tasks'} && $task_obj->{'tasks'} || []
defined $task_obj exists $task_obj->{"tasks"} $task_obj->{"tasks"}
Truth table0%0/4
A and B and C
A= defined $task_objB= exists $task_obj->{"tasks"}C= $task_obj->{"tasks"}
ABCresult
1111
0XX0
10X0
1100
178 0 ▶ return wantarray ? @{$task_arrayref} : $task_arrayref;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
179 }
180  
181 =head2 kill_tasks({ tasks => $@ids, scale => bool })
182  
183 Kills the tasks with the given @ids. Scales if the scale param is true.
184  
185 =cut
186  
187 0 ▶sub kill_tasks {
Subroutine kill_tasks: not called CC=5 SCAR=34.0
Pod documented
188 0 my ($self, $args) = @_;
189 0 ▶ my $param = $args && $args->{scale} && $args->{scale} && $args->{scale} !~ /false/i ? '?scale=true' : ''; #default is false
Branch: 1 branch
BranchTrueFalseTotal
$args && $args->{'scale'} && $args->{'scale'} && !($args->{'scale'} =~ /false/i) ? : 0 0 0
Condition0%0/9
$args and $args->{"scale"} and $args->{"scale"} and not $args->{"scale"} =~ /false/i
!ll&&!rl&&r
000
$args and $args->{"scale"} and $args->{"scale"}
!ll&&!rl&&r
000
$args and $args->{"scale"}
!ll&&!rl&&r
000
MC/DC0%0/4
$args and $args->{"scale"} and $args->{"scale"} and not $args->{"scale"} =~ /false/i
$args $args->{"scale"} $args->{"scale"} not $args->{"scale"} =~ /false/i
Truth table0%0/5
A and B and C and not(D)
A= $argsB= $args->{"scale"}C= $args->{"scale"}D= not $args->{"scale"} =~ /false/i
ABCDresult
0XXX0
10XX0
110X0
11100
11111
190 0 return $self->_put_post_delete( 'POST', '/v2/tasks/delete'.$param, { ids => $args->{tasks} } );
191 }
192  
193 =head2 get_deployments
194  
195 Returns a list of Marathon::Deployment objects with the currently running deployments.
196  
197 =cut
198  
199 0 ▶sub get_deployments {
Subroutine get_deployments: not called CC=3 SCAR=24.8
Pod documented
200 0 my $self = shift;
201 0 my $deployments = $self->_get_obj('/v2/deployments');
202 0 my @depl_objs = ();
203 0 foreach ( @{$deployments} ) {
204 0 push @depl_objs, Marathon::Deployment->new( $_, $self );
205 }
206 0 ▶ return wantarray ? @depl_objs : \@depl_objs;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
207 }
208  
209 =head2 kill_deployment( $id, { force => bool } )
210  
211 Stop the deployment with given id.
212  
213 =cut
214  
215 0 ▶sub kill_deployment {
Subroutine kill_deployment: not called CC=5 SCAR=34.0
Pod documented
216 0 my ($self, $id, $args) = @_;
217 0 ▶ my $param = $args && $args->{force} && $args->{force} && $args->{force} !~ /false/i ? '?force=true' : ''; #default is false
Branch: 1 branch
BranchTrueFalseTotal
$args && $args->{'force'} && $args->{'force'} && !($args->{'force'} =~ /false/i) ? : 0 0 0
Condition0%0/9
$args and $args->{"force"} and $args->{"force"} and not $args->{"force"} =~ /false/i
!ll&&!rl&&r
000
$args and $args->{"force"} and $args->{"force"}
!ll&&!rl&&r
000
$args and $args->{"force"}
!ll&&!rl&&r
000
MC/DC0%0/4
$args and $args->{"force"} and $args->{"force"} and not $args->{"force"} =~ /false/i
$args $args->{"force"} $args->{"force"} not $args->{"force"} =~ /false/i
Truth table0%0/5
A and B and C and not(D)
A= $argsB= $args->{"force"}C= $args->{"force"}D= not $args->{"force"} =~ /false/i
ABCDresult
0XXX0
10XX0
110X0
11100
11111
218 0 return $self->_put_post_delete( 'DELETE', '/v2/deployments/' . $id . $param );
219 }
220  
221 1 ▶sub get_endpoint {
Subroutine get_endpoint: called CC=1 SCAR=0.0
Pod undocumented
222 1 my ( $self, $path ) = @_;
223 1 my $url = $self->{_url} . $path;
224 1 $url =~ s,/+,/,g;
225 1 $url =~ s,^http:/,http://,;
226 1 return $url;
227 }
228  
229 =head2 metrics
230  
231 returns the metrics returned by the /metrics endpoint, converted from json to perl.
232  
233 =cut
234  
235 0 ▶sub metrics {
Subroutine metrics: not called CC=1 SCAR=6.9
Pod documented
236 0 my $self = shift;
237 0 return $self->_get_obj('/metrics');
238 }
239  
240 =head2 help
241  
242 returns the html returned by the /help endpoint.
243  
244 =cut
245  
246 0 ▶sub help { # string (html)
Subroutine help: not called CC=1 SCAR=6.9
Pod documented
247 0 my $self = shift;
248 0 return $self->_get_html('/help');
249 }
250  
251 =head2 logging
252  
253 returns the html returned by the /logging endpoint.
254  
255 =cut
256  
257 0 ▶sub logging { # string (html)
Subroutine logging: not called CC=1 SCAR=6.9
Pod documented
258 0 my $self = shift;
259 0 return $self->_get_html('/logging');
260 }
261  
262 =head2 ping
263  
264 returns 1 if the master responds to a ping request.
265  
266 =cut
267  
268 1 ▶sub ping { # string (plaintext)
Subroutine ping: called CC=2 SCAR=7.2
Pod documented
269 1 my $self = shift;
270 1 ▶ return $self->_get_html('/ping') =~ m,pong, ? 'pong' : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->_get_html('/ping') =~ /pong/ ? : 0 1 1
271 }
272  
273 1 ▶sub _get { # HTTP::Response
Subroutine _get: called CC=1 SCAR=0.0
274 1 my ( $self, $path ) = @_;
275 1 my $url = $self->get_endpoint( $path );
276 1 my $response = $self->{_ua}->get( $url );
277 1 $self->_response_handler( 'GET', $response );
278 1 return $response;
279 }
280  
281 1 ▶sub _get_html { # string (html) or undef on error
Subroutine _get_html: called CC=2 SCAR=7.4
282 1 my ( $self, $path ) = @_;
283 1 my $response = $self->_get($path);
284 1 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) 0 1 1
285 0 return $response->decoded_content;
286 }
287 1 return '';
288 }
289  
290 0 ▶sub _get_obj { # hashref
Subroutine _get_obj: not called CC=2 SCAR=17.9
291 0 my ( $self, $path ) = @_;
292 0 my $response = $self->_get_html($path);
293 0 ▶ if ($response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response) 0 0 0
294 0 return decode_json $response;
295 }
296 0 return undef;
297 }
298  
299 0 ▶sub _get_obj_from_json { # hashref
Subroutine _get_obj_from_json: not called CC=2 SCAR=17.9
300 0 my ( $self, $path ) = @_;
301 0 my $response = $self->_put_post_delete('GET', $path);
302 0 ▶ if ($response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response) 0 0 0
303 0 return decode_json $response;
304 }
305 0 return undef;
306 }
307  
308 0 ▶sub _post {
Subroutine _post: not called CC=1 SCAR=6.9
309 0 my ($self, $path, $payload) = @_;
310 0 return $self->_put_post_delete( 'POST', $path, $payload );
311 }
312  
313 0 ▶sub _put {
Subroutine _put: not called CC=1 SCAR=6.9
314 0 my ($self, $path, $payload) = @_;
315 0 return $self->_put_post_delete( 'PUT', $path, $payload );
316 }
317  
318 0 ▶sub _delete {
Subroutine _delete: not called CC=1 SCAR=6.9
319 0 my ($self, $path, $payload) = @_;
320 0 return $self->_put_post_delete( 'DELETE', $path, $payload );
321 }
322  
323 0 ▶sub _put_post_delete {
Subroutine _put_post_delete: not called CC=3 SCAR=24.8
324 0 my ($self, $method, $path, $payload) = @_;
325 0 my $req = HTTP::Request->new( $method, $self->get_endpoint($path) );
326 0 $req->header( 'Accept' => 'application/json' );
327 0 ▶ if ( $payload ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($payload) 0 0 0
328 0 $req->header( 'Content-Type' => 'application/json' );
329 0 $req->content( encode_json $payload );
330 }
331 0 my $response = $self->{_ua}->request( $req );
332 0 $self->_response_handler( $method, $response );
333 0 ▶ return $response->is_success ? $response->decoded_content : undef;
Branch: 1 branch
BranchTrueFalseTotal
$response->is_success ? : 0 0 0
334 }
335  
336 1 ▶sub _response_handler {
Subroutine _response_handler: called CC=3 SCAR=16.7
337 1 my ( $self, $method, $response ) = @_;
338 1 ▶ if ( $verbose ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Marathon::verbose) 0 1 1
339 0 ▶ unless ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
340 0 print STDERR 'Error doing '.$method.' against '. $response->base.': ' . $response->status_line . "\n";
341 0 print STDERR $response->decoded_content ."\n";
342 } else {
343 0 print STDERR $response->status_line . "\n"
344 }
345 }
346 1 return $response;
347 }
348  
349 =head1 AUTHOR
350  
351 Sebastian Geidies, C<< <seb at geidi.es> >>
352  
353 =head1 BUGS
354  
355 Please report [issues on github](https://github.com/geidies/perl-Marathon)
356  
357 =head1 SUPPORT
358  
359 You can find documentation for this module with the perldoc command.
360  
361 perldoc Marathon
362  
363  
364 You can also look for information at:
365  
366 =over 4
367  
368 =item * RT: CPAN's request tracker (report bugs here)
369  
370 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Marathon>
371  
372 =item * AnnoCPAN: Annotated CPAN documentation
373  
374 L<http://annocpan.org/dist/Marathon>
375  
376 =item * CPAN Ratings
377  
378 L<http://cpanratings.perl.org/d/Marathon>
379  
380 =item * Search CPAN
381  
382 L<http://search.cpan.org/dist/Marathon/>
383  
384 =back
385  
386  
387 =head1 LICENSE AND COPYRIGHT
388  
389 Copyright 2015 Sebastian Geidies.
390  
391 This program is free software; you can redistribute it and/or modify it
392 under the terms of the the Artistic License (2.0). You may obtain a
393 copy of the full license at:
394  
395 L<http://www.perlfoundation.org/artistic_license_2_0>
396  
397 Any use, modification, and distribution of the Standard or Modified
398 Versions is governed by this Artistic License. By using, modifying or
399 distributing the Package, you accept this license. Do not use, modify,
400 or distribute the Package, if you do not accept this license.
401  
402 If your Modified Version has been derived from a Modified Version made
403 by someone other than you, you are nevertheless required to ensure that
404 your Modified Version complies with the requirements of this license.
405  
406 This license does not grant you the right to use any trademark, service
407 mark, tradename, or logo of the Copyright Holder.
408  
409 This license includes the non-exclusive, worldwide, free-of-charge
410 patent license to make, have made, use, offer to sell, sell, import and
411 otherwise transfer the Package with respect to any patent claims
412 licensable by the Copyright Holder that are necessarily infringed by the
413 Package. If you institute patent litigation (including a cross-claim or
414 counterclaim) against any party alleging that the Package constitutes
415 direct or contributory patent infringement, then this Artistic License
416 to you shall terminate on the date that such litigation is filed.
417  
418 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
419 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
420 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
421 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
422 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
423 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
424 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
425 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
426  
427  
428 =cut
429  
430 4 1; # End of Marathon
« Marathon/Deployment.pm ↑ Summary Marathon/Group.pm »