53 lines
1.3 KiB
Bash
53 lines
1.3 KiB
Bash
#!/bin/bash
|
|
|
|
# Function to check list of required environment variables
|
|
check_env_vars() {
|
|
local missing_vars=()
|
|
for var in "$@"; do
|
|
if [ -z "${!var}" ]; then
|
|
missing_vars+=("$var")
|
|
fi
|
|
done
|
|
if [ ${#missing_vars[@]} -ne 0 ]; then
|
|
echo "Error: The following environment variables are not set: ${missing_vars[*]}"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Function to check if doctl is available
|
|
check_doctl() {
|
|
if ! command -v doctl &> /dev/null; then
|
|
echo "Error: doctl is not installed or not in PATH"
|
|
echo "Please install doctl: https://docs.digitalocean.com/reference/doctl/how-to/install/"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Function to destroy droplet using environment variable
|
|
cleanup_droplet() {
|
|
# Get droplet ID from environment variable
|
|
local droplet_id="$DROPLET_ID"
|
|
|
|
echo "Destroying droplet with ID: $droplet_id"
|
|
|
|
# Authenticate doctl
|
|
doctl auth init --access-token "$DO_TOKEN"
|
|
|
|
# Destroy droplet
|
|
if doctl compute droplet delete "$droplet_id" --force; then
|
|
echo "Droplet $droplet_id destroyed successfully"
|
|
return 0
|
|
else
|
|
echo "Error: Failed to destroy droplet $droplet_id"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Check required environment variables
|
|
check_env_vars "DROPLET_ID" "DO_TOKEN"
|
|
|
|
# Check if doctl is available
|
|
check_doctl
|
|
|
|
# Execute cleanup
|
|
cleanup_droplet |