-
Notifications
You must be signed in to change notification settings - Fork 2
/
nodeapp.php
431 lines (382 loc) · 18.1 KB
/
nodeapp.php
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
<?php
/**
* Extend the HestiaCP Pluginable object with our NodeApp object for
* allocating NodeJS app ports and starting & stopping apps via PM2
* and NVM for every .config.js file present.
*
* @version 1.0.0
* @license GPL-3.0
* @link https://github.com/virtuosoft-dev/hcpp-nodeapp
*
*/
if ( ! class_exists( 'NodeApp') ) {
class NodeApp {
public $domain = "";
public $user = "";
/**
* Constructor, listen for the priv_change_web_domain_proxy_tpl event
*/
public function __construct() {
global $hcpp;
$hcpp->nodeapp = $this;
$hcpp->add_action( 'priv_change_web_domain_proxy_tpl', [ $this, 'priv_change_web_domain_proxy_tpl' ] );
$hcpp->add_action( 'pre_delete_web_domain_backend', [ $this, 'pre_delete_web_domain_backend' ] );
$hcpp->add_action( 'priv_suspend_web_domain', [ $this, 'priv_suspend_web_domain' ] );
$hcpp->add_action( 'priv_unsuspend_web_domain', [ $this, 'priv_unsuspend_domain' ] ); // Bulk unsuspend domains only throws this event
$hcpp->add_action( 'priv_unsuspend_domain', [ $this, 'priv_unsuspend_domain' ] ); // Individually unsuspend domain only throws this event
$hcpp->add_action( 'hcpp_rebooted', [ $this, 'hcpp_rebooted' ] );
$hcpp->add_action( 'hcpp_runuser', [ $this, 'hcpp_runuser' ] );
}
/**
* Modify runuser to incorporate NVM
*/
public function hcpp_runuser( $cmd ) {
$cmd = 'export NVM_DIR=/opt/nvm && source /opt/nvm/nvm.sh && ' . $cmd;
return $cmd;
}
/**
* Check if system has rebooted and restart apps
*/
public function hcpp_rebooted() {
// Wait up to 60 additional seconds for MySQL to start
$i = 0;
while ( $i < 60 ) {
$i++;
$mysql = shell_exec( 'systemctl is-active mysql' );
if ( trim( $mysql ) == 'active' ) {
break;
}
sleep( 1 );
}
// Restart all PM2 apps for all user accounts
$users = scandir('/home');
global $hcpp;
$cmd = '';
foreach ( $users as $user ) {
// Ignore hidden files/folders and system folders
if ( $user == '.' || $user == '..' || $user == 'lost+found' || $user == 'systemd' ) {
continue;
}
// Check if the .pm2 folder exists in the user's home directory
if ( is_dir( "/home/$user/.pm2" ) ) {
// Restart any pm2 processes
$cmd .= 'runuser -s /bin/bash -l ' . $user . ' -c "cd /home/' . $user . ' && ';
$cmd .= 'export NVM_DIR=/opt/nvm && source /opt/nvm/nvm.sh && pm2 resurrect"' . "\n";
}
}
$cmd = $hcpp->do_action( 'nodeapp_resurrect_apps', $cmd );
if ( trim( $cmd ) != '' ) {
$hcpp->log( shell_exec( $cmd ) );
}
}
/**
* On proxy template change, copy basic nodeapp, allocate ports, and start apps
*/
public function priv_change_web_domain_proxy_tpl( $args ) {
global $hcpp;
$user = $args[0];
$domain = $args[1];
$proxy = $args[2];
// Remember for post_change_web_domain_proxy_tpl
$this->user = $user;
$this->domain = $domain;
$nodeapp_folder = "/home/$user/web/$domain/nodeapp";
if ( $proxy == 'NodeApp' ) {
if ( !is_dir( $nodeapp_folder) ) {
// Copy initial nodeapp folder
$hcpp->copy_folder( __DIR__ . '/nodeapp', $nodeapp_folder, $user );
$args = [
'user' => $user,
'domain' => $domain,
'proxy' => $proxy,
'nodeapp_folder' => $nodeapp_folder
];
$args = $hcpp->do_action( 'nodeapp_copy_files', $args );
$nodeapp_folder = $args['nodeapp_folder'];
chmod( $nodeapp_folder, 0751 );
// Install dependencies
$cmd = 'runuser -s /bin/bash -l ' . $user . ' -c "cd \"' . $nodeapp_folder . '\" && export NVM_DIR=/opt/nvm && source /opt/nvm/nvm.sh && npm install"';
$args['cmd'] = $cmd;
$args = $hcpp->do_action( 'nodeapp_install_dependencies', $args );
shell_exec( $args['cmd'] );
}
// Shutdown stray apps and startup root and subfolder apps
$this->shutdown_apps( $nodeapp_folder );
$this->allocate_ports( $nodeapp_folder );
$this->generate_nginx_files( $nodeapp_folder );
$this->startup_apps( $nodeapp_folder );
}else {
// Shutdown stray apps and only startup subfolder apps
if ( is_dir( $nodeapp_folder) ) {
$this->shutdown_apps( $nodeapp_folder );
$this->allocate_ports( $nodeapp_folder );
$this->generate_nginx_files( $nodeapp_folder, false );
$this->startup_apps( $nodeapp_folder, false );
}
}
}
/**
* On domain delete, shutdown apps
*/
public function pre_delete_web_domain_backend( $args ) {
global $hcpp;
$user = $args[0];
$domain = $args[1];
$nodeapp_folder = "/home/$user/web/$domain/nodeapp";
$this->shutdown_apps( $nodeapp_folder );
}
/**
* On domain suspend, shutdown apps
*/
public function priv_suspend_web_domain( $args ) {
global $hcpp;
$user = $args[0];
$domain = $args[1];
$nodeapp_folder = "/home/$user/web/$domain/nodeapp";
// Remove prior nginx config files; duplicate location "/" will cause nginx to fail
if ( file_exists( "/home/$user/conf/web/$domain/nginx.ssl.conf_nodeapp" ) ) {
unlink( "/home/$user/conf/web/$domain/nginx.ssl.conf_nodeapp" );
}
if ( file_exists( "/home/$user/conf/web/$domain/nginx.conf_nodeapp" ) ) {
unlink( "/home/$user/conf/web/$domain/nginx.conf_nodeapp" );
}
$this->shutdown_apps( $nodeapp_folder );
return $args;
}
/**
* On domain unsuspend, startup apps
*/
public function priv_unsuspend_domain( $args ) {
global $hcpp;
$user = $args[0];
$domain = $args[1];
$nodeapp_folder = "/home/$user/web/$domain/nodeapp";
if ( is_dir( $nodeapp_folder) ) {
$proxy = $hcpp->run("v-list-web-domain $user $domain json");
if ( $proxy != NULL ) {
$proxy = $proxy[$domain]["PROXY"];
$this->allocate_ports( $nodeapp_folder );
$this->generate_nginx_files( $nodeapp_folder, ( $proxy == "NodeApp" ) );
$this->startup_apps( $nodeapp_folder, ( $proxy == "NodeApp" ) );
}
}
return $args;
}
/**
* Scan the nodeapp folder for .config.js files and allocate a port for each
*/
public function allocate_ports( $nodeapp_folder ) {
global $hcpp;
$parse = explode( '/', $nodeapp_folder );
$user = $parse[2];
$domain = $parse[4];
// Wipe the existing ports for this domain
if ( file_exists( "/usr/local/hestia/data/hcpp/ports/$user/$domain.ports" ) ) {
unlink( "/usr/local/hestia/data/hcpp/ports/$user/$domain.ports" );
}
// Allocate a port for each .config.js file found
$files = $this->get_config_files( $nodeapp_folder );
foreach($files as $file) {
// Get the name of the app from the filename
if ( ! preg_match( '/\.config\.js$/', $file ) ) continue;
$file = explode( '/', $file );
$file = end( $file );
$name = str_replace( '.config.js', '', $file );
$name = preg_replace( "/[^a-zA-Z0-9-_]+/", "", $name ) . "_port";
// Allocate a port for the app
$hcpp->allocate_port( $name, $user, $domain );
}
// Throw a nodeapp event to allow other plugins to allocate ports
$args = [
'user' => $user,
'domain' => $domain,
'nodeapp_folder' => $nodeapp_folder
];
$hcpp->do_action( 'nodeapp_ports_allocated', $args );
}
/**
* Generate Nginx proxy settings for each .config.js file found in subfolders
* and map the subfolder to a matching reverse proxy url of the same path
*/
public function generate_nginx_files( $nodeapp_folder, $inc_root = true ) {
global $hcpp;
$parse = explode( '/', $nodeapp_folder );
$user = $parse[2];
$domain = $parse[4];
$files = $this->get_config_files( $nodeapp_folder );
// Remove prior nginx config files
if ( file_exists( "/home/$user/conf/web/$domain/nginx.ssl.conf_nodeapp" ) ) {
unlink( "/home/$user/conf/web/$domain/nginx.ssl.conf_nodeapp" );
}
if ( file_exists( "/home/$user/conf/web/$domain/nginx.conf_nodeapp" ) ) {
unlink( "/home/$user/conf/web/$domain/nginx.conf_nodeapp" );
}
// Generate new nodeapp nginx config files
$nginx = '';
foreach($files as $file) {
$subfolder = str_replace( "$nodeapp_folder/", '', $file );
if ( strpos( $subfolder, '/' ) === false ) {
$subfolder = '/';
}else{
$subfolder = '/' . $hcpp->delRightMost($subfolder, '/') . '/';
}
if ( $inc_root == false && $subfolder == '/' ) continue; // Skip root
$app = $hcpp->getRightMost( $file, '/' );
$app = str_replace( '.config.js', '', $app );
$nginx .= 'location ' . $subfolder . ' {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:$' . $app . '_port;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}' . "\n";
}
// Write the nginx config nodeapp subfolder file to the user's conf folder
if ($nginx != '') {
$args = [
'user' => $user,
'domain' => $domain,
'nginx' => $nginx
];
// Allow other plugins to modify the subfolder nginx config files
$args = $hcpp->do_action( 'nodeapp_subfolder_nginx_conf', $args );
$nginx = $args['nginx'];
file_put_contents( "/home/$user/conf/web/$domain/nginx.conf_nodeapp", $nginx );
// Overrite the proxy_hide_header in the SSL config file
$nginx .= "# Override prev. proxy_hide_header Upgrade\nadd_header Upgrade \$http_upgrade always;";
$args['nginx'] = $nginx;
$args = $hcpp->do_action( 'nodeapp_subfolder_nginx_ssl_conf', $args );
$nginx = $args['nginx'];
file_put_contents( "/home/$user/conf/web/$domain/nginx.ssl.conf_nodeapp", $nginx );
}
// Include port variables in nginx.hsts.conf_ports and nginx.forcessl.conf_ports
$file = "/usr/local/hestia/data/hcpp/ports/$user/$domain.ports";
if ( file_exists( $file ) ) {
$content = "include /usr/local/hestia/data/hcpp/ports/$user/$domain.ports;";
file_put_contents( "/home/$user/conf/web/$domain/nginx.forcessl.conf_ports", $content );
file_put_contents( "/home/$user/conf/web/$domain/nginx.hsts.conf_ports", $content );
}else{
if ( file_exists( "/home/$user/conf/web/$domain/nginx.forcessl.conf_ports" ) ) {
unlink( "/home/$user/conf/web/$domain/nginx.forcessl.conf_ports" );
}
if ( file_exists( "/home/$user/conf/web/$domain/nginx.hsts.conf_ports" ) ) {
unlink( "/home/$user/conf/web/$domain/nginx.hsts.conf_ports" );
}
}
}
/**
* Generate random alpha numeric for passwords, seeds, etc.
*
* @param int $length The length of characters to return.
* @param string $chars The set of possible characters to choose from.
* @return string The resulting randomly generated string.
*/
public function random_chars( $length = 10, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890' ) {
$string = '';
$max_index = strlen( $chars ) - 1;
for ( $i = 0; $i < $length; $i++ ) {
$string .= $chars[random_int( 0, $max_index )]; // random_int is more crypto secure
}
return $string;
}
/**
* Scan the nodeapp folder for .config.js files and start the app for each
*/
public function startup_apps( $nodeapp_folder, $inc_root = true ) {
global $hcpp;
$parse = explode( '/', $nodeapp_folder );
$user = $parse[2];
$domain = $parse[4];
$files = $this->get_config_files( $nodeapp_folder );
$cmd = 'runuser -s /bin/bash -l ' . $user . ' -c "cd \"' . $nodeapp_folder . '\" && export NVM_DIR=/opt/nvm && source /opt/nvm/nvm.sh ';
foreach($files as $file) {
// Skip the root app if inc_root is false
if ( $inc_root == false ) {
$subfolder = str_replace( "$nodeapp_folder/", '', $file );
if ( strpos( $subfolder, '/' ) == false) continue;
}
// Add app to startup
$cmd .= "; pm2 start $file ";
}
if ( strpos( $cmd, '; pm2 start ' ) ) {
$cmd .= '; pm2 save --force "';
$args = [
'user' => $user,
'domain' => $domain,
'cmd' => $cmd
];
// Run the command to start all the apps
$args = $hcpp->do_action( 'nodeapp_startup_services', $args );
$cmd = $args['cmd'];
shell_exec( $cmd );
}
}
/**
* Gather list of all running apps to delete and construct shutdown command,
* instead of pm2 delete all; delete each one individually as filters may
* alter the behavior to keep user and select domain apps running.
*/
public function shutdown_apps( $nodeapp_folder ) {
global $hcpp;
$parse = explode( '/', $nodeapp_folder );
$user = $parse[2];
$domain = $parse[4];
// Get list of apps to delete
$cmd = 'runuser -s /bin/bash -l ' . $user . ' -c "export NVM_DIR=/opt/nvm && source /opt/nvm/nvm.sh ; pm2 ls | grep ' . $domain . '"';
$lines = shell_exec( $cmd );
$lines = explode( "\n", $lines );
$cmd = 'runuser -s /bin/bash -l ' . $user . ' -c "cd \"' . $nodeapp_folder . '\" && export NVM_DIR=/opt/nvm && source /opt/nvm/nvm.sh ';
foreach( $lines as $l ) {
if ( strpos( $l, '-' . $domain ) === false ) continue;
$app = $hcpp->getRightMost( $hcpp->getLeftMost( $l, '-' ), ' ' );
$app = $app . '-' . $domain;
// Add app to shutdown by name
$cmd .= "; pm2 delete $app ";
}
if ( strpos( $cmd, '; pm2 delete ' ) ) {
$cmd .= '; pm2 save --force "';
$args = [
'user' => $user,
'domain' => $domain,
'cmd' => $cmd
];
// Run the command to shutdown all the apps
$args = $hcpp->do_action( 'nodeapp_shutdown_services', $args );
$cmd = $args['cmd'];
shell_exec( $cmd );
}
}
/**
* Gather a list of all valid PM2 configuration files that allocate ports from the given folder
*/
public function get_config_files( $dir ) {
global $hcpp;
$configFiles = array();
$files = scandir( $dir );
foreach ( $files as $file ) {
if ( $file == '.' || $file == '..' ) continue;
$path = $dir . '/' . $file;
if ( is_dir( $path ) && $file != "node_modules" ) {
$configFiles = array_merge( $configFiles, $this->get_config_files( $path ) );
} else if ( preg_match('/\.config\.js$/', $file ) ) {
// Sanitize the name of the app to prevent Nginx injection
if ( ! preg_match( '/\.config\.js$/', $file ) ) continue;
$file = explode( '/', $file );
$file = end( $file );
$name = str_replace( '.config.js', '', $file );
$name = preg_replace( "/[^a-zA-Z0-9-_]+/", "", $name );
if ( $path == $dir . '/' . $name . '.config.js' ) {
// Check for hestia pm2 port allocating validity
if ( file_exists( $path ) && strpos( file_get_contents( $path ), '/usr/local/hestia') !== false ) {
$configFiles[] = $path;
}
}
}
}
return $configFiles;
}
}
new NodeApp();
}