Skip to content

Instantly share code, notes, and snippets.

@ArnaudD
Created June 16, 2011 00:17
Show Gist options
  • Save ArnaudD/1028442 to your computer and use it in GitHub Desktop.
Save ArnaudD/1028442 to your computer and use it in GitHub Desktop.
diff --git a/lib/model/CommandPeer.php b/lib/model/CommandPeer.php
index 61dbe7f..5fe762b 100644
--- a/lib/model/CommandPeer.php
+++ b/lib/model/CommandPeer.php
@@ -86,4 +86,79 @@ class CommandPeer extends BaseCommandPeer {
return $command;
}
+ /**
+ * Lance la mise à jour le DNS.
+ * Commande à lancer lorsqu'une(des) machine(s) à été ajoutée ou modifiée.
+ *
+ *
+ * @static
+ * @param $hosts Tableau ou Object de la classe Host
+ * @return Command
+ */
+ public static function runDnsUpdate ($hosts)
+ {
+ $addScript = sfConfig::get('sf_manitou_ns_add_command');
+ $deleteScript = sfConfig::get('sf_manitou_ns_delete_command');
+ $script = '';
+
+ $keys = array ('%domain%', '%reverse_domain%', '%hostname%', '%host_ip%');
+ foreach ((array) $hosts as $host)
+ {
+ $replacedData = array (
+ escapeshellarg ($host->getDomainName ()),
+ escapeshellarg ($host->getRevDomainName ()),
+ escapeshellarg ($host->getHostname ()),
+ escapeshellarg ($host->getIpAddress ()),
+ );
+
+ // On supprime l'hôte par précaution au cas où il existe déjà
+ $script .= str_replace ($keys, $replacedData, $deleteScript).'; ';
+ $script .= str_replace ($keys, $replacedData, $addScript).'; ';
+ }
+
+ // faire exécuter toutes ces commandes par bash permet de récupérer la sortie en une seule fois
+ $script = 'bash '.escapeshellarg ($script);
+
+ $command = new Command ();
+ $command->setCommand ($script);
+ $command->setLabel ('Mise à jour des entrées du DNS');
+
+ return $command->exec (true);
+ }
+
+ /**
+ * Lance la mise à jour le DNS.
+ * Commande à lancer lorsqu'une(des) machine(s) à été ajoutée ou modifiée.
+ *
+ *
+ * @static
+ * @param $hosts Tableau ou Object de la classe Host
+ * @return Command
+ */
+ public static function runDnsDelete ($hosts)
+ {
+ $deleteScript = sfConfig::get('sf_manitou_ns_delete_command');
+ $script = '';
+
+ $keys = array ('%domain%', '%reverse_domain%', '%host_ip%');
+ foreach ((array) $hosts as $host)
+ {
+ $script .= str_replace ($keys, array (
+ escapeshellarg ($host->getDomainName ()),
+ escapeshellarg ($host->getRevDomainName ()),
+ escapeshellarg ($host->getIpAddress ()),
+ ), $deleteScript).'; ';
+ }
+
+ // faire exécuter toutes ces commandes par bash permet de récupérer la sortie en une seule fois
+ $script = 'bash '.escapeshellarg ($script);
+
+ $command = new Command ();
+ $command->setCommand ($script);
+ $command->setLabel ('Suppression d\'entrées du DNS');
+
+ return $command->exec (true);
+ }
+
+
} // CommandPeer
diff --git a/lib/model/Host.php b/lib/model/Host.php
index d1fb003..65c12ba 100644
--- a/lib/model/Host.php
+++ b/lib/model/Host.php
@@ -19,30 +19,80 @@
*/
class Host extends BaseHost {
- public function __toString () {
+ protected $needDnsUpdate = null;
+ protected $originalIpAddress = null;
+
+ /**
+ * Surcharge de la création de l'objet afin de sauvegarder la valeur originale
+ * de l'adresse IP (utilisée dans la méthode preSave)
+ *
+ * @param $row
+ * @param int $startcol
+ * @param bool $rehydrate
+ * @return void
+ */
+ public function hydrate($row, $startcol = 0, $rehydrate = false)
+ {
+ $returnValue = parent::hydrate ($row, $startcol, $rehydrate);
+ $this->originalIpAddress = $this->getIpAddress();
+ return $returnValue;
+ }
+
+ public function __toString ()
+ {
return $this->getHostname ();
}
- public function getHostname () {
+ public function getHostname ()
+ {
return $this->getProfile().'-'.$this->getRoom ().'-'.$this->getNumber ();
}
+ public function getDomainName ()
+ {
+ return $this->getSubnet()->getDomainName();
+ }
+
+ public function getRevDomainName ()
+ {
+ return $this->getSubnet()->getRevDomainName();
+ }
+
/**
* Code to be run before persisting the object
*
- * On exécute alors la commande ns-update
+ * On détermine si le DNS devra être mis à jour après l'enregistrement en base
*
* @param PropelPDO $con
*/
public function preSave(PropelPDO $con = null)
{
- // TODO récupérer les anciennes valeurs et ne déclancher la commande
- // que si l'hostname a changé
+ $this->needDnsUpdate = null;
+
+ // Si l'IP est modifiée on supprime l'entrée du DNS.
+ if (! $this->isNew () && $this->isColumnModified ('ip_address'))
+ CommandPeer::runDnsDelete ($this->copy()->setIpAddress($this->originalIpAddress));
+ // Le reste des modifications est pris en charge dans le postSave
+ elseif ($this->needDnsUpdate())
+ $this->needDnsUpdate = true; // Une fois dans postSave il est impossible de savoir ce
+ // qui a été modifié, on enregistre donc l'info dans l'objet
- return parent::preSave($con);
+ return parent::preSave ($con);
}
/**
+ * Code to be after before deleting the object in database
+ * @param PropelPDO $con
+ * @return boolean
+ */
+ public function postDelete(PropelPDO $con = null)
+ {
+ CommandPeer::runDnsDelete ($this);
+ CommandPeer::runDhcpdUpdate ();
+ parent::postDelete ($con);
+ }
+
+ /**
* Code to be run after persisting the object
*
* On exécute alors la commande de mise à jour du DHCP
@@ -51,8 +101,27 @@ class Host extends BaseHost {
*/
public function postSave(PropelPDO $con = null)
{
- CommandPeer::runDhcpdUpdate();
- return parent::postSave($con);
+ parent::postSave ($con);
+
+ CommandPeer::runDhcpdUpdate ();
+
+ // Mise à jour du DNS si nécessaire
+ if ($this->needDnsUpdate === true)
+ CommandPeer::runDnsUpdate ($this);
+ }
+
+ /**
+ * Détermine si les modifications apportées à l'object nécessitent une mise à jour
+ * de l'entrée dans le DNS
+ *
+ * @return boolean
+ */
+ public function needDnsUpdate ()
+ {
+ return ($this->isColumnModified ('profile_id') ||
+ $this->isColumnModified ('room_id') ||
+ $this->isColumnModified ('subnet_id') ||
+ $this->isColumnModified ('ip_address'));
}
} // Host
diff --git a/lib/model/Room.php b/lib/model/Room.php
index 20a4b62..c4fa00e 100644
--- a/lib/model/Room.php
+++ b/lib/model/Room.php
@@ -19,8 +19,42 @@
*/
class Room extends BaseRoom {
+ protected $needDhcpUpdate = null;
+ protected $needDnsUpdate = null;
+
public function __toString () {
return $this->getName ();
}
-
+
+ /**
+ * Code to be run before persisting the object
+ *
+ * On détermine si le DNS devra être mis à jour après l'enregistrement en base
+ *
+ * @param PropelPDO $con
+ */
+ public function preSave(PropelPDO $con = null)
+ {
+ $this->needDnsUpdate = $this->needDhcpUpdate = $this->isColumnModified ('name');
+ return parent::preSave ($con);
+ }
+
+ /**
+ * Code to be run after persisting the object
+ *
+ * On exécute alors la commande de mise à jour du DHCP
+ *
+ * @param PropelPDO $con
+ */
+ public function postSave(PropelPDO $con = null)
+ {
+ parent::postSave ($con);
+
+ if ($this->needDhcpUpdate)
+ CommandPeer::runDhcpdUpdate ();
+
+ if ($this->needDnsUpdate)
+ CommandPeer::runDnsUpdate ($this->getHosts());
+ }
+
} // Room
diff --git a/lib/model/Subnet.php b/lib/model/Subnet.php
index 185cb47..2c83192 100644
--- a/lib/model/Subnet.php
+++ b/lib/model/Subnet.php
@@ -19,6 +19,8 @@
*/
class Subnet extends BaseSubnet {
+ protected $needDnsUpdate = null;
+
public function __toString ()
{
return $this->getName ();
@@ -32,4 +34,59 @@ class Subnet extends BaseSubnet {
return implode ($networkPart, '.').'.in-addr.arpa';
}
+ /**
+ * Code to be run before persisting the object
+ *
+ * On détermine si le DNS devra être mis à jour après l'enregistrement en base
+ *
+ * @param PropelPDO $con
+ */
+ public function preSave(PropelPDO $con = null)
+ {
+ $this->needDnsUpdate = (
+ $this->isColumnModified ('ip_address') ||
+ $this->isColumnModified ('domain')
+ );
+ return parent::preSave ($con);
+ }
+
+ /**
+ * Code to be run after persisting the object
+ *
+ * On exécute alors la commande de mise à jour du DHCP
+ *
+ * @param PropelPDO $con
+ */
+ public function postSave(PropelPDO $con = null)
+ {
+ parent::postSave ($con);
+
+ CommandPeer::runDhcpdUpdate ();
+
+ if ($this->needDnsUpdate === true)
+ CommandPeer::runDnsUpdate ($this->getHosts());
+ }
+
+ /**
+ * Code to be after deleting the object in database
+ * @param PropelPDO $con
+ * @return boolean
+ */
+ public function preDelete(PropelPDO $con = null)
+ {
+ CommandPeer::runDnsDelete ($this->getHosts());
+ return parent::preDelete ($con);
+ }
+
+ /**
+ * Code to be after before deleting the object in database
+ * @param PropelPDO $con
+ * @return boolean
+ */
+ public function postDelete(PropelPDO $con = null)
+ {
+ CommandPeer::runDhcpdUpdate ();
+ parent::postDelete ($con);
+ }
+
} // Subnet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment