-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-manager.sh
More file actions
executable file
·131 lines (114 loc) · 3.51 KB
/
Copy pathserver-manager.sh
File metadata and controls
executable file
·131 lines (114 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash
#
# Ubuntu Server Manager - Entry Point with Auto-Update
# Checks for updates via git pull before launching the server manager.
#
# To disable auto-updates, create a file named DISABLE_AUTO_UPDATE
# in the same directory as this script:
# touch /path/to/server-manager/DISABLE_AUTO_UPDATE
#
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPT_NAME="$(basename "${BASH_SOURCE[0]}")"
CORE_SCRIPT="${SCRIPT_DIR}/server-manager-core.sh"
DISABLE_FILE="${SCRIPT_DIR}/DISABLE_AUTO_UPDATE"
# Toggle auto-update on/off
switch_auto_update() {
if [[ -f "$DISABLE_FILE" ]]; then
rm -f "$DISABLE_FILE"
echo "Auto-update has been enabled."
else
touch "$DISABLE_FILE"
echo "Auto-update has been disabled."
fi
exit 0
}
# Update only (do not launch the manager)
update_only() {
if ! command -v git &>/dev/null; then
echo "Error: git is not installed."
exit 1
fi
if ! git -C "$SCRIPT_DIR" rev-parse --is-inside-work-tree &>/dev/null; then
echo "Error: not a git repository."
exit 1
fi
echo "Updating server manager..."
if git -C "$SCRIPT_DIR" pull --ff-only; then
echo "Update complete."
else
echo "Update failed."
exit 1
fi
exit 0
}
# Show help
show_help() {
echo "Ubuntu Server Manager"
echo ""
echo "Usage: server-manager [OPTION]"
echo ""
echo "Options:"
echo " --help Show this help message"
echo " --update Update to the latest version without launching"
echo " --switch-auto-update Toggle auto-update on startup on/off"
echo ""
echo "Without options, the server manager launches normally"
echo "(with auto-update check if enabled)."
exit 0
}
# Handle command-line arguments
case "${1:-}" in
--help|-h)
show_help
;;
--switch-auto-update)
switch_auto_update
;;
--update)
update_only
;;
esac
# Auto-update check
auto_update() {
# Check if git is installed
if ! command -v git &>/dev/null; then
return 0
fi
# Check if we are inside a git repository
if ! git -C "$SCRIPT_DIR" rev-parse --is-inside-work-tree &>/dev/null; then
return 0
fi
# Check if the branch is master or main
local branch
branch=$(git -C "$SCRIPT_DIR" rev-parse --abbrev-ref HEAD 2>/dev/null) || return 0
if [[ "$branch" != "master" && "$branch" != "main" ]]; then
return 0
fi
# Check if auto-update is disabled
if [[ -f "${SCRIPT_DIR}/DISABLE_AUTO_UPDATE" ]]; then
return 0
fi
# Record checksum of this script before pulling
local checksum_before
checksum_before=$(md5sum "${BASH_SOURCE[0]}" 2>/dev/null | cut -d' ' -f1) || return 0
# Attempt git pull (do not fail if network is unavailable)
echo "Checking for updates..."
if git -C "$SCRIPT_DIR" pull --ff-only 2>/dev/null; then
# Check if the entry script itself was updated
local checksum_after
checksum_after=$(md5sum "${BASH_SOURCE[0]}" 2>/dev/null | cut -d' ' -f1)
if [[ "$checksum_before" != "$checksum_after" ]]; then
echo ""
echo "The updater script itself has been updated."
echo "Please restart the server manager to use the new version:"
echo " sudo $0"
exit 0
fi
else
echo "Could not check for updates (network unavailable?). Continuing..."
fi
}
auto_update
# Launch the server manager
exec "$CORE_SCRIPT" "$@"