dotfiles/.bin/OLD/hetzner-cli
2024-02-14 07:55:58 +01:00

159 lines
3.6 KiB
Bash
Executable File

#!/bin/sh -e
SERVER= # set by set_server()
SERVER_NAME= # set by set_server()
ACTION= # set by set_action()
SNAPSHOT= # set by set_snapshot()
IMAGE= # set by set_image()
show_state() {
printf "SERVER: %s\n" "$SERVER"
printf "SERVER_NAME: %s\n" "$SERVER_NAME"
printf "ACTION: %s\n" "$ACTION"
printf "SNAPSHOT: %s\n" "$SNAPSHOT"
printf "IMAGE: %s\n" "$IMAGE"
printf "\nContinue? "
read -r _c
case $_c in
[Yy]) return; ;;
*) exit 2; ;;
esac
}
########################
### SERVER SELECTION ###
########################
set_server() {
SERVER_LIST="$(hcloud server list \
-o noheader \
-o columns=id,name,status,ipv4,ipv6)"
SERVER="$(printf '%s\n%s' \
"$SERVER_LIST" \
"0 create new server" \
| fzf --tac --exact --with-nth=2.. \
| cut -d" " -f1 \
)"
}
########################
### ACTION SELECTION ###
########################
# Format: key Description...
ACTION_LIST="\
create_snap Create snapshot
delete_snap Delete snapshot
restore_snap Restore snapshot
on Turn on
off Turn off
delete_server Delete VM(!!!!)
"
set_action() {
ACTION="$(printf '%s\n' "$ACTION_LIST" \
| fzf --tac --exact --with-nth=2.. \
--header="Action for Server ID ${SERVER}" \
| cut -d" " -f1 \
)"
}
##########################
### SNAPSHOT SELECTION ###
##########################
# needs: SERVER
set_snap() {
SNAPSHOT="$(hcloud image list \
-o columns=id,description \
-o noheader \
-t snapshot \
| fzf --tac --exact --with-nth=2.. \
| cut -d" " -f1 \
)"
}
#######################
### IMAGE SELECTION ###
#######################
set_image() {
IMAGE="$(hcloud image list \
-o columns=id,description \
-o noheader \
-t system \
| fzf --tac --exact --with-nth=2.. \
| cut -d" " -f1 \
)"
}
#########################
### CREATE NEW SERVER ###
#########################
# needs: SERVER=0, IMAGE
create_server() {
echo NOT IMPLEMENTED
}
#####################
### DELETE SERVER ###
#####################
# needs: SERVER=0
delete_server() {
echo NOT IMPLEMENTED
}
#########################
### RESTORE FROM SNAP ###
#########################
# needs: SERVER, SNAPSHOT
restore_snap() {
echo NOT IMPLEMENTED
}
#######################
### CREATE SNAPSHOT ###
#######################
# needs: SERVER
create_snap() {
echo NOT IMPLEMENTED
}
#######################
### DELETE SNAPSHOT ###
#######################
# needs: SERVER, SNAPSHOT
delete_snap() {
echo NOT IMPLEMENTED
}
################################
### MAIN PROGRAM STARTS HERE ###
################################
# We need a server
set_server
# In case we want a new server, the action is implicit. For all other
# cases, we ask for user input.
if [ $SERVER -eq 0 ]; then
ACTION=create_server
else
set_action
fi
# Now we evaluate the action and execute the next steps
case $ACTION in
delete_snap) set_snap; delete_snap; ;;
restore_snap) set_snap; restore_snap; ;;
create_snap) create_snap; ;;
create_server) create_server; ;;
delete_server) delete_server; ;;
console) hcloud server request-console ${SERVER}; ;;
turn*on) hcloud server poweron ${SERVER}; exit 0; ;;
turn*off) hcloud server poweroff ${SERVER}; exit 0; ;;
esac