App-FargateStack

 view release on metacpan or  search on metacpan

lib/App/FargateStack/Init.pm  view on Meta::CPAN

  my ( $skip_init, $skip_config ) = @{$cmd_options}{qw(skip_init skip_config)};

  my $config = $skip_config ? {} : $self->_init_config;

  $self->set_config($config);

  $self->_init_defaults($config);

  return
    if $skip_init;

  my $dryrun = $self->get_dryrun;
  $self->set_dryrun( $dryrun ? '(dryrun)' : $EMPTY );

  log_die( $self, 'ERROR: when applying changes --no-update is not allowed' )
    if !$dryrun && !$self->get_update && $command eq 'apply';

  $self->section_break;

  $self->log_info( '%s %s (c) Copyright 2025 TBC Development Group, LLC', ref $self, $VERSION );
  $self->section_break;

  $self->_init_account;

  my $vpc_id = $config->{vpc_id};

  my $ec2 = $self->_init_ec2( $vpc_id, %{ $self->get_global_options } );

  if ( !$vpc_id ) {
    $vpc_id = $ec2->get_vpc_id;
    $config->{vpc_id} = $vpc_id;
  }

  $config->{subnets} = $ec2->get_subnets;

  $self->fetch_ecs->set_ec2($ec2);

  my $elb = $self->fetch_elbv2( vpc_id => $vpc_id, ec2 => $ec2 );

  # this will determine if we have an http service defined, configure
  # the ALB if it is not set explicitly and check on required parameters
  $self->_init_tasks();

  $self->_init_route53();

  $self->get_logger->trace( sub { return Dumper( [ config => $config ] ); } );

  $self->show_config;

  # only install die handler for apply - this makes sure we record any
  # provisioned resources
  if ( $self->command =~ /^(?:apply|destroy|delete-)$/xsmi ) {
    $SIG{__DIE__} = sub {
      my $msg = shift;

      if ($EXCEPTIONS_BEING_CAUGHT) {  # eval
        warn $msg;
        return;
      }

      # if config exists...we have removed last_updated and id
      if ( $config && $config->{config_name} ) {
        warn sprintf "Unclean shutdown - writing config file to [%s]\n", $config->{config_name};
        eval { YAML::DumpFile( $config->{config_name}, $config ); }
      }

      die $msg;
    };
  }

  return $TRUE;
}

########################################################################
sub _init_ec2 {
########################################################################
  my ($self) = @_;

  my $config = $self->get_config;

  my $subnets = $config->{subnets};

  my %options = (
    vpc_id => $config->{vpc_id},
    ( $self->get_cache && $subnets ) ? ( subnets => $subnets ) : (),
    %{ $self->get_global_options },
  );

  $self->log_trace( sub { return Dumper( [ options => \%options ] ); } );

  if ( $subnets && $self->get_cache ) {
    $self->log_debug( 'init-ec2: reading subnets from config...%s', $self->get_cache );
  }
  else {
    $self->log_debug('init-ec2: configuring VPC and subnets...');
  }

  my $ec2 = $self->fetch_ec2(%options);

  $subnets = $ec2->get_subnets;

  $self->log_trace( sub { return Dumper( [ subnets => $subnets ] ) } );

  $self->set_subnets($subnets);

  # make sure we have at least 2 subnets
  foreach (qw(private public)) {
    my $subnets = $subnets->{$_};
    $self->log_debug( 'init-ec2: %d %s subnets detected.', scalar( @{$subnets} ), $_ );
    next if $subnets && @{$subnets} > 1;

    log_die( $self, 'ERROR: you must specify at least 2 %s subnets', $_ );
  }

  $config->{vpc_id} = $ec2->get_vpc_id;

  # if we find subnets in the config...always validate in case they
  # got changed...
  if ($subnets) {
    $self->log_info('init-ec2: validating subnets...');
    $ec2->validate_subnets($subnets);  # this will croak if any are not invalid

lib/App/FargateStack/Init.pm  view on Meta::CPAN

  }

  if ( $self->get_cache ) {
    $self->log_info( 'init-tasks: skipping images validation...%s', $self->get_cache );
    return;
  }

  $self->log_info('init-tasks: validating images...');

  my $ecr = $self->fetch_ecr();

  $ecr->validate_images(@images);

  return;
}

