|
@@ -0,0 +1,217 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace HostBundle\Services;
|
|
|
+
|
|
|
+use HostBundle\Entity\Host;
|
|
|
+use HostBundle\Entity\HostType;
|
|
|
+use Doctrine\ORM\EntityManager;
|
|
|
+use Symfony\Component\Console\Helper\ProgressBar;
|
|
|
+use Symfony\Component\Validator\Validator\ValidatorInterface;
|
|
|
+
|
|
|
+class DHCPOptionsService
|
|
|
+{
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var EntityManager
|
|
|
+ */
|
|
|
+ private $em;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var ValidatorInterface
|
|
|
+ */
|
|
|
+ private $validator;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param EntityManager $em
|
|
|
+ * @param ValidatorInterface $validator
|
|
|
+ */
|
|
|
+ public function __construct(EntityManager $em, ValidatorInterface $validator)
|
|
|
+ {
|
|
|
+ $this->em = $em;
|
|
|
+ $this->validator = $validator;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param OutputInterface $output
|
|
|
+ *
|
|
|
+ * @return int
|
|
|
+ */
|
|
|
+ public function formatDhcpOptions($output)
|
|
|
+ {
|
|
|
+ $i = 0;
|
|
|
+ $batchSize = 20;
|
|
|
+ $detach = [];
|
|
|
+
|
|
|
+ $hostRepository = $this->em->getRepository(Host::class);
|
|
|
+ $hosts = $hostRepository->findAll();
|
|
|
+
|
|
|
+ ProgressBar::setFormatDefinition('custom', ' %current%/%max% [%bar%] %percent:3s%% %message%');
|
|
|
+ $progressBar = new ProgressBar($output, count($hosts));
|
|
|
+ $progressBar->setFormat('custom');
|
|
|
+ $progressBar->start();
|
|
|
+ foreach ($hosts as $host) {
|
|
|
+ $mtaOptions = $cpeOptions = '';
|
|
|
+ $options = $host->getOptions();
|
|
|
+ $json_options = json_decode($options, true, 512, JSON_BIGINT_AS_STRING);
|
|
|
+ // las options del host ya tienen formato json
|
|
|
+ if (json_last_error() == JSON_ERROR_NONE) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ $config = $this->setAltDhcpParsing($options, $mtaOptions, $cpeOptions);
|
|
|
+
|
|
|
+ $progressBar->setMessage('Updating DHCP options...');
|
|
|
+ $host->setDHCPOption($config);
|
|
|
+ $host->setOptions(json_encode($config));
|
|
|
+
|
|
|
+ $detach[] = $host;
|
|
|
+ $i++;
|
|
|
+
|
|
|
+ if ($mtaOptions != '') {
|
|
|
+ $progressBar->setMessage('Creating MTA Host...');
|
|
|
+ $mtaHost = $this->createHost('mta', $mtaOptions, $host);
|
|
|
+ if ($mtaHost) {
|
|
|
+ $detach[] = $mtaHost;
|
|
|
+ $i++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ($cpeOptions != '') {
|
|
|
+ $progressBar->setMessage('Creating CPE Host...');
|
|
|
+ $cpeHost = $this->createHost('cpe', $cpeOptions, $host);
|
|
|
+ if ($cpeHost) {
|
|
|
+ $detach[] = $cpeHost;
|
|
|
+ $i++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (($i % $batchSize) === 0) {
|
|
|
+ $this->em->flush();
|
|
|
+ foreach ($detach as $entity) {
|
|
|
+ $this->em->detach($entity);
|
|
|
+ }
|
|
|
+ $detach = [];
|
|
|
+ }
|
|
|
+ $progressBar->advance();
|
|
|
+ }
|
|
|
+ $progressBar->finish();
|
|
|
+
|
|
|
+ $this->em->flush();
|
|
|
+ $this->em->clear();
|
|
|
+
|
|
|
+ return $i;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Parsea la $config y retorna un array asociativo
|
|
|
+ * @param string $config
|
|
|
+ *
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ function setAltDhcpParsing($config, &$mtaOptions = '', &$cpeOptions = '')
|
|
|
+ {
|
|
|
+ $rtr = json_decode($config, true, 512, JSON_BIGINT_AS_STRING);
|
|
|
+ if (!is_array($rtr)) {
|
|
|
+ $rtr = array();
|
|
|
+ foreach (preg_split("|\n|", $config) as $line) {
|
|
|
+ $p = preg_split("|\s+|", trim($line));
|
|
|
+ if (trim($line) === "") continue;
|
|
|
+ if ($p[0] === "option") {
|
|
|
+ if ($p[1] === "routers") {
|
|
|
+ $rtr["routers"] = preg_replace("|;$|", "", $p[2]);
|
|
|
+ } else if ($p[1] === "subnet-mask") {
|
|
|
+ $rtr["subnet_mask"] = preg_replace("|;$|", "", $p[2]);
|
|
|
+ } else if ($p[1] === "broadcast-address") {
|
|
|
+ $rtr["broadcast_address"] = preg_replace("|;$|", "", $p[2]);
|
|
|
+ } else if ($p[1] === "domain-name") {
|
|
|
+ $rtr["domain_name"] = preg_replace("/\"|;$/", "", $p[2]);
|
|
|
+ } else if ($p[1] === "domain-name-servers") {
|
|
|
+ $val = implode("", array_slice($p,2));
|
|
|
+ $rtr["domain_name_servers"] = preg_replace("/;$/", "", $val);
|
|
|
+ } else if ($p[1] === "log-servers") {
|
|
|
+ $val = implode("", array_slice($p,2));
|
|
|
+ $rtr["log_servers"] = preg_replace("/;$/", "", $val);
|
|
|
+ } else if ($p[1] === "time-servers") {
|
|
|
+ $val = implode("", array_slice($p,2));
|
|
|
+ $rtr["time_servers"] = preg_replace("/;$/", "", $val);
|
|
|
+ } else if ($p[1] === "tftp-server-name") {
|
|
|
+ $rtr["tftp_server"] = preg_replace("/\"|;$/", "", $p[2]);
|
|
|
+ } else if ($p[1] === "option122.provisioning-type") {
|
|
|
+ $rtr["option122.provisioning-type"] = preg_replace("/\"|;$/", "", $p[2]);
|
|
|
+ } else if ($p[1] === "option122.provisioning-server") {
|
|
|
+ $rtr["option122.provisioning-server"] = preg_replace("/\"|;$/", "", $p[2]);
|
|
|
+ } else if ($p[1] === "option122.dhcp-server") {
|
|
|
+ $rtr["option122.dhcp-server"] = preg_replace("/;$/", "", $p[2]);
|
|
|
+ } else if ($p[1] === "time-offset") {
|
|
|
+ $rtr["time_offset"] = preg_replace("/;$/", "", $p[2]);
|
|
|
+ } else if ($p[1] === "host-name") {
|
|
|
+ $rtr["host_name"] = preg_replace("/\"|;$/", "", $p[2]);
|
|
|
+ } else {
|
|
|
+ throw new \Exception("Option not soported by setAltDhcpParsing ".$line);
|
|
|
+ }
|
|
|
+ } else if ($p[0] === "max-lease-time") {
|
|
|
+ $rtr["max_lease_time"] = preg_replace("/;$/", "", $p[1]);
|
|
|
+ } else if ($p[0] === "filename") {
|
|
|
+ $rtr["filename"] = preg_replace("/\"|;$/", "", $p[1]);
|
|
|
+ } else if ($p[0] === "fixed-address") {
|
|
|
+ $rtr["fixed_address"] = preg_replace("/;$/", "", $p[1]);
|
|
|
+ } else if ($p[0] === "next-server") {
|
|
|
+ $rtr["next_server"] = preg_replace("/;$/", "", $p[1]);
|
|
|
+ } else if ($p[0] === "default-lease-time") {
|
|
|
+ $rtr["default_lease_time"] = preg_replace("/;$/", "", $p[1]);
|
|
|
+ } else if (strpos($p[0], 'mta.') !== false) {
|
|
|
+ $mtaOptions .= preg_replace('/mta./', '', $line, 1) . "\n";
|
|
|
+ } else if (strpos($p[0], 'cpe.') !== false) {
|
|
|
+ $cpeOptions .= preg_replace('/cpe./', '', $line, 1) . "\n";
|
|
|
+ } else {
|
|
|
+ throw new \Exception("Config not soported by setAltDhcpParsing ".$line);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $rtr = (array)$rtr;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $rtr;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param string $type
|
|
|
+ * @param string $options
|
|
|
+ * @param Host $entity
|
|
|
+ *
|
|
|
+ * @return Host
|
|
|
+ */
|
|
|
+ public function createHost($type, $options, $entity)
|
|
|
+ {
|
|
|
+ // Creo los hosts mta y cpe asociados
|
|
|
+ $hostTypeRepository = $this->em->getRepository(HostType::class);
|
|
|
+ $hostType = $hostTypeRepository->findOneByShortname($type);
|
|
|
+ $hostRepository = $this->em->getRepository(Host::class);
|
|
|
+ $host = $hostRepository->findOneBy([
|
|
|
+ 'host' => $entity,
|
|
|
+ 'hostType' => $hostType,
|
|
|
+ ]);
|
|
|
+ if (is_null($host)) {
|
|
|
+ $options = $this->setAltDhcpParsing($options);
|
|
|
+
|
|
|
+ $host = new Host();
|
|
|
+ $host->setHost($entity);
|
|
|
+ $host->setHostType($hostType);
|
|
|
+ $host->setDHCPOption($options);
|
|
|
+ $host->setOptions(json_encode($options));
|
|
|
+ if (isset($options['fixed_address'])) {
|
|
|
+ $host->setFixedIP(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($this->validator->validate($host)->count() == 0) {
|
|
|
+ $this->em->persist($host);
|
|
|
+
|
|
|
+ return $host;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|