def detach_network_from_host(api, host, network_name, bond_name=None): network_id = api.networks.get(name=network_name).id attachment = _get_attachment_by_id(host, network_id) bonds = [nic for nic in host.nics.list() if bond_name and nic.name == bond_name] # there is no more than one bond removal_action = params.Action( removed_bonds=params.HostNics(host_nic=bonds), removed_network_attachments=params.NetworkAttachments( network_attachment=[params.NetworkAttachment( id=attachment.id)])) return host.setupnetworks(removal_action)
def attach_network_to_host(api, host, nic_name, network_name, ip_configuration, bonds=[]): network_attachment = params.NetworkAttachment( network=params.Network(name=network_name), host_nic=params.HostNIC(name=nic_name), ip_address_assignments=ip_configuration) attachment_action = params.Action( modified_bonds=params.HostNics(host_nic=bonds), modified_network_attachments=params.NetworkAttachments( network_attachment=[network_attachment]), check_connectivity=True) return host.setupnetworks(attachment_action)
def set_Host(self, host_name, cluster, ifaces): HOST = self.get_Host(host_name) CLUSTER = self.get_cluster(cluster) if HOST is None: setMsg("Host does not exist.") ifacelist = dict() networklist = [] manageip = '' try: for iface in ifaces: try: setMsg('creating host interface ' + iface['name']) if 'management' in iface: manageip = iface['ip'] if 'boot_protocol' not in iface: if 'ip' in iface: iface['boot_protocol'] = 'static' else: iface['boot_protocol'] = 'none' if 'ip' not in iface: iface['ip'] = '' if 'netmask' not in iface: iface['netmask'] = '' if 'gateway' not in iface: iface['gateway'] = '' if 'network' in iface: if 'bond' in iface: bond = [] for slave in iface['bond']: bond.append(ifacelist[slave]) try: tmpiface = params.Bonding( slaves=params.Slaves(host_nic=bond), options=params.Options( option=[ params.Option(name='miimon', value='100'), params.Option(name='mode', value='4') ] ) ) except Exception as e: setMsg('Failed to create the bond for ' + iface['name']) setFailed() setMsg(str(e)) return False try: tmpnetwork = params.HostNIC( network=params.Network(name=iface['network']), name=iface['name'], boot_protocol=iface['boot_protocol'], ip=params.IP( address=iface['ip'], netmask=iface['netmask'], gateway=iface['gateway'] ), override_configuration=True, bonding=tmpiface) networklist.append(tmpnetwork) setMsg('Applying network ' + iface['name']) except Exception as e: setMsg('Failed to set' + iface['name'] + ' as network interface') setFailed() setMsg(str(e)) return False else: tmpnetwork = params.HostNIC( network=params.Network(name=iface['network']), name=iface['name'], boot_protocol=iface['boot_protocol'], ip=params.IP( address=iface['ip'], netmask=iface['netmask'], gateway=iface['gateway'] )) networklist.append(tmpnetwork) setMsg('Applying network ' + iface['name']) else: tmpiface = params.HostNIC( name=iface['name'], network=params.Network(), boot_protocol=iface['boot_protocol'], ip=params.IP( address=iface['ip'], netmask=iface['netmask'], gateway=iface['gateway'] )) ifacelist[iface['name']] = tmpiface except Exception as e: setMsg('Failed to set ' + iface['name']) setFailed() setMsg(str(e)) return False except Exception as e: setMsg('Failed to set networks') setMsg(str(e)) setFailed() return False if manageip == '': setMsg('No management network is defined') setFailed() return False try: HOST = params.Host(name=host_name, address=manageip, cluster=CLUSTER, ssh=params.SSH(authentication_method='publickey')) if self.conn.hosts.add(HOST): setChanged() HOST = self.get_Host(host_name) state = HOST.status.state while (state != 'non_operational' and state != 'up'): HOST = self.get_Host(host_name) state = HOST.status.state time.sleep(1) if state == 'non_responsive': setMsg('Failed to add host to RHEVM') setFailed() return False setMsg('status host: up') time.sleep(5) HOST = self.get_Host(host_name) state = HOST.status.state setMsg('State before setting to maintenance: ' + str(state)) HOST.deactivate() while state != 'maintenance': HOST = self.get_Host(host_name) state = HOST.status.state time.sleep(1) setMsg('status host: maintenance') try: HOST.nics.setupnetworks(params.Action( force=True, check_connectivity=False, host_nics=params.HostNics(host_nic=networklist) )) setMsg('nics are set') except Exception as e: setMsg('Failed to apply networkconfig') setFailed() setMsg(str(e)) return False try: HOST.commitnetconfig() setMsg('Network config is saved') except Exception as e: setMsg('Failed to save networkconfig') setFailed() setMsg(str(e)) return False except Exception as e: if 'The Host name is already in use' in str(e): setMsg("Host already exists") else: setMsg("Failed to add host") setFailed() setMsg(str(e)) return False HOST.activate() while state != 'up': HOST = self.get_Host(host_name) state = HOST.status.state time.sleep(1) if state == 'non_responsive': setMsg('Failed to apply networkconfig.') setFailed() return False setMsg('status host: up') else: setMsg("Host exists.") return True
hostNics = api.hosts.get(HOST_NAME).nics # sync hostNicsParam = hostNics.list() for nic in hostNicsParam: nic.set_override_configuration(True) attachnic = params.HostNIC(network=params.Network(name='ovirtmgmt'), name='eth0', boot_protocol='static', ip=params.IP(address=IP_ADDRESS, netmask=IP_NETMASK, gateway=IP_GATEWAY), override_configuration=1) hostNics.setupnetworks( params.Action(force=0, check_connectivity=1, host_nics=params.HostNics(host_nic=[attachnic]))) # Active host host = api.hosts.get(HOST_NAME) host.activate() print 'Waiting for host to reach the Up status' while api.hosts.get(HOST_NAME).status.state != 'up': sleep(10) print "Host is up" except Exception as e: print 'Failed to setup network:\n%s' % str(e) # Add data domain ''' Add iscsi target storage = params.Storage(type_='iscsi', volume_group=params.VolumeGroup(logical_unit=[params.LogicalUnit(id=LUN_GUID, address=STORAGE_ADDRESS,
])) # management network on top of the bond backendNetwork = params.HostNIC(name='bond0', boot_protocol='none', override_configuration=True, bonding=bond0) # Now apply the rhevm network configuration try: host = api.hosts.get(HOST_NAME) host.nics.setupnetworks( params.Action( force=False, check_connectivity=True, host_nics=params.HostNics(host_nic=[nic0, backendNetwork]))) except Exception as err: print "Setup Network failed: %s" % err # Finally add the labels try: host.nics.get('bond0').labels.add(params.Label(id='Backend')) except Exception as err: print "Add Label failed: %s" % err # This only works in case a Virtual Network # with VLAN ID 110 gets assigned with the Label 'Backend'. # This Network is not defined in here.