Davide Polonio c3cfd99f17
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
fix: add log, set handler for SIGINT
2023-05-25 11:13:30 +02:00

110 lines
2.8 KiB
Bash
Executable File

#!/bin/bash
set -e
### ###
# #
# V Rising on-the-fly shell #
# v0.2 #
### ###
stdout() {
echo "${1}"
}
stderr() {
>&2 echo "${1}"
}
log() {
local action="${1}"
local when=""
when="$(date +%R:%S)"
echo "${when} - ${action}" >> "${THIS_LOG}" 2>&1 /dev/null
}
editWithRestart() {
local full_path_file_to_edit="${1}"
nano "${full_path_file_to_edit}"
log "File edit - ${full_path_file_to_edit}"
stdout "The server will now be restarted and you will be attached to the logs."
stdout "Use ctrl-c to detach from the logs (the service will continue to run in background)"
log "Server restart"
(cd "${VRISING_COMPOSE_HOME}" && docker-compose restart && docker-compose logs -f)
}
ctrl_c_handler() {
# shellcheck disable=SC2317
log "Logout with ctrl-c"
# shellcheck disable=SC2317
exit 0
}
trap 'ctrl_c_handler' INT
USERNAME="${1}"
shift
BASE_VRISING_FOLDER="/srv/docker/vrising/saves/Settings/"
GAME_SETTINGS="ServerGameSettings.json"
HOST_SETTINGS="ServerHostSettings.json"
VRISING_COMPOSE_HOME="/home/davide/services/vrising/"
LOGS_FOLDER="${HOME}/logs"
THIS_LOG="${USERNAME}-$(date --iso-8601=ns).log"
mkdir -p "${LOGS_FOLDER}" || (stderr "Unable to create logs folder, exiting" && exit 1)
stdout "Welcome ${USERNAME}, please remember that your current activity in the server is logged!"
while true
do
stdout """
Select one of the following actions:
0 - exit
1 - edit ${GAME_SETTINGS}
2 - edit ${HOST_SETTINGS}
3 - inspect server logs
4 - restart server
5 - recreate server (destroy and recreate container)
"""
ACTION="-1"
read -rp "> " ACTION
case "${ACTION}" in
0)
log "Exit"
exit 0
;;
1)
editWithRestart "${BASE_VRISING_FOLDER}${GAME_SETTINGS}"
;;
2)
editWithRestart "${BASE_VRISING_FOLDER}${HOST_SETTINGS}"
;;
3)
stdout "Use ctrl-c to detach from the logs at any time"
log "Show logs"
(cd "${VRISING_COMPOSE_HOME}" && docker-compose logs -f)
;;
4)
stdout "Use ctrl-c to detach from the logs at any time"
log "Server restart"
(cd "${VRISING_COMPOSE_HOME}" && docker-compose restart && docker-compose logs -f)
;;
5)
stdout "Use ctrl-c to detach from the logs at any time"
log "Recreate server"
(cd "${VRISING_COMPOSE_HOME}" && docker-compose down -v && sleep 1 && docker-compose up -d && docker-compose logs -f)
;;
*)
log "Invalid input: ${ACTION}"
stderr "The provided input is not valid. Please chose a valid action"
;;
esac
done
exit 0