Skip to content

Instantly share code, notes, and snippets.

@franzwong
Created April 13, 2023 02:11
Show Gist options
  • Save franzwong/8086a2c19b86b0b90adde995667a93cd to your computer and use it in GitHub Desktop.
Save franzwong/8086a2c19b86b0b90adde995667a93cd to your computer and use it in GitHub Desktop.
Rebuild DigitalOcean droplet
#!/bin/bash
function rebuild_droplet() {
local -r token=${1}
if [[ ${#} -ge 2 ]]; then
local -r image_slug=${2}
else
local -r image_slug=ubuntu-22-04-x64
fi
# Assume we only have 1 droplet
droplet_id=$(curl -X GET \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${token}" \
"https://api.digitalocean.com/v2/droplets?page=1&per_page=1" | jq '.droplets[0].id')
curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${token}" \
-d '{"type":"rebuild","image":"'"${image_slug}"'"}' \
"https://api.digitalocean.com/v2/droplets/${droplet_id}/actions"
}
rebuild_droplet "${@}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment