浏览代码

Merged in FD3-500 (pull request #70)

FD3-500

Approved-by: Guillermo Espinoza <guillermo@interlink.com.ar>
Gabriel Gosparo 7 年之前
父节点
当前提交
5cf5d33b39

+ 2 - 1
Dockerfile

@@ -22,7 +22,8 @@ RUN chmod 0600 /opt/keys/*
 
 RUN eval $(ssh-agent) ; ssh-add /opt/keys/bitbucket.id_rsa ; composer install --no-scripts --no-interaction --no-progress
 
-RUN apt-get install -yq php-soap
+RUN apt-get install -yq php-soap python-pip
+RUN pip install -e /opt/netmiko
 
 RUN eval $(ssh-agent) ; ssh-add /opt/keys/bitbucket.id_rsa ; git clone git@bitbucket.org:ikflowdat/json-wsdl.git /opt/json-wsdl ;  cd /opt/json-wsdl ; composer install --no-scripts --no-interaction --no-progress
 

+ 41 - 0
app/DoctrineMigrations/Version20180315124145.php

@@ -0,0 +1,41 @@
+<?php
+
+namespace Application\Migrations;
+
+use Doctrine\DBAL\Schema\Schema;
+use MigrationsBundle\Migrations\MigrationsBase;
+
+/**
+ * Auto-generated Migration: Please modify to your needs!
+ */
+class Version20180315124145 extends MigrationsBase
+{
+    /**
+     * @param Schema $schema
+     */
+    public function up(Schema $schema)
+    {
+        // this up() migration is auto-generated, please modify it to your needs
+        if (!$this->existFieldInTable('oltmodel', 'netmiko')) {
+            $this->addsql("ALTER TABLE oltmodel ADD netmiko VARCHAR(255) DEFAULT NULL;");
+        }
+        if (!$this->existFieldInTable('oltmodel_audit', 'netmiko')) {
+            $this->addsql("ALTER TABLE oltmodel_audit ADD netmiko VARCHAR(255) DEFAULT NULL;");
+        }
+        if (!$this->existFieldInTable('olt', 'backups')) {
+            $this->addSql("ALTER TABLE olt ADD backups TINYINT(1) DEFAULT '1';");
+        }
+        if (!$this->existFieldInTable('olt_audit', 'backups')) {
+            $this->addSql("ALTER TABLE olt_audit ADD backups TINYINT(1) DEFAULT '1';");
+        }
+    }
+
+    /**
+     * @param Schema $schema
+     */
+    public function down(Schema $schema)
+    {
+        // this down() migration is auto-generated, please modify it to your needs
+
+    }
+}

+ 2 - 1
app/config/config.yml

@@ -63,6 +63,7 @@ twig:
     strict_variables: "%kernel.debug%"
     form_themes:
         - 'WebserviceBundle:Type:remote_client_widget.html.twig'
+        - 'FTTHBundle:Type:show_anchor.html.twig'
     globals:
         base_url: "%url_base%"
         fd_alerts: '@base_admin.alert_service'
@@ -120,4 +121,4 @@ sonata_admin:
         layout: BaseAdminBundle::standard_layout.html.twig
         user_block: BaseOAuthClientBundle:Core:oauth_user_block.html.twig
         list: BaseAdminBundle::standard_list.html.twig
-    persist_filters: true    
+    persist_filters: true

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

@@ -56,5 +56,7 @@ parameters:
     tr069_pass: admin
     tr069_host: genieacs-nbi
     tr069_port: 7557
+    tr069_selenium_host: selenium
 
-    tr069_selenium_host: selenium
+
+    backups: '%kernel.project_dir%/web/backups-config/ftth/'

+ 2 - 1
app/config/parameters.yml.docker

@@ -51,5 +51,6 @@ parameters:
     tr069_pass: admin
     tr069_host: genieacs-nbi
     tr069_port: 7557
-
     tr069_selenium_host: selenium
+
+    backups: '%kernel.project_dir%/web/backups-config/ftth/'

+ 27 - 3
src/FTTHBundle/Admin/OLTAdmin.php

@@ -2,9 +2,13 @@
 
 namespace FTTHBundle\Admin;
 
+use FTTHBundle\Form\AnchorType;
 use Sonata\AdminBundle\Datagrid\DatagridMapper;
 use Sonata\AdminBundle\Datagrid\ListMapper;
 use Sonata\AdminBundle\Form\FormMapper;
+use Sonata\AdminBundle\Form\Type\CollectionType;
+use Sonata\AdminBundle\Form\Type\Filter\ChoiceType;
+use Sonata\AdminBundle\Form\Type\ModelType;
 use Sonata\AdminBundle\Show\ShowMapper;
 use WorkflowBundle\Admin\WorkflowBaseAdmin;
 
@@ -22,6 +26,7 @@ class OLTAdmin extends WorkflowBaseAdmin
             ->add('snmpCommunity')
             ->add('sshUser')
             ->add('sshPass')
+            ->add('backups')
         ;
     }
 
