123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- <?php
- namespace KeaBundle\Services;
- use Doctrine\ORM\EntityManager;
- use Doctrine\ORM\EntityRepository;
- use DHCPBundle\Entity\DHCP;
- use KeaBundle\Interfaces\KeaConfigInterface;
- use WebserviceBundle\Services\Webservice;
- class KeaConfigService
- {
- /**
- * @var EntityRepository
- */
- private $dhcpRepository;
- /**
- * @var EntityRepository
- */
- private $subnetRepository;
- /**
- * @var EntityRepository
- */
- private $hostRepository;
- /**
- * @var Webservice
- */
- private $webService;
- /**
- * @param Webservice $ws
- * @param EntityManager $em
- */
- public function __construct(Webservice $ws, EntityManager $em)
- {
- $this->dhcpRepository = $em->getRepository('DHCPBundle:DHCP');
- $this->subnetRepository = $em->getRepository('IPv4Bundle:SubNet');
- $this->hostRepository = $em->getRepository('HostBundle:Host');
- $this->webService = $ws;
- }
- /**
- * @param int $id
- * @param string $className
- * @param string $library
- *
- * @return string
- */
- public function getConfig($id, $className = 'BaseKea', $library = '/kea-cm-hook/kea-hook-flowdat.so')
- {
- $config = '';
- $fullClass = 'KeaBundle\\Services\\' . $className;
- if (class_exists($fullClass)) {
- $keaConfig = new $fullClass;
- if ($keaConfig instanceof KeaConfigInterface) {
- $data = [
- 'dhcp' => $id ? $this->dhcpRepository->find($id) : null,
- 'hosts' => $this->hostRepository->findAll(),
- 'subnets' => $this->subnetRepository->findAll(),
- 'library' => $library,
- ];
- $config = $keaConfig->getConfig($data);
- }
- }
- return $config;
- }
- /**
- * @param DHCP $dhcp
- * @param string $service
- *
- * @return string
- */
- public function getRemoteConfig(DHCP $dhcp, $service = "dhcp4")
- {
- $host = $dhcp->getHost();
- if (is_null($host)) $host = "kea:8080";
-
- $_params = array(
- "command" => "config-get",
- "service" => array($service),
- );
-
- $params = json_encode($_params);
-
- $config = $this->curlPost($host, $params);
- return $config;
- }
- /**
- * @param DHCP $dhcp
- * @param string $service
- *
- * @return string
- */
- public function reloadConfig(DHCP $dhcp, $service = "dhcp4")
- {
- $host = $dhcp->getHost();
- if(is_null($host)) $host = "kea:8080";
-
- $_params = array(
- "command" => "config-reload",
- "service" => array($service),
- );
-
- $params = json_encode($_params);
-
- $config = $this->curlPost($host, $params);
- return $config;
- }
- /**
- * @param DHCP $dhcp
- * @param string $service
- *
- * @return string
- */
- public function reloadHook(DHCP $dhcp, $service = "dhcp4")
- {
- $host = $dhcp->getHost();
- if(is_null($host)) $host = "kea:8080";
-
- $_params = array(
- "command" => "libreload",
- "service" => array($service),
- );
-
- $params = json_encode($_params);
-
- $config = $this->curlPost($host, $params);
- return $config;
- }
- /**
- * @param DHCP $dhcp
- * @param json params
- *
- * @return string
- */
- public function setConfig(DHCP $dhcp, $params = null)
- {
- $host = $dhcp->getHost();
- if(is_null($host)) $host = "kea:8080";
- $_params = json_decode($params, true);
- $json = json_encode($_params);
- if(isset($_params[0]) && isset($_params[0]['arguments'])) {
- $_params = array(
- "command" => "config-set",
- "service" => array("dhcp4"),
- "arguments" => $_params[0]['arguments']);
- } else {
- return json_encode(array("error" => "Config Error. Check template DHCP"));
- }
-
- $config = $this->curlPost($host, json_encode($_params));
- return $config;
- }
- /**
- * @param string $host
- * @param array $params
- *
- * @return string
- */
- private function curlPost($host, $params)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, "http://{$host}");
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($params)));
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- try {
- $return = curl_exec($ch);
- } catch (\Exception $ex) {
- return json_encode(array("error" => "Kea Connection Error."));
- }
- return $return;
- }
- }
|