소스 검색

Merged in FD3-699 (pull request #31)

FD3-699 Update kea host reservation
Guillermo Espinoza 6 년 전
부모
커밋
15fc882bc1

+ 0 - 2
src/HostBundle/Admin/HostAdmin.php

@@ -181,8 +181,6 @@ class HostAdmin extends BaseAdmin
         if (!$object->getFixedAddress() && $object->getFixedIP() == true) {
             $object->setFixedIP(false);
         }
-        
-        $object->setOptions(json_encode($object->getDHCPOption()));
     
         return parent::preUpdate($object);
     }

+ 18 - 2
src/HostBundle/Command/DHCPOptionsUpdateCommand.php

@@ -3,7 +3,7 @@
 namespace HostBundle\Command;
 
 use Buzz\Message\RequestInterface as HttpRequestInterface;
-use HostBundle\Entity\Host;
+use IPv4Bundle\Entity\SubNet;
 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
 use Symfony\Component\Console\Input\InputArgument;
 use Symfony\Component\Console\Input\InputOption;
@@ -42,7 +42,23 @@ EOT
         $result = $dhcpService->formatDhcpOptions($output);
         
         $io->newLine(2);
-        $io->success('Total entities updated: ' . $result);
+        if ($result == 0) {
+            $io->warning('Nothing to update');
+        } else {
+            $io->success('Total entities updated: ' . $result);
+        }
+        $io->newLine(2);
+        
+        $io->section("Updating SubNet entities:");
+        
+        $result = $dhcpService->formatDhcpOptions($output, SubNet::class);
+        
+        $io->newLine(2);
+        if ($result == 0) {
+            $io->warning('Nothing to update');
+        } else {
+            $io->success('Total entities updated: ' . $result);
+        }
         $io->newLine(2);
         
         $output->writeln(['', 'Done!']);

+ 1 - 1
src/HostBundle/Entity/Host.php

@@ -142,7 +142,7 @@ class Host implements WorkflowInterface
      */
     public function getMac()
     {
-        return $this->mac ?: ($this->host ? $this->host->getMac() : '');
+        return $this->mac;
     }
 
     /**

+ 26 - 4
src/HostBundle/EventListener/KEAHostReservationSubscriber.php

@@ -85,10 +85,12 @@ class KEAHostReservationSubscriber implements EventSubscriber
                 $query = "
                 INSERT INTO kea.hosts (dhcp_identifier,
                 dhcp_identifier_type,
-                ipv4_address)
+                ipv4_address, 
+                hostname)
                 VALUES (:mac,
                 (SELECT type FROM kea.host_identifier_type WHERE name=:type),
-                INET_ATON(:ip));
+                INET_ATON(:ip),
+                :hostname);
                 ";
             } else {
                 $query = "
@@ -99,9 +101,29 @@ class KEAHostReservationSubscriber implements EventSubscriber
             
             $databaseConnection = $this->serviceContainer->get('database_connection');
             $stmt = $databaseConnection->prepare($query);
-            $stmt->bindValue("mac", hex2bin(str_replace(':', '', $entity->getMac())));
+            $hostType = $entity->getHostType();
+            $shortname = $hostType->getShortname();
+            // calculo el campo dhcp_identifier_type de kea.hosts
+            if ($shortname == 'cablemodem' || $shortname == 'cm') {
+                $stmt->bindValue("mac", hex2bin(str_replace(':', '', $entity->getMac())));
+            } else {
+                // mta|cpe
+                $hex = '';
+                $prefix = $shortname . '-';
+                for ($i = 0; $i < strlen($prefix); $i++) {
+                    $ord = ord($prefix[$i]);
+                    $hexCode = dechex($ord);
+                    $hex .= substr('0'.$hexCode, -2);
+                }
+                $stmt->bindValue("mac", hex2bin($hex . str_replace(':', '', $entity->getHost()->getMac())));
+            }
             if ($entity->getFixedAddress() && $remove == false) {
-                $stmt->bindValue("type", 'hw-address');
+                if ($shortname == 'cablemodem' || $shortname == 'cm') {
+                    $stmt->bindValue("type", 'hw-address');
+                } else {
+                    $stmt->bindValue("type", 'flex-id');
+                }
+                $stmt->bindValue("hostname", $entity->getHostName());
                 $stmt->bindValue("ip", $entity->getFixedAddress());
             }
             $stmt->execute();

+ 2 - 2
src/HostBundle/Resources/translations/HostBundle.es.yml

@@ -29,7 +29,7 @@ form:
     label_domain_name_servers: Domain Name Servers
     label_host_name: Host Name
     label_domain_name: Domain Name
-    label_broadcast_address: Boradcast Address
+    label_broadcast_address: Broadcast Address
     label_default_lease_time: Default Lease Time
     label_max_lease_time: Max. Lease Time
     label_next_server: Next Server
@@ -91,7 +91,7 @@ show:
     label_domain_name_servers: Domain Name Servers
     label_host_name: Host Name
     label_domain_name: Domain Name
-    label_broadcast_address: Boradcast Address
+    label_broadcast_address: Broadcast Address
     label_default_lease_time: Default Lease Time
     label_max_lease_time: Max. Lease Time
     label_next_server: Next Server

+ 5 - 2
src/HostBundle/Services/DHCPOptionsService.php

@@ -34,22 +34,24 @@ class DHCPOptionsService
     
     /**
      * @param OutputInterface $output
+     * @param string $class Host|SubNet
      *
      * @return int
      */
-    public function formatDhcpOptions($output)
+    public function formatDhcpOptions($output, $class = Host::class)
     {
         $i = 0;
         $batchSize = 20;
         $detach = [];
         
-        $hostRepository = $this->em->getRepository(Host::class);
+        $hostRepository = $this->em->getRepository($class);
         $hosts = $hostRepository->findAll();
         
         ProgressBar::setFormatDefinition('custom', ' %current%/%max% [%bar%] %percent:3s%% %message%');
         $progressBar = new ProgressBar($output, count($hosts));
         $progressBar->setFormat('custom');
         $progressBar->start();
+        $progressBar->setMessage('Start...');
         foreach ($hosts as $host) {
             $mtaOptions = $cpeOptions = '';
             $options = $host->getOptions();
@@ -94,6 +96,7 @@ class DHCPOptionsService
             }
             $progressBar->advance();
         }
+        $progressBar->setMessage('Finished...');
         $progressBar->finish();
         
         $this->em->flush();