@@ -38,6 +43,7 @@ class OLTAdmin extends WorkflowBaseAdmin
             ->add('sshUser')
             ->add('sshPass')
             ->add('enable')
+            ->add('backups')
 //            ->add('workflow')
             ->add('currentState','string', array('template' => 'WorkflowBundle:Workflow:base_list_field_current_state.html.twig'))
             ->add('_action', 'with-workflow-action', array(
@@ -81,6 +87,15 @@ class OLTAdmin extends WorkflowBaseAdmin
                         ->add('timeOltOctets', 'integer', array('attr' => array('min' => 0)))
                     ->end()
                 ->end()
+                ->tab('Backups')
+                    ->with('Backup OLT')
+                        ->add('backups')
+                        ->add('files', AnchorType::class, array(
+                            'required' => false,
+                            'data' => $this->getSubject()->obtainFiles($this->getParameter('backups'))
+                        ))
+                    ->end()
+                ->end()
         ;
     }
 
@@ -100,7 +115,7 @@ class OLTAdmin extends WorkflowBaseAdmin
                     ->add('sshPass')
                     ->add('enable')
                 ->end()
-            ->end()   
+            ->end()
             ->tab('Log')
                 ->with('Log')
                     ->add('deviceLog','string', array(
@@ -108,8 +123,17 @@ class OLTAdmin extends WorkflowBaseAdmin
                         'translation_domain' => 'DeviceBundle',
                         ))
                 ->end()
-            ->end()   
+            ->end()
+            ->tab('Backups')
+                ->with('Backup OLT')
+                    ->add('backups')
+                    ->add('files', 'string', array(
+                            'template' => 'FTTHBundle::show_backup_log.html.twig',
+                            'translation_domain' => 'FTTHBundle'
+                            ))
+                ->end()
+            ->end()
         ;
     }
-    
+
 }

+ 4 - 0
src/FTTHBundle/Admin/OLTModelAdmin.php

@@ -19,6 +19,7 @@ class OLTModelAdmin extends BaseAdmin
             ->add('name')
             ->add('mark')
             ->add('library')
+            ->add('netmiko')
             ->add('props')
         ;
     }
@@ -32,6 +33,7 @@ class OLTModelAdmin extends BaseAdmin
             ->add('name')
             ->add('mark')
             ->add('library')
