-
Notifications
You must be signed in to change notification settings - Fork 17
/
install.sh
executable file
·207 lines (168 loc) · 4.62 KB
/
install.sh
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/bin/sh
# (Un)Install script for ServerBoxMonitor
# Check root
if [ "$(id -u)" -ne 0 ]; then
echo "Please run as root or use sudo"
exit 1
fi
download() {
# Check arch: amd64 or arm64
arch=$(uname -m)
case $arch in
x86_64)
arch=amd64
;;
aarch64)
arch=arm64
;;
*)
echo "Not support arch: $arch"
exit 1
;;
esac
# Check curl
if ! command -v curl >/dev/null 2>&1; then
echo "Please install curl"
exit 1
fi
# Generate download url
newestTag=$(curl -s https://api.github.com/repos/lollipopkit/server_box_monitor/releases/latest | grep tag_name | cut -d '"' -f 4)
# Remove 'v' at the start -> "0.1.0"
newestTagLen=$(expr length "$newestTag")
APPVER=$(expr substr "$newestTag" 2 "$newestTagLen")
DOWNLOAD_URL="https://github.com/lollipopkit/server_box_monitor/releases/download/v${APPVER}/server_box_monitor_${APPVER}_linux_$arch.tar.gz"
# Download binary
echo "Download $DOWNLOAD_URL"
curl -sL "$DOWNLOAD_URL" -o /tmp/server_box_monitor.tar.gz
if [ ! -f /tmp/server_box_monitor.tar.gz ]; then
echo "Download binary failed"
exit 1
fi
}
cleanup() {
# Clean up
echo "Cleaning up..."
rm -f /tmp/server_box_monitor.tar.gz
rm -rf /tmp/server_box_monitor
}
install_binary() {
# Extract binary
echo "Extracting binary..."
tar -xf /tmp/server_box_monitor.tar.gz -C /tmp
if [ ! -f /tmp/server_box_monitor ]; then
echo "Extract binary failed"
exit 1
fi
# Install binary
echo "Installing binary..."
mv /tmp/server_box_monitor /usr/local/bin/server_box_monitor
if [ $? -ne 0 ]; then
echo "Install binary failed"
exit 1
fi
cleanup
}
install() {
# If already installed, skip
if [ -f /usr/local/bin/server_box_monitor ] && [ -f /etc/systemd/system/server_box_monitor.service ]; then
echo "Already installed, use 'upgrade' or 'uninstall'."
exit 0
fi
download
install_binary
# Check systemd
if [ ! -d /etc/systemd ]; then
echo "Distribution without systemd is not supported yet."
exit 1
fi
# Install systemd service
echo "Installing systemd service..."
cat <<EOF > /etc/systemd/system/server_box_monitor.service
[Unit]
Description=Server Box Monitor
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/server_box_monitor serve
User=root
Restart=always
[Install]
WantedBy=multi-user.target
EOF
# Enable systemd service
echo "Enabling systemd service..."
systemctl enable server_box_monitor.service
if [ $? -ne 0 ]; then
echo "Enable systemd service failed"
exit 1
fi
# Start systemd service
echo "Starting systemd service..."
systemctl start server_box_monitor.service
if [ $? -ne 0 ]; then
echo "Start systemd service failed"
exit 1
fi
# Display systemd service status
echo "Displaying systemd service status..."
systemctl status server_box_monitor.service
echo "Install success"
}
uninstall() {
# Stop systemd service
echo "Stopping systemd service..."
systemctl stop server_box_monitor.service
if [ $? -ne 0 ]; then
echo "Stop systemd service failed"
exit 1
fi
# Disable systemd service
echo "Disabling systemd service..."
systemctl disable server_box_monitor.service
if [ $? -ne 0 ]; then
echo "Disable systemd service failed"
exit 1
fi
# Remove systemd service
echo "Removing systemd service..."
rm -f /etc/systemd/system/server_box_monitor.service
# Remove binary
echo "Removing binary..."
rm -f /usr/local/bin/server_box_monitor
echo "Uninstall success"
}
upgrade() {
# Check if installed binary and service
if [ ! -f /usr/local/bin/server_box_monitor ] || [ ! -f /etc/systemd/system/server_box_monitor.service ]; then
echo "Not installed. It will be installed"
read -p "Press enter to continue"
install
exit 0
fi
rm -f /usr/local/bin/server_box_monitor
download
install_binary
# Restart systemd service
echo "Restarting systemd service..."
systemctl restart server_box_monitor.service
if [ $? -ne 0 ]; then
echo "Restart systemd service failed"
exit 1
fi
echo "Upgrade success"
}
case $1 in
install)
install
;;
uninstall)
uninstall
;;
upgrade)
upgrade
;;
*)
echo "Usage: $0 [install|uninstall|upgrade]"
exit 1
;;
esac