def test_netdev_info_nettools_down(self, m_subp, m_which): """test netdev_info using nettools and down interfaces.""" m_subp.return_value = ( readResource("netinfo/new-ifconfig-output-down"), "", ) m_which.side_effect = lambda x: x if x == "ifconfig" else None assert netdev_info(".") == { "eth0": { "ipv4": [], "ipv6": [], "hwaddr": "00:16:3e:de:51:a6", "up": False, }, "lo": { "ipv4": [{ "ip": "127.0.0.1", "mask": "255.0.0.0" }], "ipv6": [{ "ip": "::1/128", "scope6": "host" }], "hwaddr": ".", "up": True, }, }
def handle(name, _cfg, _cloud, log, _args): default_interface = 'eth0' system_info = util.system_info() if 'aix' in system_info['platform'].lower(): default_interface = 'en0' interface = util.get_cfg_option_str(_cfg, 'set_hostname_from_interface', default=default_interface) log.debug('Setting hostname based on interface %s' % interface) full_hostname = None ipv4addr = None ipv6addr = None # Look up the IP address on the interface # and then reverse lookup the hostname in DNS info = netinfo.netdev_info() if interface in info: if 'addr' in info[interface]: ipv4addr = info[interface]['addr'] if 'addr6' in info[interface]: ipv6addr = info[interface]['addr6'].split('/')[0] else: log.warning('Interface %s was not found on the system. ' 'Interfaces found on system: %s' % (interface, info.keys())) ipaddr = ipv4addr or ipv6addr try: full_hostname, alias, iplist = socket.gethostbyaddr(ipaddr) if full_hostname: log.debug('Setting hostname on VM as %s' % full_hostname) short_hostname = full_hostname.split('.')[0] _cloud.distro.set_hostname(short_hostname, fqdn=full_hostname) except socket.error: log.warning('No hostname found for IP addresses %s' % ipaddr)
def test_netdev_info_iproute_down(self, m_subp, m_which): """Test netdev_info with ip and down interfaces.""" m_subp.return_value = ( readResource("netinfo/sample-ipaddrshow-output-down"), "") m_which.side_effect = lambda x: x if x == 'ip' else None self.assertEqual( { 'lo': { 'ipv4': [{ 'ip': '127.0.0.1', 'bcast': '.', 'mask': '255.0.0.0', 'scope': 'host' }], 'ipv6': [{ 'ip': '::1/128', 'scope6': 'host' }], 'hwaddr': '.', 'up': True }, 'eth0': { 'ipv4': [], 'ipv6': [], 'hwaddr': '00:16:3e:de:51:a6', 'up': False } }, netdev_info("."))
def test_netdev_info_iproute_down(self, m_subp, m_which, resource, is_json): """Test netdev_info with ip and down interfaces.""" m_subp.return_value = (readResource(resource), "") if not is_json: m_subp.side_effect = [ subp.ProcessExecutionError, (readResource(resource), ""), ] m_which.side_effect = lambda x: x if x == "ip" else None assert netdev_info(".") == { "lo": { "ipv4": [{ "ip": "127.0.0.1", "bcast": ".", "mask": "255.0.0.0", "scope": "host", }], "ipv6": [{ "ip": "::1/128", "scope6": "host" }], "hwaddr": ".", "up": True, }, "eth0": { "ipv4": [], "ipv6": [], "hwaddr": "00:16:3e:de:51:a6", "up": False, }, }
def test_netdev_info_nettools_down(self, m_subp, m_which): """test netdev_info using nettools and down interfaces.""" m_subp.return_value = ( readResource("netinfo/new-ifconfig-output-down"), "") m_which.side_effect = lambda x: x if x == 'ifconfig' else None self.assertEqual( { 'eth0': { 'ipv4': [], 'ipv6': [], 'hwaddr': '00:16:3e:de:51:a6', 'up': False }, 'lo': { 'ipv4': [{ 'ip': '127.0.0.1', 'mask': '255.0.0.0' }], 'ipv6': [{ 'ip': '::1/128', 'scope6': 'host' }], 'hwaddr': '.', 'up': True } }, netdev_info("."))
def test_netdev_info_iproute_down(self, m_subp, m_which): """Test netdev_info with ip and down interfaces.""" m_subp.return_value = ( readResource("netinfo/sample-ipaddrshow-output-down"), "", ) m_which.side_effect = lambda x: x if x == "ip" else None self.assertEqual( { "lo": { "ipv4": [{ "ip": "127.0.0.1", "bcast": ".", "mask": "255.0.0.0", "scope": "host", }], "ipv6": [{ "ip": "::1/128", "scope6": "host" }], "hwaddr": ".", "up": True, }, "eth0": { "ipv4": [], "ipv6": [], "hwaddr": "00:16:3e:de:51:a6", "up": False, }, }, netdev_info("."), )
def get_data(self): ''' Description: User Data is passed to the launching instance which is used to perform instance configuration. ''' dev_list = util.find_devs_with("LABEL=CLOUDMD") for device in dev_list: try: rbx_data = util.mount_cb(device, read_user_data_callback, self.distro.name) if rbx_data: break except OSError as err: if err.errno != errno.ENOENT: raise except util.MountFailedError: util.logexc(LOG, "Failed to mount %s when looking for user " "data", device) if not rbx_data: util.logexc(LOG, "Failed to load metadata and userdata") return False self.userdata_raw = rbx_data['userdata'] self.metadata = rbx_data['metadata'] self.cfg = rbx_data['cfg'] LOG.debug('RBX: metadata') LOG.debug(self.metadata) if self.metadata['network-interfaces']: LOG.debug("Updating network interfaces from %s", self) netdevices = netdev_info() for nic, data in netdevices.items(): ifdown_cmd = ['ifdown', nic] ip_down_cmd = ['ip', 'link', 'set', 'dev', nic, 'down'] ip_flush_cmd = ['ip', 'addr', 'flush', 'dev', nic] try: util.subp(ifdown_cmd) LOG.debug("Brought '%s' down.", nic) util.subp(ip_down_cmd) LOG.debug("Brought '%s' down.", nic) util.subp(ip_flush_cmd) LOG.debug("Cleared config of '%s'.", nic) except Exception: LOG.debug("Clearing config of '%s' failed.", nic) self.distro.apply_network(self.metadata['network-interfaces']) return True
def find_ipv6_ifaces(): info = netinfo.netdev_info() ifaces = [] for iface, data in info.items(): if iface == "lo": LOG.debug('Skipping localhost interface') if len(data.get("ipv4", [])) != 0: # skip this interface, as it has ipv4 addrs continue ifaces.append(iface) return ifaces
def handle(name, _cfg, _cloud, log, _args): default_interface = 'eth0' system_info = util.system_info() if 'aix' in system_info['platform'].lower(): default_interface = 'en0' interface = util.get_cfg_option_str(_cfg, 'set_hostname_from_interface', default=default_interface) log.debug('Setting hostname based on interface %s' % interface) set_hostname = False fqdn = None # Look up the IP address on the interface # and then reverse lookup the hostname in DNS info = netinfo.netdev_info() if interface in info: set_short = util.get_cfg_option_bool(_cfg, "set_dns_shortname", False) if 'addr' in info[interface] and info[interface]['addr']: # Handle IPv4 address set_hostname = _set_hostname(_cfg, _cloud, log, info[interface]['addr'], set_short) elif 'addr6' in info[interface] and info[interface]['addr6']: # Handle IPv6 addresses for ipaddr in info[interface]['addr6']: ipaddr = ipaddr.split('/')[0] set_hostname = _set_hostname(_cfg, _cloud, log, ipaddr, set_short) if set_hostname: break else: log.warning('Interface %s was not found on the system. ' 'Interfaces found on system: %s' % (interface, info.keys())) # Reverse lookup failed, fall back to cc_set_hostname way. if not set_hostname: (short_hostname, fqdn) = util.get_hostname_fqdn(_cfg, _cloud) try: log.info('Fall back to setting hostname on VM as %s' % fqdn) _cloud.distro.set_hostname(short_hostname, fqdn=fqdn) except Exception: util.logexc(log, "Failed to set the hostname to %s", fqdn) raise
def handle(name, _cfg, _cloud, log, _args): default_interface = 'eth0' system_info = util.system_info() if 'aix' in system_info['platform'].lower(): default_interface = 'en0' interface = util.get_cfg_option_str(_cfg, 'set_hostname_from_interface', default=default_interface) log.debug('Setting hostname based on interface %s' % interface) set_hostname = False fqdn = None # Look up the IP address on the interface # and then reverse lookup the hostname in DNS info = netinfo.netdev_info() if interface in info: set_short = util.get_cfg_option_bool(_cfg, "set_dns_shortname", False) if 'addr' in info[interface] and info[interface]['addr']: # Handle IPv4 address set_hostname =_set_hostname(_cfg, _cloud, log, info[interface]['addr'], set_short) elif 'addr6' in info[interface] and info[interface]['addr6']: # Handle IPv6 addresses for ipaddr in info[interface]['addr6']: ipaddr = ipaddr.split('/')[0] set_hostname = _set_hostname(_cfg, _cloud, log, ipaddr, set_short) if set_hostname: break else: log.warning('Interface %s was not found on the system. ' 'Interfaces found on system: %s' % (interface, info.keys())) # Reverse lookup failed, fall back to cc_set_hostname way. if not set_hostname: (short_hostname, fqdn) = util.get_hostname_fqdn(_cfg, _cloud) try: log.info('Fall back to setting hostname on VM as %s' % fqdn) _cloud.distro.set_hostname(short_hostname, fqdn=fqdn) except Exception: util.logexc(log, "Failed to set the hostname to %s", fqdn) raise
def handle(name, cfg, cloud, log, _args): manage_hosts = util.get_cfg_option_str(cfg, "manage_etc_hosts", False) if util.translate_bool(manage_hosts, addons=['template']): (hostname, fqdn) = util.get_hostname_fqdn(cfg, cloud) if not hostname: log.warn(("Option 'manage_etc_hosts' was set," " but no hostname was found")) return # Render from a template file tpl_fn_name = cloud.get_template_filename("hosts.%s" % (cloud.distro.osfamily)) # out_fn = cloud.paths.join(False, '/etc/hosts') out_fn = '/etc/hosts' params = {'hostname': hostname, 'fqdn': fqdn} devs = netinfo.netdev_info() for dev, info in devs.items(): params['dev_' + dev] = info['addr'] templater.render_to_file(tpl_fn_name, out_fn, params) if not tpl_fn_name: raise RuntimeError( ("No hosts template could be" " found for distro %s") % (cloud.distro.osfamily)) # templater.render_to_file(tpl_fn_name, '/etc/hosts', # {'hostname': hostname, 'fqdn': fqdn}) elif manage_hosts == "localhost": (hostname, fqdn) = util.get_hostname_fqdn(cfg, cloud) if not hostname: log.warn(("Option 'manage_etc_hosts' was set," " but no hostname was found")) return log.debug("Managing localhost in /etc/hosts") cloud.distro.update_etc_hosts(hostname, fqdn) else: log.debug(("Configuration option 'manage_etc_hosts' is not set," " not managing /etc/hosts in module %s"), name)
def generate_eni(netadp, distro): LOG.debug("RBX: generate_eni") LOG.debug(netadp) netdevices = netdev_info() ENI = [] CMD = [] ARPING = "arping -c 2 -S " ARPING_RHEL = "arping -c 2 -s " LOG.debug("Generating eni for distro: %s", distro) if distro == 'rhel' or distro == 'fedora': ARPING = ARPING_RHEL ENI.append('auto lo') ENI.append('iface lo inet loopback') ENI.append('') default_nic = [ n for n in sorted(netadp, key=lambda k: k['_id']) if n.get('default_gw') == "true" ] if (not default_nic) and netadp: default_nic = [sorted(netadp, key=lambda k: k['_id'])[0]] LOG.debug("Default interface mac: %s", default_nic[0].get('macaddress')) else: LOG.debug("No netadp to configure") return None LOG.debug("Default nic: %s", default_nic[0].get('macaddress')) for name, data in netdevices.items(): ENI.append('\n') name_split = name.split(':') if len(name_split) > 1 and name_split[1] != "": continue name = name_split[0] mac = data.get('hwaddr').lower() if not mac: continue nic = [n for n in netadp if n.get('macaddress').lower() == mac] if not nic: continue ip = nic[0].get("ip") network = nic[0].get("network") ENI.append('auto ' + name) if not ip: ENI.append('iface ' + name + ' inet manual') ENI.append('\tpre-up ip link set ' + name + ' up') ENI.append('\tpost-down ip link set ' + name + ' down') ENI.append('\n') continue ENI.append('iface ' + name + ' inet static') ENI.append('\taddress ' + ip[0].get("address")) ENI.append('\tnetmask ' + network.get("netmask")) if network.get("gateway"): if default_nic[0].get('macaddress') == nic[0].get('macaddress'): ENI.append('\tgateway ' + network.get("gateway")) if network.get("dns").get("nameservers"): ENI.append('\tdns-nameservers ' + ' '.join(network.get("dns").get("nameservers"))) CMD.append(ARPING + ip[0].get("address") + ' ' + network.get("gateway") + ' &') CMD.append(ARPING + ip[0].get("address") + ' ' + int2ip(ip2int(network.get("gateway")) + 2) + ' &') CMD.append(ARPING + ip[0].get("address") + ' ' + int2ip(ip2int(network.get("gateway")) + 3) + ' &') secondaryAddress = ip[1:] if secondaryAddress: LOG.debug('RBX: secondaryAddress') for index, ip in enumerate(secondaryAddress): ENI.append('\tup ip addr add ' + ip.get("address") + '/' + str(netmask_to_cidr(network.get("netmask"))) + ' dev ' + name) ENI.append('\tdown ip addr del ' + ip.get("address") + '/' + str(netmask_to_cidr(network.get("netmask"))) + ' dev ' + name) if network.get("gateway"): CMD.append(ARPING + ip.get("address") + ' ' + network.get("gateway") + ' &') CMD.append(ARPING + ip.get("address") + ' ' + int2ip(ip2int(network.get("gateway")) + 2) + ' &') CMD.append(ARPING + ip.get("address") + ' ' + int2ip(ip2int(network.get("gateway")) + 3) + ' &') if network.get('routing'): for route in network.get('routing'): ENI.append('\tup ip route add ' + route.get('destination') + ' via ' + route.get('via') + ' dev ' + name) ENI.append('\tdown ip route del ' + route.get('destination') + ' via ' + route.get('via') + ' dev ' + name) return {'eni': "\n".join(ENI), 'cmd': CMD}