def apply(self, args): responses = self.check(args) if responses is False: log.error("Check failed. Not applying") log.error("%s" % (responses)) return False self.save(responses) #Update network details so we write correct IP address self.getNetwork() #Apply hostname expr = 'HOSTNAME=.*' replace.replaceInFile( "/etc/sysconfig/network", expr, "HOSTNAME=%s.%s" % (responses["HOSTNAME"], responses["DNS_DOMAIN"])) #remove old hostname from /etc/hosts f = open("/etc/hosts", "r") lines = f.readlines() f.close() with open("/etc/hosts", "w") as etchosts: for line in lines: if "localhost" in line: etchosts.write(line) elif responses["HOSTNAME"] in line \ or self.oldsettings["HOSTNAME"] \ or self.netsettings[self.parent.managediface]['addr'] \ in line: continue else: etchosts.write(line) etchosts.close() #append hostname and ip address to /etc/hosts with open("/etc/hosts", "a") as etchosts: if self.netsettings[self.parent.managediface]["addr"] != "": managediface_ip = self.netsettings[ self.parent.managediface]["addr"] else: managediface_ip = "127.0.0.1" etchosts.write("%s %s.%s %s\n" % (managediface_ip, responses["HOSTNAME"], responses['DNS_DOMAIN'], responses["HOSTNAME"])) etchosts.close() def make_resolv_conf(filename): with open(filename, 'w') as f: f.write("search %s\n" % responses['DNS_SEARCH']) f.write("domain %s\n" % responses['DNS_DOMAIN']) for upstream_dns in responses['DNS_UPSTREAM'].split(','): f.write("nameserver %s\n" % upstream_dns) #Write dnsmasq upstream server make_resolv_conf('/etc/dnsmasq.upstream') # Create a temporary resolv.conf so DNS works before the cobbler # container is up and running. # TODO(asheplyakov): puppet does a similar thing, perhaps we can # use the corresponding template instead of duplicating it here. make_resolv_conf('/etc/resolv.conf') return True
def apply(self, args): responses = self.check(args) if responses is False: log.error("Check failed. Not applying") log.error("%s" % (responses)) return False self.save(responses) #Update network details so we write correct IP address self.getNetwork() #Apply hostname expr = 'HOSTNAME=.*' replace.replaceInFile("/etc/sysconfig/network", expr, "HOSTNAME=%s.%s" % (responses["HOSTNAME"], responses["DNS_DOMAIN"])) #remove old hostname from /etc/hosts f = open("/etc/hosts", "r") lines = f.readlines() f.close() with open("/etc/hosts", "w") as etchosts: for line in lines: if "localhost" in line: etchosts.write(line) elif responses["HOSTNAME"] in line \ or self.oldsettings["HOSTNAME"] \ or self.netsettings[self.parent.managediface]['addr'] \ in line: continue else: etchosts.write(line) etchosts.close() #append hostname and ip address to /etc/hosts with open("/etc/hosts", "a") as etchosts: if self.netsettings[self.parent.managediface]["addr"] != "": managediface_ip = self.netsettings[ self.parent.managediface]["addr"] else: managediface_ip = "127.0.0.1" etchosts.write( "%s %s.%s %s\n" % (managediface_ip, responses["HOSTNAME"], responses['DNS_DOMAIN'], responses["HOSTNAME"])) etchosts.close() def make_resolv_conf(filename): with open(filename, 'w') as f: f.write("search %s\n" % responses['DNS_SEARCH']) f.write("domain %s\n" % responses['DNS_DOMAIN']) for upstream_dns in responses['DNS_UPSTREAM'].split(','): f.write("nameserver %s\n" % upstream_dns) # Create a temporary resolv.conf so DNS works before the cobbler # container is up and running. # TODO(asheplyakov): puppet does a similar thing, perhaps we can # use the corresponding template instead of duplicating it here. make_resolv_conf('/etc/resolv.conf') return True
def apply(self, args): responses = self.check(args) if responses is False: self.log.error("Check failed. Not applying") self.parent.footer.set_text("Check failed. Not applying.") self.log.error("%s" % (responses)) return False self.parent.footer.set_text("Applying changes... (May take up to 20s)") puppetclasses = [] #If there is a gateway configured in /etc/sysconfig/network, unset it expr = '^GATEWAY=.*' replace.replaceInFile("/etc/sysconfig/network", expr, "") l3ifconfig = {'type': "resource", 'class': "l23network::l3::ifconfig", 'name': self.activeiface} if responses["onboot"].lower() == "no": params = {"ipaddr": "none", "gateway": ""} elif responses["bootproto"] == "dhcp": self.unset_gateway() if "dhcp_nowait" in responses.keys(): params = {"ipaddr": "dhcp", "dhcp_nowait": responses["dhcp_nowait"]} else: params = {"ipaddr": "dhcp"} else: cidr = network.netmaskToCidr(responses["netmask"]) params = {"ipaddr": "{0}/{1}".format(responses["ipaddr"], cidr), "check_by_ping": "none"} if len(responses["gateway"]) > 1: params["gateway"] = responses["gateway"] self.unset_gateway() l3ifconfig['params'] = params puppetclasses.append(l3ifconfig) self.log.info("Puppet data: %s" % (puppetclasses)) try: self.parent.refreshScreen() puppet.puppetApply(puppetclasses) ModuleHelper.getNetwork(self) gateway = self.get_default_gateway_linux() if gateway is None: gateway = "" self.fixEtcHosts() except Exception as e: self.log.error(e) self.parent.footer.set_text("Error applying changes. Check logs " "for details.") ModuleHelper.getNetwork(self) self.setNetworkDetails() return False self.parent.footer.set_text("Changes successfully applied.") ModuleHelper.getNetwork(self) self.setNetworkDetails() return True
def apply(self, args): responses = self.check(args) if responses is False: log.error("Check failed. Not applying") log.error("%s" % (responses)) return False self.save(responses) #Update network details so we write correct IP address self.getNetwork() #Apply hostname expr = 'HOSTNAME=.*' replace.replaceInFile("/etc/sysconfig/network", expr, "HOSTNAME=%s.%s" % (responses["HOSTNAME"], responses["DNS_DOMAIN"])) #remove old hostname from /etc/hosts f = open("/etc/hosts", "r") lines = f.readlines() f.close() with open("/etc/hosts", "w") as etchosts: for line in lines: if "localhost" in line: etchosts.write(line) elif responses["HOSTNAME"] in line \ or self.oldsettings["HOSTNAME"] \ or self.netsettings[self.parent.managediface]['addr'] \ in line: continue else: etchosts.write(line) etchosts.close() #append hostname and ip address to /etc/hosts with open("/etc/hosts", "a") as etchosts: if self.netsettings[self.parent.managediface]["addr"] != "": managediface_ip = self.netsettings[ self.parent.managediface]["addr"] else: managediface_ip = "127.0.0.1" etchosts.write( "%s %s.%s %s\n" % (managediface_ip, responses["HOSTNAME"], responses['DNS_DOMAIN'], responses["HOSTNAME"])) etchosts.close() #Write dnsmasq upstream server with open('/etc/dnsmasq.upstream', 'w') as f: nameservers = responses['DNS_UPSTREAM'].replace(',', ' ') f.write("nameserver %s\n" % nameservers) f.close() ###Future feature to apply post-deployment #Need to decide if we are pre-deployment or post-deployment #if self.deployment == "post": # self.updateCobbler(responses) # services.restart("cobbler") return True
def test_replace(self): filename = 'filename' with mock.patch('__builtin__.open', custom_mock_open(self.data)) \ as m_open: replace.replaceInFile(filename, 'line2', 'line4') self.assertEqual(m_open.call_args_list, [mock.call(filename), mock.call(filename, 'w')]) m_open.return_value.write.assert_called_once_with( 'line1\nline4\nline3\n')
def apply(self, args): responses = self.check(args) if responses is False: log.error("Check failed. Not applying") log.error("%s" % (responses)) return False self.save(responses) #Update network details so we write correct IP address self.getNetwork() #Apply hostname expr = 'HOSTNAME=.*' replace.replaceInFile("/etc/sysconfig/network", expr, "HOSTNAME=%s.%s" % (responses["HOSTNAME"], responses["DNS_DOMAIN"])) #remove old hostname from /etc/hosts f = open("/etc/hosts", "r") lines = f.readlines() f.close() with open("/etc/hosts", "w") as etchosts: for line in lines: if "localhost" in line: etchosts.write(line) elif responses["HOSTNAME"] in line \ or self.oldsettings["HOSTNAME"] \ or self.netsettings[self.parent.managediface]['addr'] \ in line: continue else: etchosts.write(line) etchosts.close() #append hostname and ip address to /etc/hosts with open("/etc/hosts", "a") as etchosts: if self.netsettings[self.parent.managediface]["addr"] != "": managediface_ip = self.netsettings[ self.parent.managediface]["addr"] else: managediface_ip = "127.0.0.1" etchosts.write( "%s %s.%s %s\n" % (managediface_ip, responses["HOSTNAME"], responses['DNS_DOMAIN'], responses["HOSTNAME"])) etchosts.close() #Write dnsmasq upstream server with open('/etc/dnsmasq.upstream', 'w') as f: f.write("search %s\n" % responses['DNS_SEARCH']) f.write("domain %s\n" % responses['DNS_DOMAIN']) for upstream_dns in responses['DNS_UPSTREAM'].split(','): f.write("nameserver %s\n" % upstream_dns) f.close() return True
def apply(self, args): responses = self.check(args) if responses is False: log.error("Check failed. Not applying") log.error("%s" % (responses)) return False self.save(responses) #Update network details so we write correct IP address self.getNetwork() #Apply hostname expr = 'HOSTNAME=.*' replace.replaceInFile( "/etc/sysconfig/network", expr, "HOSTNAME=%s.%s" % (responses["HOSTNAME"], responses["DNS_DOMAIN"])) #remove old hostname from /etc/hosts f = open("/etc/hosts", "r") lines = f.readlines() f.close() with open("/etc/hosts", "w") as etchosts: for line in lines: if responses["HOSTNAME"] in line \ or self.oldsettings["HOSTNAME"] in line: continue else: etchosts.write(line) etchosts.close() #append hostname and ip address to /etc/hosts with open("/etc/hosts", "a") as etchosts: if self.netsettings[self.parent.managediface]["addr"] != "": managediface_ip = self.netsettings[ self.parent.managediface]["addr"] else: managediface_ip = "127.0.0.1" etchosts.write("%s %s.%s %s\n" % (managediface_ip, responses["HOSTNAME"], responses['DNS_DOMAIN'], responses["HOSTNAME"])) etchosts.close() self.fixEtcResolv() #Write dnsmasq upstream server with open('/etc/dnsmasq.upstream', 'w') as f: nameservers = responses['DNS_UPSTREAM'].replace(',', ' ') f.write("nameserver %s\n" % nameservers) f.close() ###Future feature to apply post-deployment #Need to decide if we are pre-deployment or post-deployment #if self.deployment == "post": # self.updateCobbler(responses) # services.restart("cobbler") return True
def fixEtcHosts(self): # replace ip for env variable HOSTNAME in /etc/hosts if self.netsettings[self.parent.managediface]["addr"] != "": managediface_ip = self.netsettings[self.parent.managediface][ "addr"] else: managediface_ip = "127.0.0.1" found = False with open("/etc/hosts") as fh: for line in fh: if re.match("%s.*%s" % (managediface_ip, socket.gethostname()), line): found = True break if not found: expr = ".*%s.*" % socket.gethostname() replace.replaceInFile("/etc/hosts", expr, "%s %s %s" % ( managediface_ip, socket.gethostname(), socket.gethostname().split(".")[0]))
def fixEtcHosts(self): #replace ip for env variable HOSTNAME in /etc/hosts if self.netsettings[self.parent.managediface]["addr"] != "": managediface_ip = self.netsettings[self.parent.managediface][ "addr"] else: managediface_ip = "127.0.0.1" found = False with open("/etc/hosts") as fh: for line in fh: if re.match("%s.*%s" % (managediface_ip, socket.gethostname()), line): found = True break if not found: expr = ".*%s.*" % socket.gethostname() replace.replaceInFile("/etc/hosts", expr, "%s %s %s" % ( managediface_ip, socket.gethostname(), socket.gethostname().split(".")[0]))
def apply(self, args): responses = self.check(args) if responses is False: self.log.error("Check failed. Not applying") self.parent.footer.set_text("Check failed. Not applying.") self.log.error("%s" % (responses)) return False self.parent.footer.set_text("Applying changes... (May take up to 20s)") # Build puppet resources to apply l23network puppet module to enable # network changes puppetclasses = [] # FIXME(mattymo): install_bondtool param does not work (LP#1541028) # The following 4 lines should be removed when fixed. disable_bond = { 'type': "literal", 'name': 'K_mod <| title == "bonding" |> {ensure => absent} '} puppetclasses.append(disable_bond) # If there is a gateway configured in /etc/sysconfig/network, unset it expr = '^GATEWAY=.*' replace.replaceInFile("/etc/sysconfig/network", expr, "") # Initialize l23network class for NetworkManager fixes l23network = {'type': "resource", 'class': "class", 'name': "l23network", 'params': {'install_bondtool': False}} puppetclasses.append(l23network) # Prepare l23network interface configuration l3ifconfig = {'type': "resource", 'class': "l23network::l3::ifconfig", 'name': self.activeiface} additionalclasses = [] if responses["onboot"].lower() == "no": params = {"ipaddr": "none", "gateway": ""} elif responses["bootproto"] == "dhcp": additionalclasses = self.clear_gateways_except(self.activeiface) params = {"ipaddr": "dhcp"} else: cidr = network.addr_in_cidr_notation(responses["ipaddr"], responses["netmask"]) params = {"ipaddr": cidr, "check_by_ping": "none"} if len(responses["gateway"]) > 1: params["gateway"] = responses["gateway"] additionalclasses = self.clear_gateways_except( self.activeiface) puppetclasses.extend(additionalclasses) l3ifconfig['params'] = params puppetclasses.append(l3ifconfig) self.log.info("Puppet data: %s" % (puppetclasses)) try: self.parent.refreshScreen() result = puppet.puppetApply(puppetclasses) if result is False: raise Exception("Puppet apply failed") ModuleHelper.getNetwork(self) gateway = self.get_default_gateway_linux() if gateway is None: gateway = "" self.fixEtcHosts() if responses['bootproto'] == 'dhcp': self.parent.dns_might_have_changed = True except Exception as e: self.log.error(e) self.parent.footer.set_text("Error applying changes. Check logs " "for details.") ModuleHelper.getNetwork(self) self.setNetworkDetails() return False self.parent.footer.set_text("Changes successfully applied.") ModuleHelper.getNetwork(self) self.setNetworkDetails() return True
def apply(self, args): responses = self.check(args) if responses is False: self.log.error("Check failed. Not applying") self.parent.footer.set_text("Check failed. Not applying.") self.log.error("%s" % (responses)) return False self.parent.footer.set_text("Applying changes... (May take up to 20s)") puppetclasses = [] #If there is a gateway configured in /etc/sysconfig/network, unset it expr = '^GATEWAY=.*' replace.replaceInFile("/etc/sysconfig/network", expr, "") l3ifconfig = { 'type': "resource", 'class': "l23network::l3::ifconfig", 'name': self.activeiface } if responses["onboot"].lower() == "no": params = {"ipaddr": "none"} elif responses["bootproto"] == "dhcp": self.unset_gateway() if "dhcp_nowait" in responses.keys(): params = { "ipaddr": "dhcp", "dhcp_nowait": responses["dhcp_nowait"] } else: params = {"ipaddr": "dhcp"} else: cidr = network.netmaskToCidr(responses["netmask"]) params = { "ipaddr": "{0}/{1}".format(responses["ipaddr"], cidr), "check_by_ping": "none" } if len(responses["gateway"]) > 1: params["gateway"] = responses["gateway"] self.unset_gateway() l3ifconfig['params'] = params puppetclasses.append(l3ifconfig) self.log.info("Puppet data: %s" % (puppetclasses)) try: self.parent.refreshScreen() puppet.puppetApply(puppetclasses) ModuleHelper.getNetwork(self) gateway = self.get_default_gateway_linux() if gateway is None: gateway = "" self.fixEtcHosts() except Exception as e: self.log.error(e) self.parent.footer.set_text("Error applying changes. Check logs " "for details.") ModuleHelper.getNetwork(self) self.setNetworkDetails() return False self.parent.footer.set_text("Changes successfully applied.") ModuleHelper.getNetwork(self) self.setNetworkDetails() return True
def apply(self, args): responses = self.check(args) if responses is False: self.log.error("Check failed. Not applying") self.parent.footer.set_text("Check failed. Not applying.") self.log.error("%s" % (responses)) return False self.parent.footer.set_text("Applying changes... (May take up to 20s)") puppetclasses = [] l3ifconfig = {'type': "resource", 'class': "l23network::l3::ifconfig", 'name': self.activeiface} if responses["onboot"].lower() == "no": params = {"ipaddr": "none"} elif responses["bootproto"] == "dhcp": if "dhcp_nowait" in responses.keys(): params = {"ipaddr": "dhcp", "dhcp_nowait": responses["dhcp_nowait"]} else: params = {"ipaddr": "dhcp"} else: params = {"ipaddr": responses["ipaddr"], "netmask": responses["netmask"], "check_by_ping": "none"} if len(responses["gateway"]) > 1: params["gateway"] = responses["gateway"] elif network.inSameSubnet(self.get_default_gateway_linux(), responses["ipaddr"], responses["netmask"]): #If the current gateway is in the same subnet AND the user #sets the gateway to empty, unset gateway expr = '^GATEWAY=.*' replace.replaceInFile("/etc/sysconfig/network", expr, "GATEWAY=") l3ifconfig['params'] = params puppetclasses.append(l3ifconfig) self.log.info("Puppet data: %s" % (puppetclasses)) try: #Gateway handling so DHCP will set gateway if responses["bootproto"] == "dhcp": expr = '^GATEWAY=.*' replace.replaceInFile("/etc/sysconfig/network", expr, "GATEWAY=") self.parent.refreshScreen() puppet.puppetApply(puppetclasses) self.getNetwork() expr = '^GATEWAY=.*' gateway = self.get_default_gateway_linux() if gateway is None: gateway = "" replace.replaceInFile("/etc/sysconfig/network", expr, "GATEWAY=%s" % gateway) self.fixEtcHosts() except Exception as e: self.log.error(e) self.parent.footer.set_text("Error applying changes. Check logs " "for details.") self.getNetwork() self.setNetworkDetails() return False self.parent.footer.set_text("Changes successfully applied.") self.getNetwork() self.setNetworkDetails() return True
def apply(self, args): responses = self.check(args) if responses is False: self.log.error("Check failed. Not applying") self.parent.footer.set_text("Check failed. Not applying.") self.log.error("%s" % (responses)) return False self.parent.footer.set_text("Applying changes... (May take up to 20s)") puppetclasses = [] l3ifconfig = { 'type': "resource", 'class': "l23network::l3::ifconfig", 'name': self.activeiface } if responses["onboot"].lower() == "no": params = {"ipaddr": "none"} elif responses["bootproto"] == "dhcp": if "dhcp_nowait" in responses.keys(): params = { "ipaddr": "dhcp", "dhcp_nowait": responses["dhcp_nowait"] } else: params = {"ipaddr": "dhcp"} else: params = { "ipaddr": responses["ipaddr"], "netmask": responses["netmask"], "check_by_ping": "none" } if len(responses["gateway"]) > 1: params["gateway"] = responses["gateway"] params["default_gateway"] = True elif network.inSameSubnet(self.get_default_gateway_linux(), responses["ipaddr"], responses["netmask"]): #If the current gateway is in the same subnet AND the user #sets the gateway to empty, unset gateway expr = '^GATEWAY=.*' replace.replaceInFile("/etc/sysconfig/network", expr, "GATEWAY=") l3ifconfig['params'] = params puppetclasses.append(l3ifconfig) self.log.info("Puppet data: %s" % (puppetclasses)) try: #Gateway handling so DHCP will set gateway if responses["bootproto"] == "dhcp": expr = '^GATEWAY=.*' replace.replaceInFile("/etc/sysconfig/network", expr, "GATEWAY=") self.parent.refreshScreen() puppet.puppetApply(puppetclasses) ModuleHelper.getNetwork(self) expr = '^GATEWAY=.*' gateway = self.get_default_gateway_linux() if gateway is None: gateway = "" replace.replaceInFile("/etc/sysconfig/network", expr, "GATEWAY=%s" % gateway) self.fixEtcHosts() except Exception as e: self.log.error(e) self.parent.footer.set_text("Error applying changes. Check logs " "for details.") ModuleHelper.getNetwork(self) self.setNetworkDetails() return False self.parent.footer.set_text("Changes successfully applied.") ModuleHelper.getNetwork(self) self.setNetworkDetails() return True
def apply(self, args): responses = self.check(args) if responses is False: self.log.error("Check failed. Not applying") self.parent.footer.set_text("Check failed. Not applying.") self.log.error("%s" % (responses)) return False self.parent.footer.set_text("Applying changes... (May take up to 20s)") # Build puppet resources to apply l23network puppet module to enable # network changes puppetclasses = [] # FIXME(mattymo): install_bondtool param does not work (LP#1541028) # The following 4 lines should be removed when fixed. disable_bond = { 'type': "literal", 'name': 'K_mod <| title == "bonding" |> {ensure => absent} ' } puppetclasses.append(disable_bond) # If there is a gateway configured in /etc/sysconfig/network, unset it expr = '^GATEWAY=.*' replace.replaceInFile("/etc/sysconfig/network", expr, "") # Initialize l23network class for NetworkManager fixes l23network = { 'type': "resource", 'class': "class", 'name': "l23network", 'params': { 'install_bondtool': False } } puppetclasses.append(l23network) # Prepare l23network interface configuration l3ifconfig = { 'type': "resource", 'class': "l23network::l3::ifconfig", 'name': self.activeiface } additionalclasses = [] if responses["onboot"].lower() == "no": params = {"ipaddr": "none", "gateway": ""} elif responses["bootproto"] == "dhcp": additionalclasses = self.clear_gateways_except(self.activeiface) params = {"ipaddr": "dhcp"} else: cidr = network.addr_in_cidr_notation(responses["ipaddr"], responses["netmask"]) params = {"ipaddr": cidr, "check_by_ping": "none"} if len(responses["gateway"]) > 1: params["gateway"] = responses["gateway"] additionalclasses = self.clear_gateways_except( self.activeiface) puppetclasses.extend(additionalclasses) l3ifconfig['params'] = params puppetclasses.append(l3ifconfig) self.log.info("Puppet data: %s" % (puppetclasses)) try: self.parent.refreshScreen() result = puppet.puppetApply(puppetclasses) if not result: raise Exception("Puppet apply failed") modulehelper.ModuleHelper.getNetwork(self) gateway = self.get_default_gateway_linux() if gateway is None: gateway = "" self.fixEtcHosts() if responses['bootproto'] == 'dhcp': self.parent.dns_might_have_changed = True except Exception as e: self.log.error(e) self.parent.footer.set_text("Error applying changes. Check logs " "for details.") modulehelper.ModuleHelper.getNetwork(self) self.setNetworkDetails() return False self.parent.footer.set_text("Changes successfully applied.") modulehelper.ModuleHelper.getNetwork(self) self.setNetworkDetails() return True