Skip to content

Commit 147e701

Browse files
committed
feat: pre/post/error hooks
1 parent 4ddd8ee commit 147e701

File tree

3 files changed

+62
-47
lines changed

3 files changed

+62
-47
lines changed

lgsm/modules/core_exit.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ elif [ "${exitcode}" != "0" ]; then
4040
fn_exit_dev_debug
4141
# remove trap.
4242
trap - INT
43+
# run error hook
44+
if [ -f "${hooksdir}/hook-error-${hookoption}.sh" ]; then
45+
eval "${hooksdir}/hook-error-${hookoption}.sh"
46+
fi
47+
unset hookoption
4348
exit "${exitcode}"
4449
elif [ "${exitcode}" ] && [ "${exitcode}" == "0" ]; then
4550
# List LinuxGSM version in logs
@@ -48,6 +53,11 @@ elif [ "${exitcode}" ] && [ "${exitcode}" == "0" ]; then
4853
fn_exit_dev_debug
4954
# remove trap.
5055
trap - INT
56+
# run post hook
57+
if [ -f "${hooksdir}/hook-post-${hookoption}.sh" ]; then
58+
eval "${hooksdir}/hook-post-${hookoption}.sh"
59+
fi
60+
unset hookoption
5161
exit "${exitcode}"
5262
else
5363
# List LinuxGSM version in logs

lgsm/modules/core_getopt.sh

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,54 @@
88
moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
99

1010
### Define all commands here.
11-
## User commands | Trigger commands | Description
11+
## User commands | Option | Trigger commands | Description
1212
# Standard commands.
13-
cmd_install=("i;install" "command_install.sh" "Install the server.")
14-
cmd_auto_install=("ai;auto-install" "fn_autoinstall" "Install the server without prompts.")
15-
cmd_start=("st;start" "command_start.sh" "Start the server.")
16-
cmd_stop=("sp;stop" "command_stop.sh" "Stop the server.")
17-
cmd_restart=("r;restart" "command_restart.sh" "Restart the server.")
18-
cmd_details=("dt;details" "command_details.sh" "Display server information.")
19-
cmd_postdetails=("pd;postdetails" "command_postdetails.sh" "Post details to termbin.com (removing passwords).")
20-
cmd_backup=("b;backup" "command_backup.sh" "Create backup archives of the server.")
21-
cmd_update_linuxgsm=("ul;update-lgsm;uf;update-modules" "command_update_linuxgsm.sh" "Check and apply any LinuxGSM updates.")
22-
cmd_test_alert=("ta;test-alert" "command_test_alert.sh" "Send a test alert.")
23-
cmd_monitor=("m;monitor" "command_monitor.sh" "Check server status and restart if crashed.")
24-
cmd_skeleton=("sk;skeleton" "command_skeleton.sh" "Create a skeleton directory.")
25-
cmd_sponsor=("s;sponsor" "command_sponsor.sh" "Donation options.")
26-
cmd_send=("sd;send" "command_send.sh" "Send command to game server console.")
13+
cmd_install=("i;install" "install" "command_install.sh" "Install the server.")
14+
cmd_auto_install=("ai;auto-install" "auto-install" "fn_autoinstall" "Install the server without prompts.")
15+
cmd_start=("st;start" "start" "command_start.sh" "Start the server.")
16+
cmd_stop=("sp;stop" "stop" "command_stop.sh" "Stop the server.")
17+
cmd_restart=("r;restart" "restart" "command_restart.sh" "Restart the server.")
18+
cmd_details=("dt;details" "details" "command_details.sh" "Display server information.")
19+
cmd_postdetails=("pd;postdetails" "postdetails" "command_postdetails.sh" "Post details to termbin.com (removing passwords).")
20+
cmd_backup=("b;backup" "backup" "command_backup.sh" "Create backup archives of the server.")
21+
cmd_update_linuxgsm=("ul;update-lgsm;uf;update-modules" "update-lgsm" "command_update_linuxgsm.sh" "Check and apply any LinuxGSM updates.")
22+
cmd_test_alert=("ta;test-alert" "test-alert" "command_test_alert.sh" "Send a test alert.")
23+
cmd_monitor=("m;monitor" "monitor" "command_monitor.sh" "Check server status and restart if crashed.")
24+
cmd_skeleton=("sk;skeleton" "skeleton" "command_skeleton.sh" "Create a skeleton directory.")
25+
cmd_sponsor=("s;sponsor" "sponsor" "command_sponsor.sh" "Donation options.")
26+
cmd_send=("sd;send" "send" "command_send.sh" "Send rommand to game server console.")
2727
# Console servers only.
28-
cmd_console=("c;console" "command_console.sh" "Access server console.")
29-
cmd_debug=("d;debug" "command_debug.sh" "Start server directly in your terminal.")
28+
cmd_console=("c;console" "console" "command_console.sh" "Access server console.")
29+
cmd_debug=("d;debug" "debug" "command_debug.sh" "Start server directly in your terminal.")
3030
# Update servers only.
31-
cmd_update=("u;update" "command_update.sh" "Check and apply any server updates.")
32-
cmd_check_update=("cu;check-update" "command_check_update.sh" "Check if a gameserver update is available")
33-
cmd_force_update=("fu;force-update;update-restart;ur" "forceupdate=1; command_update.sh" "Apply server updates bypassing check.")
31+
cmd_update=("u;update" "update" "command_update.sh" "Check and apply any server updates.")
32+
cmd_check_update=("cu;check-update" "check-update" "command_check_update.sh" "Check if a gameserver update is available")
33+
cmd_force_update=("fu;force-update;update-restart;ur" "force-update" "forceupdate=1; command_update.sh" "Apply server updates bypassing check.")
3434
# SteamCMD servers only.
35-
cmd_validate=("v;validate" "command_validate.sh" "Validate server files with SteamCMD.")
35+
cmd_validate=("v;validate" "validate" "command_validate.sh" "Validate server files with SteamCMD.")
3636
# Server with mods-install.
37-
cmd_mods_install=("mi;mods-install" "command_mods_install.sh" "View and install available mods/addons.")
38-
cmd_mods_remove=("mr;mods-remove" "command_mods_remove.sh" "View and remove an installed mod/addon.")
39-
cmd_mods_update=("mu;mods-update" "command_mods_update.sh" "Update installed mods/addons.")
37+
cmd_mods_install=("mi;mods-install" "mods-install" "command_mods_install.sh" "View and install available mods/addons.")
38+
cmd_mods_remove=("mr;mods-remove" "mods-remove" "command_mods_remove.sh" "View and remove an installed mod/addon.")
39+
cmd_mods_update=("mu;mods-update" "mods-update" "command_mods_update.sh" "Update installed mods/addons.")
4040
# Server specific.
41-
cmd_change_password=("pw;change-password" "command_ts3_server_pass.sh" "Change TS3 serveradmin password.")
42-
cmd_install_default_resources=("ir;install-default-resources" "command_install_resources_mta.sh" "Install the MTA default resources.")
43-
cmd_fullwipe=("fw;full-wipe;wa;wipeall" "serverwipe=1; command_wipe.sh" "Reset the map and remove blueprint data.")
44-
cmd_mapwipe=("mw;map-wipe;w;wipe;wi" "mapwipe=1; command_wipe.sh" "Reset the map and keep blueprint data.")
45-
cmd_map_compressor_u99=("mc;map-compressor" "compress_ut99_maps.sh" "Compresses all ${gamename} server maps.")
46-
cmd_map_compressor_u2=("mc;map-compressor" "compress_unreal2_maps.sh" "Compresses all ${gamename} server maps.")
47-
cmd_install_cdkey=("cd;server-cd-key" "install_ut2k4_key.sh" "Add your server cd key.")
48-
cmd_install_dst_token=("ct;cluster-token" "install_dst_token.sh" "Configure cluster token.")
49-
cmd_install_squad_license=("li;license" "install_squad_license.sh" "Add your Squad server license.")
50-
cmd_fastdl=("fd;fastdl" "command_fastdl.sh" "Build a FastDL directory.")
41+
cmd_change_password=("pw;change-password" "change-password" "command_ts3_server_pass.sh" "Change TS3 serveradmin password.")
42+
cmd_install_default_resources=("ir;install-default-resources" "install-default-resources" "command_install_resources_mta.sh" "Install the MTA default resources.")
43+
cmd_fullwipe=("fw;full-wipe;wa;wipeall" "full-wipe" "servrrwipe=1; command_wipe.sh" "Reset the map and remove blueprint data.")
44+
cmd_mapwipe=("mw;map-wipe;w;wipe;wi" "map-wipe" "mapwipe=1; command_wipe.sh" "Reset the map and keep blueprint data.")
45+
cmd_map_compressor_u99=("mc;map-compressor" "map-compressor" "compress_ut99_maps.sh" "Compresses all ${gamename} server maps.")
46+
cmd_map_compressor_u2=("mc;map-compressor" "map-compressor" "compress_unreal2_maps.sh" "Compresses all ${gamename} server maps.")
47+
cmd_install_cdkey=("cd;server-cd-key" "server-cd-key" "install_ut2k4_key.sh" "Add your server cd key.")
48+
cmd_install_dst_token=("ct;cluster-token" "cluster-token" "install_dst_token.sh" "Configure cluster token.")
49+
cmd_install_squad_license=("li;license" "license" "install_squad_license.sh" "Add your Squad server license.")
50+
cmd_fastdl=("fd;fastdl" "fastdl" "command_fastdl.sh" "Build a FastDL directory.")
5151
# Dev commands.
52-
cmd_dev_debug=("dev;developer" "command_dev_debug.sh" "Enable developer Mode.")
53-
cmd_dev_details=("ddt;detect-details" "command_dev_details.sh" "Display parsed details.")
54-
cmd_dev_detect_deps=("dd;detect-deps" "command_dev_detect_deps.sh" "Detect required dependencies.")
55-
cmd_dev_detect_glibc=("dg;detect-glibc" "command_dev_detect_glibc.sh" "Detect required glibc.")
56-
cmd_dev_detect_ldd=("dl;detect-ldd" "command_dev_detect_ldd.sh" "Detect required dynamic dependencies.")
57-
cmd_dev_query_raw=("qr;query-raw" "command_dev_query_raw.sh" "The raw output of gamedig and gsquery.")
58-
cmd_dev_clear_modules=("cm;clear-modules" "command_dev_clear_modules.sh" "Delete the contents of the modules dir.")
59-
52+
cmd_dev_debug=("dev;developer" "developer" "command_dev_debug.sh" "Enable developer Mode.")
53+
cmd_dev_details=("ddt;detect-details" "detect-details" "command_dev_details.sh" "Display parsed details.")
54+
cmd_dev_detect_deps=("dd;detect-deps" "detect-deps" "command_dev_detect_deps.sh" "Detect required dependencies.")
55+
cmd_dev_detect_glibc=("dg;detect-glibc" "detect-glibc" "command_dev_detect_glibc.sh" "Detect required glibc.")
56+
cmd_dev_detect_ldd=("dl;detect-ldd" "detect-ldd" "command_dev_detect_ldd.sh" "Detect required dynamic dependencies.")
57+
cmd_dev_query_raw=("qr;query-raw" "query-raw" "command_dev_query_raw.sh" "The raw output of gamedig and gsquery.")
58+
cmd_dev_clear_modules=("cm;clear-modules" "clear-modules" "command_dev_clear_modules.sh" "Delete the contents of the modules dir.")
6059
### Set specific opt here.
6160

