瀏覽代碼

FD3-660 comando para actualizar cablemodem

Espinoza Guillermo 6 年之前
父節點
當前提交
56f6a69b73
共有 4 個文件被更改,包括 177 次插入7 次删除
  1. 7 0
      app/DoctrineMigrations/src/action.yml
  2. 2 1
      composer.json
  3. 7 6
      composer.lock
  4. 161 0
      src/HostBundle/Command/CablemodemUpdateCommand.php

+ 7 - 0
app/DoctrineMigrations/src/action.yml

@@ -29,3 +29,10 @@ insertorupdate:
           event: "a:3:{i:0;s:11:\"postPersist\";i:1;s:10:\"postUpdate\";i:2;s:9:\"preRemove\";}"
           event: "a:3:{i:0;s:11:\"postPersist\";i:1;s:10:\"postUpdate\";i:2;s:9:\"preRemove\";}"
           template: "echo \"{{object.mac}} Config on Create/Update/Remove (postPersist / PostUpdate / PreRemove)\"\r\nphp /opt/dhcp/bin/console workflow:apply --entity=IPv4Bundle\\\\Entity\\\\SubNet --id={{object.id}} --workflow=dhcp_workflow --transition=\"add_config\""
           template: "echo \"{{object.mac}} Config on Create/Update/Remove (postPersist / PostUpdate / PreRemove)\"\r\nphp /opt/dhcp/bin/console workflow:apply --entity=IPv4Bundle\\\\Entity\\\\SubNet --id={{object.id}} --workflow=dhcp_workflow --transition=\"add_config\""
           tenancy_id: 1
           tenancy_id: 1
+        -
+          id: 5
+          name: "Update fixed address Cablemodem"
+          object_class: "HostBundle\\Entity\\Host"
+          event: "a:1:{i:0;s:9:\"preUpdate\";}"
+          template: "echo \"Actualizando Cablemodem\"\r\n\r\nphp bin/console cablemodem:update --id={{object.id}}"
+          tenancy_id: 1  

+ 2 - 1
composer.json

@@ -180,7 +180,8 @@
             "chown -Rf www-data:www-data app/Resources/workflows",
             "chown -Rf www-data:www-data app/Resources/workflows",
             "cp -n app/Resources/workflows/workflow_list.yml.dist app/Resources/workflows/workflow_list.yml",
             "cp -n app/Resources/workflows/workflow_list.yml.dist app/Resources/workflows/workflow_list.yml",
             "chown -Rf www-data:www-data app/Resources/workflows/workflow_list.yml",
             "chown -Rf www-data:www-data app/Resources/workflows/workflow_list.yml",
-            "chown -Rf www-data:www-data web/workflows_png"
+            "chown -Rf www-data:www-data web/workflows_png",
+            "php bin/console workflow:default --class=HostBundle\\\\Entity\\\\Host --all"
         ],
         ],
         "mkdir-dir-cache-sessions-logs": "mkdir -p var/cache var/logs var/sessions",
         "mkdir-dir-cache-sessions-logs": "mkdir -p var/cache var/logs var/sessions",
         "chmod-dir-cache-sessions-logs": "chmod 777 -R var/cache var/logs var/sessions",
         "chmod-dir-cache-sessions-logs": "chmod 777 -R var/cache var/logs var/sessions",

+ 7 - 6
composer.lock

@@ -1034,6 +1034,7 @@
                 }
                 }
             ],
             ],
             "description": "Dwoo is a PHP5/PHP7 template engine which is (almost) fully compatible with Smarty templates and plugins, but is written from scratch for PHP5, and adds many features.",
             "description": "Dwoo is a PHP5/PHP7 template engine which is (almost) fully compatible with Smarty templates and plugins, but is written from scratch for PHP5, and adds many features.",
