Jelajahi Sumber

Merge branch 'FD3-669' of bitbucket.org:ikflowdat/dhcp into FD3-672

Espinoza Guillermo 6 tahun lalu
induk
melakukan
53632a8bf1

+ 3 - 0
app/config/parameters.yml.dist

@@ -43,3 +43,6 @@ parameters:
 
     # App Dummy URL
     env(HOST_DUMMY): http://www.flowdat.com/
+
+    # Database de kea
+    database_name_kea: kea

+ 3 - 0
app/config/parameters.yml.docker

@@ -39,3 +39,6 @@ parameters:
     session_names: [flowdat_base_session, flowdat_ftth_session, flowdat_mapas_session, flowdat_stats_session, flowdat_radius_session, flowdat_cablemodem_session, flowdat_dhcp_session]
 
     nginx_name: nginx-proxy
+
+    # Database de kea
+    database_name_kea: kea

+ 11 - 2
src/KeaBundle/Resources/config/services.yml

@@ -1,7 +1,16 @@
 services:
     kea.config:
         class: KeaBundle\Services\KeaConfigService
-        arguments: ["@doctrine.orm.entity_manager", "%database_user%", "%database_password%"]
+        arguments:
+            - "@doctrine.orm.entity_manager"
+            - "%database_host%"
+            - "%database_name%"
+            - "%database_name_kea%"
+            - "%database_user%"
+            - "%database_password%"
+            - "%rabbit_mq.host%"
+            - "%rabbit_mq.user%"
+            - "%rabbit_mq.password%"
 
     sonata.admin.kea_config:
         class: KeaBundle\Admin\ConfigAdmin
@@ -25,4 +34,4 @@ services:
        class: KeaBundle\Twig\PoolIPv4Extension
        arguments: [ '@pool_ipv4_service' ]
        tags:
-           - { name: twig.extension }
+           - { name: twig.extension }

+ 58 - 47
src/KeaBundle/Services/BaseKea.php

@@ -16,56 +16,47 @@ class BaseKea implements KeaConfigInterface
     /**
      * @var array
      */
-    private $hooks_libraries = [
-        [
-            'library' => '/opt/hooks/mysql/kea-hook-flowdat3-mysql.so',
-            'parameters' => '',
-        ],
-        [
-            'library' => '/opt/hooks/amqp/kea-hook-flowdat3.so',
-            'parameters' => '',
-        ],
-    ];
+    private $hooks_libraries = [];
 
     /**
      * @var array
      */
     private $interfaces_config = [];
-    
+
     /**
      * @var boolean
      */
     private $echo_client_id = false;
-    
+
     /**
      * @var array
      */
     private $option_def = [
         [
-            "name" => "subopt1", 
-            "code" => 3, 
-            "space" => "pcc", 
-            "type" => "binary", 
-            "record-types" => "", 
-            "array" => false, 
+            "name" => "subopt1",
+            "code" => 3,
+            "space" => "pcc",
+            "type" => "binary",
+            "record-types" => "",
+            "array" => false,
             "encapsulate"  => "",
         ],
         [
-            "name" => "subopt2", 
-            "code" => 6, 
-            "space" => "pcc", 
-            "type" => "binary", 
-            "record-types" => "", 
-            "array" => false, 
+            "name" => "subopt2",
+            "code" => 6,
+            "space" => "pcc",
+            "type" => "binary",
+            "record-types" => "",
+            "array" => false,
             "encapsulate" => "",
         ],
         [
-            "name" =>"clabs", 
-            "code" => 122, 
-            "space" => "dhcp4", 
-            "type" => "empty", 
-            "array" => false, 
-            "record-types" => "", 
+            "name" =>"clabs",
+            "code" => 122,
+            "space" => "dhcp4",
+            "type" => "empty",
+            "array" => false,
+            "record-types" => "",
             "encapsulate" => "pcc",
         ],
     ];
@@ -133,21 +124,21 @@ class BaseKea implements KeaConfigInterface
                 'subnet' => $subnet->getAddress(),
                 'pools' => $pools,
             ];
-            
+
             $nextServer = $subnet->getNextServer();
             if ($nextServer != '') {
                 $subnetConf['next-server'] = $nextServer;
             }
