Browse Source

Agregado del fix:nginx para dejar los archivos conf.d del nginx de acuerdo a los modulos instalados

Dockerfile agregado de la variable DOCKER_HOST

cmd.php agregado del fix:nginx

execute.sh agregado para que se pueda ejecutar el bash

Release.php agregado de la opcion de modulos

DockerInventory.php arreglo donde se escribe el archivo inventory.ini
gabriel 7 years ago
parent
commit
b304c56656
6 changed files with 227 additions and 123 deletions
  1. 2 0
      tools/Dockerfile
  2. 1 0
      tools/cmd.php
  3. 21 9
      tools/execute.sh
  4. 10 10
      tools/src/DockerInventory.php
  5. 56 0
      tools/src/FixNginx.php
  6. 137 104
      tools/src/Release.php

+ 2 - 0
tools/Dockerfile

@@ -31,4 +31,6 @@ RUN chmod -R 777 /opt/installation
 ADD . /opt/installation
 WORKDIR /opt/installation
 
+ENV DOCKER_HOST unix:///tmp/docker.sock
+
 ENTRYPOINT ["/opt/installation/execute.sh"]

+ 1 - 0
tools/cmd.php

@@ -11,6 +11,7 @@ $app->add(new FD3\Release());
 $app->add(new FD3\GetSource());
 $app->add(new FD3\MergeHostsFile());
 $app->add(new FD3\DockerInventory());
+$app->add(new FD3\FixNginx());
 $app->add(new FD3\ImportClient());
 $app->add(new FD3\Command\ImportONUCommand());
 

+ 21 - 9
tools/execute.sh

@@ -1,16 +1,28 @@
 #!/bin/bash
 
-COMMAND="php cmd.php"
-
 eval $(ssh-agent)
 ssh-add /opt/keys/bitbucket.id_rsa
 
-if [ "$#" -ne 0 ];
+if [ "$1" == "bash" ];
 then
-  for word in "$@" 
-  do 
-    COMMAND="$COMMAND $word" 
-  done
-fi
+  eval /bin/bash
+else
+  echo "########################################################"
+  echo "########################################################"
+  echo "RUN BASH WITH OPTION bash. Ej: docker run ......... bash"
+  echo "########################################################"
+  echo "########################################################"
+  echo ""
+  echo ""
 
-eval $COMMAND
+  COMMAND="php cmd.php"
+
+  if [ "$#" -ne 0 ];
+  then
+    for word in "$@"
+    do
+      COMMAND="$COMMAND $word"
+    done
+  fi
+  eval $COMMAND
+fi

+ 10 - 10
tools/src/DockerInventory.php

@@ -20,14 +20,15 @@ class DockerInventory extends Command
             ->setName('docker:inventory')
             ->setDescription('Get a ansible inventory based on the running container and the docker .')
             ->setHelp('...')
-            ->addArgument('dir', InputArgument::REQUIRED, 'The dir where the docker-composer.yml files is located.')// ->addOption('source-name', null, InputOption::VALUE_REQUIRED, 'Rename the source to this name.', "upstream")
-        ;
+            ->addArgument('dir', InputArgument::REQUIRED, 'The dir where the docker-composer.yml files is located.')
+            ->addOption('run-docker', null, InputOption::VALUE_REQUIRED, 'Lift up docker.', false);
     }
 
     protected function execute(InputInterface $input, OutputInterface $output)
     {
         $dir = $input->getArgument("dir");
-        $projname = basename(realpath($dir));
+        $dirReal = realpath($dir);
+        $projname = basename($dirReal);
         $docker = new Docker();
         $containerManager = $docker->getContainerManager();
         $containers = $containerManager->findAll();
@@ -46,19 +47,18 @@ class DockerInventory extends Command
                 }
             }
         }
-
         $contAll = "";
         $cont = "";
         foreach ($groups as $name => $group) {
-
             $cont .= "\n[" . $name . "]\n";
             foreach ($group as $host) {
-                $cont .= $host . " ansible_connection=docker\n";
-                $contAll .= $host . " ansible_connection=docker\n";
+                $cont .= $host . "\n";
+                $contAll .= $host . "\n";
+//                $cont .= $host . " ansible_connection=docker\n";
+//                $contAll .= $host . " ansible_connection=docker\n";
             }
         }
