Skip to content

Commit

Permalink
Refact no scale in
Browse files Browse the repository at this point in the history
  • Loading branch information
MatheusdeMelo committed Jan 11, 2024
1 parent 1223d4f commit 9868950
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ if [[ $deploy_process_type != "scheduledtasks" && ( -z "$ECS_SERVICE_TASK_PROCES

deploy_desired_count=1
deploy_autoscaling_policies="cpu=55"
deploy_desired_count_regex="$deploy_process_type[{};0-9]{0,}:([0-9]+)-{0,}([0-9]{0,})\[{0,}([;=0-9a-z]{0,})\]{0,}"
deploy_desired_count_regex="$deploy_process_type[{};0-9]{0,}:([0-9]+)-{0,}([0-9]{0,})\[{0,}([;=&0-9a-z]{0,})\]{0,}"
if [[ $ECS_SERVICE_TASK_PROCESSES =~ $deploy_desired_count_regex ]]; then
deploy_desired_count=${BASH_REMATCH[1]}
deploy_max_autoscaling_count=${BASH_REMATCH[2]}
Expand Down Expand Up @@ -160,8 +160,9 @@ if [[ $deploy_process_type != "scheduledtasks" && ( -z "$ECS_SERVICE_TASK_PROCES
--min-capacity $deploy_desired_count \
--max-capacity $deploy_max_autoscaling_count

for policy in ${deploy_autoscaling_policies//;/ }; do
deploy_target_value=${policy#*=}
IFS=';' read -ra policies <<< "$deploy_autoscaling_policies"
for policy in "${policies[@]}"; do
IFS='=' read -r metric value <<< "$policy"
case $policy in
cpu=*)
deploy_predefined_metric_type="ECSServiceAverageCPUUtilization"
Expand Down Expand Up @@ -189,6 +190,21 @@ if [[ $deploy_process_type != "scheduledtasks" && ( -z "$ECS_SERVICE_TASK_PROCES
exit 1
;;
esac

if [[ $value =~ ([0-9]+) ]]; then
deploy_target_value="${BASH_REMATCH[1]}"
else
echo "Error: Invalid numeric value in policy $metric=$value"
exit 1
fi

if [[ $value =~ '&nosclin' ]]; then
nosclin_value="true"
value="${value//&nosclin/}"
else
nosclin_value="false"
fi

if [[ $type_of == 'alb' ]]; then
deploy_predefined_metric_specification="{PredefinedMetricType=$deploy_predefined_metric_type,ResourceLabel=$deploy_resource_label}"
else
Expand All @@ -197,17 +213,14 @@ if [[ $deploy_process_type != "scheduledtasks" && ( -z "$ECS_SERVICE_TASK_PROCES
deploy_current_scaling_policy=$(aws application-autoscaling describe-scaling-policies --service-namespace ecs --policy-names $deploy_service_name-$type_of-scaling-policy --query 'ScalingPolicies[0].TargetTrackingScalingPolicyConfiguration')
deploy_current_scale_out_cooldown=$(echo $deploy_current_scaling_policy | jq --raw-output '.ScaleOutCooldown // 300')
deploy_current_scale_in_cooldown=$(echo $deploy_current_scaling_policy | jq --raw-output '.ScaleInCooldown // 300')
if [ -z "$DISABLE_SCALE_IN" ]; then
deploy_current_scale_in_state=$(echo $deploy_current_scaling_policy | jq --raw-output '.DisableScaleIn // true')
fi
echo "----> Registering scaling policies for $deploy_process_type with $deploy_predefined_metric_type=$deploy_target_value"
deploy_put_scaling_policy_return=$(aws application-autoscaling put-scaling-policy \
--service-namespace ecs \
--policy-name $deploy_service_name-$type_of-scaling-policy \
--resource-id service/$deploy_cluster_id/$deploy_service_name \
--scalable-dimension ecs:service:DesiredCount \
--policy-type TargetTrackingScaling \
--target-tracking-scaling-policy-configuration "TargetValue=$deploy_target_value,PredefinedMetricSpecification=$deploy_predefined_metric_specification,ScaleOutCooldown=$deploy_current_scale_out_cooldown,ScaleInCooldown=$deploy_current_scale_in_cooldown,DisableScaleIn=$deploy_current_scale_in_state")
--target-tracking-scaling-policy-configuration "TargetValue=$deploy_target_value,PredefinedMetricSpecification=$deploy_predefined_metric_specification,ScaleOutCooldown=$deploy_current_scale_out_cooldown,ScaleInCooldown=$deploy_current_scale_in_cooldown,DisableScaleIn=$nosclin_value")
done
#else
# TODO: remove scalable target if it exists
Expand Down

0 comments on commit 9868950

Please sign in to comment.