def check_vxlan_cidr(self, req): # Check qualified interface with cidr and interface name (if provided). cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = CheckVxlanCidrResponse() interf = cmd.physicalInterfaceName nics = linux.get_nics_by_cidr(cmd.cidr) if len(nics) == 0: rsp.error = "can not find qualify interface for cidr [%s]" % cmd.cidr rsp.success = False elif len(nics) == 1 and interf: if nics[0].keys()[0] == interf: rsp.vtepIp = nics[0].values()[0] else: rsp.error = "The interface with cidr [%s] is not the interface [%s] which provided" % ( cmd.cidr, interf) rsp.success = False elif len(nics) == 1: rsp.vtepIp = nics[0].values()[0] elif len(nics) > 1 and interf: for nic in nics: if nic.keys()[0] == interf: rsp.vtepIp = nics[0].values()[0] if rsp.vtepIp == None: rsp.error = "No interface both qualify with cidr [%s] and interface name [%s] provided" % ( cmd.cidr, interf) rsp.success = False else: rsp.error = "Multiple interface qualify with cidr [%s] and no interface name provided" % ( cmd.cidr) return jsonobject.dumps(rsp)
def check_vxlan_cidr(self, req): # Check qualified interface with cidr and interface name (if provided). cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = CheckVxlanCidrResponse() rsp.success = False interf = cmd.physicalInterfaceName nics = linux.get_nics_by_cidr(cmd.cidr) ips = set(map(lambda d: d.values()[0], nics)) if len(nics) == 0: rsp.error = "can not find qualify interface for cidr [%s]" % cmd.cidr elif len(nics) == 1 and interf: if nics[0].keys()[0] == interf: rsp.vtepIp = nics[0].values()[0] rsp.success = True else: rsp.error = "the interface with cidr [%s] is not the interface [%s] which provided" % ( cmd.cidr, interf) elif len(nics) == 1: rsp.vtepIp = nics[0].values()[0] rsp.success = True elif len(nics) > 1 and interf: for nic in nics: if nic.keys()[0] == interf: rsp.vtepIp = nics[0].values()[0] rsp.success = True if rsp.vtepIp == None: rsp.error = "no interface both qualify with cidr [%s] and interface name [%s] provided" % ( cmd.cidr, interf) elif len(nics) == 2 and ( linux.is_vif_on_bridge(nics[0].keys()[0], nics[1].keys()[0]) or linux.is_vif_on_bridge(nics[1].keys()[0], nics[0].keys()[0])): # Note(WeiW): This is a work around for case of a interface bound to a bridge and have same ip address, # see at zstackio/issues#4056, but note this wont make assurance that routing is true rsp.vtepIp = nics[0].values()[0] rsp.success = True elif len(nics) > 1 and len(ips) == 1: rsp.error = "the qualified vtep ip bound to multiple interfaces" else: rsp.error = "multiple interface qualify with cidr [%s] and no interface name provided" % ( cmd.cidr) return jsonobject.dumps(rsp)
def check_vxlan_cidr(self, req): def filter_vxlan_nics(nics, interf, requireIp): valid_nics = copy.copy(nics) if interf: for nic in valid_nics: if interf not in nic.keys(): valid_nics.remove(nic) if requireIp: for nic in valid_nics: if requireIp not in nic.values(): valid_nics.remove(nic) return valid_nics # Check qualified interface with cidr and interface name, vtepip address (if provided). cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = CheckVxlanCidrResponse() rsp.success = False interf = cmd.physicalInterfaceName nics = linux.get_nics_by_cidr(cmd.cidr) temp_nics = filter_vxlan_nics(nics, interf, cmd.vtepip) # if there is no valid nic after filter, try all nics match the cidr of vxlan pool if len(temp_nics) != 0: nics = temp_nics ips = set(map(lambda d: d.values()[0], nics)) nicnames = set(map(lambda d: d.keys()[0], nics)) ''' there are 4 cases: 1. there is no interface has ip address matched the vxlan or vxpool cidr 2. there is only 1 interface with 1 ip address matched 3. there is only 1 interface with more than 1 ip address matched in this case, we always return the first 1 ip address 4. there has multiple interfaces with ip address matched #1, #4 will response error ''' if len(nicnames) == 0: # case #1 rsp.error = "can not find qualify interface for cidr [%s]" % cmd.cidr elif len(nicnames) == 1 and interf: # case #2 #3 if nics[0].keys()[0] == interf: rsp.vtepIp = nics[0].values()[0] rsp.success = True else: rsp.error = "the interface with cidr [%s] is not the interface [%s] which provided" % ( cmd.cidr, interf) elif len(nicnames) == 1: # case #2 #3 rsp.vtepIp = nics[0].values()[0] rsp.success = True elif len(nicnames) > 1 and interf: # case #4 for nic in nics: if nic.keys()[0] == interf: rsp.vtepIp = nics[0].values()[0] rsp.success = True if rsp.vtepIp == None: rsp.error = "no interface both qualify with cidr [%s] and interface name [%s] provided" % ( cmd.cidr, interf) elif len(nicnames) == 2 and ( linux.is_vif_on_bridge(nicnames[0], nicnames[1]) or linux.is_vif_on_bridge(nicnames[1], nicnames[0])): # Note(WeiW): This is a work around for case of a interface bound to a bridge and have same ip address, # see at zstackio/issues#4056, but note this wont make assurance that routing is true rsp.vtepIp = nics[0].values()[0] rsp.success = True elif len(nicnames) > 1 and len(ips) == 1: rsp.error = "the qualified vtep ip bound to multiple interfaces" else: rsp.error = "multiple interface qualify with cidr [%s] and no interface name provided" % cmd.cidr return jsonobject.dumps(rsp)
def connect(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = ConnectRsp() diskPaths = set() def config_lvm(enableLvmetad=False): lvm.backup_lvm_config() lvm.reset_lvm_conf_default() if enableLvmetad: lvm.config_lvm_by_sed("use_lvmetad", "use_lvmetad=1", ["lvm.conf", "lvmlocal.conf"]) else: lvm.config_lvm_by_sed("use_lvmetad", "use_lvmetad=0", ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("issue_discards", "issue_discards=1", ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("reserved_stack", "reserved_stack=256", ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("reserved_memory", "reserved_memory=131072", ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("thin_pool_autoextend_threshold", "thin_pool_autoextend_threshold=80", ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("snapshot_autoextend_threshold", "snapshot_autoextend_threshold=80", ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_filter(["lvm.conf", "lvmlocal.conf"], True) def config_drbd(): bash.bash_r( "sed -i 's/usage-count yes/usage-count no/g' /etc/drbd.d/global_common.conf" ) bash.bash_r( "iptables -I INPUT -p tcp -m tcp --dport 20000:30000 -j ACCEPT" ) drbd.install_drbd() config_lvm() config_drbd() for diskId in cmd.diskIdentifiers: disk = CheckDisk(diskId) diskPaths.add(disk.get_path()) logger.debug("find/create vg %s ..." % cmd.vgUuid) self.create_vg_if_not_found(cmd.vgUuid, diskPaths, cmd.hostUuid, cmd.forceWipe) self.create_thin_pool_if_not_found(cmd.vgUuid, INIT_POOL_RATIO) drbd.up_all_resouces() if lvm.lvm_check_operation(cmd.vgUuid) is False: logger.warn("lvm operation test failed!") lvm.clean_vg_exists_host_tags(cmd.vgUuid, cmd.hostUuid, HEARTBEAT_TAG) lvm.add_vg_tag( cmd.vgUuid, "%s::%s::%s::%s" % (HEARTBEAT_TAG, cmd.hostUuid, time.time(), bash.bash_o('hostname').strip())) if cmd.fencerAddress: lvm.clean_vg_exists_host_tags(cmd.vgUuid, '\'\'', FENCER_TAG) lvm.add_vg_tag(cmd.vgUuid, "%s::%s" % (FENCER_TAG, cmd.fencerAddress)) lvm.clean_vg_exists_host_tags(cmd.vgUuid, '\'\'', MANAGEMENT_TAG) lvm.add_vg_tag(cmd.vgUuid, "%s::%s" % (MANAGEMENT_TAG, cmd.magementAddress)) self.generate_fencer(cmd.peerManagementAddress, cmd.peerSshUsername, cmd.peerSshPassword) if cmd.storageNetworkCidr is not None: nics = linux.get_nics_by_cidr(cmd.storageNetworkCidr) if len(nics) != 0: rsp.storageNetworkAddress = nics[0].values()[0] rsp.totalCapacity, rsp.availableCapacity = lvm.get_vg_size(cmd.vgUuid) rsp.vgLvmUuid = lvm.get_vg_lvm_uuid(cmd.vgUuid) rsp.hostUuid = cmd.hostUuid return jsonobject.dumps(rsp)
def check_vxlan_cidr(self, req): def install_iptables(rules, port): needle = '-A INPUT -p udp -m udp --dport %d' % port drules = [r.replace("-A ", "-D ") for r in rules if needle in r] for rule in drules: bash_r("iptables -w %s" % rule) bash_r("iptables -w -I INPUT -p udp --dport %s -j ACCEPT" % port) def filter_vxlan_nics(nics, interf, requireIp): valid_nics = [] if interf: for nic in nics: if interf in nic.keys(): valid_nics.append(nic) if requireIp: for nic in nics: if requireIp in nic.values(): valid_nics.append(nic) return valid_nics # Check qualified interface with cidr and interface name, vtepip address (if provided). cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = CheckVxlanCidrResponse() rsp.success = False interf = cmd.physicalInterfaceName nics = linux.get_nics_by_cidr(cmd.cidr) temp_nics = filter_vxlan_nics(nics, interf, cmd.vtepip) # if there is no valid nic after filter, try all nics match the cidr of vxlan pool if len(temp_nics) != 0: nics = temp_nics ips = set(map(lambda d: d.values()[0], nics)) nicnames = list(set(map(lambda d: d.keys()[0], nics))) ''' there are 4 cases: 1. there is no interface has ip address matched the vxlan or vxpool cidr 2. there is only 1 interface with 1 ip address matched 3. there is only 1 interface with more than 1 ip address matched in this case, we always return the first 1 ip address 4. there has multiple interfaces with ip address matched #1, #4 will response error ''' if len(nicnames) == 0: # case #1 rsp.error = "can not find qualify interface for cidr [%s]" % cmd.cidr elif len(nicnames) == 1 and interf: # case #2 #3 if nics[0].keys()[0] == interf: rsp.vtepIp = nics[0].values()[0] rsp.success = True else: rsp.error = "the interface with cidr [%s] is not the interface [%s] which provided" % (cmd.cidr, interf) elif len(nicnames) == 1: # case #2 #3 rsp.vtepIp = nics[0].values()[0] rsp.success = True elif len(nicnames) > 1 and interf: # case #4 for nic in nics: if nic.keys()[0] == interf: rsp.vtepIp = nics[0].values()[0] rsp.success = True if rsp.vtepIp == None: rsp.error = "no interface both qualify with cidr [%s] and interface name [%s] provided" % (cmd.cidr, interf) elif len(nicnames) == 2 and (linux.is_vif_on_bridge(nicnames[0], nicnames[1]) or linux.is_vif_on_bridge(nicnames[1], nicnames[0])): # Note(WeiW): This is a work around for case of a interface bound to a bridge and have same ip address, # see at zstackio/issues#4056, but note this wont make assurance that routing is true rsp.vtepIp = nics[0].values()[0] rsp.success = True elif len(nicnames) > 1 and len(ips) == 1: rsp.error = "the qualified vtep ip bound to multiple interfaces" else: rsp.error = "multiple interface qualify with cidr [%s] and no interface name provided" % cmd.cidr rules = bash_o("iptables -w -S INPUT").splitlines() install_iptables(rules, 8472) install_iptables(rules, 4789) return jsonobject.dumps(rsp)