DockerEnvironment.class.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * Define a DockerEnvironment
  4. * @author Administrateur PRINCIPAL
  5. * @category Plugin
  6. * @license MIT
  7. */
  8. class DockerEnvironment extends Entity{
  9. public $id;
  10. public $machine; //Machine hôte (Nombre Entier)
  11. public $client; //Client (Nombre Entier)
  12. public $domain; //Nom de domaine (Texte Long)
  13. public $port; //Port docker (Nombre Entier)
  14. public $mysqlRootPassword; //Pass. Mysql Root (Mot de passe)
  15. public $mysqlPassword; //Pass. Mysql App (Mot de passe)
  16. public $git; //Dépot git (Texte Long)
  17. public $memory = 256; //Mémoire max. (Décimal)
  18. public $cpu; //Cpu max. (Nombre Entier)
  19. public $ssl = 0; //SSL (Vrai ou Faux)
  20. public $state; //Etat (Liste classique)
  21. public $certificate; //Certificat SSL (string)
  22. public $comment; //Commentaire (wysiwyg)
  23. protected $TABLE_NAME = 'docker_environment';
  24. public $entityLabel = 'Environnement';
  25. public $fields = array(
  26. 'id' => array('type'=>'key', 'label' => 'Identifiant'),
  27. 'machine' => array('type'=>'integer', 'label' => 'Machine hôte','link'=>'plugin/docker/DockerMachine.class.php'),
  28. 'client' => array('type'=>'integer', 'label' => 'Client','link'=>'plugin/client/Client.class.php'),
  29. 'domain' => array('type'=>'textarea', 'label' => 'Nom de domaine'),
  30. 'port' => array('type'=>'integer', 'label' => 'Port docker'),
  31. 'mysqlRootPassword' => array('type'=>'password', 'label' => 'Pass. Mysql Root'),
  32. 'mysqlPassword' => array('type'=>'password', 'label' => 'Pass. Mysql App'),
  33. 'git' => array('type'=>'textarea', 'label' => 'Dépot git'),
  34. 'memory' => array('type'=>'decimal', 'label' => 'Mémoire max.'),
  35. 'cpu' => array('type'=>'integer', 'label' => 'Cpu max.'),
  36. 'ssl' => array('type'=>'boolean', 'label' => 'SSL'),
  37. 'state' => array('type'=>'list', 'label' => 'Etat'),
  38. 'certificate' => array('type'=>'string', 'label' => 'Certificat SSL'),
  39. 'comment' => array('type'=>'wysiwyg', 'label' => 'Commentaire'),
  40. );
  41. //liste des Etat possibles
  42. public static function states($key=null){
  43. $items = array(
  44. DockerEnvironment::ACTIVE => array('label'=>'Actif')
  45. );
  46. if(!isset($key)) return $items;
  47. return isset($items[$key]) ? $items[$key] : array('label'=>'Non définis');
  48. }
  49. //Colonnes indexées
  50. public $indexes = array();
  51. public function acronym(){
  52. return 'wp_';
  53. }
  54. public static function lastPort(){
  55. $lastPort = DockerEnvironment::staticQuery('SELECT max(port) as port FROM {{table}}')->fetch();
  56. return $lastPort!= false && is_numeric($lastPort['port']) ? $lastPort['port'] : 8008;
  57. }
  58. public function execute($ssh){
  59. require_once(__DIR__.SLASH.'SshClient.class.php');
  60. $clientDirectory = '/home/clients/'.$this->domain;
  61. return SshClient::format_output($ssh->execute('cd '.$clientDirectory.' && docker-compose up -d'));
  62. }
  63. public function createPma($ssh){
  64. require_once(__DIR__.SLASH.'SshClient.class.php');
  65. $logs = $ssh->execute('docker run --name myadmin -e UPLOAD_LIMIT=5G -d --network=proxy --link '.$this->domain.'_db:db -p 8011:80 phpmyadmin/phpmyadmin');
  66. return $logs;
  67. }
  68. public function removePma($ssh){
  69. require_once(__DIR__.SLASH.'SshClient.class.php');
  70. $logs = '<br>Remove container myadmin...<br>';
  71. $logs .= SshClient::format_output($ssh->execute('docker container stop myadmin && docker container rm myadmin'));
  72. return $logs;
  73. }
  74. public function removeContainer($ssh){
  75. require_once(__DIR__.SLASH.'SshClient.class.php');
  76. $clientDirectory = '/home/clients/'.$this->domain;
  77. if($clientDirectory == '/home/clients/') throw new Exception("Cant delete root !!!");
  78. if(strlen($clientDirectory) <= strlen('/home/clients/') ) throw new Exception("Cant delete root or root parent!!!");
  79. $logs = 'Remove vhost...<br>';
  80. $logs .= $ssh->execute('rm /etc/nginx/sites-available/'.$this->domain.'.conf');
  81. $logs .= $ssh->execute('rm /etc/nginx/sites-enabled/'.$this->domain.'.conf');
  82. $logs .= '<br>Reload nginx...<br>';
  83. $logs .= $ssh->execute('service nginx reload');
  84. $logs .= '<br>Remove containers '.$clientDirectory.'...<br>';
  85. $logs .= SshClient::format_output($ssh->execute('docker container stop '.$this->domain.'_wp && docker container rm '.$this->domain.'_wp'));
  86. $logs .= SshClient::format_output($ssh->execute('docker container stop '.$this->domain.'_db && docker container rm '.$this->domain.'_db'));
  87. $logs .= '<br>Remove client directory '.$clientDirectory.'...<br>';
  88. $logs .= $ssh->execute('rm '.$clientDirectory.' -r');
  89. return $logs;
  90. }
  91. public function createContainer($ssh){
  92. require_once(__DIR__.SLASH.'SshClient.class.php');
  93. $clientDirectory = '/home/clients/'.$this->domain;
  94. $logs = $ssh->execute('cp /home/client-template '.$clientDirectory.' -r');
  95. $localEnv = 'DB_CONTAINER='.$this->domain.'_db';
  96. $localEnv .="\n".'DB_NAME='.$this->domain;
  97. $localEnv .="\n".'DB_USER='.$this->domain;
  98. $localEnv .="\n".'DB_PASSWORD='.decrypt($this->mysqlPassword);
  99. $localEnv .="\n".'DB_ROOT_PASSWORD='.decrypt($this->mysqlRootPassword);
  100. $localEnv .="\n".'WP_CONTAINER='.$this->domain.'_wp';
  101. $localEnv .="\n".'WP_TABLE_PREFIX='.$this->acronym();
  102. $localEnv .="\n".'WP_PORT='.$this->port;
  103. $localEnv .="\n".'PMA_CONTAINER='.$this->domain.'_pma';
  104. $localEnv .="\n".'PMA_PORT='.($this->port+1);
  105. $localEnv .="\n".'DB_RAM_LIMIT=800M';
  106. $localEnv .="\n".'DB_RAM_RESERVATION=600M';
  107. $localEnv .="\n".'WP_RAM_LIMIT='.$this->memory.'M';
  108. $localEnv .="\n".'WP_RAM_RESERVATION=100M';
  109. $localEnv .="\n".'PMA_RAM_LIMIT=200M';
  110. $localEnv .="\n".'PMA_RAM_RESERVATION=100M';
  111. $localEnv .="\n".'DOMAIN='.$this->domain;
  112. $localEnv .="\n".'URL=https://'.$this->domain;
  113. $localEnv .="\n".'POSTFIX_CONTAINER='.$this->domain.'_postfix';
  114. $localEnv .="\n".'POSTFIX_PORT='.($this->port+2);
  115. $logs .= $ssh->send_stream($localEnv,$clientDirectory.'/.env');
  116. $vhost = 'server {
  117. listen 80;
  118. server_name '.$this->domain.';
  119. error_log '.$clientDirectory.'/log/proxy_error.log;
  120. location / {
  121. proxy_pass http://127.0.0.1:'.$this->port.';
  122. proxy_set_header Host $host;
  123. proxy_set_header Upgrade $http_upgrade;
  124. proxy_set_header Connection "upgrade";
  125. proxy_set_header X-Real-IP $remote_addr;
  126. proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
  127. proxy_set_header X-Forward-Proto http;
  128. proxy_set_header X-Nginx-Proxy true;
  129. }
  130. }
  131. server {
  132. listen 80;
  133. server_name www.'.$this->domain.';
  134. error_log '.$clientDirectory.'/log/proxy_error.log;
  135. location / {
  136. proxy_pass http://127.0.0.1:'.$this->port.';
  137. proxy_set_header Host $host;
  138. proxy_set_header Upgrade $http_upgrade;
  139. proxy_set_header Connection "upgrade";
  140. proxy_set_header X-Real-IP $remote_addr;
  141. proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
  142. proxy_set_header X-Forward-Proto http;
  143. proxy_set_header X-Nginx-Proxy true;
  144. }
  145. }';
  146. $logs .= $ssh->send_stream($vhost,'/etc/nginx/sites-available/'.$this->domain.'.conf');
  147. $logs .= $ssh->execute('ln -s /etc/nginx/sites-available/'.$this->domain.'.conf /etc/nginx/sites-enabled/'.$this->domain.'.conf');
  148. $logs .= $ssh->execute('service nginx reload');
  149. return SshClient::format_output($logs);
  150. }
  151. }
  152. ?>