forked from OpenGamePanel/OGP-Agent-Linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
347 lines (295 loc) · 10.6 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
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
#!/bin/bash
#
# OGP - Open Game Panel
# Copyright (C) Copyright (C) 2008 - 2013 The OGP Development Team
#
# http://www.opengamepanel.org/
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Parameters can be passed into the install.sh script to automate OGP updates
# $1 = Operation Type (Used as opType)
# $2 = OGP User (Used as ogpAgentUser)
# $3 = OGP User Sudo Pass (Used as ogpUserPass)
# $4 = Install Path (Used as ogpInsPath)
####################
# FUNCTIONs #
####################
detectSystemD(){
# Ops require sudo
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
}
copySystemDInit(){
AGENTDIR=${agent_home}
sudoPass=${sudo_password}
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. Updating OGP agent to use systemd service init script."
if [ -e "/etc/init.d/ogp_agent" ] && [ ! -e "${AGENTDIR}/ogp_agent_init" ]; then
echo -e "Taking care of existing OGP files."
echo "$sudoPass" | sudo -S -p "" service ogp_agent stop
# 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"
fi
if [ ! -e "$SystemDDir/ogp_agent.service" ]; then
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"
fi
fi
fi
}
#####################
# CODE ##########
#####################
# Parameter notifications
if [ ! -z "$1" ]; then
echo -n "Received operation type of $1 as a parameter."
opType="$1"
fi
if [ ! -z "$2" ]; then
echo -n "Received OGP user of $2 as a parameter."
ogpAgentUser="$2"
fi
if [ ! -z "$3" ]; then
echo -n "Received OGP sudo password of $3 as a parameter."
ogpUserPass="$3"
fi
if [ ! -z "$4" ]; then
echo -n "Received OGP agent path of $4 as a parameter."
ogpInsPath="$4"
fi
failed()
{
echo "ERROR: ${1}"
exit 1
}
if [ "X`which screen &> /dev/null;echo $?`" != "X0" ]; then
failed "You need to install software called 'screen', before you can install OGP agent.";
fi
if [ "X`which sed &> /dev/null;echo $?`" != "X0" ]; then
failed "You need to install software called 'sed', before you can install OGP agent.";
fi
echo
clear
echo "#######################################################################"
echo "# OGP Agent installation and configuration"
echo "# This program will:"
echo "# Create ${DEFAULT_AGENT_HOME} or user defined directory"
echo "# Copy ogp_agent files to ${DEFAULT_AGENT_HOME} or user defined dir"
echo "# Copy the ogp_agent init script to /etc/init.d or user defined dir"
echo "# Create an initial configuration file"
echo "# Thank you for using OGP. http://www.opengamepanel.org/"
echo "#######################################################################"
echo
if [ "X`which rsync &> /dev/null;echo $?`" != "X0" ]; then
echo "*** WARNING **** missing rsync client. It is not required, but needed to use the rsync game installer";
fi
if [ "X`whoami`" != "Xroot" ]
then
echo
echo "Detected non-root install..."
username=`whoami`
echo -n "Enter sudo password: ";
read sudo_password;
else
echo "Next you need to type the username of the user that owns the agent homes.";
echo "This user must own (have access to) all the game home directories that you"
echo "want to run with this agent and must to be in sudoers list so it can perform"
echo "administrative tasks.";echo
while [ 1 ]
do
if [ ! -z "$ogpAgentUser" ] ; then
username="$ogpAgentUser"
else
echo -n "Enter user name: ";
read username;
fi
if [ -z "$ogpUserPass" ] ; then
echo -n "Enter user password: ";
read sudo_password;
else
sudo_password="$ogpUserPass"
fi
if [ -z "${username}" ]
then
echo "Username can not be empty.";echo
continue;
fi
if [ "Xroot" == "X${username}" ]
then
echo "'${username}' can not be used as user for agent.";echo
continue;
fi
ID_OF_USER=`id -u ${username} 2> /dev/null`
if [ $? != 0 ]
then
echo "User with entered username (${username}) does not exist.";echo
continue;
fi
break;
done
fi
detectSystemD
readonly AGENT_USER_HOME="`cat /etc/passwd | grep "^${username}:" | cut -d':' -f6`/OGP/"
echo
echo "Next the directory for the agent needs to be chosen. The default directory";
echo "Should be fine in most of the cases."
echo
if [ -z "$ogpInsPath" ]; then
echo "Where do you want to install the agent?"
echo -n "[Default is ${AGENT_USER_HOME}]: "
read agent_home
else
agent_home="$ogpInsPath"
fi
if [ -z "${agent_home}" ]
then
agent_home=$AGENT_USER_HOME
fi
# Try to prevent users from doing damage to their systems.
case ${agent_home} in
/bin*|/boot*|/dev*|/etc*|/lib*|/proc*|/root*|/sbin*|/sys*|/)
failed "The agent home can not be ${agent_home}";
;;
esac
echo "Agent install dir is ${agent_home}"
echo
agent_home=${agent_home%/}
if [ ! -e ${agent_home} ]
then
mkdir -p ${agent_home} || failed "Failed to create the directory (${agent_home}) for agent."
elif [ ! -w ${agent_home} ]
then
failed "You do not have write permissions to the directory you assigned as agent home (${agent_home})."
fi
if [ "X`whoami`" == "Xroot" ];
then
readonly DEFAULT_INIT_DIR="/etc/init.d/"
else
readonly DEFAULT_INIT_DIR="${agent_home}/"
fi
if [ -z "$systemdPresent" ]; then
if [ "X`uname`" != "XLinux" ]
then
echo
echo "Detected non-Linux platform..."
echo "Where do you want to put the init scripts?"
echo -n "[Default ${DEFAULT_INIT_DIR}]: "
read init_dir
fi
if [ -z "$opType" ]; then
echo "Where do you want to put the init scripts?"
echo -n "[Default ${DEFAULT_INIT_DIR}]:"
read init_dir
fi
else
init_dir=${agent_home}
fi
if [ -z "${init_dir}" ]
then
init_dir=${DEFAULT_INIT_DIR}
fi
init_dir=${init_dir%/}
echo "Copying files..."
cp -avf systemd Crypt EHCP FastDownload File Frontier IspConfig KKrcon php-query Schedule Time ogp_agent.pl ogp_screenrc ogp_agent_run agent_conf.sh extPatterns.txt ${agent_home}/ || failed "Failed to copy agent files to ${agent_home}."
# Create the directory for configs.
mkdir -p ${agent_home}/Cfg || failed "Failed to create ${agent_home}/Cfg dir."
echo
if [ -e /etc/gentoo-release ]
then
echo "Copying ogp_agent.init.gentoo to $init_dir - Gentoo Specific Init"
init_file_template='includes/ogp_agent.init.gentoo'
elif [ -e /etc/sysconfig ] && [ ! -e /etc/debian_version ]
then
echo "Copying ogp_agent.init.rh to $init_dir - Redhat Style Init (also SuSE, and Mandrake)"
init_file_template='includes/ogp_agent.init.rh'
elif [ -e /etc/debian_version ]
then
echo "Copying ogp_agent.init.dbn to $init_dir - Debian Style Init"
init_file_template='includes/ogp_agent.init.dbn'
else
echo "Copying the generic init script because I don't know what kind of Linux distro this is"
init_file_template='includes/ogp_agent.init'
fi
init_file=${init_dir}/ogp_agent
cp -f $init_file_template $init_file || failed "Failed to create init file ($init_file)."
# Next we replace the OGP_AGENT_DIR with the actual dir in init file.
sed -i "s|OGP_AGENT_DIR|${agent_home}|" ${init_file} || failed "Failed to modify init file ($init_file)."
sed -i "s|OGP_USER|${username}|" ${init_file} || failed "Failed to modify init file ($init_file)."
chmod a+x $init_file
if [ "$init_dir" == "$agent_home" ] && [ ! -z "$systemdPresent" ]; then
init_file=${init_dir}/ogp_agent_init
mv ${init_dir}/ogp_agent ${init_dir}/ogp_agent_init
copySystemDInit
fi
echo;
echo "Changing files owner to user ${username}...";
# Group of the files in agent_home can differ from the user so
# lets leave them as they are. So no chown user:group here.
chown --preserve-root -R ${username} ${agent_home} || failed "Failed to chmod the agent_home ${agent_home} for user ${username}."
echo "Setting Permissions on files in ${agent_home}..."
if [ -e "${init_dir}/ogp_agent" ]; then
chmod 750 ${init_dir}/ogp_agent || failed "Failed to chmod ${init_dir}/ogp_agent to 750."
fi
if [ -e "${init_dir}/ogp_agent_init" ]; then
chmod 750 ${init_dir}/ogp_agent_init || failed "Failed to chmod ${init_dir}/ogp_agent_init to 750."
fi
chmod 750 ${agent_home}/ogp_agent.pl || failed "Failed to chmod ${agent_home}/ogp_agent.pl to 750."
chmod 750 ${agent_home}/ogp_agent_run || failed "Failed to chmod ${agent_home}/ogp_agent_run to 750."
echo "Install Successful!"
echo "Now configuring..."
echo ""
# Run the configuration script
chmod +x ${agent_home}/agent_conf.sh
if [ -z "$opType" ]; then
bash ${agent_home}/agent_conf.sh -s $sudo_password -u $username
fi
echo "Attempting to start the Open Game Panel (OGP) agent..."
systemctl daemon-reload
chkconfig ogp_agent on
rc-update add ogp_agent default
update-rc.d ogp_agent defaults
systemctl enable ogp_agent.service
service ogp_agent restart
echo;
echo "OGP installation complete!"
echo
exit 0