|
| 1 | +name: Hibernate Dev |
| 2 | + |
| 3 | +# Dev bills the same idle as busy. This parks its ECS service at zero tasks and |
| 4 | +# brings it back on demand. A dev deploy wakes it too, so forgetting to wake |
| 5 | +# costs nothing. |
| 6 | +on: |
| 7 | + workflow_dispatch: |
| 8 | + inputs: |
| 9 | + mode: |
| 10 | + description: "hibernate parks the service at zero tasks; wake restores it" |
| 11 | + required: true |
| 12 | + type: choice |
| 13 | + options: |
| 14 | + - hibernate |
| 15 | + - wake |
| 16 | + default: hibernate |
| 17 | + |
| 18 | +run-name: ${{ inputs.mode == 'wake' && 'Wake dev' || 'Hibernate dev' }} |
| 19 | + |
| 20 | +# deploy.yml's dev group, so this can never interleave with a dev deploy. |
| 21 | +concurrency: |
| 22 | + group: deploy-dev |
| 23 | + cancel-in-progress: false |
| 24 | + |
| 25 | +env: |
| 26 | + AWS_REGION: eu-west-1 |
| 27 | + TERRAFORM_VERSION: 1.16.0 |
| 28 | + |
| 29 | +jobs: |
| 30 | + capacity: |
| 31 | + name: Set Dev Capacity |
| 32 | + runs-on: ubuntu-latest |
| 33 | + permissions: |
| 34 | + id-token: write |
| 35 | + contents: read |
| 36 | + # Declared for vars.AWS_OIDC_ROLE_ARN, which resolves per Environment. No |
| 37 | + # `url`: half of what this workflow does is make that URL stop answering. |
| 38 | + environment: |
| 39 | + name: dev |
| 40 | + steps: |
| 41 | + - name: Checkout code |
| 42 | + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 43 | + |
| 44 | + - name: Configure AWS credentials |
| 45 | + uses: aws-actions/configure-aws-credentials@e6de054238d6b7531b4efff3b6587d9aade6a06c # v6.2.3 |
| 46 | + with: |
| 47 | + aws-region: ${{ env.AWS_REGION }} |
| 48 | + role-to-assume: ${{ vars.AWS_OIDC_ROLE_ARN }} |
| 49 | + |
| 50 | + - name: Setup Terraform |
| 51 | + uses: hashicorp/setup-terraform@dfe3c3f87815947d99a8997f908cb6525fc44e9e # v4.0.1 |
| 52 | + with: |
| 53 | + terraform_version: ${{ env.TERRAFORM_VERSION }} |
| 54 | + terraform_wrapper: false |
| 55 | + |
| 56 | + # From state, the way the deploy reads them: a name spelled out here would |
| 57 | + # scale nothing and still report success. |
| 58 | + - name: Read service outputs |
| 59 | + id: outputs |
| 60 | + working-directory: terraform/frontend |
| 61 | + run: | |
| 62 | + set -euo pipefail |
| 63 | +
|
| 64 | + terraform init -input=false -backend-config=../environments/dev/backend.config |
| 65 | + terraform workspace select dev |
| 66 | + { |
| 67 | + echo "cluster=$(terraform output -raw cluster_name)" |
| 68 | + echo "service=$(terraform output -raw service_name)" |
| 69 | + } >> "$GITHUB_OUTPUT" |
| 70 | +
|
| 71 | + # Suspended before the count drops, never after: Application Auto Scaling |
| 72 | + # holds the service at the target's min_capacity, so a live target scales |
| 73 | + # it straight back out. Terraform does not manage suspended_state, so the |
| 74 | + # suspension survives every apply until a wake lifts it. |
| 75 | + - name: Park the service |
| 76 | + if: inputs.mode == 'hibernate' |
| 77 | + env: |
| 78 | + CLUSTER: ${{ steps.outputs.outputs.cluster }} |
| 79 | + SERVICE: ${{ steps.outputs.outputs.service }} |
| 80 | + run: | |
| 81 | + set -euo pipefail |
| 82 | +
|
| 83 | + resource="service/${CLUSTER}/${SERVICE}" |
| 84 | + target=$(aws application-autoscaling describe-scalable-targets \ |
| 85 | + --service-namespace ecs \ |
| 86 | + --scalable-dimension ecs:service:DesiredCount \ |
| 87 | + --resource-ids "$resource" \ |
| 88 | + --no-cli-pager --query 'ScalableTargets[0].ResourceId' --output text) |
| 89 | +
|
| 90 | + if [ "$target" != "$resource" ]; then |
| 91 | + echo "No scalable target on ${SERVICE}; nothing to suspend." |
| 92 | + else |
| 93 | + aws application-autoscaling register-scalable-target \ |
| 94 | + --service-namespace ecs \ |
| 95 | + --scalable-dimension ecs:service:DesiredCount \ |
| 96 | + --resource-id "$resource" \ |
| 97 | + --suspended-state '{"DynamicScalingInSuspended":true,"DynamicScalingOutSuspended":true,"ScheduledScalingSuspended":true}' \ |
| 98 | + --no-cli-pager |
| 99 | + echo "Target tracking suspended on ${SERVICE}." |
| 100 | + fi |
| 101 | +
|
| 102 | + aws ecs update-service --cluster "$CLUSTER" --service "$SERVICE" \ |
| 103 | + --desired-count 0 --no-cli-pager --query 'service.desiredCount' --output text |
| 104 | + aws ecs wait services-stable --cluster "$CLUSTER" --services "$SERVICE" |
| 105 | + echo "${SERVICE} is drained." |
| 106 | +
|
| 107 | + # Restores to the target's own floor rather than to 1, so this stays right |
| 108 | + # if desired_count in the env YAML ever changes, and only from below it — |
| 109 | + # pressing wake on a service that is already up must not scale it in. |
| 110 | + - name: Wake the service |
| 111 | + if: inputs.mode == 'wake' |
| 112 | + env: |
| 113 | + CLUSTER: ${{ steps.outputs.outputs.cluster }} |
| 114 | + SERVICE: ${{ steps.outputs.outputs.service }} |
| 115 | + run: | |
| 116 | + set -euo pipefail |
| 117 | +
|
| 118 | + resource="service/${CLUSTER}/${SERVICE}" |
| 119 | + target=$(aws application-autoscaling describe-scalable-targets \ |
| 120 | + --service-namespace ecs \ |
| 121 | + --scalable-dimension ecs:service:DesiredCount \ |
| 122 | + --resource-ids "$resource" \ |
| 123 | + --no-cli-pager --query 'ScalableTargets[0]' --output json) |
| 124 | +
|
| 125 | + floor=$(jq -r '.MinCapacity // 1' <<<"$target") |
| 126 | + desired=$(aws ecs describe-services --cluster "$CLUSTER" --services "$SERVICE" \ |
| 127 | + --no-cli-pager --query 'services[0].desiredCount' --output text) |
| 128 | +
|
| 129 | + if [ "$(jq -r '[.SuspendedState // {} | .[]] | any' <<<"$target")" = "true" ]; then |
| 130 | + aws application-autoscaling register-scalable-target \ |
| 131 | + --service-namespace ecs \ |
| 132 | + --scalable-dimension ecs:service:DesiredCount \ |
| 133 | + --resource-id "$resource" \ |
| 134 | + --suspended-state '{"DynamicScalingInSuspended":false,"DynamicScalingOutSuspended":false,"ScheduledScalingSuspended":false}' \ |
| 135 | + --no-cli-pager |
| 136 | + echo "Target tracking resumed on ${SERVICE}." |
| 137 | + fi |
| 138 | +
|
| 139 | + if [ "$desired" -lt "$floor" ]; then |
| 140 | + aws ecs update-service --cluster "$CLUSTER" --service "$SERVICE" \ |
| 141 | + --desired-count "$floor" --no-cli-pager --query 'service.desiredCount' --output text |
| 142 | + fi |
| 143 | +
|
| 144 | + aws ecs wait services-stable --cluster "$CLUSTER" --services "$SERVICE" |
| 145 | + echo "${SERVICE} is serving on the revision it was parked with." |
| 146 | +
|
| 147 | + - name: Summary |
| 148 | + env: |
| 149 | + MODE: ${{ inputs.mode }} |
| 150 | + SERVICE: ${{ steps.outputs.outputs.service }} |
| 151 | + run: | |
| 152 | + { |
| 153 | + if [ "$MODE" = "hibernate" ]; then |
| 154 | + echo "### Dev hibernated" |
| 155 | + echo |
| 156 | + echo "- \`${SERVICE}\` is at zero tasks with target tracking suspended." |
| 157 | + echo "- https://dev.block-explorer.vechain.org now returns 503 — the ALB has no targets." |
| 158 | + echo "- Nothing alarms: dev sets \`alerts_enabled: false\`, so no alarms or AMP alert rules exist." |
| 159 | + echo "- Wake it by re-running this workflow with \`wake\`, or by deploying dev — the deploy wakes it on its own." |
| 160 | + else |
| 161 | + echo "### Dev woken" |
| 162 | + echo |
| 163 | + echo "- \`${SERVICE}\` is serving again and target tracking is live." |
| 164 | + echo "- https://dev.block-explorer.vechain.org" |
| 165 | + fi |
| 166 | + } >> "$GITHUB_STEP_SUMMARY" |
0 commit comments