-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall
executable file
·421 lines (372 loc) · 13.2 KB
/
install
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
#!/bin/bash
export DEBIAN_FRONTEND=noninteractive
apt-get update -qq
## Exit if /etc/php.pre-xdebug exists
if [ -d "/etc/php.pre-xdebug" ]; then
echo "## Xdebug is already installed"
exit 0
fi
## Backup /etc/php folder
cp -r /etc/php /etc/php.pre-xdebug
# Function to rename symbolic links
link_rename() {
local DIR=$1
local OLD_PATTERN=$2
local NEW_PATTERN=$3
find "$DIR" -type l | while read -r line; do
echo "Processing: $line"
CUR_LINK_PATH="$(readlink "$line")"
if [ -z "$CUR_LINK_PATH" ]; then
echo "Error: Unable to read link target for $line"
continue
fi
NEW_LINK_PATH="${CUR_LINK_PATH/$OLD_PATTERN/$NEW_PATTERN}"
if [ "$CUR_LINK_PATH" != "$NEW_LINK_PATH" ]; then
rm "$line"
if [ $? -ne 0 ]; then
echo "Error: Failed to remove $line"
continue
fi
ln -s "$NEW_LINK_PATH" "$line"
if [ $? -ne 0 ]; then
echo "Error: Failed to create symbolic link $line -> $NEW_LINK_PATH"
continue
fi
echo "Updated: $line -> $NEW_LINK_PATH"
else
echo "No change needed for: $line"
fi
done
}
# Function to install and configure Xdebug for a specific PHP version
install_xdebug() {
local version=$1
local version_no_dot=${version//./}
local version_underscore=${version//./_}
local port="96${version_no_dot}"
echo "## Install Xdebug for PHP $version"
apt-get -qq install php${version}-xdebug
echo "## Make Xdebug profile for PHP $version"
mv /etc/php/${version} /etc/php/${version}xdbg
link_rename /etc/php/${version}xdbg /etc/php/${version}/ /etc/php/${version}xdbg/
cp -r /etc/php.pre-xdebug/${version} /etc/php/${version}
## Clean pool.d folder of existing configurations
rm -rf /etc/php/${version}xdbg/fpm/pool.d/*
## Write a dummy pool configuration file
echo "## Write dummy.conf for PHP $version"
cat <<EOF > /etc/php/${version}xdbg/fpm/pool.d/dummy.conf
; origin-src: deb/php-fpm/dummy.conf
[www]
listen = 127.0.0.1:$port
listen.allowed_clients = 127.0.0.1
user = www-data
group = www-data
pm = ondemand
pm.max_children = 4
pm.max_requests = 4000
pm.process_idle_timeout = 10s
EOF
## Write php-fpm.conf configuration file
echo "## Write php-fpm.conf for PHP $version"
cat <<EOF > /etc/php/${version}xdbg/fpm/php-fpm.conf
[global]
pid = /run/php/php${version}xdbg-fpm.pid
error_log = /var/log/php${version}xdbg-fpm.log
log_level = error
emergency_restart_threshold = 10
emergency_restart_interval = 60s
process_control_timeout = 10s
events.mechanism = epoll
include=/etc/php/${version}xdbg/fpm/pool.d/*.conf
EOF
## Write xdebug.ini file
echo "## Write xdebug.ini"
cat <<EOF > /etc/php/${version}xdbg/mods-available/xdebug.ini
zend_extension=xdebug.so
xdebug.start_with_request=yes
xdebug.mode=debug
xdebug.client_host=localhost
EOF
## Write php service file
echo "## Write the php service file"
cat <<EOF > /lib/systemd/system/php${version}xdbg-fpm.service
[Unit]
Description=The PHP ${version} Xdebug FastCGI Process Manager
Documentation=man:php-fpm${version}(8)
After=network.target
[Service]
Type=notify
Environment=PHPRC=/etc/php/${version}xdbg/fpm
Environment=PHP_INI_SCAN_DIR=/etc/php/${version}xdbg/fpm/conf.d
ExecStart=/usr/sbin/php-fpm${version} --nodaemonize --fpm-config /etc/php/${version}xdbg/fpm/php-fpm.conf
ExecStartPost=-/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/${version}xdbg/fpm/pool.d/www.conf ${version_no_dot}
ExecStopPost=-/usr/lib/php/php-fpm-socket-helper remove /run/php/php-fpm.sock /etc/php/${version}xdbg/fpm/pool.d/www.conf ${version_no_dot}
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
EOF
## Write init.d file
echo "## Write the init.d file"
cat <<EOF > /etc/init.d/php${version}xdbg-fpm
#!/bin/sh
### BEGIN INIT INFO
# Provides: php${version}xdbg-fpm
# Required-Start: \$remote_fs \$network
# Required-Stop: \$remote_fs \$network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts php${version}xdbg-fpm
# Description: Starts The PHP Xdebug FastCGI Process Manager Daemon
### END INIT INFO
# Author: Stephen J. Carnam <steveorevo@gmail.com>
export PHPRC=/etc/php/${version}xdbg/fpm
export PHP_INI_SCAN_DIR=/etc/php/${version}xdbg/fpm/conf.d
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="PHP ${version} Xdebug FastCGI Process Manager"
NAME=php-fpm${version}
CONFFILE=/etc/php/${version}xdbg/fpm/php-fpm.conf
DAEMON=/usr/sbin/\$NAME
DAEMON_ARGS="--daemonize --fpm-config \$CONFFILE"
CONF_PIDFILE=\$(sed -n 's/^pid[ =]*//p' \$CONFFILE)
PIDFILE=\${CONF_PIDFILE:-/run/php/php${version}xdbg-fpm.pid}
TIMEOUT=30
SCRIPTNAME=/etc/init.d/\$NAME
# Exit if the package is not installed
[ -x "\$DAEMON" ] || exit 0
# Read configuration variable file if it is present
[ -r /etc/default/\$NAME ] && . /etc/default/\$NAME
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions
#
# Function that starts the daemon/service
#
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
start-stop-daemon --start --quiet --pidfile \$PIDFILE --exec \$DAEMON --test > /dev/null \
|| return 1
start-stop-daemon --start --quiet --pidfile \$PIDFILE --exec \$DAEMON -- \
\$DAEMON_ARGS 2>/dev/null \
|| return 2
# Add code here, if necessary, that waits for the process to be ready
# to handle requests from services started subsequently which depend
# on this one. As a last resort, sleep for some time.
}
#
# Function that stops the daemon/service
#
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
start-stop-daemon --stop --quiet --retry=QUIT/\$TIMEOUT/TERM/5/KILL/5 --pidfile \$PIDFILE --name \$NAME
RETVAL="\$?"
[ "\$RETVAL" = 2 ] && return 2
# Wait for children to finish too if this is a daemon that forks
# and if the daemon is only ever run from this initscript.
# If the above conditions are not satisfied then add some other code
# that waits for the process to drop all resources that could be
# needed by services started subsequently. A last resort is to
# sleep for some time.
start-stop-daemon --stop --quiet --oknodo --retry=0/30/TERM/5/KILL/5 --exec \$DAEMON
[ "\$?" = 2 ] && return 2
# Many daemons don't delete their pidfiles when they exit.
rm -f \$PIDFILE
return "\$RETVAL"
}
#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
#
# If the daemon can reload its configuration without
# restarting (for example, when it is sent a SIGHUP),
# then implement that here.
#
start-stop-daemon --stop --signal USR2 --quiet --pidfile \$PIDFILE --name \$NAME
return 0
}
case "\$1" in
start)
[ "\$VERBOSE" != no ] && log_daemon_msg "Starting \$DESC" "\$NAME"
systemd-tmpfiles --remove --create /usr/lib/tmpfiles.d/php${version}xdbg-fpm.conf
case "\$?" in
0)
do_start
case "\$?" in
0|1) [ "\$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "\$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
1) [ "\$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "\$VERBOSE" != no ] && log_daemon_msg "Stopping \$DESC" "\$NAME"
do_stop
case "\$?" in
0|1) [ "\$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "\$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
status)
status_of_proc "\$DAEMON" "\$NAME" && exit 0 || exit \$?
;;
reload|force-reload)
log_daemon_msg "Reloading \$DESC" "\$NAME"
do_reload
log_end_msg \$?
;;
reopen-logs)
log_daemon_msg "Reopening \$DESC logs" \$NAME
if start-stop-daemon --stop --signal USR1 --oknodo --quiet \
--pidfile \$PIDFILE --exec \$DAEMON
then
log_end_msg 0
else
log_end_msg 1
fi
;;
restart)
log_daemon_msg "Restarting \$DESC" "\$NAME"
do_stop
case "\$?" in
0|1)
do_start
case "\$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
*)
echo "Usage: \$SCRIPTNAME {start|stop|status|restart|reload|force-reload}" >&2
exit 1
;;
esac
:
EOF
chmod +x /etc/init.d/php${version}xdbg-fpm
## Write PHP template file
echo "## Write the template file"
cat <<EOF > /usr/local/hestia/data/templates/web/php-fpm/PHP-${version_underscore}xdbg.tpl
[Unit]
Description=The PHP ${version} Xdebug FastCGI Process Manager
Documentation=man:php-fpm${version}(8)
After=network.target
[Service]
Type=notify
Environment=PHPRC=/etc/php/${version}xdbg/fpm
Environment=PHP_INI_SCAN_DIR=/etc/php/${version}xdbg/fpm/conf.d
ExecStart=/usr/sbin/php-fpm${version} --nodaemonize --fpm-config /etc/php/${version}xdbg/fpm/php-fpm.conf
ExecStartPost=-/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/${version}xdbg/fpm/pool.d/www.conf ${version_no_dot}
ExecStopPost=-/usr/lib/php/php-fpm-socket-helper remove /run/php/php-fpm.sock /etc/php/${version}xdbg/fpm/pool.d/www.conf ${version_no_dot}
ExecReload=/bin/kill -USR2 \$MAINPID
[Install]
WantedBy=multi-user.target
EOF
## Enable and start the systemd service
echo "## Enable and start the systemd service for PHP ${version}"
systemctl enable php${version}xdbg-fpm
systemctl start php${version}xdbg-fpm
## Register and start the init.d service
echo "## Register and start the init.d service for PHP ${version}"
update-rc.d php${version}xdbg-fpm defaults
service php${version}xdbg-fpm start
}
# Invoke the function for each PHP version
install_xdebug 7.4
install_xdebug 8.0
install_xdebug 8.1
install_xdebug 8.2
install_xdebug 8.3
############################################
# Install Open VSCode Server
############################################
## Exit if /etc/php.pre-xdebug exists
if [ -d "/opt/vscode" ]; then
echo "## VSCode is already installed"
exit 0
fi
## Dynamically get the latest version of Open VSCode Server
get_latest_version() {
curl --silent "https://api.github.com/repos/gitpod-io/openvscode-server/releases/latest" | jq -r .tag_name
}
cd /tmp || exit
architecture=""
# Detect x86_64 or aarch64 system
case $(uname -m) in
i386) architecture="linux-x64" ;;
i686) architecture="linux-x64" ;;
x86_64) architecture="linux-x64" ;;
arm*) dpkg --print-architecture | grep -q "arm64" && architecture="linux-arm64" || architecture="linux-arm" ;;
aarch64) architecture="linux-arm64" ;;
esac
OPENVSCODE_SERVER_VERSION=$(get_latest_version)
OPENVSCODE_ARCH=$architecture
wget https://github.com/gitpod-io/openvscode-server/releases/download/${OPENVSCODE_SERVER_VERSION}/${OPENVSCODE_SERVER_VERSION}-${OPENVSCODE_ARCH}.tar.gz
tar -xzf ${OPENVSCODE_SERVER_VERSION}-${OPENVSCODE_ARCH}.tar.gz
mv ${OPENVSCODE_SERVER_VERSION}-${OPENVSCODE_ARCH} /opt/vscode
rm ${OPENVSCODE_SERVER_VERSION}-${OPENVSCODE_ARCH}.tar.gz
# Write the vscode.config.js file
cat <<EOF > /opt/vscode/vscode.config.js
/**
* Control Open VSCode Server via PM2
*/
module.exports = {
apps: (function() {
// Get active user, hostname, and port
const {execSync} = require('child_process');
const fs = require('fs');
let user = execSync('/bin/bash -c "whoami"').toString().trim();
let hostname = execSync('/bin/bash -c "hostname -f"').toString().trim();
hostname = hostname.split('.').slice(-2).join('.');
let port = 0;
let pfile = '/usr/local/hestia/data/hcpp/ports/' + user + '/vscode-' + user + '.' + hostname + '.ports';
let ports = fs.readFileSync(pfile, {encoding:'utf8', flag:'r'});
ports = ports.split(/\\r?\\n/);
for( let i = 0; i < ports.length; i++) {
if (ports[i].indexOf('vscode_port') > -1) {
port = ports[i];
break;
}
}
port = parseInt(port.trim().split(' ').pop());
// Create PM2 compatible app details
let details = {};
details.name = "vscode-" + user + '.' + hostname;
details.interpreter = "/opt/vscode/node";
details.script = "/opt/vscode/out/server-main.js";
details.args = "--port " + port;
// Implement restart policy
details.watch = ["/home/" + user + "/.openvscode-server/data/token"];
details.ignore_watch = [];
details.watch_delay = 5000;
details.restart_delay = 5000;
return [details];
})()
}
EOF
############################################
# Install Certbot if no dev-pw plugin
############################################
if [ ! -d "/usr/local/hestia/plugins/dev-pw" ]; then
apt-get install -qq certbot
fi
# Notify installation has finished
/usr/local/hestia/bin/v-add-user-notification admin "VSCode" "<span style=\"color:#0092FF;\"><i class=\"fas fa-file-code\"></i></span> VSCode plugin has finished installing."