########################################################################
sub _init_account {
########################################################################
  my ($self) = @_;

  my $config = $self->get_config;

  if ( $config->{account} && $self->get_cache ) {
    $self->log_info( 'init-account: reading account value from config...%s', $self->get_cache );
    $self->set_account( $config->{account} );
    return;
  }

  my $sts = $self->fetch_sts();

  $self->log_info('init-account: determining AWS account value...');

  my $result = $sts->get_caller_identity;
  $sts->check_result( message => 'ERROR: could not determine account for profile:[%s]', $self->get_profile );

  $config->{account} = $result->{Account};
  $self->log_info( 'init-account: AWS account: [%s]...', $config->{account} );

  $self->set_account( $config->{account} );

  return;
}

########################################################################
sub default_region {
########################################################################
  my ( $self, $region ) = @_;

  $region //= $self->get_region // $ENV{AWS_DEFAULT_REGION} // 'us-east-1';
  $self->set_region($region);

  return $region;
}

########################################################################
sub _init_defaults {
########################################################################
  my ( $self, $config ) = @_;

  my $last_updated = delete $config->{last_updated};
  delete $config->{id};

  $config->{region} = $self->default_region( $config->{region} );

  my $profile        = $self->get_profile;
  my $profile_source = 'command line';

  if ( !$profile && $config->{profile} ) {
    $profile        = $config->{profile};
    $profile_source = 'config';
  }
  elsif ( !$profile && $ENV{AWS_PROFILE} ) {
    $profile        = $ENV{AWS_PROFILE};
    $profile_source = 'environment';
  }

  if ( !$profile ) {
    $profile        = 'default';
    $profile_source = 'default';
  }

  $self->set_profile($profile);
  $config->{profile} = $profile;

  $self->set_profile_source($profile_source);

  my %global_options = (
    profile   => $self->get_profile,
    region    => $self->get_region,
    logger    => $self->get_logger,
    log_level => $self->get_log_level,
    unlink    => $self->get_unlink,
  );

  $self->set_global_options( \%global_options );

  my $cache = $self->get_cache;
  $self->set_cache( $cache && $last_updated ? '(cached)' : $EMPTY );

  return;
}

########################################################################
sub _init_route53 {
########################################################################
  my ($self) = @_;

  my $command = $self->command;

  my @route53_commands = qw(apply plan list-zones delete-http-service);

  return
    if none { $command eq $_ } @route53_commands;

  my $config = $self->get_config;

  $self->log_trace( sub { return Dumper( [ alb => $config->{alb}, $self->get_http ] ) } );

  my ( $route53_config, $domain ) = @{$config}{qw(route53 domain)};

  if ( !$route53_config ) {
    $route53_config = {};
    $config->{route53} = $route53_config;
  }

  if ( $self->get_route53_profile ) {
    $route53_config->{profile} = $self->get_route53_profile;
  }
  elsif ( $self->get_dns_profile ) {
    $route53_config->{profile} = $self->get_route53_profile;
  }
  else {
    $route53_config->{profile} //= $self->get_profile;
  }

  my ( $zone_id, $profile ) = @{$route53_config}{qw(zone_id profile)};

  $self->log_debug(
    sub {
      return Dumper(
        [ route53_profile => $profile,
          cli             => $self->get_route53_profile
        ]
      );
    }
  );

  my $route53 = $self->fetch_route53(
    hosted_zone_id => $zone_id,
    elb            => $self->get_elbv2,
    profile        => $profile,
  );

  return
    if !$self->get_http && $command =~ /apply|plan|list-zones/xsm;

  my $alb_type = $config->{alb}->{type};



( run in 0.600 second using v1.01-cache-2.11-cpan-e1769b4cff6 )