shcloud/shcloud
2022-08-22 06:27:07 +02:00

130 lines
4.5 KiB
Bash

#!/bin/bash
source $(dirname "$0")/.env
SERVER_NAME="\"shcloud-fsn1\""
VOLUME_ID=11742041
GAME=sfserver
FLOATING_IPv6=571649
IPv6_suffix="affe"
IPv6=$(curl -sH "Authorization: Bearer ${API_TOKEN}" "https://api.hetzner.cloud/v1/floating_ips/${FLOATING_IPv6}" | jq -r ".floating_ip.dns_ptr[0].ip")$IPv6_suffix
#echo $IPv6
select_server_type(){
SERVER_TYPE=$(whiptail --title "Choose server model" --menu " name vcpus ram ssd price/h " 20 70 10 \
"cx11" " 1 2 GB 20 GB 0,005 €/h" \
"cpx11" " 2 2 GB 40 GB 0,007 €/h" \
"cx21" " 2 4 GB 40 GB 0,010 €/h" \
"cpx21" " 3 4 GB 80 GB 0,013 €/h" \
"cx31" " 2 8 GB 80 GB 0,017 €/h" \
"cpx31" " 4 8 GB 160 GB 0,024 €/h" \
"cx41" " 4 16 GB 160 GB 0,031 €/h" \
"cpx41" " 8 16 GB 240 GB 0,045 €/h" \
"cx51" " 8 32 GB 240 GB 0,060 €/h" \
"cpx51" " 16 32 GB 360 GB 0,095 €/h" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ]; then
echo $SERVER_TYPE
return 0
else
exit 1
fi
}
#Abfragen aller Server
servers=$(curl \
-H "Authorization: Bearer $API_TOKEN" \
'https://api.hetzner.cloud/v1/servers' -s)
clouddeployed=$(echo $servers | jq ".servers[]|select(.name == $SERVER_NAME)")
clouddeployedid=$(echo $clouddeployed | jq ".id")
clouddeployedname=$(echo $clouddeployed | jq ".name")
if [ ! -z $1 ]
then
if [ $1 = "deploy" ]
then
echo ""
echo "-----------------------------------"
echo " Welcome to SHCLOUD Server Manager "
echo "-----------------------------------"
echo ""
#Check, ob Server $SERVER_NAME bereits existiert
if [ $clouddeployedname = $SERVER_NAME ] &>/dev/null
then
echo "ACHTUNG"
read -p "Server läuft bereits. Kündigen? [y/N] " delete
if [ -z $delete ] || [ $delete = "n" ] || [ $delete = "N" ]
then
echo "Dann geht das hier nicht."
exit 1
else
shcloud delete
fi
fi
#Servertyp abfragen oder direkt übernehmen
if [ -z $2 ]
then
type=$(select_server_type)
else
type=$2
fi
#Server mieten
if (whiptail --yesno "Deploy $type?" 0 0)
then
deploy=$(curl \
-X POST \
-H "Authorization: Bearer $API_TOKEN" \
-H "Content-Type: application/json" \
-d '{"name":"shcloud-fsn1","location":"fsn1","server_type":"'$type'","image":"debian-11","ssh_keys":["secondhemd"],"volumes":['$VOLUME_ID'],"user_data":"#!/bin/bash\ncurl -sL ar21.de/shinit.php?IPv6='$IPv6'\\&GAME='$GAME'\\&VOLUME='$VOLUME_ID' | bash"}' \
'https://api.hetzner.cloud/v1/servers' -s)
#echo $deploy
server_id=$(echo $deploy | jq ".server.id")
sleep 1
assign=$(curl \
-X POST \
-H "Authorization: Bearer $API_TOKEN" \
-H "Content-Type: application/json" \
-d '{"server":'$server_id'}' \
'https://api.hetzner.cloud/v1/floating_ips/'$FLOATING_IPv6'/actions/assign' -s)
echo "Server wurde mit ID $server_id erstellt und shcloud.eu zugewiesen."
fi
exit 0
elif [ $1 = "delete" ]
then
#Server stoppen
echo "Stoppe server..."
ssh shcloud.eu 'su -c "cd /gameserver/'$GAME' && /gameserver/'$GAME'/'$GAME' stop" gameserver'
echo "Fahre Server herunter..."
ssh shcloud.eu 'shutdown now'
sleep 5
#Volume entfernen
volume_detach=$(curl -X POST -H "Authorization: Bearer $API_TOKEN" 'https://api.hetzner.cloud/v1/volumes/'$VOLUME_ID'/actions/detach' -s)
echo "Aushängen des Volume: "
volume_detach_error=$(echo $volume_detach | jq ".action.error.message")
if [ $volume_detach_error = null ]
then
echo "success"
else
echo $volume_detach_error
fi
sleep 2
echo "Löschen des Servers:"
apidelete=$(curl -X DELETE -H "Authorization: Bearer $API_TOKEN" 'https://api.hetzner.cloud/v1/servers/'$clouddeployedid -s)
delete_error=$(echo $apidelete | jq ".action.error.message")
if [ $delete_error = null ]
then
echo "success"
else
echo $delete_error
fi
elif [ $1 = "info" ]
select_server_type
then
echo -e "name\tvcpus\tram\tssd\tprice/h\ncx11\t1\t2 GB\t20 GB\t0,005 €/h\ncpx11\t2\t2 GB\t40 GB\t0,007 €/h\ncx21\t2\t4 GB\t40 GB\t0,010 €/h\ncpx21\t3\t4 GB\t80 GB\t0,013 €/h\ncx31\t2\t8 GB\t80 GB\t0,017 €/h\ncpx31\t4\t8 GB\t160 GB\t0,024 €/h\ncx41\t4\t16 GB\t160 GB\t0,031 €/h\ncpx41\t8\t16 GB\t240 GB\t0,045 €/h\ncx51\t8\t32 GB\t240 GB\t0,060 €/h\ncpx51\t16\t32 GB\t360 GB\t0,095 €/h"
fi
else
echo "Nutzung:"
echo "shcloud [aktion]"
echo "- deploy"
echo "- delete"
echo "- info"
fi