Skip to content

Instantly share code, notes, and snippets.

@VMuliadi
Created July 20, 2023 12:19
Show Gist options
  • Save VMuliadi/f9089bb817f3daab1cc4e0754671be42 to your computer and use it in GitHub Desktop.
Save VMuliadi/f9089bb817f3daab1cc4e0754671be42 to your computer and use it in GitHub Desktop.
#!/bin/bash
droplet_name=""
droplet_ipv4=""
doctl_bin=$(which doctl)
function usage() {
printf """digitalocean doctl wrapper
manage digitalocean droplet management
./doctl-wrapper.sh -a action [-c context_name] [-d droplet_id]
-c : digitalocean context name
-d : digitalocean droplet id
"""
}
while getopts ":c:d:" args; do
case "${args}" in
c)
droplet_context=${OPTARG}
if [[ ! $(${doctl_bin} auth list) ]]; then
echo "Can't found ${context} in this device ..."
echo "Please login and register your context first"
echo "Check https://docs.digitalocean.com/reference/doctl/how-to/install/"
${doctl_bin} auth init --context ${droplet_context}
fi
;;
d)
droplet_id=${OPTARG}
;;
*)
usage
exit 0
;;
esac
done
shift $((OPTIND-1))
function init() {
if [[ ! $(command -v doctl) ]]; then
echo "Please install and setup your doctl first"
echo "Check https://docs.digitalocean.com/reference/doctl/how-to/install/"
exit 1
fi
}
function get_droplet_id() {
${doctl_bin} compute droplet list --output json | \
jq -r '.[] | "\(.id) \(.name) \(.networks.v4[0].ip_address)"' | \
column -t # print droplet_id droplet_name and droplet_ip
# ask until the user give non-empty respond to the prompt
while [[ (-z ${droplet_id}) || (${droplet_id} == "") ]]; do
read -p "Please insert the droplet id that you want to manage: " droplet_id
if [[ (-z ${droplet_id}) || (${droplet_id} == "") ]]; then continue; fi
done
}
function validate_droplet_id() {
while [[ $(${doctl_bin} compute droplet list --no-header \
--output text | grep ${droplet_id} | wc -l) -eq 0 ]]; do
echo "Droplet ID ${droplet_id} not found ..."
unset droplet_id; echo -e "\n" # reset value to default
get_droplet_id
done
droplet_output=$(${doctl_bin} compute droplet get ${droplet_id} --output json)
droplet_name=$(echo ${droplet_output} | jq -r ".[0].name")
droplet_ipv4=$(echo ${droplet_output} | jq -r ".[0].networks.v4[0].ip_address")
}
function droplet_toggle_power() {
droplet_action="" # get current state. if status active, ask to turn-off and vice versa ...
droplet_state=$(${doctl_bin} compute droplet get ${droplet_id} --output json | jq -r '.[0].status')
if [[ ${droplet_state} == "off" ]]; then
droplet_state="active"
droplet_action="power-on"
elif [[ ${droplet_state} == "active" ]]; then
droplet_state="turned-on"
droplet_action="power-off"
fi
echo "${droplet_id} is currently ${droplet_state} right now ..."
read -p "Are you sure to ${droplet_action} ${droplet_id} [y/N]? " droplet_action_confirmation
if [[ ${droplet_action_confirmation} == "y" || ${droplet_action_confirmation} == "Y" ]]; then
droplet_state=$(${doctl_bin} compute droplet get ${droplet_id} --output json | jq -r '.[0].status')
echo "Waiting ${droplet_id} to ${droplet_action} ..."
if [[ $(${doctl_bin} compute droplet-action ${droplet_action} ${droplet_id} --output json --wait | \
jq -r '.[0].status') == "Completed" ]]; then
echo "${droplet_action} action complete for ${droplet_id}"
fi
fi
}
function main() {
init
# check if the user define the droplet_id or not
# if the user define it, then use the defined id
# if not, ask the user by printing the droplets
if [[ -z ${droplet_id} ]]; then
echo "Droplet ID is not defined"
echo "Please input the droplet id from output below"
get_droplet_id
fi
validate_droplet_id
droplet_toggle_power
}
main $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment