/
ogp_agent_run
411 lines (371 loc) · 12.1 KB
/
ogp_agent_run
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#!/bin/bash
#
#
# A wrapper script for the OGP agent perl script.
# Performs auto-restarting of the agent on crash. You can
# extend this to log crashes and more.
#
# The ogp_agent_run script should be at the top level of the agent tree
# Make sure we are in that directory since the script assumes this is the case
#####################
# Important VARS #
#####################
AGENTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BASH_PREFS_CONF="$AGENTDIR/Cfg/bash_prefs.cfg"
REPONAME=OGP-Agent-Linux
GitHubUsername="OpenGamePanel"
#####################
# FUNCTIONS #
#####################
setupOGPDirPerms(){
chmod -Rf ug+rw $AGENTDIR 2>/dev/null
if [ -d "$AGENTDIR/steamcmd" ]; then
chmod ug+x $AGENTDIR/steamcmd/linux32/* 2>/dev/null
chmod ug+x $AGENTDIR/steamcmd/*.sh 2>/dev/null
fi
if [ -d "$AGENTDIR/screenlogs" ]; then
chmod -Rf ug=rwx $AGENTDIR/screenlogs
fi
chmod ug+x $AGENTDIR/ogp_agent.pl 2>/dev/null
chmod ug+x $AGENTDIR/ogp_agent_run 2>/dev/null
chmod ug+x $AGENTDIR/agent_conf.sh 2>/dev/null
if test `id -u` -eq 0; then
echo
echo
echo "************** WARNING ***************"
echo "Running OGP's agent as root "
echo "is highly discouraged. It is generally"
echo "unnecessary to use root privileges to "
echo "execute the agent. "
echo "**************************************"
echo
echo
timeout=10
while test $timeout -gt 0; do
echo -n "The agent will continue to launch in $timeout seconds\r"
timeout=`expr $timeout - 1`
sleep 1
done
fi
}
ogpGitCleanup(){
echo "Cleaning up..."
rm -Rf ${REPONAME}-* &> /dev/null
if [ -e "ogp_agent_latest.zip" ]; then
rm -f "ogp_agent_latest.zip"
fi
}
getSudoPassword(){
sudoPass=$(cat "$AGENTDIR/Cfg/Config.pm" | grep -o "sudo_password.*" | grep -ow "[^sudo_password( \)*=>( \)*].*" | grep -o "[^'].*[^',]")
}
detectSystemD(){
replaceSystemDService=false
# Ops require sudo
if [ ! -z "$sudoPass" ]; then
initProcessStr=$(ps -p 1 | awk '{print $4}' | tail -n 1)
if [ "$initProcessStr" == "systemd" ]; then
systemdPresent=1
if [ -e "/lib/systemd/system" ]; then
SystemDDir="/lib/systemd/system"
elif [ -e "/etc/systemd/system" ]; then
SystemDDir="/etc/systemd/system"
else
checkDir=$(ps -eaf|grep '[s]ystemd' | head -n 1 | awk '{print $8}' | grep -o ".*systemd/")
if [ -e "${checkDir}system" ]; then
SystemDDir="$checkDir"
else
# Can't find systemd dir
systemdPresent=
SystemDDir=
fi
fi
fi
if [ -e "${AGENTDIR}/systemd/ogp_agent.service" ]; then
if [ ! -z "$systemdPresent" ] && [ ! -z "$SystemDDir" ]; then
echo -e "systemd detected as the init system with a directory of $SystemDDir."
if [ -e "/etc/init.d/ogp_agent" ] && [ ! -e "${AGENTDIR}/ogp_agent_init" ]; then
echo -e "Taking care of existing OGP files."
# Kill any remaining ogp agent process
ogpPID=$(ps -ef | grep -v grep | grep ogp_agent.pl | head -n 1 | awk '{print $3}')
if [ ! -z "$ogpPID" ]; then
echo "$sudoPass" | sudo -S -p "" kill -9 "$ogpPID"
fi
echo "$sudoPass" | sudo -S -p "" cp "/etc/init.d/ogp_agent" "${AGENTDIR}/ogp_agent_init"
echo "$sudoPass" | sudo -S -p "" chmod +x "${AGENTDIR}/ogp_agent_init"
echo "$sudoPass" | sudo -S -p "" update-rc.d ogp_agent disable
echo "$sudoPass" | sudo -S -p "" chkconfig ogp_agent off
echo "$sudoPass" | sudo -S -p "" rm -rf "/etc/init.d/ogp_agent"
replaceSystemDService=true
fi
# Update service to use oneshot and not forking
if [ -e "$SystemDDir/ogp_agent.service" ]; then
# Check to see if it's using oneshot
usingOneShot=$(echo "$sudoPass" | sudo -S -p "" cat "$SystemDDir/ogp_agent.service" | grep -o "oneshot")
if [ -z "$usingOneShot" ]; then
replaceSystemDService=true
fi
fi
if [ ! -e "$SystemDDir/ogp_agent.service" ] || [ "$replaceSystemDService" = true ]; then
echo -e "Updating OGP agent systemd service init script."
echo -e "Copying ogp_agent systemd service file to $SystemDDir"
echo "$sudoPass" | sudo -S -p "" cp "${AGENTDIR}/systemd/ogp_agent.service" "$SystemDDir"
echo "$sudoPass" | sudo -S -p "" sed -i "s#{OGP_AGENT_PATH}#$AGENTDIR#g" "${SystemDDir}/ogp_agent.service"
echo "$sudoPass" | sudo -S -p "" systemctl daemon-reload
echo "$sudoPass" | sudo -S -p "" systemctl enable ogp_agent.service
echo "$sudoPass" | sudo -S -p "" service ogp_agent restart
exit 0
fi
fi
fi
fi
}
init() {
RESTART="yes"
AGENT="$AGENTDIR/ogp_agent.pl"
TIMEOUT=10 # time to wait after a crash (in seconds)
PID_FILE=""
# Should we perform an automatic update?
if [ -e $BASH_PREFS_CONF ]
then
source "$BASH_PREFS_CONF"
if [ "$agent_auto_update" -eq "1" ]
then
AUTO_UPDATE="yes"
fi
# Use custom github update address
if [ ! -z "$github_update_username" ]; then
REVISIONTest=`curl -s https://github.com/${github_update_username}/${REPONAME}/commits/master.atom | egrep -o "([a-f0-9]{40})" | awk 'NR==1{print $1}'`
if [ ! -z "$REVISIONTest" ]; then
GitHubUsername=${github_update_username}
fi
fi
else
AUTO_UPDATE="yes"
fi
while test $# -gt 0; do
case "$1" in
"-pidfile")
PID_FILE="$2"
PID_FILE_SET=1
echo $$ > $PID_FILE
shift ;;
esac
shift
done
if test ! -f "$AGENT"; then
echo "ERROR: '$AGENT' not found, exiting"
quit 1
elif test ! -x "$AGENT"; then
# Could try chmod but dont know what we will be
# chmoding so just fail.
echo "ERROR: '$AGENT' not executable, exiting"
quit 1
fi
CMD="perl $AGENT"
}
syntax () {
# Prints script syntax
echo "Syntax:"
echo "$0"
}
checkDepends() {
CURL=`which curl 2>/dev/null`
if test "$?" -gt 0; then
echo "WARNING: Failed to locate curl binary."
else
echo "INFO: Located curl: $CURL"
fi
UNZIP=`which unzip 2>/dev/null`
if test "$?" -gt 0; then
echo "WARNING: Failed to locate unzip binary."
else
echo "INFO: Located unzip: $UNZIP"
fi
}
update() {
# Check to see if limited ogpuser exists
generateOGPLimitedUser
# Run the update
if test -n "$AUTO_UPDATE"; then
if [ -z "$CURL" -o -z "$UNZIP" ]; then
checkDepends
fi
if [ -f "$CURL" -a -x "$CURL" ] && [ -f "$UNZIP" -a -x "$UNZIP" ]; then
cd $AGENTDIR
if [ ! -d tmp ]; then
mkdir tmp
fi
cd tmp
REVISION=`curl -s https://github.com/${GitHubUsername}/${REPONAME}/commits/master.atom | egrep -o "([a-f0-9]{40})" | awk 'NR==1{print $1}'`
curl -Os https://raw.githubusercontent.com/${GitHubUsername}/${REPONAME}/${REVISION}/ogp_agent_run
currentOGPAgentRunContent=$(cat "./ogp_agent_run")
# Check to make sure ogp_agent_run downloaded successfully from GitHub before we attempt to replace it.
# This should fix random 404 people have been experiencing
if [ -s "./ogp_agent_run" ] && [ "$(echo "$currentOGPAgentRunContent" | head -n 1)" != "404: Not Found" ] && [ ! -z "$(echo "$currentOGPAgentRunContent" | grep "ogp_agent.pl")" ]; then
diff ./ogp_agent_run $AGENTDIR/ogp_agent_run &>/dev/null
if test $? -ne 0; then
cp -f ./ogp_agent_run $AGENTDIR/ogp_agent_run &> /dev/null
if test $? -eq 0; then
cd $AGENTDIR
chmod ug+x ogp_agent_run 2>/dev/null
echo "`date`: The agent updater has been changed, relaunching..."
rm -Rf tmp
./ogp_agent_run
exit 0
fi
fi
fi
cd $AGENTDIR
CURRENT=$(cat $AGENTDIR/Cfg/Config.pm | grep version | grep -Eo '[0-9a-f]{40}')
if [ "$CURRENT" == "$REVISION" ]; then
echo "The agent is up to date."
else
URL=https://github.com/${GitHubUsername}/${REPONAME}/archive/${REVISION}.zip
HEAD=$(curl -L -s --head -w "%{http_code}" "$URL" -o "ogp_agent_latest.zip")
if [ "$HEAD" == "200" ]; then
echo "Updating agent using curl."
curl -L -s "$URL" -o "ogp_agent_latest.zip"
if test $? -ne 0; then
echo "`date`: curl failed to download the update package."
else
unzip -oq "ogp_agent_latest.zip"
if test $? -ne 0; then
echo "`date`: Unable to unzip the update package."
ogpGitCleanup
else
cd ${REPONAME}-${REVISION}
cp -avf systemd Frontier ArmaBE Minecraft Schedule Time FastDownload php-query ogp_agent.pl ogp_screenrc ogp_screenrc_bk ogp_agent_run agent_conf.sh $AGENTDIR &> /dev/null
if test $? -ne 0; then
echo "`date`: The agent files cannot be overwritten."
cd ..
ogpGitCleanup
echo "Agent update failed."
else
if test ! -d "$AGENTDIR/IspConfig"; then
cp -Rf IspConfig $AGENTDIR/IspConfig &> /dev/null
fi
if test ! -d "$AGENTDIR/EHCP"; then
cp -Rf EHCP $AGENTDIR/EHCP &> /dev/null
fi
if test ! -f "$AGENTDIR/Cfg/Preferences.pm"; then
cp -f Cfg/Preferences.pm $AGENTDIR/Cfg/Preferences.pm &> /dev/null
fi
echo "Fixing permissions..."
chmod ug+x $AGENTDIR/ogp_agent.pl &> /dev/null
chmod ug+x $AGENTDIR/ogp_agent_run &> /dev/null
chmod ug+x $AGENTDIR/agent_conf.sh &> /dev/null
cd ..
ogpGitCleanup
sed -i "s/version.*/version => '${REVISION}',/" $AGENTDIR/Cfg/Config.pm
echo "Agent updated successfully."
fi
fi
fi
else
echo "There is a update available (${REVISION}) but the download source is not ready.";
echo "Try again later."
fi
fi
else
echo "Update failed."
fi
fi
return 0
}
run() {
getSudoPassword
restrictAccess
update
detectSystemD
if test -n "$RESTART" ; then
echo "Agent will auto-restart if there is a crash."
#loop forever
while true
do
# Run
$CMD
echo "`date`: Agent restart in $TIMEOUT seconds"
# don't thrash the hard disk if the agent dies, wait a little
sleep $TIMEOUT
done # while true
else
$CMD
fi
}
quit() {
# Exits with the give error code, 1
# if none specified.
# exit code 2 also prints syntax
exitcode="$1"
# default to failure
if test -z "$exitcode"; then
exitcode=1
fi
case "$exitcode" in
0)
echo "`date`: OGP Agent Quit" ;;
2)
syntax ;;
*)
echo "`date`: OGP Agent Failed" ;;
esac
# Remove pid file
if test -n "$PID_FILE" && test -f "$PID_FILE" ; then
# The specified pid file
rm -f $PID_FILE
fi
# reset SIGINT and then kill ourselves properly
trap - 2
kill -2 $$
}
function generatePassword(){
if [ ! -z "$1" ]; then
PLENGTH="$1"
else
PLENGTH="10"
fi
#rPass=$(date +%s | sha256sum | base64 | head -c "$PLENGTH")
rPass=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1 | head -c "$PLENGTH")
}
function generateOGPLimitedUser(){
ogpUSER="ogp_server_runner"
echo "$sudoPass" | sudo -S -p "<prompt>" id -u "$ogpUSER"
if [ $? -eq 1 ]; then
echo "Creating ogp limited user for running servers and additional security..."
echo "$sudoPass" | sudo -S -p "<prompt>" groupdel ${ogpUSER}
echo "$sudoPass" | sudo -S -p "<prompt>" groupadd ${ogpUSER}
generatePassword "15"
ogpPass="$rPass"
echo "$sudoPass" | sudo -S -p "<prompt>" useradd --home "/home/$ogpUSER" -g ${ogpUSER} -m "$ogpUSER"
echo "$sudoPass" | sudo -S -p "<prompt>" sh -c "echo '${ogpUSER}:${ogpPass}' | chpasswd"
# Use /bin/bash shell by default per request from Omano
echo "$sudoPass" | sudo -S -p "<prompt>" usermod -s /bin/bash ${ogpUSER}
echo "$sudoPass" | sudo -S -p "<prompt>" sh -c "echo 'limited_ogp_user=${ogpUSER}
password=${ogpPass}' > /root/ogp_server_runner_info"
agentUser="$(whoami)"
echo "$sudoPass" | sudo -S -p "<prompt>" usermod -a -G "$ogpUSER" "$agentUser"
# Reload perms so we don't have to reboot system for new group to apply right now...
echo "$sudoPass" | sudo -S -p "<prompt>" exec su -l "$agentUser"
fi
# Set servers to run under their own user by default:
hasLinuxUser=$(cat "$AGENTDIR/Cfg/Preferences.pm" | grep -o "linux_user_per_game_server")
if [ -z "$hasLinuxUser" ]; then
sed -i "\$i \\\\tlinux_user_per_game_server => '1'," "$AGENTDIR/Cfg/Preferences.pm"
fi
}
function restrictAccess(){
echo "$sudoPass" | sudo -S -p "<prompt>" chmod 750 "$AGENTDIR/Cfg/Config.pm"
echo "$sudoPass" | sudo -S -p "<prompt>" chmod 750 "$AGENTDIR/Cfg/Preferences.pm"
echo "$sudoPass" | sudo -S -p "<prompt>" chmod 750 "$AGENTDIR/Cfg/bash_prefs.cfg"
}
#####################
# MAIN APP CODE #
#####################
# Setup OGP and Read Preferences
setupOGPDirPerms
# Initialise
init $*
# Run
run
# Quit normally
quit 0