+            "abandoned": "twig/twig",
             "time": "2018-07-25T13:24:51+00:00"
             "time": "2018-07-25T13:24:51+00:00"
         },
         },
         {
         {
@@ -1457,7 +1458,7 @@
             "source": {
             "source": {
                 "type": "git",
                 "type": "git",
                 "url": "ssh://git@200.50.168.30:222/VendorSoftwareFlowdat3/BaseAdmin.git",
                 "url": "ssh://git@200.50.168.30:222/VendorSoftwareFlowdat3/BaseAdmin.git",
-                "reference": "aa9e5432f4fc70ae78b4912a68479afcf3f9137b"
+                "reference": "cd1449790213410afbf0cce650997c9c24a481d8"
             },
             },
             "type": "library",
             "type": "library",
             "autoload": {
             "autoload": {
@@ -1472,7 +1473,7 @@
                 "bootstrap",
                 "bootstrap",
                 "sonata"
                 "sonata"
             ],
             ],
-            "time": "2018-08-08T13:50:30+00:00"
+            "time": "2018-08-14T12:58:30+00:00"
         },
         },
         {
         {
             "name": "ik/device-bundle",
             "name": "ik/device-bundle",
@@ -1599,7 +1600,7 @@
             "source": {
             "source": {
                 "type": "git",
                 "type": "git",
                 "url": "ssh://git@200.50.168.30:222/VendorSoftwareFlowdat3/TemplateBundle.git",
                 "url": "ssh://git@200.50.168.30:222/VendorSoftwareFlowdat3/TemplateBundle.git",
-                "reference": "7e50327475180f03fa24a2fa5ee1dc01dadd2bfa"
+                "reference": "e702a3895f9d3fe6712a76d25cb9d908216ef20f"
             },
             },
             "require": {
             "require": {
                 "ik/base-admin-bundle": "*"
                 "ik/base-admin-bundle": "*"
@@ -1617,7 +1618,7 @@
                 "bundle",
                 "bundle",
                 "template"
                 "template"
             ],
             ],
-            "time": "2018-07-04T14:44:27+00:00"
+            "time": "2018-08-09T12:30:44+00:00"
         },
         },
         {
         {
             "name": "ik/webservice-bundle",
             "name": "ik/webservice-bundle",
@@ -1646,7 +1647,7 @@
             "source": {
             "source": {
                 "type": "git",
                 "type": "git",
                 "url": "ssh://git@200.50.168.30:222/VendorSoftwareFlowdat3/WorkflowBundle.git",
                 "url": "ssh://git@200.50.168.30:222/VendorSoftwareFlowdat3/WorkflowBundle.git",
-                "reference": "fcac5fb3107129034dc231d900701c0df65d8a7c"
+                "reference": "b6ea92f53b7dc06a6697f25872039eeb0e8091f2"
             },
             },
             "require": {
             "require": {
                 "php-amqplib/rabbitmq-bundle": "^1.12"
                 "php-amqplib/rabbitmq-bundle": "^1.12"
@@ -1676,7 +1677,7 @@
                 "bundle",
                 "bundle",
                 "workflow"
                 "workflow"
             ],
             ],
-            "time": "2018-08-07T12:54:41+00:00"
+            "time": "2018-08-15T13:38:08+00:00"
         },
         },
         {
         {
             "name": "incenteev/composer-parameter-handler",
             "name": "incenteev/composer-parameter-handler",

+ 161 - 0
src/HostBundle/Command/CablemodemUpdateCommand.php

@@ -0,0 +1,161 @@
+<?php
+
+namespace HostBundle\Command;
+
+use Buzz\Message\RequestInterface as HttpRequestInterface;
+use HostBundle\Entity\Host;
+use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Style\SymfonyStyle;
+
+class CablemodemUpdateCommand extends ContainerAwareCommand
+{
+
+    /**
+     * @see Command
+     */
+    protected function configure()
+    {
+        $params = [
+            'CMD_USERNAME' => getenv("CMD_USERNAME") ? getenv("CMD_USERNAME") : 'admin',
+            'CMD_PASSWORD' => getenv("CMD_PASSWORD") ? getenv("CMD_PASSWORD") : 'admin'
+        ];
+
+        $host_cablemodem = getenv("HOST_CABLEMODEM");
+
+        $this
+            ->setName('cablemodem:update')
+            ->setDescription('Update cablemodem record associated to a host')
+            ->setDefinition(array(
+                new InputOption('id', null, InputOption::VALUE_REQUIRED, 'Host id'),
+                new InputOption(
+                    'url-get', 
+                    null, 
+                    InputOption::VALUE_OPTIONAL, 
+                    'API URL GET cablemodems', 
+                    "https://{$host_cablemodem}/api/cablemodems.json"
+                ),
+                new InputOption(
+                    'url-put', 
+                    null, 
+                    InputOption::VALUE_OPTIONAL, 
+                    'API URL PUT cablemodems', 
+                    "https://{$host_cablemodem}/api/cablemodems/{id}.json"
+                ),
+                new InputOption('api-username', null, InputOption::VALUE_OPTIONAL, 'API username', $params["CMD_USERNAME"]),
+                new InputOption('api-password', null, InputOption::VALUE_OPTIONAL, 'API password', $params["CMD_PASSWORD"]),
+            ))
+            ->setHelp(<<<EOT
+The <info>cablemodem:update</info> command updates a Cablemodem associated to a DHCP Host
+EOT
+        );
+    }
+
+    /**
+     * @param InputInterface $input
+     * @param OutputInterface $output
+     */
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+        $this->input = $input;
+        $this->io = new SymfonyStyle($input, $output);
+        
+        $this->id = $input->getOption('id');
+        
+        if (!$this->id) {
+            $output->writeln("<error>Parameter id is required</error>");
+            
+            return;
+        }
+        
+        $this->credentials = [
+            'username' => $input->getOption('api-username'),
+            'password' => $input->getOption('api-password'),
+        ];
+        $this->webservice = $this->getContainer()->get('webservice');
+        $em = $this->getContainer()->get('doctrine.orm.entity_manager');
+        
+        if ($this->host = $em->getRepository(Host::class)->find($this->id)) {
+            $mac = $this->host->getMac();
+            if (is_null($mac)) {
+                // El host no tiene mac, chequeo si tiene host asociado
+                $mac = $this->host->getHost()->getMac();
+            }
+            // Busco el cablemodem asociado
+            $this->io->section('Searching Cablemodem');
+            $this->cablemodem = $this->findCablemodem($mac);
+            $this->io->success('RESULT: ' . json_encode($this->cablemodem));
+            
+            if ($this->cablemodem) {
+                $this->io->section('Updating Cablemodem');
+                $this->cablemodem = $this->updateCablemodem($this->cablemodem, $this->host);
+                $this->io->success('RESULT: ' . json_encode($this->cablemodem));
+            } else {
+                $output->writeln("<error>Cablemodem MAC {$mac} not found</error>");
+            }
+        } else {
+            $output->writeln("<error>Host id {$this->id} not found</error>");
+        }
+    }
+    
+    /**
+     * @param string $mac
+     *
+     * @return null|array
+     */
+    private function findCablemodem($mac)
+    {
+        $ws = $this->webservice;
+        $urlGET = $this->input->getOption('url-get');
+        
+        // Consulto en Cablemodem
+        $cablemodem = null;
+        $url = $ws->buildUrl($urlGET, compact('mac'));
+        $cablemodemJSON = $ws->makeGetRequest($url, HttpRequestInterface::METHOD_GET, [], $this->credentials);
+        if ($cablemodemJSON) {
+            $cablemodem = current(json_decode($cablemodemJSON, true));
+        }
+        
+        return $cablemodem;
+    }
+    
+    /**
+     * @param array $cablemodem
+     * @param Host $host
+     *
+     * @return array
+     */
+    private function updateCablemodem($cablemodem, $host)
+    {
+        $ws = $this->webservice;
+        $id = $cablemodem['id'];
+        $urlPUT = str_replace('{id}', $id, $this->input->getOption('url-put'));
+        $fixedAddress = $host->getFixedAddress();
+        $data = [];
+        switch ($host->getHostType()->getShortname()) {
+            case 'cablemodem':
+                $data['fixedIP'] = $fixedAddress;
+                break;
+            case 'cpe':
+                $data['cpeFixedIP'] = $fixedAddress;
+                break;
+            case 'mta':
+                $data['mtaFixedIP'] = $fixedAddress;
+                break;
+            default:
+                $data['fixedIP'] = $fixedAddress;
+                break;
+        }
+        
+        $cablemodemJSON = $ws->makeGetRequest($urlPUT, HttpRequestInterface::METHOD_PUT, $data, $this->credentials);
+        if ($cablemodemJSON) {
+            $cablemodem = current(json_decode($cablemodemJSON, true));
+        }
+        
+        return $cablemodem;
+    }
+    
+}