-            
+
             $filename = $subnet->getFilename();
             if ($filename != '') {
                 $subnetConf['boot-file-name'] = $filename;
             }
-            
+
             if ($client_class != '') {
                 $subnetConf['client-class'] = $client_class;
             }
-            
+
             $netgroup = $subnet->getNetGroup();
             if ($netgroup && count($netgroup->getRelay())) {
                 $relay = [];
@@ -158,9 +149,9 @@ class BaseKea implements KeaConfigInterface
                     'ip-addresses' => $relay,
                 ];
             }
-            
+
             $this->setOptionData($subnet, $subnetConf);
-            
+
             $this->subnet4[] = $subnetConf;
         }
     }
@@ -170,6 +161,26 @@ class BaseKea implements KeaConfigInterface
      */
     private function hooksLibrariesConfig($data)
     {
+        $this->hooks_libraries = [
+            [
+                'library' => '/opt/hooks/mysql/kea-hook-flowdat3-mysql.so',
+                'parameters' => [
+                    "host" => $data['db']['host'],
+                    "database" => $data['db']['database'],
+                    "user" => $data['db']['user'],
+                    "password" => $data['db']['password'],
+                ]
+            ],
+            [
+                "library" => "/opt/hooks/amqp/kea-hook-flowdat3.so",
+                "parameters" => [
+                    "host" => $data['amqp']['host'],
+                    "user" => $data['amqp']['user'],
+                    "password" => $data['amqp']['password'],
+                ],
+            ],
+        ];
+
         $dhcp = $data['dhcp'];
         $hosts = $data['hosts'];
 
@@ -177,7 +188,7 @@ class BaseKea implements KeaConfigInterface
             'library' => $data['library'],
         ];
 
-        $dhcpModelParams = $dhcp ? $dhcp->getDhcpModel()->getData('parameters') : null;
+        $dhcpModelParams = $dhcp && $dhcp->getDhcpModel() ? $dhcp->getDhcpModel()->getData('parameters') : null;
 
         $option122 = isset($dhcpModelParams['option122']) ? $dhcpModelParams['option122'] : null;
         $ip = isset($dhcpModelParams['ip']) ? $dhcpModelParams['ip'] : null;
@@ -202,9 +213,9 @@ class BaseKea implements KeaConfigInterface
                     $mac = $host->getMac();
                     $state = $host->getState();
                     $shortname = $host->getHostType()->getShortname();
-                    
+
                     $client_class = $state != HostStatus::STATE_NONE ? $state : $shortname;
-                    
+
                     $hostConfig[$client_class][] = $mac;
                 }
             }
@@ -269,7 +280,7 @@ class BaseKea implements KeaConfigInterface
                 'type' => 'memfile'
             );
         }
-        
+
         return $config;
     }
 
@@ -280,7 +291,7 @@ class BaseKea implements KeaConfigInterface
     {
         return ['interfaces' => ['*']];
     }
-    
+
     /**
      * @param Subnet $subnet
      *
@@ -290,14 +301,14 @@ class BaseKea implements KeaConfigInterface
     {
         $option_data = [];
         $fields = [
-            "routers", 
+            "routers",
             "time-servers" => ["always-send" => true],
             "log-servers",
             "time-offset",
             "broadcast-address" => ["always-send" => true],
             "domain-name-servers",
         ];
-        
+
         foreach ($fields as $key => $options) {
             $name = is_array($options) ? $key : $options;
             $field = implode('', array_map(function ($piece) {
@@ -316,7 +327,7 @@ class BaseKea implements KeaConfigInterface
                 $option_data[] = $data;
             }
         }
-        
+
         $option_122_data = [];
         if ($subnet->getOption122ProvisioningServer() && $subnet->getOption122ProvisioningType()) {
             $option_122_data = [
@@ -343,11 +354,11 @@ class BaseKea implements KeaConfigInterface
                 ],
             ];
         }
-        
+
         $option_data = array_merge($option_data, $option_122_data);
-        
+
         $subnetConf['option-data'] = $option_data;
-                
+
         return $option_data;
     }
 

+ 38 - 25
src/KeaBundle/Services/KeaConfigService.php

@@ -29,24 +29,36 @@ class KeaConfigService
      * @var array
      */
     private $databaseConfig;