-
-        $dirObj = new FileSystem($dir);
-            $dirObj->file("inventory.ini")->content($cont . "\n[all]\n" . $contAll);
+        $dirObj = new FileSystem($dirReal);
+        $dirObj->file("inventory.ini")->content($cont . "\n[all]\n" . $contAll);
     }
 }

+ 56 - 0
tools/src/FixNginx.php

@@ -0,0 +1,56 @@
+<?php
+
+namespace FD3;
+
+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\Docker;
+
+use FD3\DevOps\FileSystem;
+
+class FixNginx extends Command
+{
+    protected function configure()
+    {
+        $this
+            ->setName('fix:nginx')
+            ->setDescription('Fix what modules run in nginx proxy.')
+            ->setHelp('...')
+            ->addArgument('dir', InputArgument::REQUIRED, 'The dir where the running.log files is located.');
+    }
+
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+        $dir = $input->getArgument("dir");
+        $dirReal = realpath($dir);
+        $parameters = parse_ini_file($dirReal . "/running.log", true);
+        $modules = null;
+        foreach ($parameters["Options"] as $key => $value) {
+            if ($key == "modules") {
+                $modules = explode(",", $value);
+            }
+        }
+
+        if ($modules != null) {
+            $dirReal = $dirReal . "/extra/nginx/conf.d/";
+            $dirOpen = opendir($dirReal);
+            while (false !== ($entrada = readdir($dirOpen))) {
+                if ($entrada != ".." && $entrada != ".") {
+                    foreach ($modules as $values) {
+                        if (strpos($entrada, $values) !== false && strpos($entrada, ".tpl") !== false) {
+                            $entrada = null;
+                            break;
+                        }
+                    }
+                    if ($entrada != null) {
+                        rename($dirReal . $entrada, $dirReal . $entrada . ".bkp");
+                    }
+                }
+            }
+        }
+    }
+}

+ 137 - 104
tools/src/Release.php

