Skip to content

Commit bf7649e

Browse files
committed
CI: Add a workflow to clean up stale instances
Sometimes we can end up with aio instances left running indefinitely. This can lead to unnecessary cloud costs. This change adds a periodic workflow that runs every 2 hours and deletes instances with the skc-ci-aio tag that are over 3 hours old.
1 parent 1d75be4 commit bf7649e

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
name: Clean up stale CI resources
3+
on:
4+
schedule:
5+
# Every 2 hours at quarter past
6+
- cron: '15 0/2 * * *'
7+
8+
jobs:
9+
ci-cleanup:
10+
name: Clean up stale CI resources
11+
if: github.repository == 'stackhpc/stackhpc-kayobe-config'
12+
runs-on: ubuntu-latest
13+
permissions: {}
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v4
17+
with:
18+
path: src/kayobe-config
19+
20+
- name: Setup Python
21+
uses: actions/setup-python@v5
22+
23+
- name: Generate clouds.yaml
24+
run: |
25+
cat << EOF > clouds.yaml
26+
${{ secrets.CLOUDS_YAML }}
27+
EOF
28+
29+
- name: Determine OpenStack release
30+
id: openstack_release
31+
run: |
32+
BRANCH=$(awk -F'=' '/defaultbranch/ {print $2}' src/kayobe-config/.gitreview)
33+
echo "openstack_release=${BRANCH}" | sed "s|stable/||" >> $GITHUB_OUTPUT
34+
35+
- name: Install OpenStack client
36+
run: |
37+
pip install python-openstackclient -c https://opendev.org/openstack/requirements/raw/branch/stable/${{ steps.openstack_release.outputs.openstack_release }}/upper-constraints.txt
38+
39+
- name: Clean up aio instances over 3 hours old
40+
run: |
41+
result=0
42+
changes_before=$(date -Imin -d -3hours)
43+
for status in ACTIVE ERROR SHUTOFF; do
44+
for instance in $(openstack server list --tags skc-ci-aio --os-compute-api-version 2.66 --format value --column Name --changes-before $changes_before --status $status); do
45+
echo "Cleaning up $status instance $instance"
46+
openstack server show $instance
47+
if ! openstack server delete $instance; then
48+
echo "Failed to delete $status instance $instance"
49+
result=1
50+
fi
51+
done
52+
done
53+
exit $result
54+
env:
55+
OS_CLOUD: openstack
56+
OS_APPLICATION_CREDENTIAL_ID: ${{ secrets.OS_APPLICATION_CREDENTIAL_ID }}
57+
OS_APPLICATION_CREDENTIAL_SECRET: ${{ secrets.OS_APPLICATION_CREDENTIAL_SECRET }}

0 commit comments

Comments
 (0)