forked from pufferpanel/pufferpanel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpufferpanel
429 lines (388 loc) · 14.7 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
#!/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 "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
print "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}" -e "SELECT host FROM information_schema.processlist WHERE ID=connection_id()" | head -n 3 | tail -n 1)
mysql -h ${mysqlHost} -P ${mysqlPort} -D ${mysqlDb} -u ${mysqlUser} --password="${mysqlPass}" -e "
GRANT SELECT, UPDATE, DELETE, ALTER, INSERT ON pufferpanel.* TO 'pufferpanel'@'${newHost}' IDENTIFIED BY '${newPw}';
GRANT SELECT, UPDATE, DELETE, ALTER, INSERT ON pufferpanel.* TO 'pufferpanel'@'localhost' IDENTIFIED BY '${newPw}';
GRANT SELECT, UPDATE, DELETE, ALTER, INSERT ON pufferpanel.* TO 'pufferpanel'@'172.17.42.%' IDENTIFIED BY '${newPw}';
"
echo "{
\"mysql\": {
\"host\": \"${mysqlHost}\",
\"database\": \"${mysqlDb}\",
\"username\": \"${newUser}\",
\"password\": \"${newPw}\",
\"port\": \"${mysqlPort}\",
\"ssl\": {
\"use\": false
}
}
}" > 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
('company_name', 'PufferPanel'),
('master_url', 'http://${siteUrl}/'),
('main_website', 'http://${siteUrl}/'),
('transport_token', NULL),
('transport_email', NULL),
('transport_method','php'),
('captcha_pub',NULL),
('captcha_priv',NULL),
('default_language', 'en'),
('https', 0),
('allow_subusers', 0) 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
while [ "${password}" == "" ]; do
echo -n "Password: "
read -s password
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', ${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
service apache2 restart
}
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
phpSocket=$(grep "listen[| ]\?=[| ]" /etc/php5/fpm/pool.d/www.conf 2>/dev/null | awk -F'[=&]' '{print $2}' | sed -e 's/ //')
if [ "${phpSocket}" = "" ]; then
phpSocket=$(grep "listen[| ]\?=[| ]" /etc/php/*/fpm/pool.d/www.conf 2>/dev/null | awk -F'[=&]' '{print $2}' | sed -e 's/ //')
if [ "${phpSocket}" = "" ]; then
phpSocket=$(grep "listen[| ]\?=[| ]" /etc/php-fpm.d/www.conf 2>/dev/null | awk -F'[=&]' '{print $2}' | sed -e 's/ //')
if [ "${phpSocket}" = "" ]; then
echo "Could not determine where PHP config is, cannot install config"
return
fi
fi
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
service nginx restart
}
case $1 in
install)
installNode=1
forceInstall=0
for i in "$@"
do
case $i in
--force)
forceInstall=1
shift
;;
--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, "5.5.0") < 0 ? "1" : "0");');
if [ "$result" == "0" ]; then
echo "PHP 5.5.0+: [${green}Installed${normal}]"
else
echo "PHP 5.5.0+: [${red}Not Installed${normal}]"
canInstall=false
fi
extensions=("curl" "hash" "openssl" "mcrypt" "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
if [ "$installNode" -eq "1" ]; then
canInstall=1
echo "Forcing installer due to flag being present"
else
echo "Dependencies are missing, cannot install"
exit 1
fi
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
fi
fi
if [ "$installNode" -eq "1" ]; then
echo "Installing pufferd daemon onto the local node"
nodeSecret=$(cat /proc/sys/kernel/random/uuid)
nodeSecret=${nodeSecret^^}
sed -e "s/{{ settings.master_url }}/http:\/\/${siteUrl}/g" app/views/templates/auto-deploy.tpl | sed -e "s/{{ node.daemon_secret }}/${nodeSecret}/g" > /tmp/pufferd.sh
chmod +x /tmp/pufferd.sh
/tmp/pufferd.sh
rm /tmp/pufferd.sh
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, daemon_base_dir, 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, '/srv/pufferd', 0, 0, '{}', '{}', 1, 0)"
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}"
;;
updatesite)
configureSite
;;
adduser)
configureUser
;;
addnginx)
configureNginx
;;
addapache)
configureApache
;;
update)
chmod +x install/update
install/update
;;
*)
echo "PufferPanel"
echo "Usage: ./pufferpanel [install/update/updatesite/addnginx/addapache/adduser]"
;;
esac