@@ -14,6 +14,7 @@ use Symfony\Component\Yaml\Yaml;
 
 class Release extends Command
 {
+    private $_running_log;
     /**
      * @var string Contiene el password del usuario root.
      */
@@ -38,6 +39,10 @@ class Release extends Command
      * @var string Contiene el dominio que se agrega al final de la linea.
      */
     private $_domain_behind;
+    /**
+     * @var array Contiene todos los modulos para la instalacion.
+     */
+    private $_modules_all;
     /**
      * @var array Contiene la configuracion de los modulos.
      */
@@ -53,11 +58,13 @@ class Release extends Command
     public function __construct($name = null)
     {
         parent::__construct($name);
+        $this->_running_log = "running.log";
         $this->_mysql_root_pass = "235r2342gtfsw";
         $this->_mysql_user = "iksop";
         $this->_mysql_pass = "235r2342gtfsw";
-        $this->_domain_behind = "fd3.flowdat.com";
-        $this->_modules = array(
+        $this->_domain_behind = "flowdat.com";
+        $this->_modules = array();
+        $this->_modules_all = array(
             "base" => array(
                 'HOST_ENV' => true,
                 "VAR_ENV" => array(
@@ -153,13 +160,21 @@ class Release extends Command
     protected function execute(InputInterface $input, OutputInterface $output)
     {
         try {
+            if (!$input->hasOption("modules")) {
+                $input->setOption("modules", implode(",", array_keys($this->_modules_all)));
+            }
+            //##############################################
+            // TODO: quitar este linea
+            $input->setOption("modules", "base,ftth,extra");
+            //##############################################
+            $this->AddModules(explode(",", $input->getOption("modules")));
             $dir = $input->getArgument('dir');
             if (!is_dir($dir)) {
                 mkdir($dir, 0777, true);
             }
-            if (file_exists($dir . "/running.log")) {
+            if (file_exists($dir . "/" . $this->_running_log)) {
                 $helper = $this->getHelper('question');
-                $question = new ConfirmationQuestion('The running.log file exist. Read file or take parameters? (Y/n)', true);
+                $question = new ConfirmationQuestion('The ' . $this->_running_log . ' file exist. Read file or take parameters? (Y/n)', true);
                 if ($helper->ask($input, $output, $question)) {
                     $this->setParametersFormFile($input);
                 }
@@ -241,18 +256,20 @@ class Release extends Command
         $registry = "";
         $host_env_file = "";
         extract($config);
-        $composer
-            ->addService($module)
-            ->image($registry . "fd3/$module:" . $version)
-            ->restart("always")
-            ->addLinks("mysql:mysql")
-            ->addLinks("amqp")
-            ->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);
+        if (array_key_exists($module, $this->_modules)) {
+            $composer
+                ->addService($module)
+                ->image($registry . "fd3/$module:" . $version)
+                ->restart("always")
+                ->addLinks("mysql:mysql")
+                ->addLinks("amqp")
+                ->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())
@@ -262,22 +279,23 @@ class Release extends Command
         $registry = "";
         $host_env_file = "";
         extract($config);
-
-        $composer
-            ->addService($module)
-            ->image($registry . "fd3/$module:" . $version)
-            ->restart("always")
-            ->addLinks("mysql:mysql")
-            ->addLinks("base")
-            ->addLinks("amqp")
-            ->addLinks("base", $this->getDomain("base"))
-            ->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);
+        if (array_key_exists($module, $this->_modules)) {
+            $composer
+                ->addService($module)
+                ->image($registry . "fd3/$module:" . $version)
+                ->restart("always")
+                ->addLinks("mysql:mysql")
+                ->addLinks("base")
+                ->addLinks("amqp")
+                ->addLinks("base", $this->getDomain("base"))
+                ->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())
@@ -287,23 +305,24 @@ class Release extends Command
         $registry = "";
         $host_env_file = "";
         extract($config);
-
-        $composer
-            ->addService($module)
-            ->image($registry . "fd3/$module:" . $version)
-            ->addLinks("mysql:mysql")
-            ->addLinks("base")
-            ->addLinks("amqp")
-            ->addLinks("base", $this->getDomain("base"))
-            //->addLinks("jsendpoint", "endpoint")
-            //->addLinks("statsd", "statsd")
-            ->addVolumes("./$module/", "/opt/$module")
-            ->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);
+        if (array_key_exists($module, $this->_modules)) {
+            $composer
+                ->addService($module)
+                ->image($registry . "fd3/$module:" . $version)
+                ->addLinks("mysql:mysql")
+                ->addLinks("base")
+                ->addLinks("amqp")
+                ->addLinks("base", $this->getDomain("base"))
+                //->addLinks("jsendpoint", "endpoint")
+                //->addLinks("statsd", "statsd")
+                ->addVolumes("./$module/", "/opt/$module")
+                ->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())
@@ -313,22 +332,23 @@ class Release extends Command
         $registry = "";
         $host_env_file = "";
         extract($config);
-
-        $composer
-            ->addService($module)
-            ->image($registry . "fd3/$module:" . $version)
-            ->addLinks("mysql:mysql")
-            ->addLinks("base")
-            ->addLinks("amqp")
-            ->addLinks("base", $this->getDomain("base"))
-            ->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);
+        if (array_key_exists($module, $this->_modules)) {
+            $composer
+                ->addService($module)
+                ->image($registry . "fd3/$module:" . $version)
+                ->addLinks("mysql:mysql")
+                ->addLinks("base")
+                ->addLinks("amqp")
+                ->addLinks("base", $this->getDomain("base"))
+                ->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 addNginx(FileFormat2 $composer, $config = array())
@@ -409,28 +429,33 @@ class Release extends Command
         $module = "grafana";
         $host_env_file = "";
         extract($config);
-
-        $composer
-            ->addService($module)
-            ->image("grafana/grafana")
-            ->addLinks("mysql")
-            ->restart("always")
-            ->addVolumes("./statsd/grafana/lib", "/var/lib/grafana");
-
-        $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,
-                "GF_SECURITY_ADMIN_PASSWORD" => "queRini6",
-                "GF_INSTALL_PLUGINS" => "grafana-simple-json-datasource",
-                "GF_DEFAULT_THEME" => "light",
-                "GF_AUTH_ANONYMOUS_ORG_NAME" => "Main Org.",
-                "GF_AUTH_ANONYMOUS_ORG_ROLE" => "Viewer",
-                "GF_AUTH_ANONYMOUS_ENABLED" => "true",
-                "GF_DATABASE_URL" => "mysql://root:" . $this->_mysql_root_pass . "@mysql:3306/grafana",
-                "GF_SERVER_ROOT_URL" => "http://" . $this->getDomain("grafana") . "/"
-            ));
+        if (array_key_exists($module, $this->_modules)) {
+            $composer
+                ->addService($module)
+                ->image("grafana/grafana")
+                ->addLinks("mysql")
+                ->restart("always")
+                ->addVolumes("./statsd/grafana/lib", "/var/lib/grafana");
+
+            $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,
+                    "GF_SECURITY_ADMIN_PASSWORD" => "queRini6",
+                    "GF_INSTALL_PLUGINS" => "grafana-simple-json-datasource",
+                    "GF_DEFAULT_THEME" => "light",
+                    "GF_AUTH_ANONYMOUS_ORG_NAME" => "Main Org.",
+                    "GF_AUTH_ANONYMOUS_ORG_ROLE" => "Viewer",
+                    "GF_AUTH_ANONYMOUS_ENABLED" => "true",
+                    "GF_DATABASE_URL" => "mysql://root:" . $this->_mysql_root_pass . "@mysql:3306/grafana",
+                    "GF_SERVER_ROOT_URL" => "http://" . $this->getDomain("grafana") . "/"
+                ));
+
+            $this->addJsonEndPoints($composer, $config + array(
+                    'mysql_root_pass' => $this->_mysql_root_pass,
+                ));
+        }
     }
 
     function addJsonEndPoints(FileFormat2 $composer, $config = array())
@@ -439,7 +464,6 @@ class Release extends Command
         $registry = "";
         $mysql_root_pass = "";
         extract($config);
-
         $composer
             ->addService("statsd")
             ->build("./extra/statsd/statsd")
@@ -575,24 +599,21 @@ class Release extends Command
                 'mysql_pass' => $this->_mysql_pass,
                 'mysql_root_pass' => $this->_mysql_root_pass,
             ));
