Skip to content

Instantly share code, notes, and snippets.

@pirafrank
Forked from Knight1/hetzner.sh
Created October 18, 2020 19:12
Show Gist options
  • Save pirafrank/e7e0b64d84927150a39f0cab4e04dd9c to your computer and use it in GitHub Desktop.
Save pirafrank/e7e0b64d84927150a39f0cab4e04dd9c to your computer and use it in GitHub Desktop.
Hetzner Cloud create temporary Windows Servers
#!/bin/bash
ID=`hcloud server list | tail -1 | grep Windoof | awk '{print $1;}'`
CONTEXT=`hcloud context active`
if [ "$CONTEXT" != "testing" ]; then
echo "[CRIT] Aborting, wrong context"
exit
fi
#stop, force stop, snapshot, delete
delete() {
hcloud server shutdown $ID
if [ "$?" != "0" ]; then
sleep 60
hcloud server poweroff $ID
fi
make_snaphot
hcloud server delete $ID
}
#Create snapshot
make_snapshot() {
hcloud server create-image $ID --type snapshot --description `date '+Windoof-%Y-%m-%d_%H-%M'` || echo "[CRIT] Snapshot error" | exit 1
}
#create from snapshot
create() {
if [ "$2" == "latest" ]; then
hcloud image list | grep snapshot | grep Windoof
read -n 1 -p "Select Snapshot ID:" SNAPID
else
SNAPID=`hcloud image list | grep snapshot | grep Windoof | tail -1 | awk '{print $1;}'`
fi
if [ "$SNAPID" == "" ]; then
exit 1
fi
hcloud server create --datacenter 2 --image $SNAPID --name Windoof --type 3
}
case "$1" in
create)
create
;;
delete)
delete
;;
snapshot)
make_snapshot
;;
*)
echo "Nothing selected"
exit
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment