_running_log = "running.log"; $this->_mysql_user = "iksop"; $this->_mysql_pass = "235r2342gtfsw"; $this->_mysql_root_pass = "235r2342gtfsw"; $this->_mysql_max_connections = 10000; $this->_docker_restart_default = "on-failure:10"; $this->_user_system = ['users' => [ ['user' => 'admin', 'password' => 'admin', 'tenancy' => 1, 'email' => 'soporte@interlink.com.ar', 'extra' => '--super-admin '], ['user' => 'iksop', 'password' => 'gran5pe', 'tenancy' => 2, 'email' => 'admin@interlink.com.ar', 'extra' => ''], ['user' => 'interno', 'password' => 'gran5pe1nterno', 'tenancy' => 2, 'email' => 'admin@interlink.com.ar', 'extra' => ''] ]]; $this->_modules = array(); $this->_ansible_vars = array(); $this->_modules_all = array( "base" => array( 'HOST_ENV' => true, "VAR_ENV" => array( 'VIRTUAL_HOST' => '', 'HTTPS_METHOD' => 'nohttps', 'AMQP_KEY' => 'base' ), 'OAUTH' => false, 'MODULE_INSTALL' => true ), "ftth" => array( 'HOST_ENV' => true, "VAR_ENV" => array( 'VIRTUAL_HOST' => '', 'HTTPS_METHOD' => 'nohttps', 'AMQP_KEY' => 'ftth' ), 'OAUTH' => true, 'MODULE_INSTALL' => true ), "mapas" => array( 'HOST_ENV' => true, "VAR_ENV" => array( 'VIRTUAL_HOST' => '', 'HTTPS_METHOD' => 'nohttps', ), 'OAUTH' => true, 'MODULE_INSTALL' => true ), "radius" => array( 'HOST_ENV' => true, "VAR_ENV" => array( 'VIRTUAL_HOST' => '', 'HTTPS_METHOD' => 'nohttps', ), 'OAUTH' => true, 'MODULE_INSTALL' => true ), "stats" => array( 'HOST_ENV' => true, "VAR_ENV" => array( 'VIRTUAL_HOST' => '', 'HTTPS_METHOD' => 'nohttps', 'AMQP_KEY' => 'stats' ), 'OAUTH' => true, 'MODULE_INSTALL' => true ), "cablemodem" => array( 'HOST_ENV' => true, "VAR_ENV" => array( 'VIRTUAL_HOST' => '', 'HTTPS_METHOD' => 'nohttps', 'AMQP_KEY' => 'cablemodem' ), 'OAUTH' => true, 'MODULE_INSTALL' => true ), "dhcp" => array( 'HOST_ENV' => true, "VAR_ENV" => array( 'VIRTUAL_HOST' => '', 'HTTPS_METHOD' => 'nohttps', ), 'OAUTH' => true, 'MODULE_INSTALL' => true ), "grafana" => array( 'HOST_ENV' => true, "VAR_ENV" => array( 'VIRTUAL_HOST' => '', 'HTTPS_METHOD' => 'nohttps', ), 'OAUTH' => false, 'MODULE_INSTALL' => true ), "pma" => array( 'HOST_ENV' => true, "VAR_ENV" => array( 'VIRTUAL_HOST' => '', ), 'OAUTH' => false, 'MODULE_INSTALL' => false ), "extra" => array( 'HOST_ENV' => false, 'OAUTH' => false, 'MODULE_INSTALL' => true ), "geoserver" => array( 'HOST_ENV' => true, "VAR_ENV" => array( 'VIRTUAL_HOST' => '', 'HTTPS_METHOD' => 'nohttps', ), 'OAUTH' => false, 'MODULE_INSTALL' => true ), ); } protected function configure() { $this ->setName('make:install') ->setDescription('Create a new install.') ->setHelp('This command allows you to create a new installation...') ->addArgument('dir', InputArgument::REQUIRED, 'The directory where to create the installation.') ->addOption('base-repo', null, InputOption::VALUE_REQUIRED, 'Git clone Url fot the app Base', "git@bitbucket.org:ikflowdat/base.git") ->addOption('base-ref', null, InputOption::VALUE_REQUIRED, 'Git reference (branch or tag) to clone the Base app', "master") ->addOption('base-build', null, InputOption::VALUE_REQUIRED, 'Generate image build', "false") ->addOption('ftth-repo', null, InputOption::VALUE_REQUIRED, 'Git clone Url fot the app FTTH', "git@bitbucket.org:ikflowdat/ftth.git") ->addOption('ftth-ref', null, InputOption::VALUE_REQUIRED, 'Git reference (branch or tag) to clone the Ftth ', "master") ->addOption('ftth-build', null, InputOption::VALUE_REQUIRED, 'Generate image build', "false") ->addOption('mapas-repo', null, InputOption::VALUE_REQUIRED, 'Git clone Url fot the app Mapas', "git@bitbucket.org:ikflowdat/mapas.git") ->addOption('mapas-ref', null, InputOption::VALUE_REQUIRED, 'Git reference (branch or tag) to clone the Mapas app', "master") ->addOption('mapas-build', null, InputOption::VALUE_REQUIRED, 'Generate image build', "false") ->addOption('radius-repo', null, InputOption::VALUE_REQUIRED, 'Git clone Url fot the app radius', "git@bitbucket.org:ikflowdat/radius.git") ->addOption('radius-ref', null, InputOption::VALUE_REQUIRED, 'Git reference (branch or tag) to clone the radius app', "master") ->addOption('radius-build', null, InputOption::VALUE_REQUIRED, 'Generate image build', "false") ->addOption('stats-repo', null, InputOption::VALUE_REQUIRED, 'Git clone Url fot the app Base', "git@bitbucket.org:ikflowdat/stats.git") ->addOption('stats-ref', null, InputOption::VALUE_REQUIRED, 'Git reference (branch or tag) to clone the Stats app', "master") ->addOption('stats-build', null, InputOption::VALUE_REQUIRED, 'Generate image build', "false") ->addOption('cablemodem-repo', null, InputOption::VALUE_REQUIRED, 'Git clone Url for the app Cablemodem', "git@bitbucket.org:ikflowdat/cablemodem.git") ->addOption('cablemodem-ref', null, InputOption::VALUE_REQUIRED, 'Git reference (branch or tag) to clone the Cablemodem files and apps', "master") ->addOption('cablemodem-build', null, InputOption::VALUE_REQUIRED, 'Generate image build', "false") ->addOption('dhcp-repo', null, InputOption::VALUE_REQUIRED, 'Git clone Url for the app DHCP', "git@bitbucket.org:ikflowdat/dhcp.git") ->addOption('dhcp-ref', null, InputOption::VALUE_REQUIRED, 'Git reference (branch or tag) to clone the DHCP files and apps', "master") ->addOption('dhcp-build', null, InputOption::VALUE_REQUIRED, 'Generate image build', "false") ->addOption('extra-repo', null, InputOption::VALUE_REQUIRED, 'Git clone Url fot the app Base', "git@bitbucket.org:ikflowdat/extra.git") ->addOption('extra-ref', null, InputOption::VALUE_REQUIRED, 'Git reference (branch or tag) to clone the Extra files and apps', "master") ->addOption('extra-build', null, InputOption::VALUE_REQUIRED, 'Generate image build', "false") ->addOption('host-ip', null, InputOption::VALUE_REQUIRED, 'Ip of the runnning host to be added to the /etc/hosts file, eventually', "127.0.1.1") ->addOption('domain', null, InputOption::VALUE_REQUIRED, 'Domain where the flowdat will be installed', "flowdat.com") ->addOption('client', null, InputOption::VALUE_REQUIRED, 'Client name, if is not provided uses, the dirname of the installation', false) ->addOption('modules', null, InputOption::VALUE_REQUIRED, 'List of modules to install separated by coma.', "all") ->addOption('inventory', null, InputOption::VALUE_REQUIRED, 'Write inventory.ini by default.', true) ->addOption('docker-tag', null, InputOption::VALUE_REQUIRED, 'Docker tag to be used', "latest") ->addOption('all-ref', null, InputOption::VALUE_REQUIRED, 'Git reference (branch or tag) to clone all app', "") ->addOption('general-version', null, InputOption::VALUE_REQUIRED, 'Set options all-ref and docker-tag with this value. Ej. 0.1.1', ""); } protected function execute(InputInterface $input, OutputInterface $output) { try { if (strlen(trim($input->getOption("general-version"))) > 0) { $input->setOption('all-ref', 'v' . $input->getOption('general-version')); $input->setOption('docker-tag', $input->getOption('general-version')); } if (strlen(trim($input->getOption("all-ref"))) > 0) { $input->setOption('base-ref', $input->getOption('all-ref')); $input->setOption('ftth-ref', $input->getOption('all-ref')); $input->setOption('mapas-ref', $input->getOption('all-ref')); $input->setOption('radius-ref', $input->getOption('all-ref')); $input->setOption('stats-ref', $input->getOption('all-ref')); $input->setOption('cablemodem-ref', $input->getOption('all-ref')); $input->setOption('dhcp-ref', $input->getOption('all-ref')); $input->setOption('extra-ref', $input->getOption('all-ref')); } if (strtolower($input->getOption("modules")) == "all") { // seteo todos los modulos a instalar por defecto $modules = ""; foreach ($this->_modules_all as $name => $value) { if (isset($value['MODULE_INSTALL']) && $value['MODULE_INSTALL']) { $modules = $modules . $name . ","; } } $modules = substr($modules, 0, strlen($modules) - 1); $input->setOption("modules", $modules); } $this->AddModules(explode(",", $input->getOption("modules"))); $this->directory = $input->getArgument('dir'); if (!is_dir($this->directory)) { mkdir($this->directory, 0777, true); } if (file_exists($this->directory . "/" . $this->_running_log)) { $helper = $this->getHelper('question'); $question = new ConfirmationQuestion('The ' . realpath($this->directory) . "/" . $this->_running_log . ' file exist. Read file or take parameters? (Y/n)', true); if ($helper->ask($input, $output, $question)) { $this->setParametersFormFile($input); } } $this->_domain = $input->getOption("domain"); $this->_client = $input->getOption("client"); if (!$this->_client) { $this->_client = basename(realpath($this->directory)); } $docker_tag = $input->getOption("docker-tag"); $this->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'][$this->internal_user_id]['user']; $this->_ansible_vars["CMD_PASSWORD"] = $this->_user_system['users'][$this->internal_user_id]['password']; $dObj = new DevOps\FileSystem(realpath($this->directory)); $dObj->dirExists()->realpath(); $this->_dObj = $dObj; // agrego las opciones del input a la configuracion _modues $this->addConfigOptions($input); // creo el archivo de log de como se ejecuto $this->createFileRunning($input, $output); // cargo las fuentes a clonar $this->createGitClone(); // creo el archivo docker-compose.yml $this->getDockerComposer($docker_tag, "host.env", "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(); // escribo un archivo con los usuarios del sistema $this->writeUserSystem(); $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" ); // copio el playbook copy(getcwd() . "/playbook.yml", $dObj->dirExists()->realpath()->getPath() . "/playbook.yml"); // copio el script de base de datos inicial copy(getcwd() . "/mysql_scripts.sql", $dObj->dirExists()->realpath()->getPath() . "/mysql_scripts.sql"); // copio el archivo que contiene los usuarios del sistema copy(getcwd() . "/user_system.json", $dObj->dirExists()->realpath()->getPath() . "/user_system.json"); // copio script mysql schema freeradius copy(getcwd() . "/mysql/freeradius/schema.sql", $dObj->dirExists()->realpath()->getPath() . "/freeradius_schema.sql"); // copio el docker-compose.service copy(getcwd() . "/docker-compose.service", $dObj->dirExists()->realpath()->getPath() . "/docker-compose.service"); // copio el archivo con variables de entorno de grafana copy(getcwd() . "/grafana.env", $dObj->dirExists()->realpath()->getPath() . "/grafana.env"); } catch (\Throwable $error) { throw $error; } finally { $this->_dObj = null; $this->_modules = null; } } /** * @return array Retorna un array con los host como key el dominio como valor. */ function getHostEnv() { $resp = array(); foreach ($this->_modules as $key => $values) { if ($values['HOST_ENV']) { $resp ["HOST_" . strtoupper($key)] = $this->getDomain($key); } } return $resp; } function getHostConfig($config_ip) { $resp = array(); foreach ($this->_modules as $key => $values) { if ($values['HOST_ENV']) { $resp [$this->getDomain($key)] = $config_ip; } } return $resp; } function addBase(FileFormat2 $composer, $config = array()) { $module = "base"; $version = "latest"; $registry = ""; $host_env_file = ""; extract($config); $composer ->addService($module) ->image($registry . "fd3/$module:" . $version) ->restart($this->_docker_restart_default) ->addLinks("mysql:mysql") ->addEnv_file("running.env") ->addEnv_file($host_env_file) ->addEnv_file($module . "." . $host_env_file) ->addVolumes("./$module/", "/opt/$module"); $this->addBuild($module, $composer); $this->writeVariablesEnviroment($module . "." . $host_env_file, $module); } function addFtth(FileFormat2 $composer, $config = array()) { $module = "ftth"; $version = "latest"; $registry = ""; $host_env_file = ""; extract($config); $composer ->addService($module) ->image($registry . "fd3/$module:" . $version) ->restart($this->_docker_restart_default) ->addLinks("mysql:mysql") ->addLinks("base") ->addLinks("base", $this->getDomain("base")) ->addEnv_file("running.env") ->addEnv_file($host_env_file) ->addEnv_file($module . "." . $host_env_file) ->addEnv_file($module . ".oauth.env") ->addVolumes("./$module/", "/opt/" . $module); $this->addBuild($module, $composer); $this->writeVariablesEnviroment($module . "." . $host_env_file, $module); } function addStats(FileFormat2 $composer, $config = array()) { $module = "stats"; $version = "latest"; $registry = ""; $host_env_file = ""; extract($config); $composer ->addService($module) ->image($registry . "fd3/$module:" . $version) ->restart($this->_docker_restart_default) ->addLinks("mysql:mysql") ->addLinks("jsonep_mysql:jsonep_mysql") ->addLinks("base") ->addLinks("base", $this->getDomain("base")) ->addVolumes("./$module/", "/opt/$module") ->addEnv_file("running.env") ->addEnv_file($host_env_file) ->addEnv_file($module . "." . $host_env_file) ->addEnv_file("$module.oauth.env"); $this->addBuild($module, $composer); $this->writeVariablesEnviroment($module . "." . $host_env_file, $module); } function addMapas(FileFormat2 $composer, $config = array()) { $module = "mapas"; $version = "latest"; $registry = ""; $host_env_file = ""; extract($config); $composer ->addService($module) ->image($registry . "fd3/$module:" . $version) ->restart($this->_docker_restart_default) ->addLinks("mysql:mysql") ->addLinks("base") ->addLinks("base", $this->getDomain("base")) ->addEnv_file("running.env") ->addEnv_file($host_env_file) ->addEnv_file($module . "." . $host_env_file) ->addEnv_file("$module.oauth.env") ->addVolumes("./$module/", "/opt/$module") ->addVolumes("./$module/web/uploads", "/opt/$module/web/uploads"); $this->addBuild($module, $composer); $this->writeVariablesEnviroment($module . "." . $host_env_file, $module); } function addCablemodem(FileFormat2 $composer, $config = array()) { $module = "cablemodem"; $version = "latest"; $registry = ""; $host_env_file = ""; extract($config); $composer ->addService($module) ->image($registry . "fd3/$module:" . $version) ->restart($this->_docker_restart_default) ->addLinks("mysql:mysql") ->addLinks("base") ->addLinks("base", $this->getDomain("base")) ->addEnv_file("running.env") ->addEnv_file($host_env_file) ->addEnv_file($module . "." . $host_env_file) ->addEnv_file("$module.oauth.env") ->addVolumes("./$module/", "/opt/$module") ->addVolumes("./$module/web/uploads", "/opt/$module/web/uploads"); $this->addBuild($module, $composer); $this->writeVariablesEnviroment($module . "." . $host_env_file, $module); } /** * atftp, tod * * @param FileFormat2 $composer * @param array $config */ function addTftp(FileFormat2 $composer, $config = array()) { $version = "latest"; $registry = ""; $host_env_file = ""; extract($config); $composer ->addService("tftp") ->build("extra/tftp/") ->image($registry . "fd3/tftp:" . $version) ->addEnv_file($host_env_file) ->restart($this->_docker_restart_default) ->addPorts(69, 69); } function addRadius(FileFormat2 $composer, $config = array()) { $module = "radius"; $version = "latest"; $registry = ""; $host_env_file = ""; extract($config); $composer ->addService($module) ->image($registry . "fd3/$module:" . $version) ->restart($this->_docker_restart_default) ->addLinks("mysql:mysql") ->addLinks("base") ->addLinks("base", $this->getDomain("base")) ->addEnv_file("running.env") ->addEnv_file($host_env_file) ->addEnv_file($module . "." . $host_env_file) ->addEnv_file("$module.oauth.env") ->addVolumes("./$module/", "/opt/$module"); $this->addBuild($module, $composer); $this->writeVariablesEnviroment($module . "." . $host_env_file, $module); } function addDHCP(FileFormat2 $composer, $config = array()) { $module = "dhcp"; $version = "latest"; $registry = ""; $host_env_file = ""; extract($config); $composer ->addService($module) ->image($registry . "fd3/$module:" . $version) ->restart($this->_docker_restart_default) ->addLinks("mysql:mysql") ->addLinks("base") ->addLinks("base", $this->getDomain("base")) ->addEnv_file("running.env") ->addEnv_file($host_env_file) ->addEnv_file($module . "." . $host_env_file) ->addEnv_file("$module.oauth.env") ->addVolumes("./$module/", "/opt/$module"); $this->addBuild($module, $composer); $this->writeVariablesEnviroment($module . "." . $host_env_file, $module); } function addNginx(FileFormat2 $composer, $config = array()) { $version = "latest"; $registry = ""; $host_env_file = ""; extract($config); $composer ->addService("nginx") ->build("extra/nginx/") ->image($registry . "fd3/nginx:" . $version) ->addEnv_file($host_env_file) ->restart($this->_docker_restart_default) ->addPorts(80, 80) ->addPorts(443, 443) ->addVolumes("/var/run/docker.sock", "/tmp/docker.sock:ro") ->addVolumes("./extra/nginx/certs", "/etc/nginx/certs:ro") ->addVolumes("./extra/nginx/conf.d", "/etc/nginx/conf.d") ->addVolumes("./extra/nginx/share", "/usr/share/nginx/html"); } function addMongDb(FileFormat2 $composer, $config = array()) { $mongdb_version = "3.4"; extract($config); $composer ->addService("mongodb") ->image("mongo:" . $mongdb_version) ->addVolumes("./mongodb", "/data/db"); } /** * @param FileFormat2 $composer * @param array $config */ function addSupervisord(FileFormat2 $composer, $config = array()) { $version = "latest"; $registry = ""; $host_env_file = ""; extract($config); $composer ->addService("supervisord") ->build("./extra/supervisord") ->image($registry . "fd3/supervisord:$version") ->privileged(true) ->restart($this->_docker_restart_default) ->addLinks("geoserver") ->addLinks("nginx", $this->getDomain("geoserver")) ->addVolumes("./extra/supervisord/", "/etc/supervisord/") ->addVolumes("./extra/supervisord/var/", "/var/log/supervisor/") ->addVolumes("./extra/supervisord/sshd_config", "/etc/ssh/sshd_config") ->addVolumes("./extra/supervisord/bin/fiberhome", "/usr/bin/fiberhome") ->addVolumes("./extra/supervisord/bin/fiberlink", "/usr/bin/fiberlink") ->addVolumes("./extra/supervisord/bin/huawei", "/usr/bin/huawei") ->addVolumes("./stats", "/opt/stats") ->addEnv_file("running.env") ->addEnv_file($host_env_file) ->addEnv_file("stats.host.env") ->addEnv_file("stats.oauth.env"); } function addMySql(FileFormat2 $composer, $config = array()) { $module = "mysql"; $version = "latest"; $registry = ""; $host_env_file = ""; extract($config); $composer ->addService($module) ->image($registry . "fd3/$module:" . $version) ->build("./extra/mysql") ->addEnv_file($module . "." . $host_env_file) ->addVolumes("./mysql/", "/var/lib/mysql/") ->addVolumes("./extra/mysql/fd3.conf", "/etc/mysql/conf.d/fd3.conf"); $this->writeVariablesEnviroment($module . "." . $host_env_file, $module, array( "MYSQL_ROOT_PASSWORD" => $this->_mysql_root_pass, "MYSQL_USER" => $this->_mysql_user, "MYSQL_PASSWORD" => $this->_mysql_pass, "MYSQL_MAX_CONNECTIONS" => $this->_mysql_max_connections, )); } function addRedis(FileFormat2 $composer, $config = array(), $flavor = "dev") { $composer ->addService("redis") ->image("redis:latest") ->command("redis-server --appendonly yes") ->restart($this->_docker_restart_default); } function addGenieACS(FileFormat2 $composer, $config = array(), $flavor = "dev") { $composer ->addService("genieacs-cwmp") ->build("extra/genieacs/genieacs-cwmp") ->addLinks("mongodb") ->addLinks("redis") ->addPorts("7547", "7547") ->restart($this->_docker_restart_default); $composer ->addService("genieacs-nbi") ->build("extra/genieacs/genieacs-nbi") ->addLinks("mongodb") ->addLinks("redis") ->addPorts("7557", "7557") ->restart($this->_docker_restart_default); $composer ->addService("genieacs-fs") ->build("extra/genieacs/genieacs-fs") ->addLinks("mongodb") ->addLinks("redis") ->addPorts("7567", "7567") ->restart($this->_docker_restart_default); $composer ->addService("genieacs-gui") ->build("extra/genieacs/genieacs-gui") ->addLinks("genieacs-nbi") ->addPorts("3001", "3000") ->restart($this->_docker_restart_default); } function addFreeradius(FileFormat2 $composer, $config = array(), $flavor = "dev") { $module = "freeradius"; $version = "latest"; $registry = ""; $host_env_file = ""; extract($config); $composer ->addService("freeradius") ->image($registry . "fd3/freeradius:" . $version) ->addLinks("mysql") ->addPorts("1812:1812/udp") ->addPorts("1813:1813/udp") ->addPorts("3799:3799/udp") ->addEnv_file("mysql." . $host_env_file) ->addEnv_file($module . "." . $host_env_file) ->addVolumes("./extra/freeradius/wsdl/code/", "/var/www/html/") ->addVolumes("./extra/freeradius/etc/cron.d", "/etc/cron.d") ->addVolumes("./extra/freeradius/etc/freeradius", "/etc/freeradius") ->addVolumes("./extra/freeradius/etc/supervisor/conf.d", "/etc/supervisor/conf.d"); $this->writeVariablesEnviroment($module . "." . $host_env_file, $module, array( "MYSQL_HOST" => "mysql", )); } function addPma(FileFormat2 $composer, $config = array()) { $module = "pma"; $host_env_file = ""; extract($config); $composer ->addService("phpmyadmin") ->image("phpmyadmin/phpmyadmin") ->restart($this->_docker_restart_default) ->addPorts(8080, 80) ->addLinks("mysql", "db") ->addEnv_file("mysql." . $host_env_file) ->addEnv_file($module . "." . $host_env_file); $this->writeVariablesEnviroment($module . "." . $host_env_file, $module); } function addGrafana(FileFormat2 $composer, $config = array()) { $module = "grafana"; $host_env_file = ""; extract($config); $composer ->addService($module) ->image("grafana/grafana:3.1.1") ->addLinks("mysql") ->restart($this->_docker_restart_default) ->addEnv_file("running.env") ->addEnv_file("grafana.env") ->addVolumes("./extra/statsd/grafana/lib", "/var/lib/grafana"); $this->addJsonEndPoints($composer, $config); } function addJsonEndPoints(FileFormat2 $composer, $config = array()) { $version = "latest"; $registry = ""; $host_env_file = ""; extract($config); $composer ->addService("statsd") ->build("./extra/statsd/statsd") ->image($registry . "fd3/statsd:$version") ->addPorts("8125", "8125/udp") ->addLinks("mysql") ->addLinks("mongodb") ->restart($this->_docker_restart_default) ->addVolumes("./extra/statsd/statsd/statsd.config.js", "/opt/config/statsd.config.js"); $composer ->addService("jsendpoint") ->build("./extra/statsd/endpoint/json") ->image($registry . "fd3/jsonep:$version") ->addVolumes("./extra/statsd/endpoint/json", "/opt/datasource") ->addLinks("jsonep_mysql") ->addLinks("jsonep_mongo") ->restart($this->_docker_restart_default); $composer ->addService("jsonep_mysql") ->build("./extra/statsd/endpoint/mysql") ->image($registry . "fd3/jsonep_mysql:$version") ->addVolumes("./extra/statsd/endpoint/mysql", "/opt/datasource") ->addLinks("mysql") ->addEnv_file("mysql." . $host_env_file) ->restart($this->_docker_restart_default); $composer ->addService("jsonep_mongo") ->build("./extra/statsd/endpoint/mongodb") ->image($registry . "fd3/jsonep_mongo:$version") ->addVolumes("./extra/statsd/endpoint/mongodb", "/opt/datasource") ->addLinks("mongodb") ->restart($this->_docker_restart_default); } function addCommandWorkers(FileFormat2 $composer, $config = array()) { $version = "latest"; $registry = ""; extract($config); $composer ->addService("base_log_worker") ->image($registry . "fd3/base:" . $version) ->build("./base/") ->command("bin/console rabbitmq:consumer log_consumer") ->restart($this->_docker_restart_default) ->addLinks("mysql") ->addLinks("base") ->addLinks("nginx", $this->getDomain("base")) ->addEnv_file("running.env") ->addEnv_file("host.env") ->addEnv_file("base.host.env") ->addEnviroment("AMQP_KEY", "base") ->addEnviroment("SYMFONY_ENV", "prod") ->addVolumes("./base/", "/opt/base"); $composer ->addService("ftth_tasklogger_worker") ->image($registry . "fd3/ftth:" . $version) ->build("./ftth/") ->command("bin/console rabbitmq:consumer flowdat_tasklogger") ->restart($this->_docker_restart_default) ->addLinks("mysql") ->addLinks("base") ->addLinks("nginx", $this->getDomain("base")) ->addEnv_file("running.env") ->addEnv_file("host.env") ->addEnv_file("ftth.host.env") ->addEnviroment("AMQP_KEY", "ftth") ->addVolumes("./ftth/", "/opt/ftth"); $composer ->addService("cablemodem_tasklogger_worker") ->image($registry . "fd3/cablemodem:" . $version) ->build("./cablemodem/") ->command("bin/console rabbitmq:consumer flowdat_tasklogger") ->restart($this->_docker_restart_default) ->addLinks("mysql") ->addLinks("base") ->addLinks("nginx", $this->getDomain("base")) ->addEnv_file("running.env") ->addEnv_file("host.env") ->addEnv_file("cablemodem.host.env") ->addEnviroment("AMQP_KEY", "cablemodem") ->addVolumes("./cablemodem/", "/opt/cablemodem"); $composer ->addService("ftth_command_worker") ->image($registry . "fd3/ftth:" . $version) ->build("./ftth/") ->command("bin/console rabbitmq:consumer command_consumer") ->restart($this->_docker_restart_default) ->addLinks("mysql") ->addLinks("base") ->addLinks("nginx", $this->getDomain("base")) ->addEnv_file("running.env") ->addEnv_file("host.env") ->addEnv_file("ftth.host.env") ->addEnviroment("AMQP_KEY", "ftth") ->addVolumes("./ftth/", "/opt/ftth"); $composer ->addService("stats_command_worker") ->image($registry . "fd3/stats:" . $version) ->build("./stats/") ->command("bin/console rabbitmq:consumer command_consumer") ->restart($this->_docker_restart_default) ->addLinks("mysql") ->addLinks("base") ->addLinks("nginx", $this->getDomain("base")) ->addLinks("geoserver") ->addLinks("nginx", $this->getDomain("geoserver")) ->addEnv_file("running.env") ->addEnv_file("host.env") ->addEnv_file("stats.host.env") ->addEnviroment("AMQP_KEY", "stats") ->addVolumes("./stats/", "/opt/stats") ->addVolumes("./geoserver/geoserver-shapes", "/var/www/shapes"); $composer ->addService("cablemodem_command_worker") ->image($registry . "fd3/cablemodem:" . $version) ->build("./cablemodem/") ->command("bin/console rabbitmq:consumer command_consumer") ->restart($this->_docker_restart_default) ->addLinks("mysql") ->addLinks("base") ->addLinks("nginx", $this->getDomain("base")) ->addEnv_file("running.env") ->addEnv_file("host.env") ->addEnv_file("cablemodem.host.env") ->addEnviroment("AMQP_KEY", "cablemodem") ->addVolumes("./cablemodem/", "/opt/cablemodem"); } /** * @param FileFormat2 $composer * @param array $config */ public function addGeoserver(FileFormat2 $composer, $config = array()) { $version = "latest"; $registry = ""; extract($config); $composer ->addService("geoserver") ->image($registry . "fd3/geoserver:" . $version) ->build("./extra/geoserver/") ->restart($this->_docker_restart_default) ->addLinks("nginx", $this->getDomain("geoserver")) ->addEnv_file("running.env") ->addEnv_file("host.env") ->addVolumes("./extra/geoserver/geoserver-data/styles", "/opt/geoserver/data_dir/styles") ->addVolumes("./geoserver/geoserver-shapes", "/var/www/shapes") ->addPorts(8081, 8080); } function getDockerComposer($version = "latest", $host_env_file = "host.env", $registry = "docker.infra.flowdat.com/") { $composer = new FileFormat2("../"); $base_vars = array( "version" => $version, "host_env_file" => $host_env_file, "registry" => $registry); $this->addNginx($composer, $base_vars); /**************************************************************************************/ /* Apps / Web UI / Grafana / PMA */ /**************************************************************************************/ foreach ($this->_modules_all as $module => $env) { $method = 'add' . ucfirst($module); if (array_key_exists($module, $this->_modules) && method_exists($this, $method)) { $this->$method($composer, $base_vars); } } /**************************************************************************************/ /* Servicios */ /**************************************************************************************/ $this->addMySql($composer, $base_vars); $composer ->addService("amqp") ->image("rabbitmq:3-management") ->restart($this->_docker_restart_default); $this->addMongDb($composer, $base_vars); $this->addRedis($composer, $base_vars); $this->addGenieACS($composer, $base_vars); $this->addFreeradius($composer, $base_vars); /**************************************************************************************/ /* Workers */ /**************************************************************************************/ $this->addSupervisord($composer, $base_vars); $this->addCommandWorkers($composer, $base_vars); $this->addTftp($composer, $base_vars); // Geoserver $this->addGeoserver($composer, $base_vars); $this->_dObj->file("docker-compose.yml")->content($composer->render()); // escribo un archivo inventory.ini por defecto para no tener que lanzar los docker $this->writeInventory($composer); } /** * Crea un array con la configuracion de los modulos. * @param InputInterface $input Contiene el input. */ private function addConfigOptions(InputInterface $input) { foreach ($this->_modules as $key => $values) { if ($input->hasOption($key . "-repo") && $input->hasOption($key . "-ref") && $input->hasOption($key . "-build") ) { $this->_modules[$key]['repo'] = $input->getOption($key . "-repo"); $this->_modules[$key]['ref'] = $input->getOption($key . "-ref"); $this->_modules[$key]['build'] = $input->getOption($key . "-build"); } } } /** * Crea un array con las direcciones de a clonar. */ private function createGitClone() { $clone = array(); $modules = array_keys($this->_modules); foreach ($modules as $name) { if (isset($this->_modules[$name]["repo"]) && isset($this->_modules[$name]["ref"])) { $clone[$name] = array( 'url' => $this->_modules[$name]["repo"], 'branch' => $this->_modules[$name]["ref"] ); } } $this->_dObj->file("git.ini")->writeIniConfig($clone); } /** * Crea un archivo conlos parametros con los que se corrio el script. * @param InputInterface $input Contiene el input * @param OutputInterface $output Contiene el output */ private function createFileRunning(InputInterface $input, OutputInterface $output) { $file = array(); $file ["Running"] = array("date" => gmdate('Y-m-d h:i:s')); $file ["Arguments"] = $input->getArguments(); $file ["Options"] = $input->getOptions(); $output->writeln("Writing " . $this->_dObj->getPath() . "/" . $this->_running_log); $this->_dObj->file($this->_running_log)->writeIniConfig($file); } /** * @param string $name Contiene el nombre del modulo. * @param string $module Contiene el nombre del modulo. * @param array $extras Contiene variables de entorno extra. * @return string|array Retorna un array con los datos de virtual host. */ function getEnviromentVarialbes($name, $module, $extras = array()) { $env = ""; if ($module != null) { foreach ($this->_modules as $nameApp => $app) { if (isset($app['VAR_ENV']) && $nameApp == $module) { foreach ($app['VAR_ENV'] as $key => $value) { if ($key == 'VIRTUAL_HOST') { $env .= "VIRTUAL_HOST=" . $this->getDomain($module) . "\n"; } else { $env .= $key . "=" . $value . "\n"; } } } } } foreach ($extras as $key => $value) { $env .= $key . "=" . $value . "\n"; } return $env; } /** * Crea el archivo modulo.oauth.env */ private function writeOAUTH() { $oautModules = ""; foreach ($this->_modules as $nameApp => $app) { if (isset($app['OAUTH']) && $app['OAUTH']) { $this->_dObj->file($nameApp . ".oauth.env")->content(""); $oautModules = $oautModules . $nameApp . ","; } } $this->_ansible_vars["MODULES_INSTALL"] = substr($oautModules, 0, strlen($oautModules) - 1); } /** * Crea el archivo host.env */ private function writeHostEnv() { $hostEnvConfig = $this->getHostEnv(); $env_content = ""; foreach ($hostEnvConfig as $var => $val) { $env_content .= $var . "=" . $val . "\n"; } $this->_dObj->file('host.env')->content($env_content); } /** * Crea el archivo hostsDile * @param string $config_ip Contiene la ip. */ private function writeHostsFile($config_ip) { $hostConfig = $this->getHostConfig($config_ip); $hostfile_content = ""; foreach ($hostConfig as $host => $ip) { $hostfile_content .= $ip . "\t" . $host . "\n"; } $this->_dObj->file("hostsFile")->content($hostfile_content); } /** * Crea un archivo con las variables de entorno particulares del modulo. * @param string $name Contiene el nombre del archivo. * @param string $module Contiene el nombre del modulo. * @param array $extras Contiene un array con las variables extras. */ private function writeVariablesEnviroment($name, $module = null, $extras = array()) { $this->_dObj->file($name)->content( $this->getEnviromentVarialbes($name, $module, $extras)); } /** * Funcion que agrega el build de acuerdo en la configuracion. * @param string $module Contiene el nombre del modulo. * @param FileFormat2 $composer Contiene el objeto FileFormat2. */ private function addBuild($module, FileFormat2 $composer) { if (isset($this->_modules[$module]['build']) && filter_var($this->_modules[$module]['build'], FILTER_VALIDATE_BOOLEAN)) { try { $composer->service($module)->build("./$module/"); } catch (ServiceNotFoundException $ignore) { } } } /** * Funcion que setea los valores que se lean desde el archivo running.log. * Solo se reemplazan las opciones. * Si se toman los argumentos puede pisar el directorio de destino y a lo mejor se quiere replicar la instalacion en * otro directorio. * @param InputInterface $input contiene el input */ private function setParametersFormFile(InputInterface $input) { $parameters = parse_ini_file($input->getArgument('dir') . "/" . $this->_running_log, true); foreach ($parameters["Options"] as $key => $value) { $input->setOption($key, $value); } } /** * @param string $module Contiene el nombre del modulo. * @return string Retorna el dominio para el modulo. */ private function getDomain($module) { return $module . "." . $this->_client . "." . $this->_domain; } /** * @param array $modules Contiene los modulos a implementar */ private function AddModules($modules) { foreach ($modules as $value) { if (array_key_exists($value, $this->_modules_all)) { $this->_modules[$value] = $this->_modules_all[$value]; } } } /** * Crea el archivo con las variables para ejecutar el ansible. */ private function writeEnvVariables() { $tmp = ""; foreach ($this->_ansible_vars as $key => $value) { $tmp = $tmp . "$key=$value\n"; } $this->_dObj->file(str_replace(".log", ".env", $this->_running_log)) ->content($tmp); } /** * Crea el archivo con los usuarios del sistema para que lea el ansible. */ private function writeUserSystem() { $this->_dObj->file("user_system.json") ->content(json_encode($this->_user_system)); } /** * Crea el archivo con las variables para ejecutar el ansible. */ private function writeInventory(FileFormat2 $composer) { $tmp = ""; $all = "[all]\n"; $prefix = basename(realpath($this->directory)); foreach ($composer->getServices() as $key => $value) { $tmp .= "[$key]\n"; $tmp .= $prefix . "_" . $key . "_1\n\n"; $all .= $prefix . "_" . $key . "_1\n"; } $this->_dObj->file("inventory.ini")->content($tmp . $all); } }