From b8c7846c557c6c0cf69003cf9475df36195e5065 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Wed, 30 May 2007 16:16:46 -0400 Subject: WIP: Allow system names to be anything, and gather mac address and IP from either the inferred sytem name or the values given to --ip-address (--ip) or --mac-address (--mac). Change the action code to use this, and not create PXE entries when such info is not available. --- cobbler/item_system.py | 120 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 95 insertions(+), 25 deletions(-) (limited to 'cobbler/item_system.py') diff --git a/cobbler/item_system.py b/cobbler/item_system.py index cb6b5c8..ebfa03c 100644 --- a/cobbler/item_system.py +++ b/cobbler/item_system.py @@ -31,7 +31,8 @@ class System(item.Item): self.profile = None # a name, not a reference self.kernel_options = {} self.ks_meta = {} - self.pxe_address = "" + self.ip_address = "" # bad naming here, to the UI, this is usually 'ip-address' + self.mac_address = "" self.netboot_enabled = 1 self.hostname = "" @@ -40,31 +41,94 @@ class System(item.Item): self.profile = self.load_item(seed_data, 'profile') self.kernel_options = self.load_item(seed_data, 'kernel_options') self.ks_meta = self.load_item(seed_data, 'ks_meta') - self.pxe_address = self.load_item(seed_data, 'pxe_address') + + # backwards compat, load --ip-address from two possible sources. + # the old --pxe-address was a bit of a misnomer, new value is --ip-address + + oldvar = self.load_item(seed_data, 'pxe_address') + if oldvar == "": # newer version, yay + self.ip_address = self.load_item(seed_data, 'ip_address') + else: + self.ip_address = oldvar + self.netboot_enabled = self.load_item(seed_data, 'netboot_enabled', 1) self.hostname = self.load_item(seed_data, 'hostname') + self.mac_address = self.load_item(seed_data, 'mac_address') # backwards compatibility -- convert string entries to dicts for storage + # this allows for better usage from the API. + if type(self.kernel_options) != dict: self.set_kernel_options(self.kernel_options) if type(self.ks_meta) != dict: self.set_ksmeta(self.ks_meta) + # backwards compatibility -- if name is an IP or a MAC, set appropriate fields + # this will only happen once, as part of an upgrade path ... + # Explanation -- older cobbler's figured out the MAC and IP + # from the system name, newer cobblers allow arbitrary naming but can tell when the + # name is an IP or a MAC and use that if that info is not supplied. + + if self.mac_address == "" and utils.is_mac(self.name): + self.mac_address = self.name + elif self.ip_address == "" and utils.is_ip(self.name): + self.ip_address = self.name + return self def set_name(self,name): """ - A name can be a resolvable hostname (it instantly resolved and replaced with the IP), - any legal ipv4 address, or any legal mac address. ipv6 is not supported yet but _should_ be. - See utils.py + In Cobbler 0.4.9, any name given is legal, but if it's not an IP or MAC, --ip-address of --mac-address must + be given for PXE options to work. """ - if name == "default": - self.name="default" - return True - new_name = utils.find_system_identifier(name) - if not new_name: - raise CX(_("system name must be an MAC address, IP, or resolvable host")) - self.name = name # we check it add time, but store the original value. + + # set appropriate fields if the name implicitly is a MAC or IP. + # if the name is a hostname though, don't intuit that, as that's hard to determine + + if utils.is_mac(self.name): + self.mac_address = self.name + elif utils.is_ip(self.name): + self.ip_address = self.name + self.name = name + + return True + + def get_mac_address(self): + """ + Get the mac address, which may be implicit in the object name or explicit with --mac-address. + Use the explicit location first. + """ + if self.mac_address != "": + return self.mac_address + elif utils.is_mac(self.name): + return self.name + else: + # no one ever set it, but that might be ok depending on usage. + return None + + def get_ip_address(self): + """ + Get the IP address, which may be implicit in the object name or explict with --ip-address. + Use the explicit location first. + """ + if self.ip_address != "": # misnomer + return self.ip_address + elif utils.is_ip(self.name): + return self.name + else: + # no one ever set it, but that might be ok depending on usage. + return None + + def is_pxe_supported(self): + """ + Can only add system PXE records if a MAC or IP address is available, else it's a koan + only record. Actually Itanium goes beyond all this and needs the IP all of the time + though this is enforced elsewhere (action_sync.py). + """ + mac = self.get_mac_address() + ip = self.get_ip_address() + if mac is None or ip is None: + return False return True def set_hostname(self,hostname): @@ -72,21 +136,24 @@ class System(item.Item): return True def set_ip_address(self,address): - # allow function to use more sensical name - return self.set_pxe_address(address) - - def set_pxe_address(self,address): """ Assign a IP or hostname in DHCP when this MAC boots. Only works if manage_dhcp is set in /var/lib/cobbler/settings """ - # restricting to address as IP only in dhcpd.conf is probably - # incorrect ... some people may want to pin the hostname instead. - # doing so, however, doesn't allow dhcpd.conf to be managed - # by cobbler (since elilo can't do MAC addresses) -- this is - # covered in the man page. - self.pxe_address = address - return True + if utils.is_ip(address): + self.ip_address = address + return True + raise CX(_("invalid format for IP address")) + + def set_mac_address(self,address): + if utils.is_mac(address): + self.mac_address = address + return True + raise CX(_("invalid format for MAC address")) + + def set_ip_address(self,address): + # backwards compatibility for API users: + return self.set_ip_address(address) def set_profile(self,profile_name): """ @@ -135,7 +202,7 @@ class System(item.Item): 'profile' : self.profile, 'kernel_options' : self.kernel_options, 'ks_meta' : self.ks_meta, - 'pxe_address' : self.pxe_address, + 'ip_address' : self.ip_address, 'netboot_enabled' : self.netboot_enabled, 'hostname' : self.hostname, } @@ -145,7 +212,10 @@ class System(item.Item): buf = buf + _("profile : %s\n") % self.profile buf = buf + _("kernel options : %s\n") % self.kernel_options buf = buf + _("ks metadata : %s\n") % self.ks_meta - buf = buf + _("ip address : %s\n") % self.pxe_address + buf = buf + _("ip address : %s\n") % self.get_ip_address() + buf = buf + _("mac address : %s\n") % self.get_mac_address() + buf = buf + _("pxe info set? : %s\n") % self.is_pxe_supported() + buf = buf + _("pxe id : %s\n") % self.get_system_identifier() buf = buf + _("hostname : %s\n") % self.hostname return buf -- cgit