diff options
author | Michael DeHaan <mdehaan@redhat.com> | 2008-05-02 11:51:33 -0400 |
---|---|---|
committer | Michael DeHaan <mdehaan@redhat.com> | 2008-05-02 11:51:33 -0400 |
commit | 7eb0fa0be305d1d682b29aaff6dde267402a9e6a (patch) | |
tree | 517d58a688935427c14a85094f8ad665bf3efc5a /cobbler | |
parent | 2f6b73def7068ee401d37b7eeee44a9fc064c9a1 (diff) | |
download | third_party-cobbler-7eb0fa0be305d1d682b29aaff6dde267402a9e6a.tar.gz third_party-cobbler-7eb0fa0be305d1d682b29aaff6dde267402a9e6a.tar.xz third_party-cobbler-7eb0fa0be305d1d682b29aaff6dde267402a9e6a.zip |
Apply Pablo's patch to add OMAPI support to dhcp config generation.
Diffstat (limited to 'cobbler')
-rw-r--r-- | cobbler/dhcpgen.py | 82 | ||||
-rw-r--r-- | cobbler/settings.py | 2 |
2 files changed, 81 insertions, 3 deletions
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@redhat.com) + 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, |