add droplet deployment
This commit is contained in:
parent
f42607b489
commit
4c67f06599
1 changed files with 150 additions and 0 deletions
150
deploy.sh
Executable file
150
deploy.sh
Executable file
|
@ -0,0 +1,150 @@
|
|||
#!/usr/bin/env bash
|
||||
set -x
|
||||
|
||||
# required for the complex pipes in the machine function
|
||||
shopt -s lastpipe
|
||||
|
||||
# get config, or create one if it doesnt exist
|
||||
config() {
|
||||
local command="$1"; shift
|
||||
|
||||
case $command in
|
||||
get)
|
||||
echo 'Checking config...'
|
||||
if [ -a "${XDG_CONFIG_HOME}/auto-mc-redux/conf" ]; then
|
||||
source "${XDG_CONFIG_HOME}/auto-mc-redux/conf"
|
||||
elif [ -a "$HOME/.config/auto-mc-redux/conf" ]; then
|
||||
source "${HOME}/.config/auto-mc-redux/conf"
|
||||
else
|
||||
echo 'No config found!'
|
||||
config create
|
||||
fi
|
||||
;;
|
||||
create)
|
||||
read -p "enter your DigialOcean API token: " TOKEN
|
||||
# this parameter expands to nothing if unset, or x if it is.
|
||||
if [ -z ${XDG_CONFIG_HOME+x} ]; then
|
||||
mkdir -p "${HOME}/.config/auto-mc-redux"
|
||||
echo export TOKEN="${TOKEN}" > "${HOME}/.config/auto-mc-redux/conf"
|
||||
chmod 600 "${HOME}/.config/auto-mc-redux/conf"
|
||||
else
|
||||
mkdir -p "${XDG_CONFIG_HOME}/auto-mc-redux"
|
||||
echo export TOKEN="${TOKEN}" > "${XDG_CONFIG_HOME}/auto-mc-redux/conf"
|
||||
chmod 600 "${XDG_CONFIG_HOME}/auto-mc-redux/conf"
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
firewall(){
|
||||
local command="$1"; shift
|
||||
|
||||
case $command in
|
||||
# exits 0 if firewall with tag auto-mc exists, 1 if it does not
|
||||
check)
|
||||
echo 'Querying firewall rules...'
|
||||
curl -s -X GET \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
https://api.digitalocean.com/v2/firewalls |\
|
||||
jq -r .firewalls[].name | grep -q "^auto-mc$"
|
||||
;;
|
||||
create)
|
||||
echo 'Creating firewall rule'
|
||||
curl -s -X POST \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-d @- \
|
||||
"https://api.digitalocean.com/v2/firewalls" <<-json
|
||||
{
|
||||
"name": "auto-mc",
|
||||
"tags":["auto-mc"],
|
||||
"inbound_rules": [
|
||||
{
|
||||
"protocol": "tcp",
|
||||
"ports": "25565",
|
||||
"sources": {
|
||||
"addresses": [
|
||||
"0.0.0.0/0",
|
||||
"::/0"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"protocol": "udp",
|
||||
"ports": "25565",
|
||||
"sources": {
|
||||
"addresses": [
|
||||
"0.0.0.0/0",
|
||||
"::/0"
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
json
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
machine(){
|
||||
local command="$1"; shift
|
||||
local arg="$1"; shift
|
||||
|
||||
case $command in
|
||||
create)
|
||||
# read isn't used in these pipelines because of job control shinanigans
|
||||
echo 'Creating Droplet...'
|
||||
curl -s -X POST \
|
||||
-H 'Content-Type: application/json' \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-d '{"name":"auto-mc",
|
||||
"size":"s-2vcpu-4gb",
|
||||
"region":"sfo2",
|
||||
"image":"ubuntu-24-10-x64",
|
||||
"tags":["auto-mc"]}' \
|
||||
"https://api.digitalocean.com/v2/droplets" |\
|
||||
jq .droplet.id | read droplet
|
||||
export droplet
|
||||
echo 'Waiting for droplet to be active...'
|
||||
while sleep 5; do
|
||||
machine show status
|
||||
if [ "$status" = 'active' ]; then
|
||||
echo 'Droplet ready!'
|
||||
break
|
||||
fi
|
||||
done
|
||||
;;
|
||||
destroy)
|
||||
curl -s -X DELETE \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
"https://api.digitalocean.com/v2/droplets?tag_name=auto-mc"
|
||||
;;
|
||||
show)
|
||||
case $arg in
|
||||
ipv4)
|
||||
curl -s -X GET \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
"https://api.digitalocean.com/v2/droplets/$droplet" |\
|
||||
jq '.droplet.networks.v4[] | select(.type == "public")'
|
||||
;;
|
||||
status)
|
||||
curl -s -X GET \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
"https://api.digitalocean.com/v2/droplets/$droplet" |\
|
||||
jq -r .droplet.status | read status
|
||||
export status
|
||||
;;
|
||||
esac
|
||||
esac
|
||||
}
|
||||
|
||||
config get
|
||||
machine create
|
||||
firewall create
|
||||
machine show ipv4
|
||||
|
||||
machine destroy
|
Loading…
Add table
Reference in a new issue