UpdateImages.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace FD3;
  3. use Docker\Composer\FileFormat;
  4. use Docker\Composer\NetworkConfig;
  5. use Docker\Composer\ServiceNotFoundException;
  6. use Dotenv\Dotenv;
  7. use FD3\Services\Amqp;
  8. use FD3\Services\Api;
  9. use FD3\Services\Base;
  10. use FD3\Services\Base_log;
  11. use FD3\Services\Cablemodem;
  12. use FD3\Services\Cablemodem_cmd;
  13. use FD3\Services\Cablemodem_task;
  14. use FD3\Services\Dhcp;
  15. use FD3\Services\Dhcp_task;
  16. use FD3\Services\Freeradius;
  17. use FD3\Services\Ftth;
  18. use FD3\Services\Ftth_cmd;
  19. use FD3\Services\Ftth_task;
  20. use FD3\Services\Geoserver;
  21. use FD3\Services\Jsendpoint;
  22. use FD3\Services\Jsonep_mongo;
  23. use FD3\Services\Jsonep_mysql;
  24. use FD3\Services\Kea;
  25. use FD3\Services\Mongodb;
  26. use FD3\Services\Mysql;
  27. use FD3\Services\Nginx;
  28. use FD3\Services\Pma;
  29. use FD3\Services\Radius;
  30. use FD3\Services\Radius_task;
  31. use FD3\Services\Redis;
  32. use FD3\Services\Stats;
  33. use FD3\Services\Stats_cmd;
  34. use FD3\Services\Statsd;
  35. use FD3\Services\Supervisord;
  36. use FD3\Services\Swagger;
  37. use FD3\Services\Tftp;
  38. use FD3\Services\Minio;
  39. use Symfony\Component\Console\Command\Command;
  40. use Symfony\Component\Console\Exception\LogicException;
  41. use Symfony\Component\Console\Input\InputArgument;
  42. use Symfony\Component\Console\Input\InputInterface;
  43. use Symfony\Component\Console\Input\InputOption;
  44. use Symfony\Component\Console\Output\OutputInterface;
  45. use Symfony\Component\Console\Question\ConfirmationQuestion;
  46. use Symfony\Component\Console\Question\Question;
  47. use Symfony\Component\Yaml\Yaml;
  48. class UpdateImages extends ReleaseImages
  49. {
  50. /**
  51. * Constructor.
  52. *
  53. * @param string|null $name The name of the command; passing null means it must be set in configure()
  54. *
  55. * @throws LogicException When the command name is empty
  56. */
  57. public function __construct($name = null)
  58. {
  59. parent::__construct($name);
  60. }
  61. protected function configure()
  62. {
  63. parent::configure();
  64. $this
  65. ->setName('make:updateImages')
  66. ->setDescription('Update an install.')
  67. ->setHelp('This command allows you to update an installation...');
  68. }
  69. protected function execute(InputInterface $input, OutputInterface $output)
  70. {
  71. try {
  72. $this->colors($output);
  73. if (file_exists($this->directory . "/" . $this->_running_env)) {
  74. $output->writeln("<red>No se pudo encontrar el archivo " . $this->_running_env . " de la instalacion anterior.\N" .
  75. "ESTE ARCHIVO NO SE PUEDE TRAER DE OTRA INSTALACION.\n " .
  76. "NO SE PUEDE ACTUALIZAR.</red>");
  77. exit();
  78. }
  79. if (file_exists(realpath($this->directory) . "/" . $this->_running_log)) {
  80. $output->writeln("<red>No se pudo encontrar el archivo " . $this->_running_log . " de la instalacion anterior.\n" .
  81. "ESTE ARCHIVO NO SE PUEDE TRAER DE OTRA INSTALACION.\n " .
  82. "NO SE PUEDE ACTUALIZAR.</red>");
  83. exit();
  84. }
  85. $this->directory = $input->getArgument('dir');
  86. if (!is_dir($this->directory)) {
  87. mkdir($this->directory, 0777, true);
  88. }
  89. $this->directory = realpath($this->directory) . "/";
  90. $this->checkInstalledModules();
  91. // seteo todos los modulos a instalar por defecto
  92. $modules = $this->selectInstallModules($input, $output, true);
  93. $this->AddModules(explode(",", $modules));
  94. $this->setParametersFormFile($input);
  95. $this->_domain = $input->getOption("domain");
  96. $this->_client = $input->getOption("client");
  97. $this->_develop = $input->getOption("develop") === "true" || $input->getOption("develop") === "1";
  98. $this->_network_ip = $input->getOption("ip_network_begin");
  99. if (!$this->_client) {
  100. $this->_client = basename(realpath($this->directory));
  101. }
  102. $version = $input->getOption("branch");
  103. $internal_user_id = 2;
  104. $this->_ansible_vars["DOMAIN"] = $this->_domain;
  105. $this->_ansible_vars["CLIENT"] = $this->_client;
  106. $this->_ansible_vars["CMD_USERNAME"] = $this->_user_system['users'][$internal_user_id]['user'];
  107. $this->_ansible_vars["CMD_PASSWORD"] = $this->_user_system['users'][$internal_user_id]['password'];
  108. $this->_ansible_vars["ENV_LIST"] = "prod,dev,test";
  109. $this->_ansible_vars["API_CIDR"] = "172.16.0.0/16";
  110. $this->_ansible_vars["IK_SUBRED"] = "200.50.160.0/21";
  111. $this->_ansible_vars["MYSQL_ROOT_PASSWORD"] = $this->_mysql_root_pass;
  112. $this->_add_nginx_links = true;
  113. $dObj = new DevOps\FileSystem(realpath($this->directory));
  114. $dObj->dirExists()->realpath();
  115. $this->_dObj = $dObj;
  116. $path = $dObj->dirExists()->realpath()->getPath();
  117. // hosts file not exists, then build it
  118. if (file_exists($path . "/hosts") === false) {
  119. copy(getcwd() . "/hosts", $path . "/hosts");
  120. $this->public_ip = $input->getOption('public_ip');
  121. while (!filter_var($this->public_ip, FILTER_VALIDATE_IP)) {
  122. $helper = $this->getHelper('question');
  123. $question = new Question('IP pública del cliente para acceder a Flowdat ? (Default: 127.0.0.1)', '127.0.0.1');
  124. $this->public_ip = $helper->ask($input, $output, $question);
  125. }
  126. $this->addHosts();
  127. }
  128. // agrego las opciones del input a la configuracion _modues
  129. $this->addConfigOptions($input, $version);
  130. // creo el archivo de log de como se ejecuto
  131. $this->createFileRunning($input, $output);
  132. // cargo las fuentes a clonar
  133. $this->createGitClone($input);
  134. // creo el archivo docker-compose.yml
  135. $this->getDockerComposer($version, "docker.infra.flowdat.com/");
  136. // escribo el archivo de host
  137. $this->writeHostsFile($input->getOption("host_ip"));
  138. // escribo el archivo con las variables de entorno
  139. $this->writeHostEnv();
  140. // escribo los archivo oauth
  141. $this->writeOAUTH();
  142. // escribo un archivo con variables para ansible
  143. $this->writeEnvVariables();
  144. $dObj->file('install.yml')->content(
  145. yaml::dump(array(
  146. "install_dir" => realpath($this->directory),
  147. 'docker_apps' => "base," . implode(",", $this->_ansible_vars),
  148. 'domain' => $this->_domain,
  149. )
  150. )
  151. );
  152. $dObj->file('ansible.cfg')->content(
  153. "[defaults]\n" .
  154. "inventory=inventory.ini\n" .
  155. "gather_timeout=30\n"
  156. );
  157. // copio el playbook
  158. copy(getcwd() . "/playbookUpdateSupport.yml", $path . "/playbook.yml");
  159. // KEA selected for install, copy the file get_kea_files.sh
  160. // otherwise delete the file if exists in the path
  161. $keaFile = "{$path}/get_kea_files.sh";
  162. if ($this->isModuleAvailable(new Kea())) {
  163. copy(getcwd() . "/get_kea_files.sh", $keaFile);
  164. } elseif (file_exists($keaFile) === true) {
  165. unlink($keaFile);
  166. }
  167. copy(getcwd() . "/get_supervisord_files.sh", $path . "/get_supervisord_files.sh");
  168. } catch (\Throwable $t) {
  169. $output->writeln($t->getTraceAsString());
  170. } finally {
  171. $this->_dObj = null;
  172. $this->_modules = null;
  173. }
  174. }
  175. /**
  176. * Marca los modulos que ya se encuentran instalados.
  177. */
  178. protected function checkInstalledModules()
  179. {
  180. $lines = file($this->directory . "/" . $this->_running_env, FILE_IGNORE_NEW_LINES);
  181. foreach ($lines as $line) {
  182. if (strpos($line, "MODULES_INSTALL") === 0) {
  183. $modules = explode(",", explode("=", $line)[1]);
  184. foreach ($modules as $mod) {
  185. foreach ($this->_modules_all as $name => $value) {
  186. if ($name == $mod || $name == "base") {
  187. $value["INSTALLED"] = true;
  188. $this->_modules_all[$name] = $value;
  189. }
  190. }
  191. }
  192. }
  193. }
  194. }
  195. /**
  196. * Crea el archivo modulo.oauth.env
  197. */
  198. protected function writeOAUTH()
  199. {
  200. $oautModules = "";
  201. $arrNotFound = [];
  202. $found = "";
  203. foreach ($this->_modules as $nameApp => $app) {
  204. if (isset($app['OAUTH']) && $app['OAUTH']) {
  205. if (!file_exists($this->directory . $nameApp . ".oauth.env")) {
  206. $oautModules = $oautModules . $nameApp . ",";
  207. $arrNotFound[] = $nameApp;
  208. } else if ($found == "") {
  209. $found = $nameApp;
  210. $oautModules = $oautModules . $nameApp . ",";
  211. } else {
  212. $oautModules = $oautModules . $nameApp . ",";
  213. }
  214. }
  215. }
  216. // copio los archivos oauth de los nuevos modulos
  217. if (count($arrNotFound) > 0) {
  218. $content = file_get_contents($this->directory . $found . ".oauth.env");
  219. foreach ($arrNotFound as $nameApp) {
  220. $this->_dObj->file($nameApp . ".oauth.env")->content($content);
  221. }
  222. }
  223. $this->_ansible_vars["MODULES_INSTALL"] = "base," . substr($oautModules, 0, strlen($oautModules) - 1);
  224. }
  225. }