From 9c3ac62b0b78125ff669d2b17c5672e48db3e82c Mon Sep 17 00:00:00 2001 From: Daniel Gibbs Date: Fri, 28 Jul 2023 15:54:05 +0100 Subject: [PATCH] refactor: refactor tmux session handling - Updated the code to use the variable `socketname` instead of `sessionname` when interacting with tmux sessions. - Added logic to create a unique socket name using a UID if it doesn't already exist. - Refactored multiple functions (`fn_start_jk2`, `fn_start_tmux`, `fn_stop_graceful_ctrlc`, `fn_stop_graceful_cmd`, `fn_stop_graceful_goldsrc`, `fn_stop_graceful_avorion`, and `fn_stop_tmux`) to use the new socket name for tmux commands. This commit improves the readability and maintainability of the code by separating the concept of session names from socket names, allowing for more flexibility in managing tmux sessions. --- lgsm/modules/check_status.sh | 2 +- lgsm/modules/command_console.sh | 2 +- lgsm/modules/command_send.sh | 2 +- lgsm/modules/command_start.sh | 20 ++++++++++++++------ lgsm/modules/command_stop.sh | 12 ++++++------ lgsm/modules/info_distro.sh | 2 +- linuxgsm.sh | 1 + 7 files changed, 25 insertions(+), 16 deletions(-) diff --git a/lgsm/modules/check_status.sh b/lgsm/modules/check_status.sh index 6edd4151bf..906675e81f 100644 --- a/lgsm/modules/check_status.sh +++ b/lgsm/modules/check_status.sh @@ -7,4 +7,4 @@ moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")" -status=$(tmux -L "${sessionname}" list-sessions -F "#{session_name}" 2> /dev/null | grep -Ecx "^${sessionname}") +status=$(tmux -L "${socketname}" list-sessions -F "#{session_name}" 2> /dev/null | grep -Ecx "^${sessionname}") diff --git a/lgsm/modules/command_console.sh b/lgsm/modules/command_console.sh index 95945dbe3b..a5d49d7f2f 100644 --- a/lgsm/modules/command_console.sh +++ b/lgsm/modules/command_console.sh @@ -42,7 +42,7 @@ check_status.sh if [ "${status}" != "0" ]; then fn_print_ok_nl "Accessing console" fn_script_log_pass "Console accessed" - tmux -L "${sessionname}" attach-session -t "${sessionname}" + tmux -L "${socketname}" attach-session -t "${sessionname}" fn_print_ok_nl "Closing console" fn_script_log_pass "Console closed" else diff --git a/lgsm/modules/command_send.sh b/lgsm/modules/command_send.sh index 9f7c20dbbd..e3dafd6930 100644 --- a/lgsm/modules/command_send.sh +++ b/lgsm/modules/command_send.sh @@ -26,7 +26,7 @@ if [ "${status}" != "0" ]; then fi echo "" fn_print_dots "Sending command to console: \"${commandtosend}\"" - tmux -L "${sessionname}" send-keys -t "${servicename}" "${commandtosend}" ENTER + tmux -L "${socketname}" send-keys -t "${servicename}" "${commandtosend}" ENTER fn_print_ok_nl "Sending command to console: \"${commandtosend}\"" fn_script_log_pass "Command \"${commandtosend}\" sent to console" else diff --git a/lgsm/modules/command_start.sh b/lgsm/modules/command_start.sh index b9f8fa51c6..295b2c68d2 100644 --- a/lgsm/modules/command_start.sh +++ b/lgsm/modules/command_start.sh @@ -37,7 +37,7 @@ fn_start_teamspeak3() { # Used to allow update to detect JK2MV server version. fn_start_jk2() { fn_start_tmux - tmux -L "${sessionname}" end -t "${sessionname}" version ENTER > /dev/null 2>&1 + tmux -L "${socketname}" end -t "${sessionname}" version ENTER > /dev/null 2>&1 } fn_start_tmux() { @@ -74,13 +74,21 @@ fn_start_tmux() { echo "${port}" >> "${lockdir}/${selfname}.lock" fn_reload_startparameters + # create uid to ensure unique tmux socket + if [ ! -f "${datadir}/${selfname}.uid" ]; then + uid=$(date '+%s' | sha1sum | head -c 8) + echo "${uid}" > "${datadir}/${selfname}.uid" + socketname="${sessionname}-$(cat "${datadir}/${selfname}.uid")" + fi + + if [ "${shortname}" == "av" ]; then cd "${systemdir}" || exit else cd "${executabledir}" || exit fi - tmux -L "${sessionname}" new-session -d -x "${sessionwidth}" -y "${sessionheight}" -s "${sessionname}" "${preexecutable} ${executable} ${startparameters}" 2> "${lgsmlogdir}/.${selfname}-tmux-error.tmp" + tmux -L "${socketname}" new-session -d -x "${sessionwidth}" -y "${sessionheight}" -s "${sessionname}" "${preexecutable} ${executable} ${startparameters}" 2> "${lgsmlogdir}/.${selfname}-tmux-error.tmp" # Create logfile. touch "${consolelog}" @@ -94,9 +102,9 @@ fn_start_tmux() { echo -e "tmux version: master (user compiled)" >> "${consolelog}" if [ "${consolelogging}" == "on" ] || [ -z "${consolelogging}" ]; then if [ "$logtimestamp" == "on" ]; then - tmux -L "${sessionname}" pipe-pane -o -t "${sessionname}" "exec bash -c \"cat | $addtimestamp\" >> '${consolelog}'" + tmux -L "${socketname}" pipe-pane -o -t "${sessionname}" "exec bash -c \"cat | $addtimestamp\" >> '${consolelog}'" else - tmux -L "${sessionname}" pipe-pane -o -t "${sessionname}" "exec cat >> '${consolelog}'" + tmux -L "${socketname}" pipe-pane -o -t "${sessionname}" "exec cat >> '${consolelog}'" fi fi @@ -115,9 +123,9 @@ fn_start_tmux() { # Console logging enable or not set. elif [ "${consolelogging}" == "on" ] || [ -z "${consolelogging}" ]; then if [ "$logtimestamp" == "on" ]; then - tmux -L "${sessionname}" pipe-pane -o -t "${sessionname}" "exec bash -c \"cat | $addtimestamp\" >> '${consolelog}'" + tmux -L "${socketname}" pipe-pane -o -t "${sessionname}" "exec bash -c \"cat | $addtimestamp\" >> '${consolelog}'" else - tmux -L "${sessionname}" pipe-pane -o -t "${sessionname}" "exec cat >> '${consolelog}'" + tmux -L "${socketname}" pipe-pane -o -t "${sessionname}" "exec cat >> '${consolelog}'" fi fi else diff --git a/lgsm/modules/command_stop.sh b/lgsm/modules/command_stop.sh index a0cc180763..e19b87f6e3 100644 --- a/lgsm/modules/command_stop.sh +++ b/lgsm/modules/command_stop.sh @@ -15,7 +15,7 @@ fn_stop_graceful_ctrlc() { fn_print_dots "Graceful: CTRL+c" fn_script_log_info "Graceful: CTRL+c" # Sends quit. - tmux -L "${sessionname}" send-keys -t "${sessionname}" C-c > /dev/null 2>&1 + tmux -L "${socketname}" send-keys -t "${sessionname}" C-c > /dev/null 2>&1 # Waits up to 30 seconds giving the server time to shutdown gracefuly. for seconds in {1..30}; do check_status.sh @@ -43,7 +43,7 @@ fn_stop_graceful_cmd() { fn_print_dots "Graceful: sending \"${1}\"" fn_script_log_info "Graceful: sending \"${1}\"" # Sends specific stop command. - tmux -L "${sessionname}" send -t "${sessionname}" ENTER "${1}" ENTER > /dev/null 2>&1 + tmux -L "${socketname}" send -t "${sessionname}" ENTER "${1}" ENTER > /dev/null 2>&1 # Waits up to ${seconds} seconds giving the server time to shutdown gracefully. for ((seconds = 1; seconds <= ${2}; seconds++)); do check_status.sh @@ -71,7 +71,7 @@ fn_stop_graceful_goldsrc() { fn_print_dots "Graceful: sending \"quit\"" fn_script_log_info "Graceful: sending \"quit\"" # sends quit - tmux -L "${sessionname}" send -t "${sessionname}" quit ENTER > /dev/null 2>&1 + tmux -L "${socketname}" send -t "${sessionname}" quit ENTER > /dev/null 2>&1 # Waits 3 seconds as goldsrc servers restart with the quit command. for seconds in {1..3}; do sleep 1 @@ -184,10 +184,10 @@ fn_stop_graceful_avorion() { fn_print_dots "Graceful: /save /stop" fn_script_log_info "Graceful: /save /stop" # Sends /save. - tmux -L "${sessionname}" send-keys -t "${sessionname}" /save ENTER > /dev/null 2>&1 + tmux -L "${socketname}" send-keys -t "${sessionname}" /save ENTER > /dev/null 2>&1 sleep 5 # Sends /quit. - tmux -L "${sessionname}" send-keys -t "${sessionname}" /stop ENTER > /dev/null 2>&1 + tmux -L "${socketname}" send-keys -t "${sessionname}" /stop ENTER > /dev/null 2>&1 # Waits up to 30 seconds giving the server time to shutdown gracefuly. for seconds in {1..30}; do check_status.sh @@ -240,7 +240,7 @@ fn_stop_tmux() { fn_print_dots "${servername}" fn_script_log_info "tmux kill-session: ${sessionname}: ${servername}" # Kill tmux session. - tmux -L "${sessionname}" kill-session -t "${sessionname}" > /dev/null 2>&1 + tmux -L "${socketname}" kill-session -t "${sessionname}" > /dev/null 2>&1 sleep 0.5 check_status.sh if [ "${status}" == "0" ]; then diff --git a/lgsm/modules/info_distro.sh b/lgsm/modules/info_distro.sh index 8c29a4ca27..4e57754b5b 100644 --- a/lgsm/modules/info_distro.sh +++ b/lgsm/modules/info_distro.sh @@ -10,7 +10,7 @@ moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")" ### Game Server pid if [ "${status}" == "1" ]; then - gameserverpid="$(tmux -L "${sessionname}" list-sessions -F "#{session_name} #{pane_pid}" | grep "^${sessionname} " | awk '{print $NF}')" + gameserverpid="$(tmux -L "${socketname}" list-sessions -F "#{session_name} #{pane_pid}" | grep "^${sessionname} " | awk '{print $NF}')" if [ "${engine}" == "source" ]; then srcdslinuxpid="$(ps -ef | grep -v grep | grep "${gameserverpid}" | grep srcds_linux | awk '{print $2}')" elif [ "${engine}" == "goldsrc" ]; then diff --git a/linuxgsm.sh b/linuxgsm.sh index 52d12faca2..3c71d288f1 100755 --- a/linuxgsm.sh +++ b/linuxgsm.sh @@ -36,6 +36,7 @@ modulesdir="${lgsmdir}/modules" tmpdir="${lgsmdir}/tmp" datadir="${lgsmdir}/data" lockdir="${lgsmdir}/lock" +[ -f "${datadir}/${selfname}.uid" ] && socketname="${sessionname}-$(cat "${datadir}/${selfname}.uid")" || socketname="${sessionname}" serverlist="${datadir}/serverlist.csv" serverlistmenu="${datadir}/serverlistmenu.csv" [ -n "${LGSM_CONFIG}" ] && configdir="${LGSM_CONFIG}" || configdir="${lgsmdir}/config-lgsm"