44#
55# Auto create multiple Hesia containers with various features enabled/disabled
66# lxc/lxd should be allready configured
7+ # echo "root:1000:1" | sudo tee -a /etc/subuid
8+ # echo "root:1000:1" | sudo tee -a /etc/subgid
9+ #
710# - container name will be generated depending on enabled features (os,proxy,webserver and php)
811# - 'SHARED_HOST_FOLDER' will be mounted in the (guest lxc) container at '/home/ubuntu/source/' and hestiacp src folder is expected to be there
912# - wildcard dns *.hst.domain.tld can be used to point to vm host
5356# define('HST_EMAIL', 'user@domain.tld');
5457define ('HST_BRANCH ' , '~localsrc ' );
5558define ('HST_ARGS ' , '--force --interactive no --clamav no -p ' . HST_PASS . ' --email ' . HST_EMAIL );
56- define ('LXC_TIMEOUT ' , 15 );
59+ define ('LXC_TIMEOUT ' , 30 );
5760
5861if ( !defined ('SHARED_HOST_FOLDER ' ) || !defined ('HST_PASS ' ) || !defined ('HST_EMAIL ' ) || !defined ('HST_BRANCH ' ) || !defined ('DOMAIN ' ) ) {
5962 die ("Error: missing variables " .PHP_EOL );
6568 ['description ' =>'ub1804 ngx fpm ' , 'os ' =>'ubuntu18.04 ' , 'nginx ' =>true , 'apache2 ' =>false , 'php ' =>'fpm ' , 'dns ' =>'auto ' , 'exim ' =>'auto ' ],
6669 ['description ' =>'ub1804 ngx a2 ' , 'os ' =>'ubuntu18.04 ' , 'nginx ' =>true , 'apache2 ' =>true , 'php ' =>'auto ' , 'dns ' =>'auto ' , 'exim ' =>'auto ' ],
6770 ['description ' =>'ub1804 ngx a2 mphp ' , 'os ' =>'ubuntu18.04 ' , 'nginx ' =>true , 'apache2 ' =>true , 'php ' =>'multiphp ' , 'dns ' =>'auto ' , 'exim ' =>'auto ' ],
71+ ['description ' =>'ub1804 ngx a2 fpm ' , 'os ' =>'ubuntu18.04 ' , 'nginx ' =>true , 'apache2 ' =>true , 'php ' =>'fpm ' , 'dns ' =>'auto ' , 'exim ' =>'auto ' ],
6872 ['description ' =>'ub1804 a2 mphp ' , 'os ' =>'ubuntu18.04 ' , 'nginx ' =>false , 'apache2 ' =>true , 'php ' =>'multiphp ' , 'dns ' =>'auto ' , 'exim ' =>'auto ' ],
73+ ['description ' =>'ub1804 a2 fpm ' , 'os ' =>'ubuntu18.04 ' , 'nginx ' =>false , 'apache2 ' =>true , 'php ' =>'fpm ' , 'dns ' =>'auto ' , 'exim ' =>'auto ' ],
6974 ['description ' =>'ub1804 a2 ' , 'os ' =>'ubuntu18.04 ' , 'nginx ' =>false , 'apache2 ' =>true , 'php ' =>'auto ' , 'dns ' =>'auto ' ],
7075 ['description ' =>'ub1604 a2 mphp ' , 'os ' =>'ubuntu16.04 ' , 'nginx ' =>false , 'apache2 ' =>true , 'php ' =>'multiphp ' , 'dns ' =>'auto ' , 'exim ' =>'auto ' ],
7176];
@@ -211,7 +216,11 @@ function check_lxc_container($container) {
211216 lxc_run (['info ' , $ container ['lxc_name ' ]], $ rc );
212217 if (isset ($ rc ) && $ rc === 0 )
213218 return ;
214-
219+
220+ $ pid = pcntl_fork ();
221+ if ($ pid > 0 )
222+ return $ pid ;
223+
215224 echo "Creating container " .$ container ['lxc_name ' ] . PHP_EOL ;
216225 lxc_run (['init ' , $ container ['lxc_image ' ], $ container ['lxc_name ' ]], $ rc );
217226 exec ('lxc config set ' .escapeshellarg ($ container ['lxc_name ' ]).' raw.idmap "both 1000 1000" 2>/dev/null ' , $ devnull , $ rc );
@@ -223,12 +232,14 @@ function check_lxc_container($container) {
223232 $ lxc_retry ++;
224233 $ cip = get_lxc_ip ($ container ['lxc_name ' ]);
225234 if ($ cip )
226- echo "container ip : $ cip " . PHP_EOL ;
235+ echo "Container " . $ container[ ' lxc_name ' ]. " IP : $ cip " . PHP_EOL ;
227236 sleep (1 );
228237 } while ($ lxc_retry <= LXC_TIMEOUT && filter_var ($ cip , FILTER_VALIDATE_IP ) === false );
229238
230239 echo "Updating container: " . $ container ['lxc_name ' ] . PHP_EOL ;
231240 exec ('lxc exec ' . $ container ['lxc_name ' ] . ' -- apt update ' , $ devnull , $ rc );
241+
242+ exit (0 );
232243}
233244
234245function hst_installer_worker ($ container ) {
@@ -248,10 +259,29 @@ function hst_installer_worker($container) {
248259 exit (0 );
249260}
250261
262+
263+ // Create and update containers
251264$ worker_pool = [];
252265foreach ($ containers as $ container ) {
253- check_lxc_container ($ container );
266+ $ worker_pid = check_lxc_container ($ container );
267+ if ($ worker_pid > 0 )
268+ $ worker_pool [] = $ worker_pid ;
269+ }
254270
271+ echo count ($ worker_pool ) . " LXC workers started " . PHP_EOL ;
272+ # waiting for workers to finish
273+ while (count ($ worker_pool )) {
274+ echo "Wait for LXC workers to finish " .PHP_EOL ;
275+ $ child_pid = pcntl_wait ($ status );
276+ if ($ child_pid ) {
277+ $ worker_pos = array_search ($ child_pid , $ worker_pool );
278+ unset($ worker_pool [$ worker_pos ]);
279+ }
280+ }
281+
282+ // Install Hestia
283+ $ worker_pool = [];
284+ foreach ($ containers as $ container ) {
255285 # Is hestia installed?
256286 lxc_run ('exec ' .$ container ['lxc_name ' ].' -- sudo --login "v-list-sys-config" ' , $ rc );
257287 if (isset ($ rc ) && $ rc ===0 )
@@ -263,7 +293,6 @@ function hst_installer_worker($container) {
263293}
264294
265295echo count ($ worker_pool ) . " background workers started " . PHP_EOL ;
266-
267296# waiting for workers to finish
268297while (count ($ worker_pool )) {
269298 echo "Wait for workers to finish " .PHP_EOL ;
@@ -274,6 +303,7 @@ function hst_installer_worker($container) {
274303 }
275304}
276305
306+ // Custom config
277307foreach ($ containers as $ container ) {
278308 echo "Apply custom config on: " .$ container ['lxc_name ' ].PHP_EOL ;
279309
0 commit comments