forked from pufferpanel/pufferpanel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpufferpanel
executable file
·614 lines (551 loc) · 20.5 KB
/
pufferpanel
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
#!/usr/bin/env bash
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
blue=$(tput setaf 4)
normal=$(tput sgr0)
bold=$(tput bold)
mysqlHost=""
mysqlPort="3306"
mysqlDb="pufferpanel"
mysqlUser="root"
mysqlPass=""
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
function printSeparator() {
echo "------------"
}
function loadConfig() {
if [ "${mysqlHost}" == "" ]; then
configPath=${DIR}/config.json
mysqlHost=$(php -r 'echo json_decode(file_get_contents("'${configPath}'"))->mysql->host;');
mysqlDb=$(php -r 'echo json_decode(file_get_contents("'${configPath}'"))->mysql->database;');
mysqlUser=$(php -r 'echo json_decode(file_get_contents("'${configPath}'"))->mysql->username;');
mysqlPass=$(php -r 'echo json_decode(file_get_contents("'${configPath}'"))->mysql->password;');
mysqlPort=$(php -r 'echo json_decode(file_get_contents("'${configPath}'"))->mysql->port;');
fi
}
function configureMysql() {
mysqlHost="localhost"
type mysql 1>/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "MySQL is not found within your PATH, cannot proceed"
exit 1;
fi
echo "Thank you for using the PufferPanel installer!"
echo "${bold}Please do not run this installer if PufferPanel has already been installed without first running ./pufferpanel purge${normal}"
echo ""
echo "Before we can complete the installation, we need to ask you some questions"
echo "These questions will help configure PufferPanel so that you can get to using it"
printSeparator
echo "MySQL Configuration"
echo -n "Enter the MySQL host [localhost]: "
read temp
if [ "${temp}" != "" ]; then
mysqlHost=${temp}
fi
echo -n "Enter the MySQL port [3306]: "
read temp
if [ "${temp}" != "" ]; then
mysqlPort=${temp}
fi
echo -n "Enter the MySQL username (MUST HAVE GRANT) [root]: "
read temp
if [ "${temp}" != "" ]; then
mysqlUser=${temp}
fi
notValid=true
while ${notValid}; do
echo -n "Enter the MySQL account password: "
read -s temp
if [ "${temp}" != "" ]; then
mysqlPass=${temp}
fi
if mysql -h ${mysqlHost} -P ${mysqlPort} -u ${mysqlUser} --password="${mysqlPass}" -e "exit"; then
notValid=false
else
echo "Database connection could not be established"
fi
done
echo ""
echo "Creating pufferpanel account and installing database..."
mysql -h ${mysqlHost} -P ${mysqlPort} -u ${mysqlUser} --password="${mysqlPass}" < install/install.sql
mysql -h ${mysqlHost} -P ${mysqlPort} -u ${mysqlUser} --password="${mysqlPass}" -e "SET GLOBAL event_scheduler = ON;"
newUser="pufferpanel"
newPw=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9,.:\-@[]-_\-#+*~&%' | fold -w 32 | head -n 1)
newHost=$(mysql -h ${mysqlHost} -P ${mysqlPort} -D ${mysqlDb} -u ${mysqlUser} --password="${mysqlPass}" -sN -e "SELECT USER()")
newHost=${newHost/${mysqlUser}@}
mysql -h ${mysqlHost} -P ${mysqlPort} -D ${mysqlDb} -u ${mysqlUser} --password="${mysqlPass}" -e "
GRANT ALL PRIVILEGES ON pufferpanel.* TO 'pufferpanel'@'${newHost}' IDENTIFIED BY '${newPw}';
GRANT ALL PRIVILEGES ON pufferpanel.* TO 'pufferpanel'@'localhost' IDENTIFIED BY '${newPw}';
"
echo "{
\"mysql\": {
\"host\": \"${mysqlHost}\",
\"database\": \"${mysqlDb}\",
\"username\": \"${newUser}\",
\"password\": \"${newPw}\",
\"port\": \"${mysqlPort}\"
}
}" > config.json
echo "MySQL has been configured and the database was installed"
echo "Switching to new user for further commands"
mysqlHost=""
loadConfig
}
function configureSite() {
echo "Configuring site details"
siteUrl=""
while [ "${siteUrl}" == "" ]; do
echo -n "Please enter the domain or IP (if you do not have a domain) for your site (do NOT include http(s)://): "
read siteUrl
done
siteUrl=$(echo ${siteUrl} | sed 's/^http\(\|s\):\/\///g')
if [[ "${siteUrl}" == */ ]]; then
siteUrl="${siteUrl%?}"
fi
loadConfig
mysql -h ${mysqlHost} -P ${mysqlPort} -D ${mysqlDb} -u ${mysqlUser} --password="${mysqlPass}" -e "
INSERT INTO acp_settings (setting_ref, setting_val) VALUES
('master_url', 'http://${siteUrl}/') ON DUPLICATE KEY UPDATE setting_val = VALUES(setting_val)"
echo "Settings saved to database"
}
function configureUser() {
loadConfig
echo "Please enter the following information for the new admin user"
username=""
email=""
password=""
uuid=$(cat /proc/sys/kernel/random/uuid)
while [ "${username}" == "" ]; do
echo -n "Username: "
read username
done
while [ "${email}" == "" ]; do
echo -n "Email: "
read email
done
password=""
password2=""
while true; do
while [ "${password}" == "" ]; do
echo -n "Password: "
read -s password
echo ""
done
while [ "${password2}" == "" ]; do
echo -n "Confirm Password: "
read -s password2
echo ""
done
if [ "${password}" != "${password2}" ]; then
echo "Password does not match the confirm password! Please try again."
password=""
password2=""
else
break
fi
done
password=$(php -r "echo password_hash('${password}', PASSWORD_BCRYPT);");
time=$(php -r 'echo time();');
echo ""
echo "Installing user..."
mysql -h ${mysqlHost} -P ${mysqlPort} -D ${mysqlDb} -u ${mysqlUser} --password="${mysqlPass}" -e "
INSERT INTO users VALUES (NULL, '${uuid}', '${username}', '${email}', '${password}', 'en_US', ${time}, NULL, NULL, 1, 0, 1, 0, NULL) ON DUPLICATE KEY UPDATE password='${password}'"
}
function configureApache() {
if [ -d "/etc/apache2" ]; then
echo "Installing apache2 config (if possible)"
else
echo "Apache folder does not exist, will not install config"
return
fi
if [ "${siteUrl}" == "" ]; then
loadConfig
siteUrl=$(mysql -h ${mysqlHost} -P ${mysqlPort} -D ${mysqlDb} -u ${mysqlUser} --password="${mysqlPass}" -e "
SELECT setting_val FROM acp_settings WHERE setting_ref='master_url'" | head -n 3 | tail -n 1 | sed 's/^http\(\|s\):\/\///g')
if [[ "${siteUrl}" == */ ]]; then
siteUrl="${siteUrl%?}"
fi
fi
conf="<VirtualHost *:80>
ServerName ${siteUrl}
DocumentRoot ${PWD}/public
</VirtualHost>"
if [ -d "/etc/apache2/sites-enabled" ]; then
if [ -f "/etc/apache2/sites-enabled/pufferpanel.conf" ]; then
echo "An existing configuration exists in /etc/apache2/sites-enabled. Enable it with `a2ensite pufferpanel`"
return
fi
if [ -f "/etc/apache2/sites-available/pufferpanel.conf" ]; then
echo "An existing configuration exists in /etc/apache2/sites-available. Enable it with `a2ensite pufferpanel`"
return
fi
echo "${conf}" > /etc/apache2/sites-available/pufferpanel.conf
ln -s /etc/apache2/sites-available/pufferpanel.conf /etc/apache2/sites-enabled/pufferpanel.conf
else
echo "Could not determine where to install the config"
return
fi
if command -v systemctl >/dev/null; then
systemctl restart httpd
elif command -v service >/dev/null; then
service apache2 restart
fi
}
function configureHttpd() {
if [ -d "/etc/httpd" ]; then
echo "Installing Apache HTTPD config (if possible)"
else
echo "Apache HTTPD folder does not exist, will not install config"
return
fi
if [ "${siteUrl}" == "" ]; then
loadConfig
siteUrl=$(mysql -h ${mysqlHost} -P ${mysqlPort} -D ${mysqlDb} -u ${mysqlUser} --password="${mysqlPass}" -e "
SELECT setting_val FROM acp_settings WHERE setting_ref='master_url'" | head -n 3 | tail -n 1 | sed 's/^http\(\|s\):\/\///g')
if [[ "${siteUrl}" == */ ]]; then
siteUrl="${siteUrl%?}"
fi
fi
conf="<VirtualHost *:80>
ServerName ${siteUrl}
DocumentRoot ${PWD}/public
</VirtualHost>
<Directory '/srv/pufferpanel/public'>
Require all granted
AllowOverride All
</Directory>"
if [ -d "/etc/httpd/conf/extra" ]; then
echo "${conf}" > /etc/httpd/conf/extra/pufferpanel.conf
grep "Include conf/extra/pufferpanel.conf" /etc/httpd/conf/httpd.conf >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo -e "\nInclude conf/extra/pufferpanel.conf\n" >> /etc/httpd/conf/httpd.conf
fi
else
echo "Could not determine where to install the config"
return
fi
sed -i 's|#LoadModule rewrite_module modules/mod_rewrite.so|LoadModule rewrite_module modules/mod_rewrite.so|' /etc/httpd/conf/httpd.conf
systemctl restart httpd
}
function configureNginx() {
if [ -d "/etc/nginx" ]; then
echo "Installing nginx config (if possible)"
else
echo "nginx folder does not exist, will not install config"
return
fi
if [ "${siteUrl}" == "" ]; then
loadConfig
siteUrl=$(mysql -h ${mysqlHost} -P ${mysqlPort} -D ${mysqlDb} -u ${mysqlUser} --password="${mysqlPass}" -e "
SELECT setting_val FROM acp_settings WHERE setting_ref='master_url'" | head -n 3 | tail -n 1 | sed 's/^http\(\|s\):\/\///g')
if [[ "${siteUrl}" == */ ]]; then
siteUrl="${siteUrl%?}"
fi
fi
getPhpConfLocation
if [ "${phpWWWConfLocation}" = "" ]; then
echo -e "Could not determine where PHP config is, cannot install config"
return
fi
phpSocket=$(grep "^listen[| ]\?=[| ]" $phpWWWConfLocation 2>/dev/null | awk -F'[=&]' '{print $2}' | sed -e 's/ //')
if [ "${phpSocket}" = "" ]; then
echo -e "Could not determine where PHP is listening, cannot install config"
return
fi
if [[ "$(echo ${phpSocket:0:1})" == "/" ]]; then
phpSocket="unix:${phpSocket}"
fi
conf="server {
listen 80;
root ${PWD};
index index.php;
server_name ${siteUrl};
client_max_body_size 20m;
client_body_timeout 120s;
location / {
try_files /public/router.php =404;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_pass ${phpSocket};
fastcgi_index router.php;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location /assets {
try_files /app/\$uri =404;
}
}
#server {
# listen 443;
# root ${PWD};
# index index.php;
#
# server_name ${siteUrl};
#
# ssl on;
# ssl_certificate /etc/nginx/ssl/${siteUrl}.crt;
# ssl_certificate_key /etc/nginx/ssl/${siteUrl}.key;
#
# location / {
# try_files /public/router.php =404;
# fastcgi_split_path_info ^(.+?\.php)(/.*)$;
# fastcgi_pass ${phpSocket};
# fastcgi_index router.php;
# fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
# include /etc/nginx/fastcgi_params;
# }
#
# location /assets {
# try_files /app/\$uri =404;
# }
#}
"
if [ -d "/etc/nginx/sites-enabled" ]; then
if [ -f "/etc/nginx/sites-enabled/pufferpanel.conf" ]; then
echo "An existing configuration exists in /etc/nginx/sites-enabled"
return
fi
if [ -f "/etc/nginx/sites-available/pufferpanel.conf" ]; then
echo "An existing configuration exists in /etc/nginx/sites-available"
return
fi
echo "${conf}" > /etc/nginx/sites-available/pufferpanel.conf
ln -s /etc/nginx/sites-available/pufferpanel.conf /etc/nginx/sites-enabled/pufferpanel.conf
elif [ -d "/etc/nginx/conf.d/" ]; then
if [ -f "/etc/nginx/conf.d/pufferpanel.conf" ]; then
echo "An existing configuration exists in /etc/nginx/conf.d"
return
fi
echo "${conf}" > /etc/nginx/conf.d/pufferpanel.conf
else
echo "Could not determine where to install the config"
return
fi
if command -v systemctl >/dev/null; then
systemctl restart nginx
elif command -v service >/dev/null; then
service nginx restart
fi
}
function updateLogOwner() {
getPhpConfLocation
if [ "${phpWWWConfLocation}" = "" ]; then
echo -e "Could not determine where PHP config is, cannot install config"
return
fi
phpUser=$(grep "^user \?= \?" $phpWWWConfLocation 2>/dev/null | awk -F'[=&]' '{print $2}' | sed -e 's/ //')
if [ "${phpUser}" = "" ]; then
echo -e "Could not determine PHP user, cannot correct log folder owner"
return
fi
phpGroup=$(grep "^user \?= \?" $phpWWWConfLocation 2>/dev/null | awk -F'[=&]' '{print $2}' | sed -e 's/ //')
if [ "${phpGroup}" = "" ]; then
echo -e "Could not determine PHP group, cannot correct log folder owner"
return
fi
chown -R ${phpUser}:${phpGroup} /srv/pufferpanel/logs
}
function getPhpConfLocation(){
declare -a wwwConfLocations=(
"/etc/php/*/fpm/pool.d/www.conf"
"/etc/php-fpm.d/www.conf"
"/etc/php/php-fpm.d/www.conf"
)
phpWWWConfLocation=""
for loc in "${wwwConfLocations[@]}"; do
echo "$loc"
if compgen -G "$loc"; then
phpWWWConfLocation="$loc"
break
fi
done
}
case $1 in
install)
installNode=1
for i in "$@"
do
case ${i} in
--withoutDaemon)
installNode=0
shift
;;
esac
done
type php 1>/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "PHP is not found within your PATH, cannot proceed"
exit 1;
fi
result=$(php -r 'exit (version_compare(PHP_VERSION, "7.1.0") < 0 ? "1" : "0");');
if [ "${result}" == "0" ]; then
echo "PHP 7.1.0+: [${green}Installed${normal}]"
else
echo "PHP 7.1.0+: [${red}Not Installed${normal}]"
canInstall=0
fi
extensions=("curl" "hash" "openssl" "pdo" "pdo_mysql")
canInstall=1
for i in ${extensions[@]}; do
phpcmd=$(php -r 'echo extension_loaded("'${i}'") ? 1 : 0;')
if [ "$phpcmd" -eq "1" ]; then
echo "PHP-${i}: [${green}Installed${normal}]"
else
echo "PHP-${i}: [${red}Not Installed${normal}]"
canInstall=0
fi
done
if [ "${canInstall}" -eq "0" ]; then
echo "Dependencies are missing, cannot install"
exit 1
fi
printSeparator
configureMysql
printSeparator
configureSite
printSeparator
configureUser
type nginx 1>/dev/null 2>&1
if [ $? -eq 0 ]; then
configureNginx
else
type apache2 1>/dev/null 2>&1
if [ $? -eq 0 ]; then
configureApache
else
type httpd 1>/dev/null 2>&1
if [ $? -eq 0 ]; then
configureHttpd
fi
fi
fi
updateLogOwner
if [ "$installNode" -eq "1" ]; then
echo "Installing pufferd daemon onto the local node"
nodeSecret=$(cat /proc/sys/kernel/random/uuid)
nodeSecret=${nodeSecret^^}
pufferdVersion=$(cat src/versions/pufferd)
sed -e "s/{{ settings.master_url }}/http:\/\/${siteUrl}/g" -e "s/{{ node.daemon_secret }}/${nodeSecret}/g" -e "s/{{ pufferdVersion }}/${pufferdVersion}/g" app/views/templates/auto-deploy.tpl > /tmp/pufferd.sh
chmod +x /tmp/pufferd.sh
/tmp/pufferd.sh
installWorked=$?
rm /tmp/pufferd.sh
if [ $installWorked -eq 0 ]; then
mysql -h ${mysqlHost} -P ${mysqlPort} -D ${mysqlDb} -u ${mysqlUser} --password="${mysqlPass}" -e "
INSERT INTO nodes (name, location, fqdn, ip, daemon_secret,daemon_listen,daemon_sftp, allocate_memory, allocate_disk, ips, ports, public, docker)
VALUES('LocalNode', (SELECT id FROM locations WHERE short = 'Localhost'), '${siteUrl}', '127.0.0.1', '${nodeSecret}', 5656, 5657, 0, 0, '{}', '{}', 1, 0)"
else
echo "${red}Installation of pufferd failed locally, you will have to create a node manually on this machine if needed${normal}"
fi
fi
printSeparator
shopt -s nocasematch
echo -n "Would you like to thank us? Each thanks gives us the motivation to provide this software to you. [Y/n]: "
read thanks
if [[ "${thanks}" == "Y" ]] || [[ "${thanks}" == "" ]]; then
curl -X POST https://thankyou.pufferpanel.com >/dev/null 2>&1
fi
printSeparator
echo "Thank you for installing PufferPanel!"
echo "Assuming that the installation completed, you will be able to visit your new panel at ${blue}http://${siteUrl}${normal}"
;;
purge)
echo "${red}WARNING: This will remove the PufferPanel database, including all users and servers!${normal}"
shopt -s nocasematch
echo -n "Are you sure you wish delete the PufferPanel database? [y/N]: "
read upgradeOverride
if [[ "${upgradeOverride}" != "y" ]]; then
exit
fi
loadConfig
if [ "${mysqlHost}" == "" ]; then
printSeparator
mysqlHost=""
mysqlPort="3306"
mysqlDb="pufferpanel"
mysqlUser="root"
echo "MySQL configuration not found, please provide the root MySQL credentials"
echo -n "Enter the MySQL host [localhost]: "
read temp
if [ "${temp}" != "" ]; then
mysqlHost=${temp}
fi
echo -n "Enter the MySQL port [3306]: "
read temp
if [ "${temp}" != "" ]; then
mysqlPort=${temp}
fi
echo -n "Enter the MySQL username (MUST HAVE GRANT) [root]: "
read temp
if [ "${temp}" != "" ]; then
mysqlUser=${temp}
fi
notValid=true
while ${notValid}; do
echo -n "Enter the MySQL account password: "
read -s temp
if [ "${temp}" != "" ]; then
mysqlPass=${temp}
fi
if mysql -h ${mysqlHost} -P ${mysqlPort} -u ${mysqlUser} --password="${mysqlPass}" -e "exit"; then
notValid=false
else
echo "Database connection could not be established"
fi
done
fi
mysql -h ${mysqlHost} -P ${mysqlPort} -u ${mysqlUser} --password="${mysqlPass}" -e "DROP DATABASE pufferpanel;"
echo "Database deleted. You may now run ./pufferpanel install if you wish to reinstall PufferPanel."
;;
updatesite)
configureSite
;;
adduser)
configureUser
;;
addnginx)
configureNginx
;;
addapache)
configureApache
;;
addhttpd)
configureHttpd
;;
update)
echo "The current install version is v1.2.0, there are no updates available yet."
;;
upgrade)
loadConfig
updateLogOwner
dbVersion=$(mysql -h ${mysqlHost} -P ${mysqlPort} -D ${mysqlDb} -u ${mysqlUser} --password="${mysqlPass}" -N -e "
SELECT metaValue FROM _meta WHERE metaKey='version'" 2>/dev/null)
ppVersion=$(<src/versions/current)
if [[ "${dbVersion}" == "${ppVersion}" ]]; then
echo "This update has already been run"
shopt -s nocasematch
echo -n "Are you sure you wish to run the update anyway? [y/N]: "
read updateOverride
if [[ "${updateOverride}" != "y" ]]; then
exit
fi
fi
if [[ "${dbVersion}" != "${ppVersion}" ]] || [[ "${updateOverride}" == "y" ]]; then
echo "Running update"
mysql -h ${mysqlHost} -P ${mysqlPort} -D ${mysqlDb} -u ${mysqlUser} --password="${mysqlPass}" < install/update.sql
echo "Database query executed. If no errors were reported then the update was successful"
fi
;;
version)
loadConfig
ppVersion=$(<src/versions/current)
dbVersion=$(mysql -h ${mysqlHost} -P ${mysqlPort} -D ${mysqlDb} -u ${mysqlUser} --password="${mysqlPass}" -N -e "
SELECT metaValue FROM _meta WHERE metaKey='version'" 2>/dev/null)
echo "PufferPanel version ${ppVersion}"
echo "Database schema version ${dbVersion}"
;;
*)
echo "PufferPanel"
echo "Usage: ./pufferpanel [install/update/upgrade/purge/updatesite/addnginx/addapache/addhttpd/adduser/version]"
;;
esac