1
0

make-test-containers.php 11 KB

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