Hi all
Attached is a second patch which joins previous two patches and
adds another feature:
Parses /var/lib/dhcpd/dhpcd.leases and removes any "host" entry
using OMAPI as previous step to write the new ones, this is done to
ensure that hosts renamed, deleted or previous ones, still exist at
leases, that is... resets the state to cobbler known hosts.
Comments are still welcome :)
Regards!
Pablo
PD: Thist patch will get into ticket as previous
--
Pablo Iranzo Gómez
(http://Alufis35.uv.es/~iranzo/)
(PGPKey Available on http://www.uv.es/~iranzop/PGPKey.pgp)
--
Postulado de Boling sobre la Ley de Murphy:
Si se encuentra bien, no se preocupe. Se le pasará
diff --git a/cobbler/dhcpgen.py b/cobbler/dhcpgen.py
index 2d6facf..cc6d410 100644
--- a/cobbler/dhcpgen.py
+++ b/cobbler/dhcpgen.py
@@ -22,6 +22,9 @@ import sys
import glob
import traceback
import errno
+import popen2
+from shlex import shlex
+
import utils
from cexceptions import *
@@ -53,6 +56,50 @@ class DHCPGen:
self.repos = config.repos()
self.templar = templar.Templar(config)
+ def writeDHCPLease(self,port,host,ip,mac):
+ """writeDHCPLease(port,host,ip,mac)
+ Use DHCP's API to create a DHCP entry in the /var/lib/dhcpd/dhcpd.leases file """
+ #Code from http://svn.osgdc.org/browse/kusu/kusu/trunk/src/kits/base/packages/kusu-base-installer/lib/kusu/nodefun.py?r=3025
+ fromchild, tochild = popen2.popen2("/usr/bin/omshell")
+ tochild.write("port %s\n" % port)
+ tochild.flush()
+ tochild.write("connect\n")
+ tochild.flush()
+ tochild.write("new host\n")
+ tochild.flush()
+ tochild.write('set name = \"%s\"\n' % host)
+ tochild.flush()
+ tochild.write("set ip-address = %s\n" % ip)
+ tochild.flush()
+ tochild.write("set hardware-address = %s\n" % mac)
+ tochild.flush()
+ tochild.write("set hardware-type = 1\n")
+ tochild.flush()
+ tochild.write("create\n")
+ tochild.flush()
+ tochild.close()
+ fromchild.close()
+
+ def removeDHCPLease(self,port,host):
+ """removeDHCPLease(port,host)
+ Use DHCP's API to delete a DHCP entry in the /var/lib/dhcpd/dhcpd.leases file """
+ fromchild, tochild = popen2.popen2("/usr/bin/omshell")
+ tochild.write("port %s\n" % port)
+ tochild.flush()
+ tochild.write("connect\n")
+ tochild.flush()
+ tochild.write("new host\n")
+ tochild.flush()
+ tochild.write('set name = \"%s\"\n' % host)
+ tochild.flush()
+ tochild.write("open\n") # opens register with host information
+ tochild.flush()
+ tochild.write("remove\n")
+ tochild.flush()
+ tochild.close()
+ fromchild.close()
+
+
def write_dhcp_file(self):
"""
DHCP files are written when manage_dhcp is set in
@@ -84,10 +131,28 @@ class DHCPGen:
system_definitions = {}
counter = 0
+
+
+ # Clean system definitions in /var/lib/dhcpd/dhcpd.leases just in
+ # case to avoid conflicts with the hosts we're defining and to clean
+ # possible removed hosts (only if using OMAPI)
+ #
+ # Pablo Iranzo Gómez (Pablo.Iranzo@xxxxxxxxxx)
+ if self.settings.omapi and self.settings.omapi_port:
+ file = open('/var/lib/dhcpd/dhcpd.leases')
+ item = shlex(file)
+ while 1:
+ elem = item.get_token()
+ if not elem:
+ break
+ if elem == 'host':
+ hostremove = item.get_token()
+ self.removeDHCPLease(self.settings.omapi_port,hostremove)
+
# we used to just loop through each system, but now we must loop
# through each network interface of each system.
-
+
for system in self.systems:
profile = system.get_conceptual_parent()
distro = profile.get_conceptual_parent()
@@ -121,6 +186,19 @@ class DHCPGen:
if ip is not None and ip != "":
systxt = systxt + " fixed-address %s;\n" % ip
systxt = systxt + "}\n"
+
+ # If we have all values defined and we're using omapi,
+ # we will just create entries dinamically into DHCPD
+ # without requiring a restart (but file will be written
+ # as usual for having it working after restart)
+
+ if ip is not None and ip != "":
+ if mac is not None and mac != "":
+ if host is not None and host != "":
+ if self.settings.omapi and self.settings.omapi_port:
+ self.removeDHCPLease(self.settings.omapi_port,host)
+ self.writeDHCPLease(self.settings.omapi_port,host,ip,mac)
+
else:
# dnsmasq. don't have to write IP and other info here, but we do tag
@@ -192,5 +270,3 @@ class DHCPGen:
if host is not None and host != "" and ip is not None and ip != "":
fh.write(ip + "\t" + host + "\n")
fh.close()
-
-
diff --git a/cobbler/settings.py b/cobbler/settings.py
index 40ed571..491de75 100644
--- a/cobbler/settings.py
+++ b/cobbler/settings.py
@@ -58,6 +58,8 @@ DEFAULTS = {
"manage_dhcp" : 0,
"manage_dhcp_mode" : "isc",
"next_server" : "127.0.0.1",
+ "omapi" : 1,
+ "omapi_port" : 647,
"pxe_just_once" : 0,
"register_new_installs" : 0,
"run_install_triggers" : 1,
diff --git a/config/settings b/config/settings
index 10e06e2..724f419 100644
--- a/config/settings
+++ b/config/settings
@@ -32,6 +32,8 @@ ldap_search_prefix: 'uid='
manage_dhcp: 0
manage_dhcp_mode: isc
next_server: '127.0.0.1'
+omapi: 1
+omapi_port: 647
pxe_just_once: 0
register_new_installs: 0
run_install_triggers: 1
diff --git a/templates/dhcp.template b/templates/dhcp.template
index 705cc77..19afcad 100644
--- a/templates/dhcp.template
+++ b/templates/dhcp.template
@@ -9,6 +9,7 @@ ddns-update-style interim;
allow booting;
allow bootp;
+omapi-port 647;
ignore client-updates;
set vendorclass = option vendor-class-identifier;
diff --git a/triggers/restart-services.trigger b/triggers/restart-services.trigger
index b65b825..6a0e320 100644
--- a/triggers/restart-services.trigger
+++ b/triggers/restart-services.trigger
@@ -8,11 +8,19 @@ bootapi = capi.BootAPI()
settings = bootapi.settings()
manage_dhcp = str(settings.manage_dhcp).lower()
manage_dhcp_mode = str(settings.manage_dhcp_mode).lower()
+omapi = settings.omapi
+omapi_port = settings.omapi_port
+
+
+
+# We're just going to restart DHCPD if using ISC and if not using OMAPI at all
rc = 0
if manage_dhcp != "0":
if manage_dhcp_mode == "isc":
- rc = os.system("/sbin/service dhcpd restart")
+ if not omapi:
+ if not omapi_port:
+ rc = os.system("/sbin/service dhcpd restart")
elif manage_dhcp_mode == "dnsmasq":
rc = os.system("/sbin/service dnsmasq restart")
else:
_______________________________________________
et-mgmt-tools mailing list
et-mgmt-tools@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/et-mgmt-tools