+            ->add('netmiko')
             ->add('props')
             ->add('extraData')
             ->add('_action', null, array(
@@ -55,6 +57,7 @@ class OLTModelAdmin extends BaseAdmin
             ->add('name')
             ->add('mark', 'choice', array('required'=>true, 'choices' => $marks))
             ->add('library', 'choice', array('required'=>true, 'choices' => $libraries))
+            ->add('netmiko')
             ->add('props')
             ->add('extraData')
         ;
@@ -69,6 +72,7 @@ class OLTModelAdmin extends BaseAdmin
             ->add('name')
             ->add('mark')
             ->add('library')
+            ->add('netmiko')
             ->add('props')
             ->add('extraData')
         ;

+ 51 - 0
src/FTTHBundle/Command/GetConfigOLTCommand.php

@@ -0,0 +1,51 @@
+<?php
+
+namespace FTTHBundle\Command;
+
+use FTTHBundle\Entity\OLT;
+use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class GetConfigOLTCommand extends ContainerAwareCommand
+{
+    protected function configure()
+    {
+        $this
+            ->setName('ik:config:olt')
+            ->setDescription('Get config from olt and save in file.')
+            ->setHelp('Get config from olt and save in file.')
+            ->addArgument('olt', InputArgument::OPTIONAL, 'Get all config', 'all');
+    }
+
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+        try {
+            $em = $this->getContainer()->get('doctrine')->getManager();
+            if ($input->getArgument('olt')) {
+                $olts = $em->getRepository("FTTHBundle\Entity\OLT")->findBy(array('backups' => 1));
+            } else {
+                $olts = array();
+                $olts[] = $em->getRepository("FTTHBundle\Entity\OLT")->findOneBy(array('name' => $input->getArgument('olt')));
+            }
+            foreach ($olts as $olt) {
+                if ($olt->getIp() && $olt->getSshUser() && $olt->getSshPass()) {
+                    $cmd = "python /opt/netmiko/run/run_ik.py ";
+                    $cmd .= " --ip '" . $olt->getIp() . "'";
+                    $cmd .= " --username '" . $olt->getSshUser() . "'";
+                    $cmd .= " --password '" . $olt->getSshPass() . "'";
+                    $cmd .= " --secret '" . $olt->getSshPass() . "'";
+                    $cmd .= " --name '" . $olt->getId() . "'";
+                    $cmd .= " --directory " . $this->getContainer()->getParameter('backups') . '/olt';
+                    $cmd .= " --device_type " . $olt->getModel()->getNetmiko();
+                    shell_exec($cmd);
+                }
+            }
+        } catch (\Throwable $e) {
+            echo("\nERROR CODE: " . $e->getCode() . "\nERROR MESSAGE: " . $e->getMessage() . "\n");
+            echo("\nTRACE: \n" . $e->getTraceAsString() . "\n\n");
+        }
+    }
+}

+ 57 - 8
src/FTTHBundle/Entity/OLT.php

@@ -50,7 +50,6 @@ class OLT implements DeviceInterface, TenancyIdTraitInterface, LocationInterface
      * @var string $name
      *
      * @ORM\Column(type="string", length=255)
-
      * @Assert\NotNull
      */
     protected $name;
@@ -160,13 +159,17 @@ class OLT implements DeviceInterface, TenancyIdTraitInterface, LocationInterface
      */
     protected $enable;
 
+    /**
+     * @ORM\Column(type="boolean", options={"default":true})
+     */
+    protected $backups;
 
     /**
      * @return string
      */
     public function __toString()
     {
-        return (string) $this->name;
+        return (string)$this->name;
     }
 
     /**
@@ -527,17 +530,17 @@ class OLT implements DeviceInterface, TenancyIdTraitInterface, LocationInterface
     }
 
     /**
-    * @return int
-    */
+     * @return int
+     */
     public function getLibraryVersion()
     {
         return $this->libraryVersion;
     }
-    
+
     /**
-    * @return array
-    */
-    public function getSoftDeleteCriteria() 
+     * @return array
+     */
+    public function getSoftDeleteCriteria()
     {
         return array('ip' => $this->ip);
     }
@@ -559,4 +562,50 @@ class OLT implements DeviceInterface, TenancyIdTraitInterface, LocationInterface
         return $entities;
     }
 
+    /**
+     * @return mixed
+     */
+    public function getBackups()
+    {
+        return $this->backups;
+    }
+
+    /**
+     * @param mixed $backups
+     */
+    public function setBackups($backups)
+    {
+        $this->backups = $backups;
+    }
+
+    /**
+     * Obtiene los archivos de backups generados.
+     * @param mixed $directory Contiene el directorio a leer.
+     * @return array Retorna un array con los archivos del directorio.
+     */
+    public function obtainFiles($directory)
+    {
+        $resp = array();
+        try {
+            if ($this->getId()) {
+                $dir = $directory . '/olt/' . $this->getId() . '/';
+                $files = scandir($dir);
+                rsort($files);
+                $nc = 0;
+                foreach ($files as $file) {
+                    if ($file != '.' && $file != '..') {
+                        if ($nc < 10) {
+                            $resp[$file] = $dir . $file;
+                        } else {
+                            break;
+                        }
+                        $nc++;
+                    }
+                }
+            }
+        } catch (\Throwable $ignore) {
+        }
+        return $resp;
+    }
+
 }

+ 37 - 13
src/FTTHBundle/Entity/OLTModel.php

@@ -34,10 +34,10 @@ class OLTModel implements TenancyIdTraitInterface
      * @Assert\NotNull
      */
     private $name;
-    
+
     /**
      * @ORM\OneToMany(targetEntity="OLT", mappedBy="model", fetch="EXTRA_LAZY")
-     * 
+     *
      * @JMS\MaxDepth(2)
      */
     protected $olts;
@@ -53,7 +53,7 @@ class OLTModel implements TenancyIdTraitInterface
      * @ORM\Column(type="string", length=255, nullable=true)
      */
     protected $mark;
-    
+
     /**
      * @var string $library
      *
@@ -61,15 +61,21 @@ class OLTModel implements TenancyIdTraitInterface
      */
     protected $library;
 
+    /**
+     * @var string $netmiko
+     *
+     * @ORM\Column(type="string", length=255, nullable=true)
+     */
+    protected $netmiko;
 
     /**
      * @return string
      */
     public function __toString()
     {
-        return (string) $this->name;
+        return (string)$this->name;
     }
-    
+
     /**
      * Get id
      *
@@ -99,7 +105,7 @@ class OLTModel implements TenancyIdTraitInterface
     {
         return $this->name;
     }
-    
+
     /**
      * @param OLT $olt
      * @return OLTModel
@@ -107,7 +113,7 @@ class OLTModel implements TenancyIdTraitInterface
     public function addOlt(ONU $olt)
     {
         $this->olts[] = $olt;
-        
+
         return $this;
     }
 
@@ -118,12 +124,12 @@ class OLTModel implements TenancyIdTraitInterface
     public function removeOlt(OLT $olt)
     {
         $this->olts->removeElement($olt);
-        
+
         return $this;
     }
-    
+
     /**
-     * @return Doctrine\Common\Collections\Collection 
+     * @return Doctrine\Common\Collections\Collection
      */
     public function getOlts()
     {
@@ -145,10 +151,10 @@ class OLTModel implements TenancyIdTraitInterface
     public function setProps($props)
     {
         $this->props = $props;
-        
+
         return $this;
     }
-    
+
     /**
      * @param string $mark
      *
@@ -168,7 +174,7 @@ class OLTModel implements TenancyIdTraitInterface
     {
         return $this->mark;
     }
-    
+
     /**
      * @param string $library
      *
@@ -188,4 +194,22 @@ class OLTModel implements TenancyIdTraitInterface
     {
         return $this->library;
     }
+
+    /**
+     * @return string
+     */
+    public function getNetmiko()
+    {
+        return $this->netmiko;
+    }
+
+    /**
+     * @param string $netmiko
+     */
+    public function setNetmiko($netmiko)
+    {
+        $this->netmiko = $netmiko;
+    }
+
+
 }

+ 10 - 0
src/FTTHBundle/Form/AnchorType.php

@@ -0,0 +1,10 @@
+<?php
+namespace FTTHBundle\Form;
+
+
+use Symfony\Component\Form\AbstractType;
+
+class AnchorType extends AbstractType
+{
+
+}

+ 2 - 0
src/FTTHBundle/Form/OLTType.php

@@ -35,8 +35,10 @@ class OLTType extends AbstractType
             ->add('extraData')
             ->add('tenancyId')
             ->add('enable')
+            ->add('backups')
         ;
 
