make-test-containers.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #!/usr/bin/env php
  2. <?php
  3. #
  4. # Auto create multiple Hesia containers with various features enabled/disabled
  5. # lxc/lxd should be allready configured
  6. # - container name will be generated depending on enabled features (os,proxy,webserver and php)
  7. # - 'SHARED_HOST_FOLDER' will be mounted in the (guest lxc) container at '/home/ubuntu/source/' and hestiacp src folder is expected to be there
  8. # - wildcard dns *.hst.domain.tld can be used to point to vm host
  9. # - watch install log ex:(host) tail -n 100 -f /tmp/hst_installer_hst-ub1604-a2-mphp
  10. #
  11. # CONFIG HOST STEPS:
  12. # export SHARED_HOST_FOLDER="/home/myuser/projectfiles"
  13. # mkdir -p $SHARED_HOST_FOLDER
  14. # cd $SHARED_HOST_FOLDER && git clone https://github.com/hestiacp/hestiacp.git && cd hestiacp && git checkout ..branch..
  15. #
  16. ## Uncomment and configure the following vars
  17. # define('DOMAIN', 'hst.domain.tld');
  18. # define('SHARED_HOST_FOLDER', '/home/myuser/projectfiles');
  19. # define('HST_PASS', ''); // <- # openssl rand -base64 12
  20. # define('HST_EMAIL', '[email protected]');
  21. define('HST_BRANCH', '~localsrc');
  22. define('HST_ARGS', '--force --interactive no --clamav no -p ' . HST_PASS . ' --email ' . HST_EMAIL);
  23. define('LXC_TIMEOUT', 15);
  24. if( !defined('SHARED_HOST_FOLDER') || !defined('HST_PASS') || !defined('HST_EMAIL') || !defined('HST_BRANCH') || !defined('DOMAIN') ) {
  25. die("Error: missing variables".PHP_EOL);
  26. }
  27. $containers = [
  28. // ['description'=>'hst-d9-ngx-a2-mphp', 'os'=>'debian9', 'nginx'=>true, 'apache2'=>true, 'php'=>'multiphp', 'dns'=>'auto', 'exim'=>'auto'],
  29. ['description'=>'ub1804 ngx mphp', 'os'=>'ubuntu18.04', 'nginx'=>true, 'apache2'=>false, 'php'=>'multiphp', 'dns'=>'auto', 'exim'=>'auto'],
  30. ['description'=>'ub1804 ngx fpm', 'os'=>'ubuntu18.04', 'nginx'=>true, 'apache2'=>false, 'php'=>'fpm', 'dns'=>'auto', 'exim'=>'auto'],
  31. ['description'=>'ub1804 ngx a2', 'os'=>'ubuntu18.04', 'nginx'=>true, 'apache2'=>true, 'php'=>'auto', 'dns'=>'auto', 'exim'=>'auto'],
  32. ['description'=>'ub1804 ngx a2 mphp', 'os'=>'ubuntu18.04', 'nginx'=>true, 'apache2'=>true, 'php'=>'multiphp', 'dns'=>'auto', 'exim'=>'auto'],
  33. ['description'=>'ub1804 a2 mphp', 'os'=>'ubuntu18.04', 'nginx'=>false, 'apache2'=>true, 'php'=>'multiphp', 'dns'=>'auto', 'exim'=>'auto'],
  34. ['description'=>'ub1804 a2', 'os'=>'ubuntu18.04', 'nginx'=>false, 'apache2'=>true, 'php'=>'auto', 'dns'=>'auto'],
  35. ['description'=>'ub1604 a2 mphp', 'os'=>'ubuntu16.04', 'nginx'=>false, 'apache2'=>true, 'php'=>'multiphp', 'dns'=>'auto', 'exim'=>'auto'],
  36. ];
  37. array_walk($containers, function(&$element) {
  38. $lxc_name='hst-';
  39. $hst_args = HST_ARGS;
  40. $element['hst_installer'] = 'hst-install-ubuntu.sh';
  41. $element['lxc_image'] = 'ubuntu:18.04';
  42. if($element['os'] == "ubuntu16.04") {
  43. $element['lxc_image'] = 'ubuntu:16.04';
  44. $lxc_name .= 'ub1604';
  45. } else if($element['os'] == "debian8") {
  46. $element['lxc_image'] = 'images:debian/8';
  47. $element['hst_installer'] = 'hst-install-debian.sh';
  48. $lxc_name .= 'd8';
  49. } else if($element['os'] == "debian9") {
  50. $element['lxc_image'] = 'images:debian/9';
  51. $element['hst_installer'] = 'hst-install-debian.sh';
  52. $lxc_name .= 'd9';
  53. } else {
  54. $lxc_name .= 'ub1804';
  55. $element['os'] = "ubuntu18.04";
  56. }
  57. if($element['nginx'] === true) {
  58. $lxc_name .= '-ngx';
  59. $hst_args .= " --nginx yes";
  60. } else
  61. $hst_args .= " --nginx no";
  62. if($element['apache2'] === true) {
  63. $lxc_name .= '-a2';
  64. $hst_args .= " --apache yes";
  65. } else
  66. $hst_args .= " --apache no";
  67. if($element['php'] == 'fpm') {
  68. $lxc_name .= '-fpm';
  69. $hst_args .= " --phpfpm yes";
  70. } else if($element['php'] == 'multiphp') {
  71. $lxc_name .= '-mphp';
  72. $hst_args .= " --multiphp yes";
  73. }
  74. if(isset($element['dns'])) {
  75. if($element['dns'] === true || $element['dns'] == 'auto') {
  76. $hst_args .= " --named yes";
  77. } else {
  78. $hst_args .= " --named no";
  79. }
  80. }
  81. if(isset($element['exim'])) {
  82. if($element['exim'] === true || $element['exim'] == 'auto') {
  83. $hst_args .= " --exim yes";
  84. } else {
  85. $hst_args .= " --exim no";
  86. }
  87. }
  88. if(isset($element['webmail'])) {
  89. if($element['webmail'] === true || $element['webmail'] == 'auto') {
  90. $hst_args .= " --dovecot yes";
  91. } else {
  92. $hst_args .= " --dovecot no";
  93. }
  94. }
  95. $element['lxc_name'] = $lxc_name;
  96. $element['hostname'] = $lxc_name . '.' . DOMAIN;
  97. // $hst_args .= ' --with-debs /home/ubuntu/source/hestiacp/src/pkgs/develop/' . $element['os'];
  98. $hst_args .= ' --with-debs /tmp/hestiacp-src/debs';
  99. $hst_args .= ' --hostname ' . $element['hostname'];
  100. $element['hst_args'] = $hst_args;
  101. });
  102. function lxc_run($args, &$rc) {
  103. $cmd_args = "";
  104. if(is_array($args)) {
  105. foreach ($args as $arg) {
  106. $cmd_args .= ' ' . escapeshellarg($arg);
  107. }
  108. } else
  109. $cmd_args = $args;
  110. exec('lxc ' . $cmd_args . ' 2>/dev/null', $cmdout, $rc);
  111. if(isset($rc) && $rc !== 0)
  112. return false;
  113. if(json_decode(implode(PHP_EOL, $cmdout),true) === null)
  114. return $cmdout;
  115. return json_decode(implode(PHP_EOL, $cmdout),true);
  116. }
  117. function getHestiaVersion($branch) {
  118. $control_file = '';
  119. if($branch==='~localsrc')
  120. $control_file = file_get_contents(SHARED_HOST_FOLDER . '/hestiacp/src/deb/hestia/control');
  121. else {
  122. $control_file = file_get_contents("https://raw.githubusercontent.com/hestiacp/hestiacp/${branch}/src/deb/hestia/control");
  123. }
  124. foreach(explode(PHP_EOL, $control_file) as $line) {
  125. if(empty($line))
  126. continue;
  127. list($key,$value) = explode(':', $line);
  128. if(strtolower($key) === 'version')
  129. return trim($value);
  130. }
  131. throw new Exception("Error reading Hestia version for branch: [${branch}]", 1);
  132. }
  133. function get_lxc_ip($name) {
  134. $result = lxc_run(['list', '--format', 'csv', '-c', 'n,4'],$rc);
  135. if(empty($result))
  136. return false;
  137. foreach ($result as $line) {
  138. list($cnt, $address) = explode(',', $line);
  139. if($cnt == $name) {
  140. $iface = explode(' ', $address);
  141. if(filter_var($iface[0], FILTER_VALIDATE_IP))
  142. return $iface[0];
  143. else
  144. return false;
  145. }
  146. }
  147. }
  148. function check_lxc_container($container) {
  149. echo "Check container:".$container['lxc_name'].PHP_EOL;
  150. lxc_run(['info', $container['lxc_name']], $rc);
  151. if(isset($rc) && $rc === 0)
  152. return;
  153. echo "Creating container ".$container['lxc_name'] . PHP_EOL;
  154. lxc_run(['init', $container['lxc_image'], $container['lxc_name']], $rc);
  155. exec('lxc config set '.escapeshellarg($container['lxc_name']).' raw.idmap "both 1000 1000" 2>/dev/null', $devnull, $rc);
  156. exec('lxc config device add '.escapeshellarg($container['lxc_name']).' hestiasrc disk path=/home/ubuntu/source source='.SHARED_HOST_FOLDER.' 2>/dev/null', $devnull, $rc);
  157. lxc_run(['start', $container['lxc_name']], $rc);
  158. $lxc_retry = 0;
  159. do {
  160. $lxc_retry++;
  161. $cip = get_lxc_ip($container['lxc_name']);
  162. if($cip)
  163. echo "container ip: $cip" . PHP_EOL;
  164. sleep(1);
  165. } while ($lxc_retry <= LXC_TIMEOUT && filter_var($cip, FILTER_VALIDATE_IP) === false);
  166. echo "Updating container: " . $container['lxc_name'] . PHP_EOL;
  167. exec('lxc exec ' . $container['lxc_name'] . ' -- apt update', $devnull, $rc);
  168. }
  169. function hst_installer_worker($container) {
  170. $pid = pcntl_fork();
  171. if($pid > 0)
  172. return $pid;
  173. system( 'lxc exec '.$container['lxc_name'].' -- bash -c "/home/ubuntu/source/hestiacp/src/hst_autocompile.sh --hestia \"'.HST_BRANCH.'\" no"');
  174. $hver = getHestiaVersion(HST_BRANCH);
  175. echo "Install Hestia ${hver} on " . $container['lxc_name'] . PHP_EOL;
  176. echo "Args: " . $container['hst_args'] . PHP_EOL;
  177. system( 'lxc exec '.$container['lxc_name'].' -- bash -c "cd \"/home/ubuntu/source/hestiacp\"; install/'.$container['hst_installer'].
  178. ' '.$container['hst_args'].'" 2>&1 > /tmp/hst_installer_'.$container['lxc_name']);
  179. exit(0);
  180. }
  181. $worker_pool = [];
  182. foreach ($containers as $container) {
  183. check_lxc_container($container);
  184. # Is hestia installed?
  185. lxc_run('exec '.$container['lxc_name'].' -- sudo --login "v-list-sys-config"', $rc);
  186. if(isset($rc) && $rc===0)
  187. continue;
  188. $worker_pid = hst_installer_worker($container);
  189. if($worker_pid > 0)
  190. $worker_pool[] = $worker_pid;
  191. }
  192. echo count($worker_pool) . " background workers started" . PHP_EOL;
  193. # waiting for workers to finish
  194. while(count($worker_pool)) {
  195. echo "Wait for workers to finish".PHP_EOL;
  196. $child_pid = pcntl_wait($status);
  197. if($child_pid) {
  198. $worker_pos = array_search($child_pid, $worker_pool);
  199. unset($worker_pool[$worker_pos]);
  200. }
  201. }
  202. foreach ($containers as $container) {
  203. echo "Apply custom config on: ".$container['lxc_name'].PHP_EOL;
  204. # Allow running a reverse proxy in front of Hestia
  205. system( 'lxc exec '.$container['lxc_name'].' -- bash -c "sed -i \'s/session.cookie_secure] = on\$/session.cookie_secure] = off/\' /usr/local/hestia/php/etc/php-fpm.conf"');
  206. # get rid off "mesg: ttyname failed: No such device" error
  207. system( 'lxc exec '.$container['lxc_name'].' -- bash -c "sed -i -re \'s/^(mesg n)(.*)$/#\1\2/g\' /root/.profile"');
  208. # Use LE sandbox server, prevents hitting rate limits
  209. system( 'lxc exec '.$container['lxc_name'].' -- bash -c "sed -i \'/LE_STAGING/d\' /usr/local/hestia/conf/hestia.conf"');
  210. system( 'lxc exec '.$container['lxc_name'].' -- bash -c "echo \'LE_STAGING=\"YES\"\' >> /usr/local/hestia/conf/hestia.conf"');
  211. system( 'lxc exec '.$container['lxc_name'].' -- bash -c "service hestia restart"');
  212. }
  213. echo "Hestia containers configured".PHP_EOL;