Skip to content

Commit

Permalink
feat: Add "Automatic Instance Shelving" workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
jcfr committed Aug 22, 2024
1 parent f5b17b8 commit b97c041
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions .github/workflows/automatic-instance-shelving.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Automatic Instance Shelving
on:
schedule:
# Run every 5 mins
- cron: "* * * * *"
workflow_dispatch:

permissions:
contents: read

jobs:
auto-shelve:
runs-on: self-hosted
steps:
- uses: actions/checkout@v4

- name: Auto Shelve
run: |
export OS_CLOUD=BIO180006_IU # Select openstack auth settings defined in ".config/openstack/clouds.yaml"
source ~/venv/bin/activate
instance_prefix=${PREFIX:+${PREFIX}_}
instance_basename="${instance_prefix}instance"
openstack server list --name "^${instance_basename}-\d+" -f json | \
jq -r '.[] | [.Name, .Status, ."OS-EXT-STS:task_state"] | @tsv' | \
while IFS=$'\t' read -r instance_name status task_state; do
if [[ "$status" != "ACTIVE" ]]; then
# Skip because instance is not active
continue
fi
if [[ "$task_state" != "null" ]]; then
# Skip because instance status is being updated
continue
fi
# Get instance IP
instance_ip=$(
openstack server show $instance_name -c addresses -f json | \
jq -r '.addresses.auto_allocated_network[1]'
)
echo "instance_ip [$instance_ip]"
if [[ "$instance_ip" == "null" ]]; then
echo "::warning ::Failed to retrieve $instance_name IP"
continue
fi
# Retrieve uptime
uptime_seconds=$(ssh \
-o StrictHostKeyChecking=no \
-o UserKnownHostsFile=/dev/null \
-o LogLevel=ERROR \
exouser@$instance_ip \
'cat /proc/uptime | awk "{print \$1}"')
uptime_hours=$(echo "scale=2; $uptime_seconds / 3600" | bc)
# Check uptime
if $(python3 -c "valid=($uptime_hours > 3.5 and $uptime_hours <= 4.0); EXIT_SUCCESS=0; EXIT_FAILURE=1; exit(EXIT_SUCCESS if valid else EXIT_FAILURE)"); then
action="notify"
elif $(python3 -c "valid=($uptime_hours > 4.0); EXIT_SUCCESS=0; EXIT_FAILURE=1; exit(EXIT_SUCCESS if valid else EXIT_FAILURE)"); then
action="shelve"
else
action=""
fi
echo "instance_name [$instance_name] -> action[$action]"
done

0 comments on commit b97c041

Please sign in to comment.