Pārlūkot izejas kodu

Se agrega ip en entero al host y se agregan reservations por subnets.

Maximiliano Schvindt 6 gadi atpakaļ
vecāks
revīzija
5d0a2d9a6c

+ 39 - 0
src/HostBundle/Entity/Host.php

@@ -103,6 +103,13 @@ class Host implements WorkflowInterface
      */
     protected $associatedHosts;
 
+    /**
+     * @var integer $ipv4Address
+     *
+     * @ORM\Column(name="ipv4_address", type="integer", nullable=true)
+     */
+    protected $ipv4Address;
+
 
     public function __construct() 
     {
@@ -278,4 +285,36 @@ class Host implements WorkflowInterface
         }
     }
 
+    /**
+     * @return integer
+     */
+    public function getIpv4Address()
+    {
+        return $this->ipv4Address;
+    }
+
+    /**
+     * @param integer $ipv4Address
+     *
+     * @return Host
+     */
+    public function setIpv4Address($ipv4Address)
+    {
+        $this->ipv4Address = $ipv4Address;
+
+        return $this;
+    }
+
+    function setFixedIntegerAddress()
+    {
+        $options = $this->getDHCPOption();
+        
+        if(isset($options['fixed_address']) && !empty($options['fixed_address'])) {
+            // IPV4
+            $this->setIpv4Address(ip2long($options['fixed_address']));
+        } else {
+            $this->setIpv4Address(null);
+        }
+    }
+
 }

+ 1 - 0
src/HostBundle/EventListener/AssignHostFixedAddressSubscriber.php

@@ -72,6 +72,7 @@ class AssignHostFixedAddressSubscriber implements EventSubscriber
             }
             
             $entity->setOptions(json_encode($entity->getDHCPOption()));
+            $entity->setFixedIntegerAddress();
 
             if ($eventName == 'preUpdate') {
                 $uow = $args->getEntityManager()->getUnitOfWork();

+ 1 - 1
src/KeaBundle/Resources/views/Config/get.html.twig

@@ -61,7 +61,7 @@
                                     <div style="width:55%; float: left">
                                     {{ form_widget(form.template, { id: 'textarea_ace_editor' }) }}
                                     </div>
-                                    <div style="width:40%; float: left">
+                                    <div style="width:40%; float: left; margin-left: 15px">
                                         <br />
                                         <p class="text-uppercase">{{ 'kea_options'|trans({}, 'KeaBundle') }}</p>
                                         <br />

+ 10 - 6
src/KeaBundle/Services/BaseKea.php

@@ -71,7 +71,7 @@ class BaseKea implements KeaConfigInterface
     public function getConfig($data = array(), $logging = false)
     {
         if (isset($data['subnets'])) {
-            $this->subnetConfig($data['subnets']);
+            $this->subnetConfig($data['subnets'], $data['reservations']);
         }
 
         if (isset($data['library'])) {
@@ -98,7 +98,7 @@ class BaseKea implements KeaConfigInterface
     /**
      * @param array $subnets
      */
-    private function subnetConfig($subnets)
+    private function subnetConfig($subnets, $reservations)
     {
         foreach ($subnets as $subnet) {
             $pools = [];
@@ -110,9 +110,9 @@ class BaseKea implements KeaConfigInterface
 
             $hostType = $subnet->getAllowedHostType();
             $client_class = '';
-            if ($hostType) {
+            /* if ($hostType) {
                 $client_class = $hostType->getShortname();
-            }
+            } */
             if ($subnet->getStatus() != HostStatus::STATE_NONE && $subnet->getStatus() != '') {
                 if ($client_class != '') {
                     $client_class .= '-';
@@ -125,6 +125,10 @@ class BaseKea implements KeaConfigInterface
                 'pools' => $pools,
             ];
 
+            if(isset($reservations[$subnet->getId()])) {
+                $subnetConf['reservations'] = $reservations[$subnet->getId()];
+            }
+
             $nextServer = $subnet->getNextServer();
             if ($nextServer != '') {
                 $subnetConf['next-server'] = $nextServer;
@@ -162,7 +166,7 @@ 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'],
@@ -170,7 +174,7 @@ class BaseKea implements KeaConfigInterface
                     "user" => $data['db']['user'],
                     "password" => $data['db']['password'],
                 ]
-            ],
+            ], */
             [
                 "library" => "/opt/hooks/amqp/kea-hook-flowdat3.so",
                 "parameters" => [

+ 53 - 0
src/KeaBundle/Services/KeaConfigService.php

@@ -10,6 +10,11 @@ use KeaBundle\Interfaces\KeaConfigInterface;
 class KeaConfigService
 {
 
+    /**
+     * @var EntityManager
+     */
+    private $em;
+    
     /**
      * @var EntityRepository
      */
@@ -43,6 +48,7 @@ class KeaConfigService
      */
     public function __construct(EntityManager $em, $databaseHost, $databaseName, $databaseNameKea, $databaseUser, $databasePassword, $amqpHost, $amqpUser, $amqpPassword)
     {
+        $this->em = $em;
         $this->dhcpRepository = $em->getRepository('DHCPBundle:DHCP');
         $this->subnetRepository = $em->getRepository('IPv4Bundle:SubNet');
         $this->hostRepository = $em->getRepository('HostBundle:Host');
@@ -80,6 +86,7 @@ class KeaConfigService
                     'dhcp' => $id ? $this->dhcpRepository->find($id) : null,
                     'hosts' => $this->hostRepository->findAll(),
                     'subnets' => $this->subnetRepository->findAll(),
+                    'reservations' => $this->getHostsReservations(),
                     'library' => $library,
                     'db' => $this->databaseConfig,
                     'amqp' => $this->amqpConfig,
@@ -234,4 +241,50 @@ class KeaConfigService
         return $return;
     }
 
+    /**
+     * @return array
+     */
+    private function getHostsReservations() 
+    {
+        $subnets = $this->subnetRepository->findAll();
+        $reservations = [];
+        
+        if(is_null($subnets) || empty($subnets)) {
+            return $reservations;
+        }
+
+        foreach ($subnets as $subnet) {
+            
+            foreach ($subnet->getIpPool() as $pool) {
+                
+                $start = ip2long($pool->getFirstIp());
+                $end = ip2long($pool->getLastIp());
+
+                $qb = $this->em->createQueryBuilder();
+
+                $qb->select(array('h'))
+                    ->from('HostBundle:Host', 'h')
+                    ->where(
+                        $qb->expr()->andX(
+                            $qb->expr()->isNotNull('h.ipv4Address'),
+                            $qb->expr()->between('h.ipv4Address',$start,$end)
+                        )
+                    );
+                
+                $hosts = $qb->getQuery()->getResult();
+                
+                $results = array();
+                if($hosts) {
+                    foreach($hosts as $host) {
+                        $results[] = ['hw-address' => "{$host->getMac()}",'ip-address' => "{$host->getFixedAddress()}"];
+                    }
+
+                    $reservations[$subnet->getId()] = $results;
+                }
+            }
+        }
+
+        return $reservations;
+    }
+
 }