def _setup_hostname(self, client): hostname = utils.get_hostname(client) completed = False if os.path.exists('/usr/bin/hostnamectl'): utils.backup_file(self.hostname_file) p = Popen( ['hostnamectl', 'set-hostname', hostname], stdout=PIPE, stderr=PIPE, stdin=PIPE ) out, err = p.communicate() if p.returncode != 0: log.error('Error using hostnamectl: {0}'.format(err)) else: # Do not run hostname since hostnamectl was successful completed = True if not completed: log.debug('Falling back to use hostname command') p = Popen( ['hostname', hostname], stdout=PIPE, stderr=PIPE, stdin=PIPE ) out, err = p.communicate() if p.returncode != 0: log.error('Error using hostname: {0}'.format(err)) else: log.debug('Writing file {0}'.format(self.hostname_file)) with open(self.hostname_file, 'w') as host_file: host_file.write(hostname) return p.returncode, hostname
def resetnetwork(self, name, value): ifaces = {} xen_macs = utils.list_xenstore_macaddrs() for iface in utils.list_hw_interfaces(): mac = utils.get_hw_addr(iface) if not mac or mac not in xen_macs: continue ifaces[iface] = utils.get_interface(mac) # set hostname utils.backup_file('/etc/hostname') hostname = utils.get_hostname() p = Popen(['hostnamectl', 'set-hostname', hostname], stdout=PIPE, stderr=PIPE, stdin=PIPE) out, err = p.communicate() if p.returncode != 0: return (str(p.returncode), 'Error setting hostname') # setup interface files for ifname, iface in ifaces.items(): self._setup_interface(ifname, iface) p = Popen(['netctl', 'restart', ifname], stdout=PIPE, stderr=PIPE, stdin=PIPE) out, err = p.communicate() if p.returncode != 0: return (str(p.returncode), 'Error restarting network') return ('0', '')
def resetnetwork(self, name, value): ifaces = {} xen_macs = utils.list_xenstore_macaddrs() for iface in utils.list_hw_interfaces(): mac = utils.get_hw_addr(iface) if not mac or mac not in xen_macs: continue ifaces[iface] = utils.get_interface(mac) # set hostname utils.backup_file('/etc/hostname') utils.backup_file('/etc/conf.d/hostname') hostname = utils.get_hostname() with open('/etc/hostname', 'w') as hostnamefile: print(hostname, file=hostnamefile) with open('/etc/conf.d/hostname', 'w') as hostnamefile: print('HOSTNAME={0}'.format(hostname), file=hostnamefile) p = Popen(['hostname', hostname], stdout=PIPE, stderr=PIPE, stdin=PIPE) out, err = p.communicate() if p.returncode != 0: return (str(p.returncode), 'Error setting hostname: hostname') # setup interface files utils.backup_file('/etc/conf.d/net') with open('/etc/conf.d/net', 'w') as iffile: print('modules="iproute2"', file=iffile) for ifname, iface in ifaces.items(): self._setup_interface(ifname, iface) p = Popen(['/etc/init.d/net.{0}'.format(ifname), 'restart'], stdout=PIPE, stderr=PIPE, stdin=PIPE) out, err = p.communicate() if p.returncode != 0: return (str(p.returncode), 'Error restarting network') return ('0', '')
def _setup_hostname(self, client): """ hostnamectl is available in some Debian systems and depends on dbus """ hostname = utils.get_hostname(client) completed = False if os.path.exists('/usr/bin/hostnamectl'): utils.backup_file(self.hostname_file) p = Popen(['hostnamectl', 'set-hostname', hostname], stdout=PIPE, stderr=PIPE, stdin=PIPE) out, err = p.communicate() if p.returncode != 0: log.error('Error using hostnamectl: {0}'.format(err)) else: # Do not run hostname since it was successful completed = True if not completed: p = Popen(['hostname', hostname], stdout=PIPE, stderr=PIPE, stdin=PIPE) out, err = p.communicate() if p.returncode != 0: log.error('Error using hostname: {0}'.format(err)) return p.returncode
def resetnetwork(self, name, value): ifaces = {} xen_macs = utils.list_xenstore_macaddrs() for iface in utils.list_hw_interfaces(): mac = utils.get_hw_addr(iface) if not mac or mac not in xen_macs: continue ifaces[iface] = utils.get_interface(mac) # set hostname hostname = utils.get_hostname() p = Popen(["hostname", hostname], stdout=PIPE, stderr=PIPE, stdin=PIPE) out, err = p.communicate() if p.returncode != 0: return str(p.returncode) self._setup_hostname(hostname) # setup interface files for ifname, iface in ifaces.items(): self._setup_interface(ifname, iface) p = Popen(["service", "resolv", "restart"], stdout=PIPE, stderr=PIPE, stdin=PIPE) out, err = p.communicate() p = Popen(["service", "netif", "restart"], stdout=PIPE, stderr=PIPE, stdin=PIPE) out, err = p.communicate() p = Popen(["service", "routing", "start"], stdout=PIPE, stderr=PIPE, stdin=PIPE) out, err = p.communicate() return ("0", "")
def resetnetwork(self, name, value): ifaces = {} xen_macs = utils.list_xenstore_macaddrs() for iface in utils.list_hw_interfaces(): mac = utils.get_hw_addr(iface) if not mac or mac not in xen_macs: continue ifaces[iface] = utils.get_interface(mac) # set hostname utils.backup_file('/etc/hostname') hostname = utils.get_hostname() with open('/etc/hostname', 'w') as hostnamefile: print(hostname, file=hostnamefile) p = Popen(['hostname', hostname], stdout=PIPE, stderr=PIPE, stdin=PIPE) out, err = p.communicate() if p.returncode != 0: return (str(p.returncode), 'Error setting hostname: hostname') # setup interface files self._setup_loopback() for ifname, iface in ifaces.items(): self._setup_interface(ifname, iface) p = Popen(['ifdown', ifname], stdout=PIPE, stderr=PIPE, stdin=PIPE) out, err = p.communicate() p = Popen(['ifup', ifname], stdout=PIPE, stderr=PIPE, stdin=PIPE) out, err = p.communicate() if p.returncode != 0: return (str(p.returncode), 'Error restarting network') return ('0', '')
def test_get_hostname_success(self): client = ClientTest(xen_data.get_hostname(True)) hostname = utils.get_hostname(client) self.assertEqual( hostname, 'test-server', 'Hostname does not match expected ouput' )
def test_get_hostname_success_popen(self): with mock.patch('novaagent.xenstore.xenstore.Popen') as popen: popen.return_value.communicate.return_value = ( utils_data.get_hostname(True)) popen.return_value.returncode = 0 hostname = utils.get_hostname(None) self.assertEqual(hostname, 'test-server', 'Hostname does not match expected ouput')
def test_get_hostname_success_socket(self): with mock.patch('novaagent.utils.xenstore.xenstore_read', side_effect=ValueError): with mock.patch('novaagent.utils.socket') as get: get.gethostname.return_value = (xen_data.get_hostname(False)) hostname = utils.get_hostname('dummy_client') self.assertEqual(hostname, 'test-server', 'Hostname does not match expected ouput')
def test_get_hostname_exception_popen(self): with mock.patch('novaagent.xenstore.xenstore.Popen', side_effect=ValueError): with mock.patch('novaagent.utils.socket') as get: get.gethostname.return_value = (utils_data.get_hostname(False)) hostname = utils.get_hostname(None) self.assertEqual(hostname, 'test-server', 'Hostname does not match expected ouput')
def test_get_hostname_failure_popen(self): with mock.patch('novaagent.xenstore.xenstore.Popen') as popen: popen.return_value.communicate.return_value = (b'', '') popen.return_value.returncode = 1 with mock.patch('novaagent.utils.socket') as get: get.gethostname.return_value = (utils_data.get_hostname(False)) hostname = utils.get_hostname(None) self.assertEqual(hostname, 'test-server', 'Hostname does not match expected ouput')
def resetnetwork(self, name, value): ifaces = {} xen_macs = utils.list_xenstore_macaddrs() for iface in utils.list_hw_interfaces(): mac = utils.get_hw_addr(iface) if not mac or mac not in xen_macs: continue ifaces[iface] = utils.get_interface(mac) # set hostname utils.backup_file('/etc/sysconfig/network') hostname = utils.get_hostname() with open('/etc/sysconfig/network', 'w') as netfile: print('NETWORKING=yes', file=netfile) print('NOZEROCONF=yes', file=netfile) print('NETWORKING_IPV6=yes', file=netfile) print('HOSTNAME={0}'.format(hostname), file=netfile) if os.path.exists('/usr/bin/hostnamectl'): utils.backup_file('/etc/hostname') p = Popen(['hostnamectl', 'set-hostname', hostname], stdout=PIPE, stderr=PIPE, stdin=PIPE) out, err = p.communicate() if p.returncode != 0: return (str(p.returncode), 'Error setting hostname') else: p = Popen(['hostname', hostname], stdout=PIPE, stderr=PIPE, stdin=PIPE) out, err = p.communicate() if p.returncode != 0: return (str(p.returncode), 'Error setting hostname') # setup interface files for ifname, iface in ifaces.items(): self._setup_interface(ifname, iface) if 'routes' in iface: self._setup_routes(ifname, iface) p = Popen(['service', 'network', 'stop'], stdout=PIPE, stderr=PIPE, stdin=PIPE) p = Popen(['service', 'network', 'start'], stdout=PIPE, stderr=PIPE, stdin=PIPE) out, err = p.communicate() if p.returncode != 0: return (str(p.returncode), 'Error restarting network') return ('0', '')
def _setup_hostname(self, client): hostname = utils.get_hostname(client) if os.path.exists('/usr/bin/hostnamectl'): utils.backup_file(self.hostname_file) p = Popen(['hostnamectl', 'set-hostname', hostname], stdout=PIPE, stderr=PIPE, stdin=PIPE) out, err = p.communicate() else: p = Popen(['hostname', hostname], stdout=PIPE, stderr=PIPE, stdin=PIPE) out, err = p.communicate() return p.returncode, hostname
def _setup_hostname(self, client): """ hostnamectl is available in some Debian systems and depends on dbus """ hostname = utils.get_hostname(client) if os.path.exists('/usr/bin/hostnamectl'): utils.backup_file(self.hostname_file) p = Popen(['hostnamectl', 'set-hostname', hostname], stdout=PIPE, stderr=PIPE, stdin=PIPE) out, err = p.communicate() else: p = Popen(['hostname', hostname], stdout=PIPE, stderr=PIPE, stdin=PIPE) out, err = p.communicate() return p.returncode, hostname
def resetnetwork(self, name, value): ifaces = {} xen_macs = utils.list_xenstore_macaddrs() for iface in utils.list_hw_interfaces(): mac = utils.get_hw_addr(iface) if not mac or mac not in xen_macs: continue ifaces[iface] = utils.get_interface(mac) # set hostname hostname = utils.get_hostname() utils.backup_file('/etc/HOSTNAME') with open('/etc/HOSTNAME', 'w') as hostfile: print(hostname, file=hostfile) if os.path.exists('/usr/bin/hostnamectl'): utils.backup_file('/etc/hostname') p = Popen(['hostnamectl', 'set-hostname', hostname], stdout=PIPE, stderr=PIPE, stdin=PIPE) out, err = p.communicate() if p.returncode != 0: return (str(p.returncode), 'Error setting hostname') else: p = Popen(['hostname', hostname], stdout=PIPE, stderr=PIPE, stdin=PIPE) out, err = p.communicate() if p.returncode != 0: return (str(p.returncode), 'Error setting hostname') # setup interface files for ifname, iface in ifaces.items(): self._setup_interface(ifname, iface) self._setup_routes(ifname, iface) self._setup_dns(ifname, iface) p = Popen(['systemctl', 'restart', 'network.service'], stdout=PIPE, stderr=PIPE, stdin=PIPE) out, err = p.communicate() if p.returncode != 0: return (str(p.returncode), 'Error restarting network') return ('0', '')
def resetnetwork(self, name, value): ifaces = {} xen_macs = utils.list_xenstore_macaddrs() for iface in utils.list_hw_interfaces(): mac = utils.get_hw_addr(iface) if not mac or mac not in xen_macs: continue ifaces[iface] = utils.get_interface(mac) # set hostname hostname = utils.get_hostname() p = Popen(['hostname', hostname], stdout=PIPE, stderr=PIPE, stdin=PIPE) out, err = p.communicate() if p.returncode != 0: return str(p.returncode) self._setup_hostname(hostname) # setup interface files for ifname, iface in ifaces.items(): self._setup_interface(ifname, iface) p = Popen(['service', 'resolv', 'restart'], stdout=PIPE, stderr=PIPE, stdin=PIPE) out, err = p.communicate() p = Popen(['service', 'netif', 'restart'], stdout=PIPE, stderr=PIPE, stdin=PIPE) out, err = p.communicate() p = Popen(['service', 'routing', 'start'], stdout=PIPE, stderr=PIPE, stdin=PIPE) out, err = p.communicate() return ('0', '')