6261
currentopt=("${cmd_start[@]}" "${cmd_stop[@]}" "${cmd_restart[@]}" "${cmd_monitor[@]}" "${cmd_test_alert[@]}" "${cmd_details[@]}" "${cmd_postdetails[@]}" "${cmd_skeleton[@]}")
@@ -155,7 +154,7 @@ currentopt+=("${cmd_sponsor[@]}")
155154
### Build list of available commands.
156155
optcommands=()
157156
index="0"
158-
for ((index = "0"; index < ${#currentopt[@]}; index += 3)); do
157+
for ((index = "0"; index < ${#currentopt[@]}; index += 4)); do
159158
cmdamount=$(echo -e "${currentopt[index]}" | awk -F ';' '{ print NF }')
160159
for ((cmdindex = 1; cmdindex <= cmdamount; cmdindex++)); do
161160
optcommands+=("$(echo -e "${currentopt[index]}" | awk -F ';' -v x=${cmdindex} '{ print $x }')")
@@ -175,8 +174,8 @@ fn_opt_usage() {
175174
{
176175
for ((index = "0"; index < ${#currentopt[@]}; index += 3)); do
177176
# Hide developer commands.
178-
if [ "${currentopt[index + 2]}" != "DEVCOMMAND" ]; then
179-
echo -e "${cyan}$(echo -e "${currentopt[index]}" | awk -F ';' '{ print $2 }')\t${default}$(echo -e "${currentopt[index]}" | awk -F ';' '{ print $1 }')\t| ${currentopt[index + 2]}"
177+
if [ "${currentopt[index + 3]}" != "DEVCOMMAND" ]; then
178+
echo -e "${cyan}$(echo -e "${currentopt[index]}" | awk -F ';' '{ print $2 }')\t${default}$(echo -e "${currentopt[index]}" | awk -F ';' '{ print $1 }')\t| ${currentopt[index + 3]}"
180179
fi
181180
done
182181
} | column -s $'\t' -t
@@ -193,12 +192,17 @@ for i in "${optcommands[@]}"; do
193192
if [ "${i}" == "${getopt}" ]; then
194193
# Seek and run command.
195194
index="0"
196-
for ((index = "0"; index < ${#currentopt[@]}; index += 3)); do
195+
for ((index = "0"; index < ${#currentopt[@]}; index += 4)); do
197196
currcmdamount=$(echo -e "${currentopt[index]}" | awk -F ';' '{ print NF }')
198197
for ((currcmdindex = 1; currcmdindex <= currcmdamount; currcmdindex++)); do
199198
if [ "$(echo -e "${currentopt[index]}" | awk -F ';' -v x=${currcmdindex} '{ print $x }')" == "${getopt}" ]; then
199+
# run pre hook
200+
hookoption="${currentopt[index + 1]}"
201+
if [ -f "${hooksdir}/hook-pre-${hookoption}.sh" ]; then
202+
eval "${hooksdir}/hook-pre-${hookoption}.sh"
203+
fi
200204
# Run command.
201-
eval "${currentopt[index + 1]}"
205+
eval "${currentopt[index + 2]}"
202206
# Exit should occur in modules. Should this not happen print an error
203207
fn_print_error2_nl "Command did not exit correctly: ${getopt}"
204208
fn_script_log_error "Command did not exit correctly: ${getopt}"

linuxgsm.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ lgsmlogdir="${logdir}/lgsm"
3232
steamcmddir="${HOME}/.steam/steamcmd"
3333
[ -n "${LGSM_SERVERFILES}" ] && serverfiles="${LGSM_SERVERFILES}" || serverfiles="${rootdir}/serverfiles"
3434
modulesdir="${lgsmdir}/modules"
35+
hooksdir="${lgsmdir}/hooks"
3536
tmpdir="${lgsmdir}/tmp"
3637
datadir="${lgsmdir}/data"
3738
lockdir="${lgsmdir}/lock"

0 commit comments

Comments
 (0)