|
3 | 3 |
|
4 | 4 | function scale_services(){
|
5 | 5 | UP_DOWN="${1:?}"
|
| 6 | + SERVICE_NAME="${2:?}" # Take service name as an argument |
| 7 | + ADJUST_AUTOSCALING="${3:-}" |
| 8 | + COUNT="${4:-1}" |
| 9 | + |
6 | 10 | # Set the minimum capacity and desired count in the cluster to 0 to scale down or to the original size to scale back to normal.
|
7 | 11 |
|
8 | 12 | # This is a bit hacky, but the update-service has to happen first when scaling up and second when scaling down.
|
9 | 13 | # Assume scaling down unless "up".
|
10 | 14 | CAPACITY=0
|
11 | 15 | if [ "${UP_DOWN:?}" = "up" ]; then
|
12 |
| - aws ecs update-service --region "${REGION:?}" --cluster "${ECS_CLUSTER:?}" --service "${ECS_SERVICE:?}" --desired-count "${DESIRED_COUNT:?}" |
| 16 | + aws ecs update-service --region "${REGION:?}" --cluster "${ECS_CLUSTER:?}" --service "${SERVICE_NAME:?}" --desired-count "${COUNT:?}" |
13 | 17 | CAPACITY="${MIN_CAPACITY:?}"
|
14 | 18 | fi
|
15 |
| - aws application-autoscaling register-scalable-target --region "${REGION:?}" --service-namespace ecs --resource-id "service/${ECS_CLUSTER:?}/${ECS_SERVICE:?}" --scalable-dimension "ecs:service:DesiredCount" --min-capacity "${CAPACITY:?}" |
| 19 | + |
| 20 | + if [ -n "${ADJUST_AUTOSCALING}" ]; then |
| 21 | + aws application-autoscaling register-scalable-target --region "${REGION:?}" --service-namespace ecs --resource-id "service/${ECS_CLUSTER:?}/${SERVICE_NAME:?}" --scalable-dimension "ecs:service:DesiredCount" --min-capacity "${CAPACITY:?}" |
| 22 | + fi |
16 | 23 | # We are scaling down, make it 0
|
17 | 24 | if [ "${UP_DOWN:?}" != "up" ]; then
|
18 |
| - aws ecs update-service --region "${REGION:?}" --cluster "${ECS_CLUSTER:?}" --service "${ECS_SERVICE:?}" --desired-count 0 |
| 25 | + aws ecs update-service --region "${REGION:?}" --cluster "${ECS_CLUSTER:?}" --service "${SERVICE_NAME:?}" --desired-count 0 |
19 | 26 | fi
|
20 |
| - # The first task defintion might never get stable because it never had initial migrations so don't wait before continuing |
| 27 | + # The first task definition might never get stable because it never had initial migrations so don't wait before continuing |
21 | 28 | if [ "${TASK_DEFINITION_REVISION}" != "1" ]; then
|
22 | 29 | # Wait for scale-down to succeed
|
23 |
| - aws ecs wait services-stable --region "${REGION:?}" --cluster "${ECS_CLUSTER:?}" --service "${ECS_SERVICE:?}" |
| 30 | + aws ecs wait services-stable --region "${REGION:?}" --cluster "${ECS_CLUSTER:?}" --services "${SERVICE_NAME:?}" |
24 | 31 | fi
|
25 | 32 | }
|
26 | 33 |
|
27 | 34 | for ARGUMENT in "$@"
|
28 | 35 | do
|
29 | 36 | KEY=$(echo $ARGUMENT | cut -f1 -d=)
|
30 |
| - |
31 | 37 | KEY_LENGTH=${#KEY}
|
32 | 38 | VALUE="${ARGUMENT:$KEY_LENGTH+1}"
|
33 |
| - |
34 | 39 | export "$KEY"="$VALUE"
|
35 | 40 | done
|
36 | 41 |
|
37 |
| -scale_services down |
| 42 | +scale_services down "${ECS_SERVICE:?}" true "${DESIRED_COUNT}" |
| 43 | + |
| 44 | +if [ -n "${VULN_SERVICE}" ]; then |
| 45 | + scale_services down "${VULN_SERVICE:?}" |
| 46 | +fi |
38 | 47 |
|
39 | 48 | # Call aws ecs run-task
|
40 | 49 | TASK_ARN="$(aws ecs run-task --region "${REGION:?}" --cluster "${ECS_CLUSTER:?}" --task-definition "${TASK_DEFINITION:?}":"${TASK_DEFINITION_REVISION:?}" --launch-type FARGATE --network-configuration "awsvpcConfiguration={subnets="${SUBNETS:?}",securityGroups="${SECURITY_GROUPS:?}"}" --query 'tasks[].taskArn' --overrides '{"containerOverrides": [{"name": "fleet", "command": ["fleet", "prepare", "db"]}]}' --output text | rev | cut -d'/' -f1 | rev)"
|
41 | 50 |
|
42 | 51 | # Wait for completion
|
43 | 52 | aws ecs wait tasks-stopped --region "${REGION:?}" --cluster="${ECS_CLUSTER:?}" --tasks="${TASK_ARN:?}"
|
44 | 53 |
|
45 |
| -scale_services up |
| 54 | +scale_services up "${ECS_SERVICE:?}" true "${DESIRED_COUNT}" |
| 55 | + |
| 56 | +if [ -n "${VULN_SERVICE}" ]; then |
| 57 | + scale_services up "${VULN_SERVICE:?}" |
| 58 | +fi |
46 | 59 |
|
47 | 60 | # Exit with task's exit code
|
48 |
| -TASK_EXIT_CODE=$(aws ecs describe-tasks --region "${REGION:?}" --cluster ${ECS_CLUSTER:?} --tasks ${TASK_ARN:?} --query "tasks[0].containers[?name=='fleet'].exitCode" --output text) |
| 61 | +TASK_EXIT_CODE=$(aws ecs describe-tasks --region "${REGION:?}" --cluster "${ECS_CLUSTER:?}" --tasks "${TASK_ARN:?}" --query "tasks[0].containers[?name=='fleet'].exitCode" --output text) |
49 | 62 | exit "${TASK_EXIT_CODE}"
|
0 commit comments