-
Notifications
You must be signed in to change notification settings - Fork 276
48 lines (41 loc) · 2.32 KB
/
terminate-zombie-build-instances.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Required since we cannot guarantee instances are always terminated.
# Also a failsafe against a dev writing a workflow that does not terminate build instances.
# Though the average Airbyte build runtime as of this commit is ~20 mins, connector builds
# can take up to 3 hours. Set this to 3 hours to include these longer runs.
name: Terminate Zombie Build Instances
on:
workflow_dispatch:
schedule:
- cron: "0 */1 * * *"
jobs:
terminate:
runs-on: ubuntu-latest
steps:
- name: List and Terminate Instances Older Than 4 Hours
env:
AWS_ACCESS_KEY_ID: ${{ secrets.SELF_RUNNER_AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.SELF_RUNNER_AWS_SECRET_ACCESS_KEY }}
# See https://github.com/aws/aws-cli/issues/5623
AWS_EC2_METADATA_DISABLED: true
run: |
set -euxo pipefail
export TIME_LIMIT=14400 # 4 hours
aws configure set default.region us-east-2
# See https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/describe-instances.html for describe command.
# Since the AWS cli returns an ISO HH:MM timestamp, and Jq only accepts Z timestamps, we define a function toZ to convert this.
export to_terminate=$(aws ec2 describe-instances --no-paginate --filters Name=instance-type,Values=c5.2xlarge Name=instance-state-name,Values=running \
--query 'Reservations[*].Instances[*].{Instance:InstanceId,LaunchTime:LaunchTime}' --output json \
| jq 'def toZ(str): str | (split("+")[0] + "Z") | fromdate ;
flatten | map( { InstanceId: .Instance, LaunchTime: toZ(.LaunchTime) } ) | map( select ( .LaunchTime < (now - (env.TIME_LIMIT|tonumber)) ) )')
echo "MARKED FOR TERMINATION: ${to_terminate}"
# See https://docs.aws.amazon.com/cli/latest/reference/ec2/terminate-instances.html for terminate command.
echo $to_terminate | jq '.[] | .InstanceId' | xargs --no-run-if-empty --max-args=1 aws ec2 terminate-instances --instance-ids
terminate-github-instances:
runs-on: ubuntu-latest
steps:
- name: Checkout Airbyte
uses: actions/checkout@v3
- name: List and Terminate GH actions in status 'offline'
env:
GITHUB_PAT: ${{ secrets.GH_PAT_MAINTENANCE_OCTAVIA }}
run: ./tools/bin/gh_action_zombie_killer