make-test-containers.php 12 KB

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