Skip to content

Commit d185df9

Browse files
authored
Add a GH workflow to clean up stale CI resources (azimuth-cloud#224)
1 parent 3dffee5 commit d185df9

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
name: Clean up stale CI resources
3+
on:
4+
schedule:
5+
# Every 2 hours at 8 minutes past
6+
- cron: '8 0/2 * * *'
7+
workflow_dispatch:
8+
inputs:
9+
delete-resources:
10+
type: boolean
11+
description: "Delete resources older than 6h"
12+
required: true
13+
delete-all-keypairs:
14+
type: boolean
15+
description: "Delete all CI user keypairs"
16+
required: true
17+
18+
permissions: {}
19+
20+
jobs:
21+
ci-cleanup:
22+
strategy:
23+
matrix:
24+
target-cloud: [arcus, leafcloud]
25+
name: Clean up stale CI resources
26+
if: github.repository == 'azimuth-cloud/azimuth-config'
27+
runs-on: ubuntu-latest
28+
permissions: {}
29+
steps:
30+
- name: Setup Python
31+
uses: actions/setup-python@v5
32+
33+
- name: Generate clouds.yaml
34+
run: |
35+
cat << EOF > clouds.yaml
36+
${{ secrets.OS_CLOUDS }}
37+
EOF
38+
39+
- name: Install OpenStack client
40+
run: |
41+
pip install python-openstackclient
42+
43+
- name: Clean up instances and attached volumes over 6 hours old
44+
if: ${{ github.event_name == 'schedule' || inputs.delete-resources }}
45+
run: |
46+
result=0
47+
changes_before=$(date -Imin -d -6hours)
48+
for status in ACTIVE BUILD ERROR SHUTOFF; do
49+
for instance in $(openstack server list --unlocked --format value --column ID --changes-before $changes_before --status $status); do
50+
echo "Cleaning up $status instance $instance"
51+
openstack server show $instance
52+
echo "Getting volumes for instance $instance"
53+
volumes=$(openstack server volume list -f value -c "Volume ID" $instance)
54+
keypair=$(openstack server show $instance -f value -c key_name)
55+
if ! openstack server delete $instance; then
56+
echo "Failed to delete $status instance $instance"
57+
result=1
58+
fi
59+
echo "Deleting keypair for instance $instance"
60+
# This shouldn't fail, but might if the keypair is in-use elsewhere
61+
openstack keypair delete $keypair || true
62+
for volume in $volumes; do
63+
echo "Cleaning up volume $volume from instance $instance"
64+
openstack volume show $volume
65+
if ! openstack volume delete $volume; then
66+
echo "Failed to delete volume $volume"
67+
result=1
68+
fi
69+
done
70+
done
71+
done
72+
exit $result
73+
env:
74+
OS_CLOUD: ${{ matrix.target-cloud }}
75+
76+
- name: Clean up all SSH keypairs
77+
if: ${{ inputs.delete-all-keypairs }}
78+
run: |
79+
for keypair in $(openstack keypair list --format value -c Name); do
80+
openstack keypair delete $keypair
81+
echo "Deleted keypair $keypair"
82+
done
83+
env:
84+
OS_CLOUD: ${{ matrix.target-cloud }}

0 commit comments

Comments
 (0)