App-FargateStack

 view release on metacpan or  search on metacpan

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

  $self->update_config;

  return;
}

# resolves the resource value from the hash of resources
# TBD: better documentation of this method
########################################################################
sub resolve_resource_value {
########################################################################
  my ( $value, $dryrun ) = @_;

  return [$value]
    if !ref $value;

  if ( reftype($value) eq 'ARRAY' ) {
    my @values;

    foreach my $v ( @{$value} ) {
      if ( ref $v && reftype($v) eq 'CODE' ) {
        push @values, $v->($dryrun);
      }
      else {
        push @values, $v;
      }
    }
    return \@values;
  }
  else {
    return [ $value->($dryrun) ];
  }
}

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

  my $config = $self->get_config;
  my @scheduled_action_names;

  foreach ( keys %{ $config->{tasks} } ) {
    next if !exists $config->{tasks}->{$_}->{autoscaling};
    next if !exists $config->{tasks}->{$_}->{autoscaling}->{scheduled};
    push @scheduled_action_names, keys %{ $config->{tasks}->{$_}->{autoscaling}->{scheduled} };
  }

  return @scheduled_action_names;
}

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

  $Data::Dumper::Sortkeys = $TRUE;

  my $config = $self->get_config;

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

  my $md5_hex = md5_hex( Dumper( $self->get_config ) );

  $config->{id} = $md5_hex;

  $config->{last_updated} = scalar localtime;

  return $config;
}

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

  $self->log_warn( 'builder: config file %s %s be updated', $self->get_config_name, $self->get_update ? 'will' : 'will not' );

  return
    if !$self->get_update;

  my ( $fh, $filename ) = tempfile( 'fargate-stack-XXXX', SUFFIX => '.yml' );

  my $config = $self->update_config_id();

  DumpFile( $filename, $config );

  my $config_name = $self->get_config_name;

  rename $config_name, "${config_name}.bak";

  rename $filename, $config_name;

  return;
}

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

  my $config = $self->get_config;

  my $is_secure = $self->has_https_service;

  # default ALB type is private with a listener on port 80
  my $alb_config = $config->{alb} // {};
  $alb_config->{type} //= $is_secure ? 'public' : 'private';
  $alb_config->{port} //= $is_secure ? '443'    : '80';

  # if it was already defined as public w/o a port, then 443 and
  # redirect 80 -> 443
  if ( $alb_config->{type} eq 'public' && !$alb_config->{port} ) {
    $alb_config->{port} = '443';
    if ( !defined $alb_config->{redirect_80} ) {
      $alb_config->{redirect_80} = $TRUE;
    }
  }

  croak "invalid alb type, must be 'public' or 'private'\n"
    if $alb_config->{type} !~ /^public|private$/xsm;

  $config->{alb} = $alb_config;

  return;
}

########################################################################
sub remove_listener_rules {
########################################################################
  my ( $self, $alb_arn ) = @_;

  my ( $config, $dryrun ) = $self->common_args(qw(config dryrun));

  my $elb = $self->get_elbv2;

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

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

  ######################################################################
  # -- delete service
  ######################################################################

  if ( $type =~ /^(?:daemon|https?)$/xsm ) {
    ####################################################################
    # -- remove alias record
    ####################################################################
    if ( $type ne 'daemon' ) {
      $self->_delete_alias_record($confirm_all);
    }

    $self->_delete_service( $confirm_all, $task_name, $type );
  }

  ######################################################################
  # -- delete http(s)
  ######################################################################
  if ( $type =~ /^(?:https?)$/xsm ) {
    $self->_delete_http( $confirm_all, $task_name );
  }

  ####################################################################
  # -- remove target group
  ####################################################################
  $self->_delete_target_group( $confirm_all, $task_name );

  ######################################################################
  # -- delete security group
  ######################################################################
  if ( $num_tasks == 1 ) {
    $self->_delete_security_group( $confirm_all, $task_name );
  }

  ######################################################################
  # -- delete cluster
  ######################################################################
  if ( $num_tasks == 1 ) {
    $self->_delete_cluster( $confirm_all, $task_name );
  }

  ######################################################################
  # -- iam role (resource changes may require new policy)
  ######################################################################
  if ( $num_tasks != 1 ) {
    $self->log_warn('iam: looking to see if we need to update role...');
    $self->build_iam_role();
  }

  ######################################################################
  # NOTE: Skeletons of deleted tasks are left in the confguration file
  # for single task config and for multi-task configs. --purge-config
  # will allow purging of tasks in multi-task configs only...
  ######################################################################
  if ( $self->get_purge_config ) {
    delete $tasks->{$task_name};
  }

  if ( !$dryrun ) {
    delete $config->{id};
    delete $config->{last_updated};

    $self->update_config;
    $self->log_info(
      'builder: A skeleton configuration remains that will allow you to recreate the stack. Run "app-FargateStack plan" to rehydrate your configuration.'
    );
  }

  return $SUCCESS;
}

########################################################################
sub _delete_cluster {
########################################################################
  my ( $self, $confirm_all, $service_name, $type ) = @_;

  my ( $config, $dryrun, $tasks, $cluster ) = $self->common_args(qw(config dryrun tasks cluster));

  my $cluster_name = $cluster->{name};
  my $confirmed    = $confirm_all ? confirm( 'cluster: delete cluster: [%s]', $cluster_name ) : $TRUE;

  if ($confirmed) {
    my $ecs = $self->fetch_ecs;

    $self->log_warn( 'cluster: [%s] will be deleted...%s', $cluster_name, $dryrun );

    if ( !$dryrun ) {
      my $result = $ecs->delete_cluster($cluster_name);
      $ecs->check_result(
        message => 'ERROR: could not delete cluster: [%s]',
        params  => [$cluster_name],
        regexp  => qr/nosuchentity/xmsi,
        warn    => $TRUE,
      );

      $self->log_warn( 'cluster: [%s] deleted successfully...status: [%s]', $cluster_name, $result->{cluster}->{status} );

      delete $config->{cluster};
    }
  }
  else {
    $self->log_warn( 'cluster: deletion skipped...%s', $dryrun );
  }
  return;
}

########################################################################
sub _delete_alias_record {
########################################################################
  my ( $self, $confirm_all, $service_name, $type ) = @_;

  my ( $config, $dryrun, $tasks, $alb ) = $self->common_args(qw(config dryrun tasks alb));
  my $domain = $config->{domain};

  return  # belt & suspenders...we should never get here
    if !$domain;

  my $confirmed = $confirm_all ? confirm( 'route53: delete alias record for: [%s]', $domain ) : $TRUE;

  if ($confirmed) {
    $self->log_warn( 'route53: alias record for: [%s] will be deleted...%s', $domain, $dryrun );



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