KeaConfigCommand.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace KeaBundle\Command;
  3. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  4. use Symfony\Component\Console\Input\InputInterface;
  5. use Symfony\Component\Console\Input\InputOption;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. class KeaConfigCommand extends ContainerAwareCommand
  8. {
  9. protected function configure()
  10. {
  11. $this
  12. ->setName('kea:config')
  13. ->setDescription('Kea DHCP Config')
  14. ->setHelp('Create Kea DHCP Config')
  15. ->addOption('class', null, InputOption::VALUE_OPTIONAL, 'Kea Class Config in KeaBundle\Services. e.g. BaseKea', 'BaseKea')
  16. ->addOption('id', null, InputOption::VALUE_OPTIONAL, 'DHCP Entity ID. e.g.: 1', 1)
  17. ->addOption('library', null, InputOption::VALUE_OPTIONAL, 'Hook library path', '/kea-cm-hook/kea-hook-flowdat.so')
  18. ->addOption('set', null, InputOption::VALUE_NONE, 'Send Config DHCP4 to KEA')
  19. ;
  20. }
  21. /**
  22. * @param InputInterface $input
  23. * @param OutputInterface $output
  24. */
  25. protected function execute(InputInterface $input, OutputInterface $output)
  26. {
  27. $this->output = $output;
  28. $class = $input->getOption('class');
  29. $id = $input->getOption('id');
  30. $library = $input->getOption('library');
  31. $keaConfigService = $this->getContainer()->get('kea.config');
  32. $setConfig = (int) $input->getOption('set');
  33. if ($setConfig) {
  34. $dhcpRepository = $this->getContainer()->get("doctrine.orm.entity_manager")->getRepository("DHCPBundle\Entity\DHCP");
  35. $dhcp = $dhcpRepository->findOneById($id);
  36. // selecciono el primer KEA DHCP que se encuentre
  37. if (!$dhcp) {
  38. $dhcpAll = $dhcpRepository->findAll();
  39. $dhcp = count($dhcpAll) ? current($dhcpAll) : null;
  40. }
  41. if ($dhcp) {
  42. $config = $keaConfigService->getConfig($id, $class, $library);
  43. $output->writeln($keaConfigService->setConfig($dhcp, $config));
  44. } else {
  45. $output->writeln("KEA DHCP id:{$id} <error>not found!</error>");
  46. }
  47. } else {
  48. $output->writeln($keaConfigService->getConfig($id, $class, $library));
  49. }
  50. }
  51. }