setName('make:updateImages')
->setDescription('Update an install.')
->setHelp('This command allows you to update an installation...');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
try {
$this->colors($output);
if (file_exists($this->directory . "/" . $this->_running_env)) {
$output->writeln("No se pudo encontrar el archivo " . $this->_running_env . " de la instalacion anterior.\N" .
"ESTE ARCHIVO NO SE PUEDE TRAER DE OTRA INSTALACION.\n " .
"NO SE PUEDE ACTUALIZAR.");
exit();
}
if (file_exists(realpath($this->directory) . "/" . $this->_running_log)) {
$output->writeln("No se pudo encontrar el archivo " . $this->_running_log . " de la instalacion anterior.\n" .
"ESTE ARCHIVO NO SE PUEDE TRAER DE OTRA INSTALACION.\n " .
"NO SE PUEDE ACTUALIZAR.");
exit();
}
$this->directory = $input->getArgument('dir');
if (!is_dir($this->directory)) {
mkdir($this->directory, 0777, true);
}
$this->directory = realpath($this->directory) . "/";
$this->checkInstalledModules();
// seteo todos los modulos a instalar por defecto
$modules = $this->selectInstallModules($input, $output, true);
$this->AddModules(explode(",", $modules));
$this->setParametersFormFile($input);
$this->_domain = $input->getOption("domain");
$this->_client = $input->getOption("client");
$this->_develop = $input->getOption("develop") === "true" || $input->getOption("develop") === "1";
$this->_network_ip = $input->getOption("ip_network_begin");
if (!$this->_client) {
$this->_client = basename(realpath($this->directory));
}
$version = $input->getOption("branch");
$internal_user_id = 2;
$this->_ansible_vars["DOMAIN"] = $this->_domain;
$this->_ansible_vars["CLIENT"] = $this->_client;
$this->_ansible_vars["CMD_USERNAME"] = $this->_user_system['users'][$internal_user_id]['user'];
$this->_ansible_vars["CMD_PASSWORD"] = $this->_user_system['users'][$internal_user_id]['password'];
$this->_ansible_vars["ENV_LIST"] = "prod,dev,test";
$this->_ansible_vars["API_CIDR"] = "172.16.0.0/16";
$this->_ansible_vars["IK_SUBRED"] = "200.50.160.0/21";
$this->_ansible_vars["MYSQL_ROOT_PASSWORD"] = $this->_mysql_root_pass;
$this->_add_nginx_links = true;
$dObj = new DevOps\FileSystem(realpath($this->directory));
$dObj->dirExists()->realpath();
$this->_dObj = $dObj;
$path = $dObj->dirExists()->realpath()->getPath();
// hosts file not exists, then build it
if (file_exists($path . "/hosts") === false) {
copy(getcwd() . "/hosts", $path . "/hosts");
$this->public_ip = $input->getOption('public_ip');
while (!filter_var($this->public_ip, FILTER_VALIDATE_IP)) {
$helper = $this->getHelper('question');
$question = new Question('IP pública del cliente para acceder a Flowdat ? (Default: 127.0.0.1)', '127.0.0.1');
$this->public_ip = $helper->ask($input, $output, $question);
}
$this->addHosts();
}
// agrego las opciones del input a la configuracion _modues
$this->addConfigOptions($input, $version);
// creo el archivo de log de como se ejecuto
$this->createFileRunning($input, $output);
// cargo las fuentes a clonar
$this->createGitClone($input);
// creo el archivo docker-compose.yml
$this->getDockerComposer($version, "docker.infra.flowdat.com/");
// escribo el archivo de host
$this->writeHostsFile($input->getOption("host_ip"));
// escribo el archivo con las variables de entorno
$this->writeHostEnv();
// escribo los archivo oauth
$this->writeOAUTH();
// escribo un archivo con variables para ansible
$this->writeEnvVariables();
$dObj->file('install.yml')->content(
yaml::dump(array(
"install_dir" => realpath($this->directory),
'docker_apps' => "base," . implode(",", $this->_ansible_vars),
'domain' => $this->_domain,
)
)
);
$dObj->file('ansible.cfg')->content(
"[defaults]\n" .
"inventory=inventory.ini\n" .
"gather_timeout=30\n"
);
// copio el playbook
copy(getcwd() . "/playbookUpdateSupport.yml", $path . "/playbook.yml");
// KEA selected for install, copy the file get_kea_files.sh
// otherwise delete the file if exists in the path
$keaFile = "{$path}/get_kea_files.sh";
if ($this->isModuleAvailable(new Kea())) {
copy(getcwd() . "/get_kea_files.sh", $keaFile);
} elseif (file_exists($keaFile) === true) {
unlink($keaFile);
}
copy(getcwd() . "/get_supervisord_files.sh", $path . "/get_supervisord_files.sh");
} catch (\Throwable $t) {
$output->writeln($t->getTraceAsString());
} finally {
$this->_dObj = null;
$this->_modules = null;
}
}
/**
* Marca los modulos que ya se encuentran instalados.
*/
protected function checkInstalledModules()
{
$lines = file($this->directory . "/" . $this->_running_env, FILE_IGNORE_NEW_LINES);
foreach ($lines as $line) {
if (strpos($line, "MODULES_INSTALL") === 0) {
$modules = explode(",", explode("=", $line)[1]);
foreach ($modules as $mod) {
foreach ($this->_modules_all as $name => $value) {
if ($name == $mod || $name == "base") {
$value["INSTALLED"] = true;
$this->_modules_all[$name] = $value;
}
}
}
}
}
}
/**
* Crea el archivo modulo.oauth.env
*/
protected function writeOAUTH()
{
$oautModules = "";
$arrNotFound = [];
$found = "";
foreach ($this->_modules as $nameApp => $app) {
if (isset($app['OAUTH']) && $app['OAUTH']) {
if (!file_exists($this->directory . $nameApp . ".oauth.env")) {
$oautModules = $oautModules . $nameApp . ",";
$arrNotFound[] = $nameApp;
} else if ($found == "") {
$found = $nameApp;
$oautModules = $oautModules . $nameApp . ",";
} else {
$oautModules = $oautModules . $nameApp . ",";
}
}
}
// copio los archivos oauth de los nuevos modulos
if (count($arrNotFound) > 0) {
$content = file_get_contents($this->directory . $found . ".oauth.env");
foreach ($arrNotFound as $nameApp) {
$this->_dObj->file($nameApp . ".oauth.env")->content($content);
}
}
$this->_ansible_vars["MODULES_INSTALL"] = "base," . substr($oautModules, 0, strlen($oautModules) - 1);
}
}