| 1 | package App::FargateStack::Builder::Service; | ||||||||||||||||||||||||||||
| 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 Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use English qw(-no_match_vars); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use List::Util qw(any); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use App::FargateStack::Constants; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Role::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | ######################################################################## | ||||||||||||||||||||||||||||
| 16 | 0 | ▶ | sub build_service { | ||||||||||||||||||||||||||
|
Subroutine
build_service: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 17 | ######################################################################## | ||||||||||||||||||||||||||||
| 18 | 0 | my ( $self, $service_name, $desired_count ) = @_; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | my ( $config, $dryrun, $tasks, $cluster ) = $self->common_args(qw(config dryrun tasks cluster)); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | my $ecs = $self->fetch_ecs; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | my $task = $tasks->{$service_name}; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | my @vpc_subnets = $self->get_service_subnets( $task->{type} ); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | ▶ | my @services = @{ $ecs->list_services( $cluster->{name}, 'serviceArns' ) || [] }; | ||||||||||||||||||||||||||
|
Condition0%0/2
$ecs->list_services($cluster->{'name'}, 'serviceArns') || []
MC/DC0%0/1
$ecs->list_services($cluster->{'name'}, 'serviceArns') || []
$ecs->list_services($cluster->{'name'}, 'serviceArns')
Truth table0%0/2
A
A= $ecs->list_services($cluster->{'name'}, 'serviceArns')
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | my @desired_services = $service_name ? ($service_name) : keys %{$tasks}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | $self->log_warn( 'service: creating %d service(s) [%s]', scalar(@desired_services), join q{,}, @desired_services ); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | my $public_ip = $FALSE; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | foreach my $task_name (@desired_services) { | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | my $task = $tasks->{$task_name}; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | if ( any { $_ eq $task_name } @services ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 0 | $self->get_logger->info( sprintf 'service: [%s] already exists...skipping', $task_name ); | |||||||||||||||||||||||||||
| 42 | 0 | next; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | $self->get_logger->info( sprintf 'service: creating service: [%s] with [%s] task(s) in subnets: [%s]...%s', | |||||||||||||||||||||||||||
| 46 | $task_name, $desired_count, ( join q{,}, @vpc_subnets ), $dryrun ); | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | if ( !$dryrun ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | my $result = $ecs->create_service( | ||||||||||||||||||||||||||||
| 50 | service_name => $task_name, | ||||||||||||||||||||||||||||
| 51 | container_name => $task_name, | ||||||||||||||||||||||||||||
| 52 | cluster_name => $config->{cluster}->{name}, | ||||||||||||||||||||||||||||
| 53 | task_definition => $task_name, | ||||||||||||||||||||||||||||
| 54 | desired_count => $desired_count, | ||||||||||||||||||||||||||||
| 55 | subnets => \@vpc_subnets, | ||||||||||||||||||||||||||||
| 56 | public_ip => $public_ip, | ||||||||||||||||||||||||||||
| 57 | security_groups => [ $config->{security_groups}->{fargate}->{group_id} ], | ||||||||||||||||||||||||||||
| 58 | $self->get_http ? ( target_group_arn => $task->{target_group_arn} ) : (), | ||||||||||||||||||||||||||||
| 59 | 0 | ▶ | $task->{container_port} ? ( container_port => 0 + $task->{container_port} ) : (), | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | $self->log_debug( sub { return Dumper( [ result => $result ] ); } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | $ecs->check_result( { message => 'ERROR: could not create service: [%s], desired count: [%s]' }, | |||||||||||||||||||||||||||
| 65 | $task_name, $desired_count ); | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | return; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | ######################################################################## | ||||||||||||||||||||||||||||
| 73 | # Best Practice: if we have are an HTTP service we should put the task in the same | ||||||||||||||||||||||||||||
| 74 | # AZs as the ALB | ||||||||||||||||||||||||||||
| 75 | ######################################################################## | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | sub get_service_subnets { | ||||||||||||||||||||||||||
|
Subroutine
get_service_subnets: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | ######################################################################## | ||||||||||||||||||||||||||||
| 78 | 0 | my ( $self, $type ) = @_; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | my ( $config, $alb ) = $self->common_args(qw(config alb)); | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | my $alb_arn = defined $alb && $alb->{arn}; | ||||||||||||||||||||||||||
|
Condition0%0/3
defined $alb && $alb->{'arn'}
MC/DC0%0/2
defined $alb && $alb->{'arn'}
defined $alb
$alb->{'arn'}
Truth table0%0/3
A && B
A= defined $albB= $alb->{'arn'}
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | if ( $alb_arn && $type =~ /^https?/xsm ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$alb_arn and $type =~ /^https?/msx
MC/DC0%0/2
$alb_arn and $type =~ /^https?/msx
$alb_arn
$type =~ /^https?/msx
Truth table0%0/3
A and B
A= $alb_arnB= $type =~ /^https?/msx
| |||||||||||||||||||||||||||||
| 85 | 0 | $self->get_logger->warn('service: service will be placed in the same AZs as load balancer.'); | |||||||||||||||||||||||||||
| 86 | 0 | return $self->get_task_subnets($alb_arn); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # this is for tasks and services other than HTTP or HTTPS | ||||||||||||||||||||||||||||
| 90 | 0 | my $subnets = $self->get_subnets->{private}; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | if ( !$subnets || !@{$subnets} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$subnets and @{$subnets;}
MC/DC0%0/2
$subnets and @{$subnets;}
$subnets
@{$subnets;}
Truth table0%0/3
A and B
A= $subnetsB= @{$subnets;}
| |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if ( $subnets = $self->get_subnets->{public} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | $self->get_logger->warn('service: no private subnets! Using public subnets is not recommended.'); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | else { | ||||||||||||||||||||||||||||
| 97 | 0 | croak sprintf "no subnets in %s found\n", $config->{vpc_id}; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | return @{$subnets}[ 0, 1 ]; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | ######################################################################## | ||||||||||||||||||||||||||||
| 105 | 0 | ▶ | sub get_task_subnets { | ||||||||||||||||||||||||||
|
Subroutine
get_task_subnets: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 106 | ######################################################################## | ||||||||||||||||||||||||||||
| 107 | 0 | my ( $self, $alb_arn ) = @_; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # fetch ALB AZs | ||||||||||||||||||||||||||||
| 110 | 0 | my $elb = $self->fetch_elbv2; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | my $az_names = $elb->describe_load_balancers( | |||||||||||||||||||||||||||
| 113 | arn => $alb_arn, | ||||||||||||||||||||||||||||
| 114 | query => 'LoadBalancers[].AvailabilityZones[].ZoneName', | ||||||||||||||||||||||||||||
| 115 | ); | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | $elb->check_result( message => 'ERROR: could not describe load balancer: [%s]', $alb_arn ); | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # fetch AZs of private subnets | ||||||||||||||||||||||||||||
| 120 | 0 | my $ec2 = $self->fetch_ec2; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | my $private_subnets = $self->get_config->{subnets}->{private} // []; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->get_config->{"subnets"}{"private"} // []
MC/DC0%0/1
$self->get_config->{"subnets"}{"private"} // []
$self->get_config->{"subnets"}{"private"}
Truth table0%0/2
A
A= $self->get_config->{"subnets"}{"private"}
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # we should not be able to get here...since we check this on startup | ||||||||||||||||||||||||||||
| 125 | 0 | ▶ | if ( !$private_subnets || !@{$private_subnets} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$private_subnets and @{$private_subnets;}
MC/DC0%0/2
$private_subnets and @{$private_subnets;}
$private_subnets
@{$private_subnets;}
Truth table0%0/3
A and B
A= $private_subnetsB= @{$private_subnets;}
| |||||||||||||||||||||||||||||
| 126 | 0 | $self->log_error('ERROR: no private subnets available.'); | |||||||||||||||||||||||||||
| 127 | 0 | $self->log_errror('App::FargateStack does not support placing tasks in a public subnets for HTTP services.'); | |||||||||||||||||||||||||||
| 128 | 0 | exit 1; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | my $result = $ec2->describe_subnets( | |||||||||||||||||||||||||||
| 132 | { subnets => $private_subnets, | ||||||||||||||||||||||||||||
| 133 | query => 'Subnets[].{az_name:AvailabilityZone,subnet_id:SubnetId}' | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | ); | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | $ec2->check_result( message => 'ERROR: could not describe subnets: [%s]', join q{,}, @{$private_subnets} ); | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | $self->log_debug( sub { return Dumper( [ subnet => $result ] ) } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | my %subnets_by_zone = map { $_->{az_name} => $_->{subnet_id} } @{$result}; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | $self->log_debug( sub { return Dumper( [ subnets_by_zone => \%subnets_by_zone ] ) } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | # Best practice: pick subnets whose AZs match the ALB's AZs, preserve order | ||||||||||||||||||||||||||||
| 146 | 0 | my @selected = grep {defined} @subnets_by_zone{ @{$az_names} }; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 0 | return @selected; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 1 | 1; | |||||||||||||||||||||||||||