FargateStack/CreateStack.pm

filter: Statementstmt 22.8% 34 / 149 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 68.7% 11 / 16 Podpod 0.0% 0 / 5 totaltotal 17.3% 45 / 259 CC 39 SCAR 59.6
CC
39
Cov
15%
cmd_create_stack
756.0
parse_autoscaling_option
42.0
resolve_image_name
30.0
CRAP 386.5
↑ Summary FargateStack/Builder/HTTPService.pm »
1 package App::FargateStack::CreateStack;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use App::FargateStack::Builder::Utils qw(log_die dmp);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use App::FargateStack::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use CLI::Simple::Constants qw(:booleans :chars);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use YAML qw(Dump DumpFile);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 ########################################################################
18 0 ▶sub cmd_create_stack {
Subroutine cmd_create_stack: not called CC=27 SCAR=66.3
Pod undocumented
19 ########################################################################
20 0 my ($self) = @_;
21  
22 0 my ( $app_name, @args ) = $self->get_args;
23  
24 0 ▶ if ( lc $app_name !~ /\A[[:lower:]\d_-]+\z/xsm ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc !($app_name =~ /\A[[:lower:]\d_-]+\z/msx)) 0 0 0
25 0 ▶ if ( $app_name =~ /^(?:daemon|https?|scheduled|task):(.*?)$/xsm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($app_name =~ /^(?:daemon|https?|scheduled|task):(.*?)$/msx) { } 0 0 0
26 0 unshift @args, $app_name;
27 0 $app_name = $1;
28 0 carp sprintf "WARNING: no app name provided...using the task name: [%s]\n", $app_name;
29 }
30 else {
31 0 croak sprintf "ERROR: app names should only have a-z_-0-9 characters\n";
32 }
33 }
34  
35 0 ▶ my $config_name = $self->get_config_name // "$app_name.yml";
Condition0%0/2
$self->get_config_name // "$app_name.yml"
l!l
00
MC/DC0%0/1
$self->get_config_name // "$app_name.yml"
$self->get_config_name
Truth table0%0/2
A
A= $self->get_config_name
Aresult
11
00
36 0 $self->set_config_name($config_name);
37  
38 0 my $sts = $self->fetch_sts;
39  
40 0 my $account = $sts->get_caller_identity('Account');
41 0 $sts->check_result( message => 'ERROR: could not determine account for profile: [%s]', $self->get_profile );
42  
43 0 my $config = {
44 account => $account,
45 profile => $self->get_profile,
46 region => $self->get_region,
47 app => { name => $app_name },
48 };
49  
50 0 my %tasks;
51  
52 0 ▶ while ( my $tag = shift @args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag = shift @args) 0 0 0
53  
54 0 ▶ if ( $tag =~ /^(daemon|task|scheduled|https?):/xsm ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tag =~ /^(daemon|task|scheduled|https?):/msx) { } 0 0 0
elsif (not keys %tasks) { } 0 0 0
55 0 $tasks{$tag} = [];
56  
57 0 ▶ while ( my $next_tag = shift @args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next_tag = shift @args) 0 0 0
58 0 ▶ if ( $next_tag =~ /^(?:daemon|task|scheduled|https?):/xsm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_tag =~ /^(?:daemon|task|scheduled|https?):/msx) 0 0 0
59 0 unshift @args, $next_tag;
60 0 last;
61 }
62 0 push @{ $tasks{$tag} }, $next_tag;
63 }
64 }
65 elsif ( !keys %tasks ) {
66 0 croak "ERROR: start your task definition with daemon|task|scheduled|http|https\n";
67 }
68 }
69  
70 0 foreach my $t ( keys %tasks ) {
71 0 my ( $type, $name ) = split /[:]/xsm, $t;
72 0 $self->log_info( 'create-stack: configuring: %s %s', $type, $name );
73  
74 0 my @options = @{ $tasks{$t} };
75  
76 0 ▶ my $task = { type => $type eq 'scheduled' ? 'task' : $type };
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'scheduled' ? : 0 0 0
77 0 $config->{tasks}->{$name} = $task;
78  
79 0 my ($image) = grep {/^image:/xsm} @options;
80  
81 0 ▶ croak sprintf "ERROR: task [%s] does not have an image...every task must have an image\n", $name
Branch: 1 branch
BranchTrueFalseTotal
unless $image 0 0 0
82 if !$image;
83  
84 0 my ($image_name) = $image =~ /^image:(.*)$/xsm;
85  
86 # If the user omitted a tag, you can optionally default to :latest
87 # $image_name .= ':latest' if $image_name !~ /:[^\/]+$/;
88  
89 0 my ( $repo, $tag ) = split /:/xsm, $image_name, 2;
90 0 ▶ $tag //= 'latest';
Condition0%0/2
$tag //= "latest"
l!l
00
MC/DC0%0/1
$tag //= "latest"
$tag
Truth table0%0/2
A
A= $tag
Aresult
11
00
91  
92 0 ▶ if ( $image_name !~ m{/}xsm ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $image_name =~ m[/]msx) { } 0 0 0
93 # ECR shorthand (repo[:tag]) in current account
94  
95 0 my $ecr = $self->fetch_ecr;
96 0 my $images = $ecr->describe_images( $repo, 'imageDetails' );
97  
98 0 ▶ if ( !$images || !@{$images} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $images && @{$images;}) { } 0 0 0
Condition0%0/3
$images && @{$images;}
!ll&&!rl&&r
000
MC/DC0%0/2
$images && @{$images;}
$images @{$images;}
Truth table0%0/3
A && B
A= $imagesB= @{$images;}
ABresult
0X0
100
111
99 0 $self->log_warn( 'create-stack: %s not found in ECR...assuming public image', $image );
100 0 $task->{image} = "$image_name:$tag"; # leave as-is; ECS will try public registries
101 }
102 else {
103 0 my $uri = sprintf '%s.dkr.ecr.%s.amazonaws.com/%s:%s', $account, $self->get_region, $image_name, $tag;
104 0 $task->{image} = $uri;
105 }
106 }
107 else {
108 # Fully-qualified external or cross-account reference; accept as-is
109 0 $task->{image} = "$repo:$tag";
110 }
111  
112 0 my ($schedule) = grep {/^schedule:/xsm} @options;
113  
114 0 ▶ if ( $t =~ /^scheduled:/xsm || ( $t =~ /task:/sxm && $schedule ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t =~ /^scheduled:/msx or $t =~ /task:/msx and $schedule) 0 0 0
Condition0%0/6
$t =~ /^scheduled:/msx or $t =~ /task:/msx and $schedule
l!l&&r!l&&!r
000
$t =~ /task:/msx and $schedule
!ll&&!rl&&r
000
MC/DC0%0/3
$t =~ /^scheduled:/msx or $t =~ /task:/msx and $schedule
$t =~ /^scheduled:/msx $t =~ /task:/msx $schedule
Truth table0%0/4
A or B and C
A= $t =~ /^scheduled:/msxB= $t =~ /task:/msxC= $schedule
ABCresult
1XX1
0111
00X0
0100
115  
116 0 ▶ croak "ERROR: scheduled tasks must have a schedule\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $schedule 0 0 0
117 if !$schedule;
118  
119 0 $task->{schedule} = ( split /[:]/xsm, $schedule )[-1];
120 }
121  
122 0 ▶ if ( $t =~ /^(https?):/xsm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t =~ /^(https?):/msx) 0 0 0
123 0 my ($domain) = grep {/^domain:/xsm} @options;
124  
125 0 ▶ croak "ERROR: HTTP and HTTPS services must have a domain\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $domain 0 0 0
126 if !$domain;
127  
128 0 $config->{domain} = ( split /[:]/xsm, $domain )[-1];
129 }
130  
131 ####################################################################
132 # Environment
133 ####################################################################
134  
135 0 my @environment = grep {/^env(?:ironment)?:/xsm} @options;
136  
137 0 $self->parse_environment_option(
138 config => $config,
139 task => $task,
140 tag => \@environment
141 );
142 ####################################################################
143 # WAF
144 ####################################################################
145 0 my ($waf) = grep {/^waf:.*$/xsm} @options;
146  
147 0 ▶ if ($waf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($waf) 0 0 0
148 0 ▶ croak "ERROR: waf option is only available for https services:\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $t =~ /^https:/msx 0 0 0
149 if $t !~ /^https:/xsm;
150  
151 0 $self->parse_waf_option(
152 task => $task,
153 config => $config,
154 tag => $waf
155 );
156 }
157  
158 ####################################################################
159 # Autoscaling
160 ####################################################################
161 0 my ($autoscaling) = grep {/autoscaling:.*$/xsm} @options;
162  
163 0 ▶ if ($autoscaling) {
Branch: 1 branch
BranchTrueFalseTotal
if ($autoscaling) 0 0 0
164 0 ▶ croak "ERROR: autoscaling option is only available for http and daemon services:\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $t =~ /^(?:https?|daemon):/msx 0 0 0
165 if $t !~ /^(?:https?|daemon):/xsm;
166  
167 0 $self->parse_autoscaling_option(
168 task => $task,
169 config => $config,
170 tag => $autoscaling
171 );
172 }
173  
174 }
175  
176 0 $self->fetch_option_defaults;
177  
178 0 print {*STDOUT} Dump($config);
179  
180 0 ▶ if ( $self->get_update ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_update) 0 0 0
181 0 DumpFile( $config_name, $config );
182 }
183  
184 0 return;
185 }
186  
187 ########################################################################
188 0 ▶sub parse_environment_option {
Subroutine parse_environment_option: not called CC=2 SCAR=17.9
Pod undocumented
189 ########################################################################
190 0 my ( $self, %args ) = @_;
191  
192 0 my ( $task, $environment ) = @args{qw(task tag)};
193  
194 0 foreach ( @{$environment} ) {
195 0 my ( $key, $value ) = split /\s*=\s*/xsm, ( split /:/xsm )[-1];
196 0 $task->{environment} = { $key => $value };
197 }
198  
199 0 return;
200 }
201  
202 ########################################################################
203 0 ▶sub parse_autoscaling_option {
Subroutine parse_autoscaling_option: not called CC=6 SCAR=37.4
Pod undocumented
204 ########################################################################
205 0 my ( $self, %args ) = @_;
206  
207 0 my ( $config, $task, $autoscaling ) = @args{qw(config task tag)};
208  
209 my $autoscaling_config = $task->{autoscaling} = {
210 0 min_capacity => $DEFAULT_AUTOSCALING_MIN_CAPACITY,
211 max_capacity => $DEFAULT_AUTOSCALING_MAX_CAPACITY,
212 scale_out_cooldown => $DEFAULT_AUTOSCALING_SCALE_OUT_COOLDOWN,
213 scale_in_cooldown => $DEFAULT_AUTOSCALING_SCALE_IN_COOLDOWN,
214 };
215  
216 0 my ($metric) = $autoscaling =~ /:([^=]+)/xsm;
217  
218 croak "ERROR: requests can only be used with http services\n"
219 0 ▶ if $metric eq 'requests' && $task->{type} !~ /^http/xsm;
Branch: 1 branch
BranchTrueFalseTotal
if $metric eq "requests" and not $task->{"type"} =~ /^http/msx 0 0 0
Condition0%0/3
$metric eq "requests" and not $task->{"type"} =~ /^http/msx
!ll&&!rl&&r
000
MC/DC0%0/2
$metric eq "requests" and not $task->{"type"} =~ /^http/msx
$metric eq "requests" not $task->{"type"} =~ /^http/msx
Truth table0%0/3
A and not(B)
A= $metric eq "requests"B= not $task->{"type"} =~ /^http/msx
ABresult
0X0
100
111
220  
221 0 ▶ croak "ERROR: metric must be cpu or requests\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $metric =~ /^cpu|requests/msx 0 0 0
222 if $metric !~ /^cpu|requests/xsm;
223  
224 0 my ($metric_value) = $autoscaling =~ /=(.*)$/xsm;
225  
226 0 ▶ if ( !$metric_value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($metric_value) 0 0 0
227 0 ▶ $metric_value = $metric eq 'cpu' ? $DEFAULT_CPU_SCALING_LEVEL : $DEFAULT_REQUESTS_SCALING_LEVEL;
Branch: 1 branch
BranchTrueFalseTotal
$metric eq 'cpu' ? : 0 0 0
228 }
229  
230 # TBD: check for sane values
231 0 $autoscaling_config->{$metric} = $metric_value;
232  
233 0 return;
234 }
235  
236 ########################################################################
237 0 ▶sub parse_waf_option {
Subroutine parse_waf_option: not called CC=3 SCAR=24.8
Pod undocumented
238 ########################################################################
239 0 my ( $self, %args ) = @_;
240  
241 0 my ( $config, $task, $waf ) = @args{qw(config task tag)};
242  
243 0 $config->{alb}->{waf} = { enabled => 'true' };
244  
245 0 my ($rule_list) = reverse split /:/xsm, $waf;
246  
247 0 my @rules;
248  
249 0 ▶ if ( $rule_list =~ /^(?:enabled|true|default)$/xsm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule_list =~ /^(?:enabled|true|default)$/msx) { } 0 0 0
250 0 push @rules, 'default';
251 }
252 else {
253 0 @rules = split /,/xsm, $rule_list;
254 }
255  
256 0 ▶ if (@rules) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rules) 0 0 0
257 0 $config->{alb}->{waf}->{managed_rules} = \@rules;
258 }
259  
260 0 return;
261 }
262  
263 ########################################################################
264 0 ▶sub resolve_image_name {
Subroutine resolve_image_name: not called CC=5 SCAR=34.0
Pod undocumented
265 ########################################################################
266 0 my ( $self, $image_name ) = @_;
267  
268 0 ▶ return $image_name
Branch: 1 branch
BranchTrueFalseTotal
if $image_name =~ m[/]msx 0 0 0
269 if $image_name =~ m{/}xsm;
270  
271 # ECR shorthand (repo[:tag]) in current account
272 0 my ( $repo, $tag ) = split /:/xsm, $image_name, 2;
273 0 ▶ $tag //= 'latest';
Condition0%0/2
$tag //= "latest"
l!l
00
MC/DC0%0/1
$tag //= "latest"
$tag
Truth table0%0/2
A
A= $tag
Aresult
11
00
274  
275 0 my $ecr = $self->fetch_ecr;
276 0 my $images = $ecr->describe_images( $repo, 'imageDetails' );
277  
278 return $image_name
279 0 ▶ if !$images || !@{$images};
Branch: 1 branch
BranchTrueFalseTotal
unless $images and @{$images;} 0 0 0
Condition0%0/3
$images and @{$images;}
!ll&&!rl&&r
000
MC/DC0%0/2
$images and @{$images;}
$images @{$images;}
Truth table0%0/3
A and B
A= $imagesB= @{$images;}
ABresult
0X0
100
111
280  
281 0 return sprintf '%s.dkr.ecr.%s.amazonaws.com/%s:%s', $self->get_account, $self->get_region, $image_name, $tag;
282 }
283  
284 1 1;
↑ Summary FargateStack/Builder/HTTPService.pm »