Skip to content

Commit

Permalink
refactor: refactor check_last_update.sh, command_backup.sh, command_d…
Browse files Browse the repository at this point in the history
…ebug.sh, command_mods_remove.sh, command_monitor.sh, command_start.sh, command_stop.sh, command_ts3_server_pass.sh and core_steamcmd.sh

- Refactored code to improve readability and maintainability.
- Renamed variables for clarity.
- Removed unnecessary comments and unused code.
- Fixed typos and formatting issues.
  • Loading branch information
dgibbs64 committed Jul 30, 2023
1 parent d5a5639 commit b4c6a2a
Show file tree
Hide file tree
Showing 19 changed files with 67 additions and 59 deletions.
6 changes: 3 additions & 3 deletions lgsm/modules/check_last_update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
if [ -f "${lockdir}/${selfname}-last-started.lock" ]; then
laststart=$(cat "${lockdir}/${selfname}-last-started.lock")
fi
if [ -f "${lockdir}/lastupdate.lock" ]; then
lastupdate=$(cat "${lockdir}/lastupdate.lock")
if [ -f last-updated.lock" ]; then
lastupdate=$(cat last-updated.lock")
fi
check_status.sh
if [ -f "${lockdir}/lastupdate.lock" ] && [ "${status}" != "0" ]; then
if [ -f last-updated.lock" ] && [ "${status}" != "0" ]; then
if [ ! -f "${lockdir}/${selfname}-last-started.lock" ] || [ "${laststart}" -lt "${lastupdate}" ]; then
fn_print_info "${selfname} has not been restarted since last update"
fn_script_log_info "${selfname} has not been restarted since last update"
Expand Down
2 changes: 1 addition & 1 deletion lgsm/modules/command_backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ fn_backup_migrate_olddir() {

fn_backup_create_lockfile() {
# Create lockfile.
date '+%s' > "${lockdir}/backup.lock"
date '+%s' > "${lockdir:?}/backup.lock"
fn_script_log_info "Backup lockfile generated"
fn_script_log_info "${lockdir}/backup.lock"
# trap to remove lockfile on quit.
Expand Down
2 changes: 1 addition & 1 deletion lgsm/modules/command_debug.sh
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ fn_script_log_info "Starting debug"
fn_print_ok_nl "Starting debug"

# Create lockfile.
date '+%s' > "${lockdir}/${selfname}-started.lock"
date '+%s' > "${lockdir:?}/${selfname}-started.lock"
echo "${version}" >> "${lockdir}/${selfname}-started.lock"
echo "${port}" >> "${lockdir}/${selfname}-started.lock"
fn_script_log_info "Lockfile generated"
Expand Down
2 changes: 1 addition & 1 deletion lgsm/modules/command_mods_remove.sh
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ fi
if [ "${engine}" == "unity3d" ] && [[ "${modprettyname}" == *"Oxide"* ]]; then
fn_print_information_nl "Validating to restore original ${gamename} files replaced by Oxide"
fn_script_log "Validating to restore original ${gamename} files replaced by Oxide"
exitbypass="1"
exitbypass=1
command_validate.sh
fn_firstcommand_reset
unset exitbypass
Expand Down
32 changes: 14 additions & 18 deletions lgsm/modules/command_monitor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,31 @@ fn_monitor_check_lockfile() {

# Fix if lockfile is not unix time or contains letters
if [ -f "${lockdir}/${selfname}-started.lock" ] && [[ "$(head -n 1 "${lockdir}/${selfname}-started.lock")" =~ [A-Za-z] ]]; then
date '+%s' > "${lockdir}/${selfname}-started.lock"
date '+%s' > "${lockdir:?}/${selfname}-started.lock"
echo "${version}" >> "${lockdir}/${selfname}-started.lock"
echo "${port}" >> "${lockdir}/${selfname}-started.lock"
fi
}

fn_monitor_check_install() {
# Monitor will check if update is running.
if [ "$(pgrep -fc -u "${USER}" "${selfname} install")" != "0" ] || [ "$(pgrep -fc -u "${USER}" "${selfname} i")" != "0" ] || [ "$(pgrep -fc -u "${USER}" "${selfname} auto-install")" != "0" ] || [ "$(pgrep -fc -u "${USER}" "${selfname} ai")" != "0" ]; then
fn_print_dots "Checking installer: "
fn_print_checking_eol
fn_script_log_info "Checking installer: CHECKING"
fn_print_info "Checking installer: LinuxGSM is currently installing: "
fn_print_info "Checking installer: Installer is : "
fn_print_info_eol
fn_script_log_pass "Checking installer: LinuxGSM is currently installing"
fn_script_log_pass "Checking installer: LinuxGSM is installing"
core_exit.sh
fi
}

fn_monitor_check_debug() {
# Monitor will check if debug is running.
if [ "$(pgrep -fc -u "${USER}" "${selfname} debug")" != "0" ] || [ "$(pgrep -fc -u "${USER}" "${selfname} d")" != "0" ]; then
fn_print_dots "Checking debug: "
fn_print_checking_eol
fn_print_info "Checking debug: LinuxGSM is currently in debug mode: "
fn_print_info "Checking debug: Debug is running: "
fn_print_info_eol
fn_script_log_pass "Checking debug: LinuxGSM is currently in debug mode"
fn_script_log_pass "Checking debug: Debug is running"
core_exit.sh
fi
}
Expand All @@ -73,9 +71,9 @@ fn_monitor_check_starting(){
if [ -f "${lockdir}/starting.lock" ] && [[ "$(pgrep -fc -u "${USER}" "${selfname} start")" != "0" || "$(pgrep -fc -u "${USER}" "${selfname} s")" != "0" ]]; then
fn_print_dots "Checking start: "
fn_print_checking_eol
fn_print_info "Checking start: LinuxGSM is currently starting: "
fn_print_info "Checking start: LinuxGSM is starting: "
fn_print_info_eol
fn_script_log_info "Checking backup: LinuxGSM is currently starting"
fn_script_log_info "Checking backup: LinuxGSM is starting"
core_exit.sh
fi
}
Expand All @@ -96,9 +94,9 @@ fn_monitor_check_stopping(){
if [ -f "${lockdir}/stopping.lock" ] && [[ "$(pgrep -fc -u "${USER}" "${selfname} stop")" != "0" || "$(pgrep -fc -u "${USER}" "${selfname} s")" != "0" ]]; then
fn_print_dots "Checking stop: "
fn_print_checking_eol
fn_print_info "Checking stop: LinuxGSM is currently stopping: "
fn_print_info "Checking stop: LinuxGSM is stopping: "
fn_print_info_eol
fn_script_log_info "Checking backup: LinuxGSM is currently stopping"
fn_script_log_info "Checking backup: LinuxGSM is stopping"
core_exit.sh
fi
}
Expand All @@ -116,18 +114,16 @@ fn_monitor_check_backup() {
fi
fi

# Monitor will check if backup is running.
if [ -f "${lockdir}/backup.lock" ] && [[ "$(pgrep -fc -u "${USER}" "${selfname} backup")" != "0" || "$(pgrep -fc -u "${USER}" "${selfname} b")" != "0" ]]; then
fn_print_dots "Checking backup: "
fn_print_checking_eol
fn_print_info "Checking backup: LinuxGSM is currently running a backup: "
fn_print_info "Checking backup: Backup is running: "
fn_print_info_eol
fn_script_log_info "Checking backup: LinuxGSM is currently running a backup"
fn_script_log_info "Checking backup: Backup is running"
core_exit.sh
fi
}

# This includes all update related commands.
fn_monitor_check_update() {
# Remove stale lockfile.
if [ -f "${lockdir}/${selfname}-update.lock" ]; then
Expand All @@ -144,13 +140,14 @@ fn_monitor_check_update() {
if [ -f "${lockdir}/${selfname}-update.lock" ] && [[ "$(pgrep -fc -u "${USER}" "${selfname} update")" != "0" || "$(pgrep -fc -u "${USER}" "${selfname} validate")" != "0" || "$(pgrep -fc -u "${USER}" "${selfname} v")" != "0" || "$(pgrep -fc force-update "${USER}" "${selfname} fu")" != "0" ]]; then
fn_print_dots "Checking update: "
fn_print_checking_eol
fn_print_info_nl "Checking update: LinuxGSM is currently updating: "
fn_print_info_nl "Checking update: LinuxGSM is updating: "
fn_print_info_eol
fn_script_log_pass "Checking update: LinuxGSM is currently updating"
fn_script_log_pass "Checking update: LinuxGSM is updating"
core_exit.sh
fi
}

# Source engine games may display a messages to indicate the server needs restarting.
fn_monitor_check_update_source(){
if [ -f "${consolelogdir}/${selfname}-console.log" ] && [ "${engine}" == "source" ]; then
if grep -q "Your server needs to be restarted in order to receive the latest update." "${consolelogdir}/${selfname}-console.log"; then
Expand All @@ -172,7 +169,6 @@ fn_monitor_check_session() {
fn_print_dots "Checking session: "
fn_print_checking_eol
fn_script_log_info "Checking session: CHECKING"
# uses status var from check_status.sh
if [ "${status}" != "0" ]; then
fn_print_ok "Checking session: "
fn_print_ok_eol_nl
Expand Down
26 changes: 16 additions & 10 deletions lgsm/modules/command_start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,7 @@ fn_start_tmux() {
fi

# Create a starting lockfile that only exists while the start command is running.
date '+%s' > "${lockdir}/${selfname}-starting.lock"

# Create start lockfile that exists only when the server is running.
date '+%s' > "${lockdir}/${selfname}-started.lock"
echo "${version}" >> "${lockdir}/${selfname}-started.lock"
echo "${port}" >> "${lockdir}/${selfname}-started.lock"
date '+%s' > "${lockdir:?}/${selfname}-starting.lock"

fn_reload_startparameters

Expand All @@ -74,10 +69,6 @@ fn_start_tmux() {
# Create logfile.
touch "${consolelog}"

# Create last started Lockfile.
# TODO: should this be since last successful update?
date +%s > "${lockdir}/${selfname}-last-started.lock"

# tmux compiled from source will return "master", therefore ignore it.
if [ "${tmuxv}" == "master" ]; then
fn_script_log "tmux version: master (user compiled)"
Expand Down Expand Up @@ -170,6 +161,16 @@ fn_start_tmux() {
rm -f "${lockdir:?}/${selfname}-starting.lock"
core_exit.sh
else
# Create start lockfile that exists only when the server is running.
date '+%s' > "${lockdir:?}/${selfname}-started.lock"
echo "${version}" >> "${lockdir}/${selfname}-started.lock"
echo "${port}" >> "${lockdir}/${selfname}-started.lock"
fn_print_ok "${servername}"
fn_script_log_pass "Started ${servername}"

# Create last started Lockfile.
date +%s > "${lockdir}/${selfname}-last-started.lock"

fn_print_ok "${servername}"
fn_script_log_pass "Started ${servername}"
fi
Expand All @@ -179,6 +180,11 @@ fn_start_tmux() {

check.sh

# If user ran the start command monitor will become enabled.
if [ -z "${exitbypass}" ]; then
date '+%s' > "${lockdir:?}/${selfname}-monitoring.lock"
fi

# If the server already started dont start again.
if [ "${status}" != "0" ]; then
fn_print_dots "${servername}"
Expand Down
8 changes: 7 additions & 1 deletion lgsm/modules/command_stop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ fn_stop_pre_check() {
check.sh

# Create a stopping lockfile that only exists while the stop command is running.
date '+%s' > "${lockdir}/${selfname}-stopping.lock"
date '+%s' > "${lockdir:?}/${selfname}-stopping.lock"

fn_print_dots "${servername}"

Expand All @@ -281,6 +281,12 @@ fn_stop_pre_check
# Remove started lockfile.
rm -f "${lockdir:?}/${selfname}-started.lock"

# If user ran the stop command monitor will become disabled.
if [ -z "${exitbypass}" ]; then
rm -f > "${lockdir:?}/${selfname}-monitoring.lock"
fi


# Remove stopping lockfile.
rm -f "${lockdir:?}/${selfname}-stopping.lock"

Expand Down
4 changes: 2 additions & 2 deletions lgsm/modules/command_ts3_server_pass.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn_serveradmin_password_prompt() {
fn_serveradmin_password_set() {
# Start server in "new password mode".
ts3serverpass="1"
exitbypass="1"
exitbypass=1
command_start.sh
fn_firstcommand_reset
fn_print_ok_nl "New password applied"
Expand All @@ -40,7 +40,7 @@ check.sh
fn_serveradmin_password_prompt
if [ "${status}" != "0" ]; then
# Stop any running server.
exitbypass="1"
exitbypass=1
command_stop.sh
fn_firstcommand_reset
fn_serveradmin_password_set
Expand Down
4 changes: 2 additions & 2 deletions lgsm/modules/core_steamcmd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ fn_update_steamcmd_compare() {
# Update has been found or force update.
if [ "${localbuild}" != "${remotebuildversion}" ] || [ "${forceupdate}" == "1" ]; then
# Create update lockfile.
date '+%s' > "${lockdir}/${selfname}-update.lock"
date '+%s' > "${lockdir:?}/${selfname}-update.lock"
fn_print_ok_nl "Checking for update: ${remotelocation}"
echo -en "\n"
echo -e "Update available"
Expand Down Expand Up @@ -251,7 +251,7 @@ fn_update_steamcmd_compare() {
fn_firstcommand_reset
fi
unset exitbypass
date +%s > "${lockdir}/lastupdate.lock"
date +%s > last-updated.lock"
alert="update"
elif [ "${commandname}" == "CHECK-UPDATE" ]; then
alert="check-update"
Expand Down
4 changes: 2 additions & 2 deletions lgsm/modules/update_factorio.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fn_update_compare() {
# Update has been found or force update.
if [ "${localbuild}" != "${remotebuildversion}" ] || [ "${forceupdate}" == "1" ]; then
# Create update lockfile.
date '+%s' > "${lockdir}/${selfname}-update.lock"
date '+%s' > "${lockdir:?}/${selfname}-update.lock"
fn_print_ok_nl "Checking for update: ${remotelocation}"
echo -en "\n"
echo -e "Update available"
Expand Down Expand Up @@ -120,7 +120,7 @@ fn_update_compare() {
fn_firstcommand_reset
fi
unset exitbypass
date +%s > "${lockdir}/lastupdate.lock"
date +%s > "${lockdir}/last-updated.lock"
alert="update"
elif [ "${commandname}" == "CHECK-UPDATE" ]; then
alert="check-update"
Expand Down
4 changes: 2 additions & 2 deletions lgsm/modules/update_jediknight2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn_update_compare() {
# Update has been found or force update.
if [ "${localbuild}" != "${remotebuildversion}" ] || [ "${forceupdate}" == "1" ]; then
# Create update lockfile.
date '+%s' > "${lockdir}/${selfname}-update.lock"
date '+%s' > "${lockdir:?}/${selfname}-update.lock"
fn_print_ok_nl "Checking for update: ${remotelocation}"
echo -en "\n"
echo -e "Update available"
Expand Down Expand Up @@ -117,7 +117,7 @@ fn_update_compare() {
fn_firstcommand_reset
fi
unset exitbypass
date +%s > "${lockdir}/lastupdate.lock"
date +%s > "${lockdir}/last-updated.lock"
alert="update"
elif [ "${commandname}" == "CHECK-UPDATE" ]; then
alert="check-update"
Expand Down
4 changes: 2 additions & 2 deletions lgsm/modules/update_minecraft.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn_update_compare() {
# Update has been found or force update.
if [ "${localbuild}" != "${remotebuildversion}" ] || [ "${forceupdate}" == "1" ]; then
# Create update lockfile.
date '+%s' > "${lockdir}/${selfname}-update.lock"
date '+%s' > "${lockdir:?}/${selfname}-update.lock"
fn_print_ok_nl "Checking for update: ${remotelocation}"
echo -en "\n"
echo -e "Update available"
Expand Down Expand Up @@ -131,7 +131,7 @@ fn_update_compare() {
fn_firstcommand_reset
fi
unset exitbypass
date +%s > "${lockdir}/lastupdate.lock"
date +%s > "${lockdir}/last-updated.lock"
alert="update"
elif [ "${commandname}" == "CHECK-UPDATE" ]; then
alert="check-update"
Expand Down
4 changes: 2 additions & 2 deletions lgsm/modules/update_minecraft_bedrock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fn_update_compare() {
# Update has been found or force update.
if [ "${localbuild}" != "${remotebuildversion}" ] || [ "${forceupdate}" == "1" ]; then
# Create update lockfile.
date '+%s' > "${lockdir}/${selfname}-update.lock"
date '+%s' > "${lockdir:?}/${selfname}-update.lock"
fn_print_ok_nl "Checking for update: ${remotelocation}"
echo -en "\n"
echo -e "Update available"
Expand Down Expand Up @@ -136,7 +136,7 @@ fn_update_compare() {
fn_firstcommand_reset
fi
unset exitbypass
date +%s > "${lockdir}/lastupdate.lock"
date +%s > "${lockdir}/last-updated.lock"
alert="update"
elif [ "${commandname}" == "CHECK-UPDATE" ]; then
alert="check-update"
Expand Down
4 changes: 2 additions & 2 deletions lgsm/modules/update_mta.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn_update_compare() {
# Update has been found or force update.
if [ "${localbuild}" != "${remotebuildversion}" ] || [ "${forceupdate}" == "1" ]; then
# Create update lockfile.
date '+%s' > "${lockdir}/${selfname}-update.lock"
date '+%s' > "${lockdir:?}/${selfname}-update.lock"
if [ "${forceupdate}" == "1" ]; then
# forceupdate bypasses checks, useful for small build changes
mtaupdatestatus="forced"
Expand Down Expand Up @@ -121,7 +121,7 @@ fn_update_compare() {
fn_firstcommand_reset
fi
unset exitbypass
date +%s > "${lockdir}/lastupdate.lock"
date +%s > "${lockdir}/last-updated.lock"
alert="update"
elif [ "${commandname}" == "CHECK-UPDATE" ]; then
alert="check-update"
Expand Down
4 changes: 2 additions & 2 deletions lgsm/modules/update_papermc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn_update_compare() {
# Update has been found or force update.
if [ "${localbuild}" != "${remotebuildversion}" ] || [ "${forceupdate}" == "1" ]; then
# Create update lockfile.
date '+%s' > "${lockdir}/${selfname}-update.lock"
date '+%s' > "${lockdir:?}/${selfname}-update.lock"
fn_print_ok_nl "Checking for update: ${remotelocation}"
echo -en "\n"
echo -e "Update available"
Expand Down Expand Up @@ -141,7 +141,7 @@ fn_update_compare() {
fn_firstcommand_reset
fi
unset exitbypass
date +%s > "${lockdir}/lastupdate.lock"
date +%s > "${lockdir}/last-updated.lock"
alert="update"
elif [ "${commandname}" == "CHECK-UPDATE" ]; then
alert="check-update"
Expand Down
4 changes: 2 additions & 2 deletions lgsm/modules/update_steamcmd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ if [ "${forceupdate}" == "1" ]; then
command_stop.sh
fn_firstcommand_reset
fn_dl_steamcmd
date +%s > "${lockdir}/lastupdate.lock"
date +%s > "${lockdir}/last-updated.lock"
exitbypass=1
command_start.sh
fn_firstcommand_reset
else
fn_dl_steamcmd
date +%s > "${lockdir}/lastupdate.lock"
date +%s > "${lockdir}/last-updated.lock"
fi
else
fn_update_steamcmd_localbuild
Expand Down
Loading

0 comments on commit b4c6a2a

Please sign in to comment.