-        $this->addJsonEndPoints($composer, $base_vars + array(
-                'mysql_root_pass' => $this->_mysql_root_pass,
-            ));
 
         /**************************************************************************************/
         /* Workers */
         /**************************************************************************************/
-        $this->addSupervisord($composer, $base_vars);
-        $this->addCommandWorkers($composer, $base_vars);
-
-        //$composer->addService("phpmyadmin")->image("phpmyadmin/phpmyadmin")
-        //	->restart("always")
-        //	->addPorts(8080, 80)
-        //	->addLinks("mysql", "db")
-        //	->addEnviroment("MYSQL_ROOT_PASSWORD", $mysql_root_pass)
-        //	->addEnviroment("VIRTUAL_HOST", "pma.".$fd_domain)
-        //	->addEnviroment("HTTPS_METHOD", 	"nohttps")
-        //;
+//        $this->addSupervisord($composer, $base_vars);
+//        $this->addCommandWorkers($composer, $base_vars);
+
+////        $composer->addService("phpmyadmin")->image("phpmyadmin/phpmyadmin")
+////        	->restart("always")
+////        	->addPorts(8080, 80)
+////        	->addLinks("mysql", "db")
+////        	->addEnviroment("MYSQL_ROOT_PASSWORD", $mysql_root_pass)
+////        	->addEnviroment("VIRTUAL_HOST", "pma.".$fd_domain)
+////        	->addEnviroment("HTTPS_METHOD", 	"nohttps")
+////        ;
         $this->_dObj->file("docker-compose.yml")->content($composer->render());
     }
 
@@ -643,7 +664,7 @@ class Release extends Command
         $file ["Running"] = array("date" => gmdate('Y-m-d h:i:s'));
         $file ["Arguments"] = $input->getArguments();
         $file ["Options"] = $input->getOptions();
-        $this->_dObj->file("running.log")->writeIniConfig($file);
+        $this->_dObj->file($this->_running_log)->writeIniConfig($file);
     }
 
     /**
@@ -749,7 +770,7 @@ class Release extends Command
      */
     private function setParametersFormFile(InputInterface $input)
     {
-        $parameters = parse_ini_file($input->getArgument('dir') . "/running.log", true);
+        $parameters = parse_ini_file($input->getArgument('dir') . "/" . $this->_running_log, true);
         foreach ($parameters["Options"] as $key => $value) {
             $input->setOption($key, $value);
         }
@@ -763,4 +784,16 @@ class Release extends Command
     {
         return $module . "." . $this->_domain . "." . $this->_domain_behind;
     }
+
+    /**
+     * @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];
+            }
+        }
+    }
 }