12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229 |
- <?php
- namespace FD3;
- use Docker\Composer\ServiceNotFoundException;
- use function GuzzleHttp\default_user_agent;
- use League\Flysystem\File;
- use Symfony\Component\Console\Command\Command;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Input\InputArgument;
- use Symfony\Component\Console\Input\InputOption;
- use Symfony\Component\Console\Output\OutputInterface;
- use Docker\Composer\FileFormat2;
- use Symfony\Component\Console\Exception\LogicException;
- use Symfony\Component\Console\Question\ConfirmationQuestion;
- use Symfony\Component\Yaml\Yaml;
- class Release extends Command
- {
- /**
- * @var string Nombre del archivo de log.
- */
- private $_running_log;
- /**
- * @var string Contiene la politica de restart de los dockers.
- */
- private $_docker_restart_default;
- /**
- * @var array Contiene las variables que se utilizar en la ejecucion del ansible.
- */
- private $_ansible_vars;
- /**
- * @var string Contiene el password del usuario root.
- */
- private $_mysql_root_pass;
- /**
- * @var string Contiene el usuario de base de datos.
- */
- private $_mysql_user;
- /**
- * @var string Contiene la contrasena del usuario de base de datos.
- */
- private $_mysql_pass;
- /**
- * @var DevOps\FileSystem Me permite crear archivos.
- */
- private $_dObj;
- /**
- * @var string Contiene el dominio.
- */
- private $_domain;
- /**
- * @var string Contiene el nombre del cliente.
- */
- private $_client;
- /**
- * @var array Contiene todos los modulos para la instalacion.
- */
- private $_modules_all;
- /**
- * @var array Contiene la configuracion de los modulos.
- */
- private $_modules;
- /**
- * @var array Contiene los usuarios que van a poder acceder al sistema.
- */
- private $_user_system;
- /**
- * @var string $directory Directorio de instalación
- */
- private $directory;
- /**
- * Constructor.
- *
- * @param string|null $name The name of the command; passing null means it must be set in configure()
- *
- * @throws LogicException When the command name is empty
- */
- public function __construct($name = null)
- {
- parent::__construct($name);
- $this->_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);
- }
- }
|