+
         $builder->addEventListener(
             FormEvents::PRE_SUBMIT,
             function (FormEvent $event) {

+ 6 - 0
src/FTTHBundle/Resources/config/services.yml

@@ -71,3 +71,9 @@ services:
         class: FTTHBundle\EventListener\NAPAddressSubscriber
         tags:
             - { name: doctrine.event_subscriber, connection: default }
+
+    ftth.twig_show_file_system_extension:
+       class: FTTHBundle\Twig\ShowFileSystemExtension
+       arguments: ["@service_container"]
+       tags:
+           - { name: twig.extension }

+ 13 - 1
src/FTTHBundle/Resources/translations/FTTHBundle.es.yml

@@ -46,6 +46,9 @@ filter:
     label_slot: Slot
     label_link: Link
     label_address: Dirección
+    label_netmiko: Netmiko
+    label_backups: Habilitar Backup
+    label_files: Archivos
     label_capacity: Capacidad
 
 breadcrumb:
@@ -125,6 +128,9 @@ form:
     label_tr069_ip: IP
     label_tr069_broadcast: Broadcast SSID
     label_tr069_status: Enable SSID
+    label_netmiko: Netmiko
+    label_backups: Habilitar Backup
+    label_files: Archivos
     label_capacity: Capacidad
 
 list:
@@ -169,6 +175,9 @@ list:
     label_address: Dirección
     label_service_port: Service Port
     label_position: Posición
+    label_netmiko: Netmiko
+    label_backups: Habilitar Backup
+    label_files: Archivos
     label_capacity: Capacidad
 
 show:
@@ -222,8 +231,10 @@ show:
     label_tr069_ip: IP
     label_tr069_broadcast: Broadcast SSID
     label_tr069_status: Enable SSID
+    label_netmiko: Netmiko
+    label_backups: Habilitar Backup
+    label_files: Archivos
     label_capacity: Capacidad
-    
 helps:
     check_address: Verifique la dirección para el cálculo de distancias.
     onu_serial_number: Serial Number de la ONU conformado por 16 caracteres, si es vacío lo calculamos desde el Identificador.
@@ -261,3 +272,4 @@ without_olt: SIN OLT
 onu_distance_nap_not_configurated: Requiere configuración de distancia
 nap_not_positioned: NAP no posicionada
 unique_field_ponserialnumber: Existe una ONU creada con el identificador %ponSerialNumber%
+Files: Archivos

+ 9 - 0
src/FTTHBundle/Resources/views/Type/show_anchor.html.twig

@@ -0,0 +1,9 @@
+{% block anchor_widget %}
+    {% spaceless %}
+        {{ form_start(form)}}
+        {%  set data = form.vars.data %}
+        {% for name,path in data %}
+            <a href="{{ path }}" download id="download">{{ name }}</a><br>
+        {% endfor %}
+    {% endspaceless %}
+{% endblock %}

+ 9 - 0
src/FTTHBundle/Resources/views/show_backup_log.html.twig

@@ -0,0 +1,9 @@
+{% extends 'SonataAdminBundle:CRUD:base_show_field.html.twig' %}
+
+{% block field %}
+    {% set data = show_file_system(object) %}
+    {% for name,path in data %}
+        <a href="{{ path }}" download id="download">{{ name }}</a><br>
+    {% endfor %}
+
+{% endblock %}

+ 67 - 0
src/FTTHBundle/Twig/ShowFileSystemExtension.php

@@ -0,0 +1,67 @@
+<?php
+
+namespace FTTHBundle\Twig;
+
+
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+class ShowFileSystemExtension extends \Twig_Extension
+{
+    /**
+     * @var ContainerInterface
+     */
+    protected $container;
+
+    /**
+     * ClientFilterListListener constructor.
+     */
+    public function __construct(ContainerInterface $container)
+    {
+        $this->setContainer($container);
+    }
+
+    /**
+     * @return ContainerInterface
+     */
+    public function getContainer()
+    {
+        return $this->container;
+    }
+
+    /**
+     * @param ContainerInterface $container
+     */
+    public function setContainer($container)
+    {
+        $this->container = $container;
+    }
+
+    /**
+     * @return array
+     */
+    public function getFunctions()
+    {
+        return array(
+            new \Twig_SimpleFunction('show_file_system', array($this, 'getShowFileSystem')),
+        );
+    }
+
+    /**
+     * @param Entity $object
+     *
+     * @return array
+     */
+    public function getShowFileSystem($object)
+    {
+        return $object->obtainFiles($this->getContainer()->getParameter('backups'));
+    }
+
+    /**
+     * @return string
+     */
+    public function getName()
+    {
+        return 'show_file_system_extension';
+    }
+
+}

+ 2 - 0
src/FTTHBundle/tests/ONURESTControllerTest.php

@@ -138,6 +138,7 @@ class ONURESTControllerTest extends WebTestCaseBase
         $datos['timePonStats'] = 5;
         $datos['timeOnuStats'] = 5;
         $datos['tenancyId'] = 1;
+        $datos['backups'] = 1;
 
         if ($key == null) {
             return $datos;
@@ -203,6 +204,7 @@ class ONURESTControllerTest extends WebTestCaseBase
         // hago la inserccion llamando al servicio por post
         $this->getClient()->request('POST', $this->getOLTUri(), $this->obtainOLTData());
         $response = $this->getClient()->getResponse();
+        var_dump($response);
         $this->getClient()->request('POST', $this->getNAPUri(), $this->obtainNAPData());
         $response = $this->getClient()->getResponse();
         $this->getClient()->request('POST', $this->getUri(), $this->obtainData());