-    
+
+    /**
+     * @var array
+     */
+    private $amqpConfig;
+
 
     /**
      * @param EntityManager $em
      * @param string $databaseUser
      * @param string $databasePassword
      */
-    public function __construct(EntityManager $em, $databaseUser, $databasePassword)
+    public function __construct(EntityManager $em, $databaseHost, $databaseName, $databaseNameKea, $databaseUser, $databasePassword, $amqpHost, $amqpUser, $amqpPassword)
     {
         $this->dhcpRepository = $em->getRepository('DHCPBundle:DHCP');
         $this->subnetRepository = $em->getRepository('IPv4Bundle:SubNet');
         $this->hostRepository = $em->getRepository('HostBundle:Host');
-        
+
         $this->databaseConfig = [
-            'host' => 'mysql',
-            'name' => 'kea',
+            'host' => $databaseHost,
+            'name' => $databaseNameKea,
             'user' => $databaseUser,
             'password' => $databasePassword,
+            'database' => $databaseName,
+        ];
+
+        $this->amqpConfig = [
+            'host' => $amqpHost,
+            'user' => $amqpUser,
+            'password' => $amqpPassword,
         ];
     }
 
@@ -70,6 +82,7 @@ class KeaConfigService
                     'subnets' => $this->subnetRepository->findAll(),
                     'library' => $library,
                     'db' => $this->databaseConfig,
+                    'amqp' => $this->amqpConfig,
                 ];
                 $config = $keaConfig->getConfig($data);
             }
@@ -88,14 +101,14 @@ class KeaConfigService
     {
         $host = $dhcp->getHost();
         if (is_null($host)) $host = "kea:8080";
-        
+
         $_params = array(
-            "command" => "config-get", 
+            "command" => "config-get",
             "service" => array($service),
         );
-        
+
         $params = json_encode($_params);
-        
+
         $config = $this->curlPost($host, $params);
 
         return $config;
@@ -111,14 +124,14 @@ class KeaConfigService
     {
         $host = $dhcp->getHost();
         if(is_null($host)) $host = "kea:8080";
-        
+
         $_params = array(
-            "command" => "config-reload", 
+            "command" => "config-reload",
             "service" => array($service),
         );
-        
+
         $params = json_encode($_params);
-        
+
         $config = $this->curlPost($host, $params);
 
         return $config;
@@ -134,14 +147,14 @@ class KeaConfigService
     {
         $host = $dhcp->getHost();
         if(is_null($host)) $host = "kea:8080";
-        
+
         $_params = array(
-            "command" => "libreload", 
+            "command" => "libreload",
             "service" => array($service),
         );
-        
+
         $params = json_encode($_params);
-        
+
         $config = $this->curlPost($host, $params);
 
         return $config;
@@ -152,7 +165,7 @@ class KeaConfigService
      * que está montado como volumen en el docker de KEA
      *
      * @param json params
-     * 
+     *
      * @return boolean
      */
     public function saveFileConfig($params = null)
@@ -161,17 +174,17 @@ class KeaConfigService
         if(isset($_params[0]) && isset($_params[0]['arguments'])) {
             $json = json_encode($_params[0]['arguments'], JSON_PRETTY_PRINT);
             file_put_contents("kea/kea-dhcp4.conf", $json);
-            
+
             return true;
         }
-        
+
         return false;
     }
 
     /**
      * @param DHCP $dhcp
      * @param json params
-     * 
+     *
      * @return string
      */
     public function setConfig(DHCP $dhcp, $params = null)
@@ -184,13 +197,13 @@ class KeaConfigService
 
         if(isset($_params[0]) && isset($_params[0]['arguments'])) {
             $_params = array(
-                "command" => "config-set", 
-                "service" => array("dhcp4"), 
+                "command" => "config-set",
+                "service" => array("dhcp4"),
                 "arguments" => $_params[0]['arguments']);
         } else {
             return json_encode(array("error" => "Config Error. Check template DHCP"));
         }
-        
+
         $config = $this->curlPost($host, json_encode($_params));
 
         return $config;
@@ -202,7 +215,7 @@ class KeaConfigService
      *
      * @return string
      */
-    private function curlPost($host, $params) 
+    private function curlPost($host, $params)
     {
         $ch = curl_init();