forked from 3RaGaming/Web_Control
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
315 lines (298 loc) · 10.7 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
#!/bin/bash
if [ "$EUID" -ne 0 ]
then printf "Please run as root\n"
exit
fi
install_dir="/usr/share/factorio"
#install_dir="/root/test"
#compressed file extraction function. 0.14 is in tar.gz, and .15 is in tar.xz, for some reason.
function extract () {
if [ -f $1 ] ; then
case $1 in
*.tar.gz) tar --strip-components=1 -xzf $1 -C $2/$3; printf "Done!\n";;
*.tar.xz) tar --strip-components=1 -xf $1 -C $2/$3; printf "Done!\n";;
*) printf "Unknown compressiong type can't extract from $1\n"; break;;
esac
else
printf "Unable to access file $1... We'll deal with that later...\n"
fi
}
#version checker. Will need this in the case node is already installed
function version_gt() {
test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1";
}
printf "Welcome to 3Ra Gaming's Factorio Web Control Installer!\n\n"
#printf "This tool will automatically check that all required dependancies are installed\n"
#printf "If any are not yet installed, it will attempt to install them.\n\n"
printf "This script should verify all dependancies and will attempt to install them. You will be asked before each dependency is installed.\n"
while true; do
read -p "Are you currently running Ubuntu 16.04 or higher? " yn
case $yn in
[Yy]* ) break;;
[Nn]* )
printf "\nUnfortunately, this installer was built for Ubuntu :(\n\n";
printf "Please consult the github for manual instructions\n";
printf "http://www.3ragaming.com/github\n";
printf "You may also join our Discord and we will do our best to assist you\n";
printf "http://www.3ragaming.com/discord\n\n";
exit;;
* ) echo "Please answer yes[Y] or no[N].";;
esac
done
#Define dependencies
#depend_arr+=("");
depend_arr=();
depend_arr+=("curl");
depend_arr+=("zip");
depend_arr+=("unzip");
depend_arr+=("tar");
depend_arr+=("gcc");
depend_arr+=("crontab");
depend_arr+=("npm");
depend_arr+=("xz-utils");
depend_arr+=("apache2");
depend_arr+=("php");
depend_arr+=("php-curl");
depend_arr+=("libapache2-mod-php");
depend_needed=;
for depend_item in "${depend_arr[@]}"; do
if ! type $depend_item &> /dev/null2>&1; then
depend_needed="$depend_needed $depend_item";
fi
done
#Install dependencies
echo "will verify install of:$depend_needed";
while true; do
read -p "Are you ok with installing these now? (you must to continue with install) " yn
case $yn in
[Yy]* )
break;;
[Nn]* )
printf "We cannot proceed without these installed.";
exit;;
* ) echo "Please answer yes[Y] or no[N].\n";;
esac
done
apt install --force-yes --yes $depend_needed
printf "\n\nBase Dependencies Installed!\n";
function install_node () {
url="https://deb.nodesource.com/setup_6.x";
curl -sL $url | sudo -E bash -
apt install --force-yes --yes nodejs
}
#check/install node version
printf "Checking if Node JS is installed\n";
if ! type node &> /dev/null2>&1; then
while true; do
read -p "Node JS is not installed. Install now?" yn
case $yn in
[Yy]* ) break;;
[Nn]* )
printf "\nUnfortunately, Node JS is required for the web control to function.\n\n";
exit;;
* ) echo "Please answer yes[Y] or no[N].";;
esac
done
install_node;
else
version=`node -v`;
supported_node="6.9.5";
if version_gt $supported_node $version; then
printf "Only node $supported_node and above is supported.\nYou currently have $version installed\n";
while true; do
read -p "Attempt to update now?" yn
case $yn in
[Yy]* ) break;;
[Nn]* )
printf "\nPlease manually update your node JS then attempt install again.\n\n";
exit;;
* ) echo "Please answer yes[Y] or no[N].";;
esac
done
install_node;
fi
fi
if ! type node &> /dev/null2>&1; then
printf "for some reason, Node JS was unable to install. Please manually insatll node js version 6.9.5 or greater, ensure that it is installed with \`which node\`, and run this install script again";
exit;
fi
version=`node -v`;
printf "Node JS $version is installed\n\n";
#Factorio Install
if [ ! -d "$install_dir/" ]; then
printf "Factorio is not installed.\nAttempting to identify latest stable version...\n";
latest_version=`curl -v --silent https://updater.factorio.com/get-available-versions 2>&1 | grep stable | awk '{ print $2 }' | tr -d '"'`;
if [ "${latest_version}" ]; then
printf "Latest stable Factorio version is $latest_version. ";
while true; do
read -p "Download the latest version? " yn
case $yn in
[Yy]* )
mkdir $install_dir
download=`curl -JLO# https://www.factorio.com/get-download/$latest_version/headless/linux64`
download=`echo $download | awk '{ print $5 }' | tr -d "'"`
if [ "${download}" ]; then
printf "Downloaded to $download\n"
printf "extracting to $install_dir/$latest_version/ ... "
mkdir $install_dir/$latest_version
extract $download $install_dir $latest_version
chown -R www-data:www-data $install_dir/
else
printf "Unable to download latest version. Don't worry. We can install this later\n"
fi
break;;
[Nn]* ) printf "That's alright. We can download it later.\n"; break;;
* ) echo "Please answer yes[Y] or no[N].";;
esac
done
else
printf "Unable to download latest version. Don't worry. We can install this later\n"
fi
fi
printf "Downloading latest version of Web Control\n";
wget -q https://github.com/3RaGaming/Web_Control/archive/master.zip -O /tmp/master.zip
printf "Unzipping\n";
unzip -u /tmp/master.zip -d /tmp/
printf "Creating directories\n";
mkdir -p /var/www/
printf "Installing Web Control\n";
cp -R /tmp/Web_Control-master/* /var/www/
printf "Adjusting permissions\n";
chown -R www-data:www-data /var/www/
chmod +x /var/www/factorio/manage.sh
chmod +x /var/www/html/update.sh
printf "We need to install a cronjob for managing deleting old file logs and checking file permissions periodically.\n";
printf "We will save your current cronjob file as \"cronjob_old.txt\" in case you need to add anything custom back to it\n";
printf "Press Enter when ready.\r";
read
printf "Activating cron job for permissions\n";
crontab -l > cronjob_old.txt
crontab /var/www/cronjob.txt
printf "Compiling managepgm\n";
cd /var/www/factorio/
printf "\nPreparing to compile the manager, and install discord js.\n";
printf "Some warning messages about discord will appear. These are normal, you may ignore them.\n";
printf "\nPress Enter to continue.\n";
read
gcc -o managepgm -pthread manage.c
printf "Installing Discord.js\n"
npm install discord.js --save
printf "Cleaning temporary files\n"
rm -Rf /tmp/master.zip /tmp/Web_Control-master/
printf "Enabling SSL and restarting web server\n";
a2enmod ssl
a2ensite default-ssl
printf "Checking upload limits\n";
php_ini=`php --ini | grep Loaded | awk '{ print $4 }'`
if [ -f "$php_ini" ]
then
echo "$php_ini found."
php_ini_post_max_size_raw=`grep post_max_size $php_ini`
php_ini_post_max_size=`grep post_max_size $php_ini | awk '{ print $3 }' | tr -dc '0-9'`
php_ini_upload_max_filesize_raw=`grep upload_max_filesize $php_ini`
php_ini_upload_max_filesize=`grep upload_max_filesize $php_ini | awk '{ print $3 }' | tr -dc '0-9'`
if [ "$php_ini_post_max_size" -lt "156" ]; then
#change it to
php_ini_post_max_size_raw=`grep post_max_size $php_ini`
php_ini_most_max_size_new="post_max_size = 156M";
sed -i -e "s/$php_ini_post_max_size_raw/$php_ini_most_max_size_new/g" "$php_ini"
printf "Updated post_max_size to 156M\n";
else
printf "$php_ini_post_max_size_raw, this will do\n";
fi
if [ "$php_ini_upload_max_filesize" -lt "150" ]; then
#change it
php_ini_upload_max_filesize_raw=`grep upload_max_filesize $php_ini`
php_ini_upload_max_filesize_new="upload_max_filesize = 150M";
sed -i -e "s/$php_ini_upload_max_filesize_raw/$php_ini_upload_max_filesize_new/g" "$php_ini"
printf "Updated upload_max_filesize to 150M\n";
else
printf "$php_ini_upload_max_filesize_raw, this will do\n";
fi
else
printf "Unable to location php_ini file.\nYou will be required to change the 'post_max_size' and 'upload_max_filesize' in your php.ini file.";
fi
systemctl restart apache2
#request to remove index.html
file="/var/www/html/index.html";
if [ -f "$file" ]; then
printf "Ubuntu likes to install a default web file at $file\n";
printf "This file is un-needed and will make using the web control difficult.";
while true; do
read -p "Should we remove this file now? " yn
case $yn in
[Yy]* )
rm -f $file
printf "File $file removed.\n";
break;;
[Nn]* )
printf "If you have made this file yourself, please rename it (anything but index) so the web control may function properly.\n";
printf "Press Enter to continue...";
read
break;;
* ) echo "Please answer yes[Y] or no[N].";;
esac
done
fi
printf "Installation complete!\n\n";
printf "We will need to setup a user for you to login without discord authentication for now.\n";
function set_username(){
printf "Please enter a username to create:\n"
read username
if [[ -n "$username" ]]; then
a=$(echo $username | tr -d "\n" | wc -c)
b=$(echo $username | tr -cd "[:alnum:]" | wc -c)
if [[ $a != $b ]]; then
printf "Only letters and numbers are supported for usernames.\n"
set_username
else
g_username=$(echo $username | tr -cd "[:alnum:]")
fi
else
printf "Username cannot be left empty.\n"
set_username
fi
}
function set_password(){
printf "Enter a password:\n"
read -s password_1
if [[ -n "$password_1" ]]; then
a=$(echo -n $password_1 | md5sum | awk '{ print $1 }')
printf "Re-enter password:\n"
read -s password_2
if [[ -n "$password_2" ]]; then
b=$(echo -n $password_2 | md5sum | awk '{ print $1 }')
if [[ $a != $b ]]; then
printf "Passwords do not match.\n"
set_password
else
g_password=$b
fi
else
printf "Passwords do not match.\n"
set_password
fi
else
printf "Password cannot be left empty.\n"
set_password
fi
}
while true; do
read -p "Would you like to setup this user now? (This will remove all other users from the users.txt file) " yn
case $yn in
[Yy]* )
set_username
set_password
printf "Will create user \"$g_username\" with password $g_password\n";
echo "$g_username|$g_password|admin" > /var/www/users.txt;
break;;
[Nn]* ) printf "If you find you cannot login using /altlogin.php, edit the /var/www/users.txt file.\n"; break;;
* ) echo "Please answer yes[Y] or no[N].";;
esac
done
printf "Additional users may be added using additional lines in /var/www/users.txt. Passwords are MD5 encrypted\n";
printf "Access your site with https://IP_ADDRESS/altlogin.php\n";
printf "Eventually we will have a splash page for first time logins to assit the rest of the web control setup.\n";
printf "Until then, the rest of the configuration must be done manually in /var/www/factorio/config.json\n";
printf "Press Enter to exit.\n";
read