-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add workflow for collecting instance uptimes
- Loading branch information
Showing
1 changed file
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
name: Collect Instance Uptime | ||
on: | ||
# schedule: | ||
# # Run every 5 mins | ||
# - cron: "* * * * *" | ||
workflow_dispatch: | ||
|
||
jobs: | ||
collect-instance-uptime: | ||
runs-on: self-hosted | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Collect Instance Uptime | ||
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" | ||
JSON_FILE=uptime.json | ||
JSON_FILE_TMP=uptime.json.tmp | ||
if [[ ! -f $JSON_FILE ]]; then | ||
jq --null-input \ | ||
--arg allocation_id "$OS_CLOUD" \ | ||
'{"allocations": [{"id": $allocation_id, "instances": []}]}' > $JSON_FILE | ||
fi | ||
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 | ||
echo "instance_name [$instance_name] status [$status] task_state [$task_state]" | ||
if [[ "$status" != "ACTIVE" ]]; then | ||
# Skip because instance is not active | ||
continue | ||
fi | ||
if [[ "$task_state" != "" ]]; then | ||
# Skip because instance status is being updated | ||
continue | ||
fi | ||
# Extract issue number | ||
issue_number=${instance_name##*-} | ||
echo "issue_number [$issue_number]" | ||
# 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=$(ssh \ | ||
-o StrictHostKeyChecking=no \ | ||
-o UserKnownHostsFile=/dev/null \ | ||
-o LogLevel=ERROR \ | ||
exouser@$instance_ip \ | ||
'cat /proc/uptime | awk "{print \$1}"') | ||
# Retrieve startup time in yyyy-mm-dd HH:MM:SS format | ||
startup_time=$(ssh \ | ||
-o StrictHostKeyChecking=no \ | ||
-o UserKnownHostsFile=/dev/null \ | ||
-o LogLevel=ERROR \ | ||
exouser@$instance_ip \ | ||
'uptime -s') | ||
# Check if there is an entry for instance_name | ||
has_instance=(cat $JSON_FILE | jq \ | ||
--arg allocation_id "$OS_CLOUD" \ | ||
--arg instance_name "$instance_name" \ | ||
--arg startup_time "$startup_time" \ | ||
'any(.allocations[] | select(.id == $allocation_id) | .instances[]; .name == $instance_name)') | ||
if [[ "$has_instance" == "false" ]]; then | ||
# Add entry | ||
cat $JSON_FILE | jq \ | ||
--arg allocation_id "$OS_CLOUD" \ | ||
--arg instance_name "$instance_name" \ | ||
'(.allocations[] | select(.id == $allocation_id) | .instances) | ||
+= [{"name": $instance_name, "uptimes": {}}]' > $JSON_FILE_TMP \ | ||
&& mv $JSON_FILE_TMP $JSON_FILE | ||
fi | ||
# Check if there is an entry for startup_time | ||
has_startup_time=(cat $JSON_FILE | jq \ | ||
--arg allocation_id "$OS_CLOUD" \ | ||
--arg instance_name "$instance_name" \ | ||
--arg startup_time "$startup_time" \ | ||
'(.allocations[] | select(.id == $allocation_id) | .instances[] | select(.name == $instance_name) | .uptimes | has($startup_time))') | ||
if [[ "$has_startup_time" == "false"]]; then | ||
# Add entry | ||
cat $JSON_FILE | jq \ | ||
--arg allocation_id "$OS_CLOUD" \ | ||
--arg instance_name "$instance_name" \ | ||
--arg startup_time "$startup_time" \ | ||
--arg uptime "$uptime" \ | ||
'(.allocations[] | select(.id == $allocation_id) | .instances[] | select(.name == $instance_name) | .uptimes) | ||
+= {($startup_time): $uptime|tonumber}' > $JSON_FILE_TMP \ | ||
&& mv $JSON_FILE_TMP $JSON_FILE | ||
else | ||
# Update entry | ||
cat $JSON_FILE | jq \ | ||
--arg allocation_id "$OS_CLOUD" \ | ||
--arg instance_name "$instance_name" \ | ||
--arg startup_time "$startup_time" \ | ||
--arg uptime "$uptime" \ | ||
'(.allocations[] | select(.id == $allocation_id) | .instances[] | select(.name == $instance_name) | .uptimes[$startup_time]) | ||
= ($uptime|tonumber)' > $JSON_FILE_TMP \ | ||
&& mv $JSON_FILE_TMP $JSON_FILE | ||
fi | ||
done | ||
echo "--------" | ||
cat $JSON_FILE | ||
echo "--------" | ||
env: | ||
PREFIX: ${{ vars.INSTANCE_NAME_PREFIX }} |