瀏覽代碼

Merged in FD3-492 (pull request #9)

FD3-492 kea:config command

Approved-by: Maximiliano Schvindt <maximiliano@interlink.com.ar>
Guillermo Espinoza 7 年之前
父節點
當前提交
ae8d2cc5db

+ 1 - 0
app/AppKernel.php

@@ -42,6 +42,7 @@ class AppKernel extends Kernel
             new AuditBundle\AuditBundle(),
             new AuditBundle\AuditBundle(),
             new IPv4Bundle\IPv4Bundle(),
             new IPv4Bundle\IPv4Bundle(),
             new DHCPBundle\DHCPBundle(),
             new DHCPBundle\DHCPBundle(),
+            new KeaBundle\KeaBundle(),
         ];
         ];
 
 
         if (in_array($this->getEnvironment(), ['dev', 'test'], true)) {
         if (in_array($this->getEnvironment(), ['dev', 'test'], true)) {

+ 4 - 3
app/config/config.yml

@@ -2,9 +2,6 @@ imports:
     - { resource: parameters.yml }
     - { resource: parameters.yml }
     - { resource: services.yml }
     - { resource: services.yml }
 
 
-    - { resource: "@DHCPBundle/Resources/config/services.yml" }
-    - { resource: "@IPv4Bundle/Resources/config/services.yml" }
-
     - { resource: "@AuditBundle/Resources/config/services.yml" }
     - { resource: "@AuditBundle/Resources/config/services.yml" }
     - { resource: "@AuthBundle/Resources/config/services.yml" }
     - { resource: "@AuthBundle/Resources/config/services.yml" }
     - { resource: '@BaseAdminBundle/Resources/config/services.yml' }
     - { resource: '@BaseAdminBundle/Resources/config/services.yml' }
@@ -25,6 +22,10 @@ imports:
     - { resource: "bundles/ik/device-bundle/parameters.yml" }
     - { resource: "bundles/ik/device-bundle/parameters.yml" }
     - { resource: "bundles/ik/webservice-bundle/parameters.yml" }
     - { resource: "bundles/ik/webservice-bundle/parameters.yml" }
 
 
+    - { resource: "@DHCPBundle/Resources/config/services.yml" }
+    - { resource: "@IPv4Bundle/Resources/config/services.yml" }
+    - { resource: "@KeaBundle/Resources/config/services.yml" }
+
 # Put parameters here that don't need to change on each machine where the app is deployed
 # Put parameters here that don't need to change on each machine where the app is deployed
 # http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
 # http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
 parameters:
 parameters:

+ 36 - 0
src/KeaBundle/Command/KeaConfigCommand.php

@@ -0,0 +1,36 @@
+<?php
+
+namespace KeaBundle\Command;
+
+use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class KeaConfigCommand extends ContainerAwareCommand
+{
+
+    protected function configure()
+    {
+        $this
+            ->setName('kea:config')
+            ->setDescription('Kea DHCP Config')
+            ->setHelp('Create Kea DHCP Config')
+            ->addOption('class', null, InputOption::VALUE_OPTIONAL, 'Kea Class Config in KeaBundle\Services. e.g. BaseKea', 'BaseKea')
+            ;
+    }
+
+    /**
+     * @param InputInterface $input
+     * @param OutputInterface $output
+     */
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+        $this->output = $output;
+        $class = $input->getOption('class');
+        $keaConfigService = $this->getContainer()->get('kea.config');
+
+        $output->writeln($keaConfigService->getConfig($class));
+    }
+
+}

+ 9 - 0
src/KeaBundle/KeaBundle.php

@@ -0,0 +1,9 @@
+<?php
+
+namespace KeaBundle;
+
+use Symfony\Component\HttpKernel\Bundle\Bundle;
+
+class KeaBundle extends Bundle
+{
+}

+ 4 - 0
src/KeaBundle/Resources/config/services.yml

@@ -0,0 +1,4 @@
+services:
+   kea.config:
+       class: KeaBundle\Services\KeaConfigService
+       arguments: ["@doctrine.orm.entity_manager"]

+ 60 - 0
src/KeaBundle/Services/BaseKea.php

@@ -0,0 +1,60 @@
+<?php
+
+namespace KeaBundle\Services;
+
+class BaseKea
+{
+
+    public $subnet4 = [];
+
+
+    /**
+     * @param array $data
+     */
+    public function getConfig($data = array(), $logging = false)
+    {
+        if (isset($data['subnets'])) {
+            $this->subnetConfig($data['subnets']);
+        }
+
+        return json_encode([
+            'Dhcp4' => $this,
+            'Logging' => $this->loggingConfig()
+        ]);
+    }
+
+    /**
+     * @param array $subnets
+     */
+    private function subnetConfig($subnets)
+    {
+        foreach ($subnets as $subnet) {
+            $pools = [];
+            foreach ($subnet->getIpPool() as $pool) {
+                $pools[] = [
+                    'pool' => $pool->getFirstIp() . ' - ' . $pool->getLastIp(),
+                ];
+            }
+            $this->subnet4[] = [
+                'subnet' => $subnet->getAddress(),
+                'pools' => $pools,
+            ];
+        }
+    }
+
+    /**
+     * @return string
+     */
+    private function loggingConfig()
+    {
+        return array(
+            'loggers' => array(
+                'name' => 'kea-dhcp4',
+                'severity' => 'DEBUG',
+                'debuglevel' => 99,
+                'output_options' => array(array('output' => 'stdout')),
+            )
+        );
+    }
+
+}

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

@@ -0,0 +1,42 @@
+<?php
+
+namespace KeaBundle\Services;
+
+class KeaConfigService
+{
+
+    /**
+     * @var Repository
+     */
+    private $subnetRepository;
+
+
+    /**
+     * @param EntityManager $em
+     */
+    public function __construct($em)
+    {
+        $this->subnetRepository = $em->getRepository('IPv4Bundle:SubNet');
+    }
+
+    /**
+     * @param string $className
+     *
+     * @return string
+     */
+    public function getConfig($className = 'BaseKea')
+    {
+        $config = '';
+        $fullClass = 'KeaBundle\\Services\\' . $className;
+        if (class_exists($fullClass)) {
+            $data = [
+                'subnets' => $this->subnetRepository->findAll(),
+            ];
+            $keaConfig = new $fullClass;
+            $config = $keaConfig->getConfig($data);
+        }
+
+        return $config;
+    }
+
+}