App-FargateStack

 view release on metacpan or  search on metacpan

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

    service_namespace  => 'ecs',
  );

  $app_autoscaling->check_result(
    { message => 'ERROR: could not delete scaling policy: [%s]',
      params  => [$policy_name],
      regexp  => qr/no\sscaling\spolicy\sfound/xsmi
    }
  );

  foreach (qw(policy_arn policy_name cpu requests scale_in_cooldown scale_out_cooldown max_capacity min_capacity)) {
    delete $config->{tasks}->{$task_name}->{autoscaling}->{$_};
  }

  if ( !keys %{ $config->{tasks}->{$task_name}->{autoscaling} } ) {
    delete $config->{tasks}->{$task_name}->{autoscaling};
  }

  $self->update_config;

  return $SUCCESS;
}

########################################################################
sub _get_autoscaling_task_name {
########################################################################
  my ( $self, $tasks ) = @_;

  my (@args) = $self->get_args;

  my $possible_task_name = shift @args;

  return ( $possible_task_name, @args )
    if $tasks->{$possible_task_name};

  my ( $task_name, $err ) = grep { $tasks->{$_}->{type} =~ /^(daemon|https?)$/xsm } keys %{$tasks};

  croak "ERROR: You need to select which task to apply autoscaling to\n"
    if $err;

  return ( $task_name, $possible_task_name, @args );
}

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

  my $tasks = $self->get_config->{tasks};

  my ( $task_name, @args ) = $self->_get_autoscaling_task_name($tasks);

  croak
    "usage: add-scheduled-action [task] name start-time end-time days min-capacity max-capacity (ex: 00:18 00:02 MON-FRI 2/1 2/2)\n"
    if @args != 6;

  my $schedule_name = shift @args;

  my $scheduled = $tasks->{$task_name}->{autoscaling}->{scheduled} // {};

  # plan will verify these values
  $scheduled->{$schedule_name} = {
    start_time   => $args[0],
    end_time     => $args[1],
    days         => $args[2],
    min_capacity => $args[3],
    max_capacity => $args[4],
  };

  my $autoscaling_config = App::FargateStack::AutoscalingConfig->new(
    config    => $tasks->{$task_name}->{autoscaling},
    task_name => $task_name
  );

  $self->update_config;

  print {*STDOUT} sprintf "Scheduled action: [%s] added to your configuration. Run plan to verify the configuration.\n",
    $schedule_name;

  return $SUCCESS;
}

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

  my $tasks = $self->get_config->{tasks};

  my ( $task_name, @args ) = $self->_get_autoscaling_task_name($tasks);

  croak
    "usage: add-scaling-policy [task] cpu|requests metric-value [min_capacity max_capacity [scale_out_cooldown scale_in_cooldown]]\n"
    if !( @args == 2 || @args == 4 || @args == 6 ) || $args[0] !~ /^(?:cpu|requests)$/xsm;

  my $autoscaling_config = App::FargateStack::AutoscalingConfig->new(
    config             => $tasks->{$task_name}->{autoscaling},
    task_name          => $task_name,
    metric             => sprintf( '%s:%s', @args[ 0, 1 ] ),
    min_capacity       => $args[2],
    max_capacity       => $args[3],
    scale_out_cooldown => $args[4],
    scale_in_cooldown  => $args[5],
    policy_name        => $self->create_default( 'autoscaling-policy-name', $task_name ),
  );

  print {*STDOUT} sprintf "Scaling policy: [%s] added to your configuration. Run plan to verify the configuration.\n",
    $autoscaling_config->get_policy_name;

  $self->update_config;

  return $SUCCESS;
}

1;



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