def _cli_add_subnet_to_other_tenant_existing_external_gateway_with_snat(self): enable_snat_states = [False, True] cidr_net = IPNetwork('99.99.0.0/24') for enable_snat in enable_snat_states: # create the external networks and routers as admin users of admin project self._as_admin() network_name = data_utils.rand_name('existing-ext-pat-network-' + str(self.nuage_pat_ini)) self.network = self.create_network_with_args(network_name, ' --router:external') router_name = data_utils.rand_name('existing-pat-router-' + str(self.nuage_pat_ini)) external_gateway_info_cli = '--external_gateway_info type=dict network_id=' + \ self.network['id'] + ',enable_snat=' + str(enable_snat) self.router = self.create_router_with_args(router_name, external_gateway_info_cli) compare_snat_str = '"enable_snat": ' + str(enable_snat) self.assertIn(compare_snat_str.lower(), self.router['external_gateway_info']) # Now create a subnet aas non-admin and try to add it to the external network: must fail self._as_tenant() subnet_name = data_utils.rand_name('os-subnet-to-existing-ext-gw') # Todo: make sure that the demo user is not an admin member in the demo project # Now run as "demo" user (non-admin) of demo project # convert the cidr_net into a string cidr = cidr_net.__str__() # Note: (.*) is a wild card for reg expressions in python; in this case for the tenant_id in the response exp_message = "Tenant (.*) not allowed to create subnet on this network" # exp_message = "The request you have made requires authentication." self.assertRaisesRegexp(exceptions.SSHExecCommandFailed, exp_message, self.create_subnet_with_args, network_name, cidr, "--name ", subnet_name) # increase cidr_net to next /24 subnet cidr_net = cidr_net.next(1)
def check_permission(permission=None): office_ips = [] permissions = dict() permissions["ips"] = [] for cidrip in permission.get('IpRanges'): p =dict() ipv4 = IPNetwork(cidrip.get('CidrIp')) p["ip"] = ipv4.__str__() if p["ip"] in office_ips: p["ip_status"] = "Office IP IPv4" elif ipv4.is_private(): p["ip_status"] = "Private IPv4" else: p["ip_status"] = "Public IPv4" p["description"] = cidrip.get('Description') permissions["ips"].append(p) for cidripv6 in permission.get('Ipv6Ranges'): p = dict() ipv6 = IPNetwork(cidripv6.get('CidrIpv6')) p["ip"] = ipv6.__str__() if p["ip"] in office_ips: p["ip_status"] = "Office IP IPv6" elif ipv6.is_private(): p["ip_status"] = "Private IPv6" else: p["ip_status"] = "Public IPv6" p["description"] = cidripv6.get('Description') permissions["ips"].append(p) if permissions["ips"]: permissions["ports"] = normalize_ports(permission=permission) if not permissions["ports"]: # In case of icmp return else: # If the permission has mapped security groups , it is of no interest to us return return permissions
def _cli_show_external_subnet_with_underlay(self): """ Show an external fip subnet created with underlay Response includes underlay values according default setting in the .ini file """ # self.needs_ini_nuage_fip_underlay(default_underlay) # avoid overlapping cidr's: use different ones ;-) # cleanup is at class level ftb, so use a different cidr according the value of default_underlay default_underlay = self.nuage_fip_underlay_ini if default_underlay is None: cidr_addition = 0 elif default_underlay is False: cidr_addition = 20 else: cidr_addition = 500 cidr_net = IPNetwork('100.99.100.0/24').next(cidr_addition) underlay_states = [False, True] for underlay in underlay_states: rand_name_str = data_utils.rand_name() ext_network_name = "ext-fip-network-" + rand_name_str ext_network = self.create_network_with_args( ext_network_name, " --router:external") ext_subnet_name = "ext-fip-underlay-subnet-" + rand_name_str underlay_str = "--underlay=" + str(underlay) subnet = self.create_subnet_with_args(ext_network['name'], cidr_net.__str__(), "--name ", ext_subnet_name, underlay_str) show_subnet = self.show_subnet(subnet['id']) # underlay value should match the default one self.assertIn( str(underlay).lower(), str(show_subnet['underlay']).lower()) cidr_net = cidr_net.next(1) pass
def gera_config(rack): id_core1=None id_core2=None name_sp1=None name_sp2=None name_sp3=None name_sp4=None name_core1=None name_core2=None int_sp1=None int_sp2=None int_sp3=None int_sp4=None int_lf1_sp1=None int_lf1_sp2=None int_lf2_sp3=None int_lf2_sp4=None int_oob_mgmtlf1=None int_oob_mgmtlf2=None int_oob_core1=None int_oob_core2=None int_core1_oob=None int_core2_oob=None #Equipamentos num_rack = rack.numero try: id_lf1 = rack.id_sw1.id name_lf1 = rack.id_sw1.nome id_lf2 = rack.id_sw2.id name_lf2 = rack.id_sw2.nome id_oob = rack.id_ilo.id name_oob = rack.id_ilo.nome except: raise RackConfigError(None,rack.nome,"Erro: Rack incompleto.") #Interface leaf01 try: interfaces = Interface.search(id_lf1) for interface in interfaces: try: sw = interface.get_switch_and_router_interface_from_host_interface(None) if sw.equipamento.nome.split('-')[2]=='01' or sw.equipamento.nome.split('-')[2]=='1': int_lf1_sp1 = interface.interface name_sp1 = sw.equipamento.nome id_sp1 = sw.equipamento.id int_sp1 = sw.interface elif sw.equipamento.nome.split('-')[2]=='02' or sw.equipamento.nome.split('-')[2]=='2': int_lf1_sp2 = interface.interface name_sp2 = sw.equipamento.nome id_sp2 = sw.equipamento.id int_sp2 = sw.interface elif sw.equipamento.nome.split('-')[0]=='OOB': int_oob_mgmtlf1 = sw.interface except: pass except InterfaceNotFoundError: raise RackConfigError(None,rack.nome,"Erro ao buscar as interfaces associadas ao Leaf 01.") if int_sp1==None or int_sp2==None or int_oob_mgmtlf1==None: raise RackConfigError(None,rack.nome,"Erro: As interfaces do Leaf01 nao foram cadastradas.") #Interface leaf02 try: interfaces1 = Interface.search(id_lf2) for interface1 in interfaces1: try: sw = interface1.get_switch_and_router_interface_from_host_interface(None) if sw.equipamento.nome.split('-')[2]=='03' or sw.equipamento.nome.split('-')[2]=='3': int_lf2_sp3 = interface1.interface name_sp3 = sw.equipamento.nome id_sp3 = sw.equipamento.id int_sp3 = sw.interface elif sw.equipamento.nome.split('-')[2]=='04' or sw.equipamento.nome.split('-')[2]=='4': int_lf2_sp4 = interface1.interface name_sp4 = sw.equipamento.nome id_sp4 = sw.equipamento.id int_sp4 = sw.interface elif sw.equipamento.nome.split('-')[0]=='OOB': int_oob_mgmtlf2 = sw.interface except: pass except InterfaceNotFoundError: raise RackConfigError(None,rack.nome,"Erro ao buscar as interfaces associadas ao Leaf 02.") if int_sp3==None or int_sp4==None or int_oob_mgmtlf2==None: raise RackConfigError(None,rack.nome,"Erro: As interfaces do Leaf02 nao foram cadastradas.") #Interface OOB try: interfaces2 = Interface.search(id_oob) for interface2 in interfaces2: try: sw = interface2.get_switch_and_router_interface_from_host_interface(None) if sw.equipamento.nome.split('-')[0]=='OOB': if sw.equipamento.nome.split('-')[2]=='01' or sw.equipamento.nome.split('-')[2]=='1': int_oob_core1 = interface2.interface name_core1 = sw.equipamento.nome int_core1_oob = sw.interface id_core1 = sw.equipamento.id elif sw.equipamento.nome.split('-')[2]=='02' or sw.equipamento.nome.split('-')[2]=='2': int_oob_core2 = interface2.interface name_core2 = sw.equipamento.nome int_core2_oob = sw.interface id_core2 = sw.equipamento.id except: pass except InterfaceNotFoundError: raise RackConfigError(None,rack.nome,"Erro ao buscar as interfaces associadas ao Switch de gerencia.") if int_oob_core1==None or int_core1_oob==None or int_oob_core2==None or int_core2_oob==None: raise RackConfigError(None,rack.nome,"Erro: As interfaces do Switch de gerencia nao foram cadastradas.") #Roteiro LF01 try: FILEINLF1 = buscar_roteiro(id_lf1, "CONFIGURACAO") except: raise RackConfigError(None,rack.nome,"Erro ao buscar o roteiro do Leaf 01.") #Roteiro LF02 try: FILEINLF2 = buscar_roteiro(id_lf2, "CONFIGURACAO") except: raise RackConfigError(None,rack.nome,"Erro ao buscar o roteiro do Leaf 02.") #Roteiro SPN01 try: FILEINSP1 = buscar_roteiro(id_sp1, "CONFIGURACAO") except: raise RackConfigError(None,rack.nome,"Erro ao buscar o roteiro do Spine 01.") #Roteiro SPN02 try: FILEINSP2 = buscar_roteiro(id_sp2, "CONFIGURACAO") except: raise RackConfigError(None,rack.nome,"Erro ao buscar o roteiro do Spine 02.") #Roteiro SPN03 try: FILEINSP3 = buscar_roteiro(id_sp3, "CONFIGURACAO") except: raise RackConfigError(None,rack.nome,"Erro ao buscar o roteiro do Spine 03.") #Roteiro SPN04 try: FILEINSP4 = buscar_roteiro(id_sp4, "CONFIGURACAO") except: raise RackConfigError(None,rack.nome,"Erro ao buscar o roteiro do Spine 04.") #Roteiro Core 01 try: FILEINCR1 = buscar_roteiro(id_core1, "CONFIGURACAO") except: raise RackConfigError(None,rack.nome,"Erro ao buscar o roteiro do Core 01.") #Roteiro Core 02 try: FILEINCR2 = buscar_roteiro(id_core2, "CONFIGURACAO") except: raise RackConfigError(None,rack.nome,"Erro ao buscar o roteiro do Core 02.") #Roteiro OOB try: FILEINOOB = buscar_roteiro(id_oob, "CONFIGURACAO") except: raise RackConfigError(None,rack.nome,"Erro ao buscar o roteiro do switch de gerencia.") #Ip LF01 try: ip_mgmtlf1 = buscar_ip(id_lf1) except: raise RackConfigError(None,rack.nome,"Erro ao buscar o ip de gerencia do leaf 01.") #Ip LF02 try: ip_mgmtlf2 = buscar_ip(id_lf2) except: raise RackConfigError(None,rack.nome,"Erro ao buscar o ip de gerencia do leaf 02.") #Ip OOB try: ip_mgmtoob = buscar_ip(id_oob) except: raise RackConfigError(None,rack.nome,"Erro ao buscar o ip de gerencia do oob.") try: NETWORKAPI_USE_FOREMAN = int(get_variable("use_foreman")) NETWORKAPI_FOREMAN_URL = get_variable("foreman_url") NETWORKAPI_FOREMAN_USERNAME = get_variable("foreman_username") NETWORKAPI_FOREMAN_PASSWORD = get_variable("foreman_password") FOREMAN_HOSTS_ENVIRONMENT_ID = get_variable("foreman_hosts_environment_id") except ObjectDoesNotExist: raise var_exceptions.VariableDoesNotExistException("Erro buscando as variáveis relativas ao Foreman.") #begin - Create Foreman entries for rack switches if NETWORKAPI_USE_FOREMAN: foreman = Foreman(NETWORKAPI_FOREMAN_URL, (NETWORKAPI_FOREMAN_USERNAME, NETWORKAPI_FOREMAN_PASSWORD), api_version=2) #for each switch, check the switch ip against foreman know networks, finds foreman hostgroup # based on model and brand and inserts the host in foreman # if host already exists, delete and recreate with new information for [switch, mac] in [[rack.id_sw1, rack.mac_sw1], [rack.id_sw2, rack.mac_sw2], [rack.id_ilo, rack.mac_ilo]]: #Get all foremand subnets and compare with the IP address of the switches until find it if mac == None: raise RackConfigError(None, rack.nome, ("Could not create entry for %s. There is no mac address." % (switch.nome))) ip = buscar_ip(switch.id) switch_cadastrado=0 for subnet in foreman.subnets.index()['results']: network = IPNetwork(ip+'/'+subnet['mask']).network #check if switches ip network is the same as subnet['subnet']['network'] e subnet['subnet']['mask'] if network.__str__() == subnet['network']: subnet_id = subnet['id'] hosts = foreman.hosts.index(search=switch.nome)['results'] if len(hosts) == 1: foreman.hosts.destroy(id=hosts[0]['id']) elif len(hosts) > 1: raise RackConfigError(None, rack.nome, ("Could not create entry for %s. There are multiple entries with the sam name." % (switch.nome))) #Lookup foreman hostgroup #By definition, hostgroup should be Marca+"_"+Modelo hostgroup_name = switch.modelo.marca.nome+"_"+switch.modelo.nome hostgroups = foreman.hostgroups.index(search=hostgroup_name) if len(hostgroups['results']) == 0: raise RackConfigError(None, rack.nome, "Could not create entry for %s. Could not find hostgroup %s in foreman." % (switch.nome, hostgroup_name)) elif len(hostgroups['results'])>1: raise RackConfigError(None, rack.nome, "Could not create entry for %s. Multiple hostgroups %s found in Foreman." % (switch.nome, hostgroup_name)) else: hostgroup_id = hostgroups['results'][0]['id'] host = foreman.hosts.create(host={'name': switch.nome, 'ip': ip, 'mac': mac, 'environment_id': FOREMAN_HOSTS_ENVIRONMENT_ID, 'hostgroup_id': hostgroup_id, 'subnet_id': subnet_id, 'build': 'true', 'overwrite': 'true'}) switch_cadastrado=1 if not switch_cadastrado: raise RackConfigError(None, rack.nome, "Unknown error. Could not create entry for %s in foreman." % (switch.nome)) #end - Create Foreman entries for rack switches var1 = autoprovision_splf(num_rack, FILEINLF1, FILEINLF2, FILEINSP1, FILEINSP2, FILEINSP3, FILEINSP4, name_lf1, name_lf2, name_oob, name_sp1, name_sp2, name_sp3, name_sp4, ip_mgmtlf1, ip_mgmtlf2, int_oob_mgmtlf1, int_oob_mgmtlf2, int_sp1, int_sp2, int_sp3, int_sp4, int_lf1_sp1, int_lf1_sp2, int_lf2_sp3, int_lf2_sp4) var2 = autoprovision_coreoob(num_rack, FILEINCR1, FILEINCR2, FILEINOOB, name_core1, name_core2, name_oob, name_lf1, name_lf2, ip_mgmtoob, int_oob_core1, int_oob_core2, int_core1_oob, int_core2_oob ) if var1 and var2: return True return False
def api_foreman(rack): try: NETWORKAPI_FOREMAN_URL = get_variable("foreman_url") NETWORKAPI_FOREMAN_USERNAME = get_variable("foreman_username") NETWORKAPI_FOREMAN_PASSWORD = get_variable("foreman_password") FOREMAN_HOSTS_ENVIRONMENT_ID = get_variable( "foreman_hosts_environment_id") except ObjectDoesNotExist: raise var_exceptions.VariableDoesNotExistException( "Erro buscando as variáveis relativas ao Foreman.") foreman = Foreman( NETWORKAPI_FOREMAN_URL, (NETWORKAPI_FOREMAN_USERNAME, NETWORKAPI_FOREMAN_PASSWORD), api_version=2) # for each switch, check the switch ip against foreman know networks, finds foreman hostgroup # based on model and brand and inserts the host in foreman # if host already exists, delete and recreate with new information for [switch, mac] in [[rack.id_sw1, rack.mac_sw1], [rack.id_sw2, rack.mac_sw2], [rack.id_ilo, rack.mac_ilo]]: # Get all foremand subnets and compare with the IP address of the switches until find it if mac == None: raise RackConfigError( None, rack.nome, ("Could not create entry for %s. There is no mac address." % switch.nome)) ip = _buscar_ip(switch.id) if ip == None: raise RackConfigError( None, rack.nome, ("Could not create entry for %s. There is no management IP." % switch.nome)) switch_cadastrado = 0 for subnet in foreman.subnets.index()['results']: network = IPNetwork(ip + '/' + subnet['mask']).network # check if switches ip network is the same as subnet['subnet']['network'] e subnet['subnet']['mask'] if network.__str__() == subnet['network']: subnet_id = subnet['id'] hosts = foreman.hosts.index(search=switch.nome)['results'] if len(hosts) == 1: foreman.hosts.destroy(id=hosts[0]['id']) elif len(hosts) > 1: raise RackConfigError(None, rack.nome, ( "Could not create entry for %s. There are multiple entries " "with the sam name." % switch.nome)) # Lookup foreman hostgroup # By definition, hostgroup should be Marca+"_"+Modelo hostgroup_name = switch.modelo.marca.nome + "_" + switch.modelo.nome hostgroups = foreman.hostgroups.index(search=hostgroup_name) if len(hostgroups['results']) == 0: raise RackConfigError( None, rack.nome, "Could not create entry for %s. Could not find hostgroup %s " "in foreman." % (switch.nome, hostgroup_name)) elif len(hostgroups['results']) > 1: raise RackConfigError( None, rack.nome, "Could not create entry for %s. Multiple hostgroups %s found" " in Foreman." % (switch.nome, hostgroup_name)) else: hostgroup_id = hostgroups['results'][0]['id'] host = foreman.hosts.create( host={ 'name': switch.nome, 'ip': ip, 'mac': mac, 'environment_id': FOREMAN_HOSTS_ENVIRONMENT_ID, 'hostgroup_id': hostgroup_id, 'subnet_id': subnet_id, 'build': 'true', 'overwrite': 'true' }) switch_cadastrado = 1 if not switch_cadastrado: raise RackConfigError( None, rack.nome, "Unknown error. Could not create entry for %s in foreman." % switch.nome)
def gera_config(rack): id_core1 = None id_core2 = None name_sp1 = None name_sp2 = None name_sp3 = None name_sp4 = None name_core1 = None name_core2 = None int_sp1 = None int_sp2 = None int_sp3 = None int_sp4 = None int_lf1_sp1 = None int_lf1_sp2 = None int_lf2_sp3 = None int_lf2_sp4 = None int_oob_mgmtlf1 = None int_oob_mgmtlf2 = None int_oob_core1 = None int_oob_core2 = None int_core1_oob = None int_core2_oob = None prefix_spine = 'SPN' prefix_oob = 'OOB' # Equipamentos num_rack = rack.numero try: id_lf1 = rack.id_sw1.id name_lf1 = rack.id_sw1.nome id_lf2 = rack.id_sw2.id name_lf2 = rack.id_sw2.nome id_oob = rack.id_ilo.id name_oob = rack.id_ilo.nome except: raise RackConfigError(None, rack.nome, 'Erro: Rack incompleto.') # Interface leaf01 try: interfaces = Interface.search(id_lf1) for interface in interfaces: try: sw = interface.get_switch_and_router_interface_from_host_interface( None) if sw.equipamento.nome.split('-')[0] == prefix_spine: if sw.equipamento.nome.split('-')[2] == '01' or sw.equipamento.nome.split('-')[2] == '1': int_lf1_sp1 = interface.interface name_sp1 = sw.equipamento.nome id_sp1 = sw.equipamento.id int_sp1 = sw.interface elif sw.equipamento.nome.split('-')[2] == '02' or sw.equipamento.nome.split('-')[2] == '2': int_lf1_sp2 = interface.interface name_sp2 = sw.equipamento.nome id_sp2 = sw.equipamento.id int_sp2 = sw.interface elif sw.equipamento.nome.split('-')[0] == prefix_oob: int_oob_mgmtlf1 = sw.interface except: pass except InterfaceNotFoundError: raise RackConfigError( None, rack.nome, 'Erro ao buscar as interfaces associadas ao Leaf 01.') if int_sp1 is None or int_sp2 is None or int_oob_mgmtlf1 is None: raise RackConfigError( None, rack.nome, 'Erro: As interfaces do Leaf01 nao foram cadastradas.') # Interface leaf02 try: interfaces1 = Interface.search(id_lf2) for interface1 in interfaces1: try: sw = interface1.get_switch_and_router_interface_from_host_interface( None) if sw.equipamento.nome.split('-')[0] == prefix_spine: if sw.equipamento.nome.split('-')[2] == '03' or sw.equipamento.nome.split('-')[2] == '3': int_lf2_sp3 = interface1.interface name_sp3 = sw.equipamento.nome id_sp3 = sw.equipamento.id int_sp3 = sw.interface elif sw.equipamento.nome.split('-')[2] == '04' or sw.equipamento.nome.split('-')[2] == '4': int_lf2_sp4 = interface1.interface name_sp4 = sw.equipamento.nome id_sp4 = sw.equipamento.id int_sp4 = sw.interface elif sw.equipamento.nome.split('-')[0] == prefix_oob: int_oob_mgmtlf2 = sw.interface except: pass except InterfaceNotFoundError: raise RackConfigError( None, rack.nome, 'Erro ao buscar as interfaces associadas ao Leaf 02.') if int_sp3 is None or int_sp4 is None or int_oob_mgmtlf2 is None: raise RackConfigError( None, rack.nome, 'Erro: As interfaces do Leaf02 nao foram cadastradas.') # Interface OOB try: interfaces2 = Interface.search(id_oob) for interface2 in interfaces2: try: sw = interface2.get_switch_and_router_interface_from_host_interface( None) if sw.equipamento.nome.split('-')[0] == prefix_oob: if sw.equipamento.nome.split('-')[2] == '01' or sw.equipamento.nome.split('-')[2] == '1': int_oob_core1 = interface2.interface name_core1 = sw.equipamento.nome int_core1_oob = sw.interface id_core1 = sw.equipamento.id elif sw.equipamento.nome.split('-')[2] == '02' or sw.equipamento.nome.split('-')[2] == '2': int_oob_core2 = interface2.interface name_core2 = sw.equipamento.nome int_core2_oob = sw.interface id_core2 = sw.equipamento.id except: pass except InterfaceNotFoundError: raise RackConfigError( None, rack.nome, 'Erro ao buscar as interfaces associadas ao Switch de gerencia.') if int_oob_core1 is None or int_core1_oob is None or int_oob_core2 is None or int_core2_oob is None: raise RackConfigError( None, rack.nome, 'Erro: As interfaces do Switch de gerencia nao foram cadastradas.') # Roteiro LF01 try: FILEINLF1 = buscar_roteiro(id_lf1, 'CONFIGURACAO') except: raise RackConfigError( None, rack.nome, 'Erro ao buscar o roteiro do Leaf 01.') # Roteiro LF02 try: FILEINLF2 = buscar_roteiro(id_lf2, 'CONFIGURACAO') except: raise RackConfigError( None, rack.nome, 'Erro ao buscar o roteiro do Leaf 02.') # Roteiro SPN01 try: FILEINSP1 = buscar_roteiro(id_sp1, 'CONFIGURACAO') except: raise RackConfigError( None, rack.nome, 'Erro ao buscar o roteiro do Spine 01.') # Roteiro SPN02 try: FILEINSP2 = buscar_roteiro(id_sp2, 'CONFIGURACAO') except: raise RackConfigError( None, rack.nome, 'Erro ao buscar o roteiro do Spine 02.') # Roteiro SPN03 try: FILEINSP3 = buscar_roteiro(id_sp3, 'CONFIGURACAO') except: raise RackConfigError( None, rack.nome, 'Erro ao buscar o roteiro do Spine 03.') # Roteiro SPN04 try: FILEINSP4 = buscar_roteiro(id_sp4, 'CONFIGURACAO') except: raise RackConfigError( None, rack.nome, 'Erro ao buscar o roteiro do Spine 04.') # Roteiro Core 01 try: FILEINCR1 = buscar_roteiro(id_core1, 'CONFIGURACAO') except: raise RackConfigError( None, rack.nome, 'Erro ao buscar o roteiro do Core 01.') # Roteiro Core 02 try: FILEINCR2 = buscar_roteiro(id_core2, 'CONFIGURACAO') except: raise RackConfigError( None, rack.nome, 'Erro ao buscar o roteiro do Core 02.') # Roteiro OOB try: FILEINOOB = buscar_roteiro(id_oob, 'CONFIGURACAO') except: raise RackConfigError( None, rack.nome, 'Erro ao buscar o roteiro do switch de gerencia.') # Ip LF01 try: ip_mgmtlf1 = buscar_ip(id_lf1) except: raise RackConfigError( None, rack.nome, 'Erro ao buscar o ip de gerencia do leaf 01.') # Ip LF02 try: ip_mgmtlf2 = buscar_ip(id_lf2) except: raise RackConfigError( None, rack.nome, 'Erro ao buscar o ip de gerencia do leaf 02.') # Ip OOB try: ip_mgmtoob = buscar_ip(id_oob) except: raise RackConfigError( None, rack.nome, 'Erro ao buscar o ip de gerencia do oob.') try: NETWORKAPI_USE_FOREMAN = int(get_variable('use_foreman')) NETWORKAPI_FOREMAN_URL = get_variable('foreman_url') NETWORKAPI_FOREMAN_USERNAME = get_variable('foreman_username') NETWORKAPI_FOREMAN_PASSWORD = get_variable('foreman_password') FOREMAN_HOSTS_ENVIRONMENT_ID = get_variable( 'foreman_hosts_environment_id') except ObjectDoesNotExist: raise var_exceptions.VariableDoesNotExistException( 'Erro buscando as variáveis relativas ao Foreman.') # begin - Create Foreman entries for rack switches if NETWORKAPI_USE_FOREMAN: foreman = Foreman(NETWORKAPI_FOREMAN_URL, (NETWORKAPI_FOREMAN_USERNAME, NETWORKAPI_FOREMAN_PASSWORD), api_version=2) # for each switch, check the switch ip against foreman know networks, finds foreman hostgroup # based on model and brand and inserts the host in foreman # if host already exists, delete and recreate with new information for [switch, mac] in [[rack.id_sw1, rack.mac_sw1], [rack.id_sw2, rack.mac_sw2], [rack.id_ilo, rack.mac_ilo]]: # Get all foremand subnets and compare with the IP address of the # switches until find it if mac is None: raise RackConfigError( None, rack.nome, ('Could not create entry for %s. There is no mac address.' % (switch.nome))) ip = buscar_ip(switch.id) if ip is None: raise RackConfigError( None, rack.nome, ('Could not create entry for %s. There is no management IP.' % (switch.nome))) switch_cadastrado = 0 for subnet in foreman.subnets.index()['results']: network = IPNetwork(ip + '/' + subnet['mask']).network # check if switches ip network is the same as # subnet['subnet']['network'] e subnet['subnet']['mask'] if network.__str__() == subnet['network']: subnet_id = subnet['id'] hosts = foreman.hosts.index(search=switch.nome)['results'] if len(hosts) == 1: foreman.hosts.destroy(id=hosts[0]['id']) elif len(hosts) > 1: raise RackConfigError( None, rack.nome, ('Could not create entry for %s. There are multiple entries with the sam name.' % (switch.nome))) # Lookup foreman hostgroup # By definition, hostgroup should be Marca+"_"+Modelo hostgroup_name = switch.modelo.marca.nome + '_' + switch.modelo.nome hostgroups = foreman.hostgroups.index( search=hostgroup_name) if len(hostgroups['results']) == 0: raise RackConfigError(None, rack.nome, 'Could not create entry for %s. Could not find hostgroup %s in foreman.' % ( switch.nome, hostgroup_name)) elif len(hostgroups['results']) > 1: raise RackConfigError(None, rack.nome, 'Could not create entry for %s. Multiple hostgroups %s found in Foreman.' % ( switch.nome, hostgroup_name)) else: hostgroup_id = hostgroups['results'][0]['id'] host = foreman.hosts.create(host={'name': switch.nome, 'ip': ip, 'mac': mac, 'environment_id': FOREMAN_HOSTS_ENVIRONMENT_ID, 'hostgroup_id': hostgroup_id, 'subnet_id': subnet_id, 'build': 'true', 'overwrite': 'true'}) switch_cadastrado = 1 if not switch_cadastrado: raise RackConfigError(None, rack.nome, "Unknown error. Could not create entry for %s in foreman." % (switch.nome)) #end - Create Foreman entries for rack switches var1 = autoprovision_splf(num_rack, FILEINLF1, FILEINLF2, FILEINSP1, FILEINSP2, FILEINSP3, FILEINSP4, name_lf1, name_lf2, name_oob, name_sp1, name_sp2, name_sp3, name_sp4, ip_mgmtlf1, ip_mgmtlf2, int_oob_mgmtlf1, int_oob_mgmtlf2, int_sp1, int_sp2, int_sp3, int_sp4, int_lf1_sp1, int_lf1_sp2, int_lf2_sp3, int_lf2_sp4) var2 = autoprovision_coreoob(num_rack, FILEINCR1, FILEINCR2, FILEINOOB, name_core1, name_core2, name_oob, name_lf1, name_lf2, ip_mgmtoob, int_oob_core1, int_oob_core2, int_core1_oob, int_core2_oob ) if var1 and var2: return True return False
def getCIDR(addr, mask): ip = IPNetwork(str(addr) + "/" + str(mask)) return ip.__str__()
if len(sys.argv) < 2: sys.stderr.write("usage: " + sys.argv[0] + " config-file-path") sys.exit(1) with open(sys.argv[1], 'r') as confStream: return json.load(confStream) if __name__ == '__main__': logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) config = load_config() for prefix,ipnet in config['prefixes'].iteritems(): prefixToNetwork[prefix.lower()] = IPNetwork(ipnet) noPrefixNetwork = IPNetwork(config['noPrefixNetwork']) logger.debug("Parsed configuration:") logger.debug(" Prefixless network: " + noPrefixNetwork.__str__()) logger.debug(" Prefixed networks:") for prefix,ipnet in prefixToNetwork.iteritems(): logger.debug(" " + prefix + ": " + ipnet.__str__()) httpd = BaseHTTPServer.HTTPServer(('', 8080), DnsUpdateApiHandler) try: httpd.serve_forever() except KeyboardInterrupt: pass httpd.server_close()
def api_foreman(rack): try: NETWORKAPI_FOREMAN_URL = get_variable("foreman_url") NETWORKAPI_FOREMAN_USERNAME = get_variable("foreman_username") NETWORKAPI_FOREMAN_PASSWORD = get_variable("foreman_password") FOREMAN_HOSTS_ENVIRONMENT_ID = get_variable("foreman_hosts_environment_id") except ObjectDoesNotExist: raise var_exceptions.VariableDoesNotExistException("Erro buscando as variáveis relativas ao Foreman.") foreman = Foreman(NETWORKAPI_FOREMAN_URL, (NETWORKAPI_FOREMAN_USERNAME, NETWORKAPI_FOREMAN_PASSWORD), api_version=2) # for each switch, check the switch ip against foreman know networks, finds foreman hostgroup # based on model and brand and inserts the host in foreman # if host already exists, delete and recreate with new information for [switch, mac] in [[rack.id_sw1, rack.mac_sw1], [rack.id_sw2, rack.mac_sw2], [rack.id_ilo, rack.mac_ilo]]: # Get all foremand subnets and compare with the IP address of the switches until find it if mac == None: raise RackConfigError(None, rack.nome, ("Could not create entry for %s. There is no mac address." % switch.nome)) ip = _buscar_ip(switch.id) if ip == None: raise RackConfigError(None, rack.nome, ("Could not create entry for %s. There is no management IP." % switch.nome)) switch_cadastrado = 0 for subnet in foreman.subnets.index()['results']: network = IPNetwork(ip + '/' + subnet['mask']).network # check if switches ip network is the same as subnet['subnet']['network'] e subnet['subnet']['mask'] if network.__str__() == subnet['network']: subnet_id = subnet['id'] hosts = foreman.hosts.index(search = switch.nome)['results'] if len(hosts) == 1: foreman.hosts.destroy(id = hosts[0]['id']) elif len(hosts) > 1: raise RackConfigError(None, rack.nome, ("Could not create entry for %s. There are multiple entries " "with the sam name." % switch.nome)) # Lookup foreman hostgroup # By definition, hostgroup should be Marca+"_"+Modelo hostgroup_name = switch.modelo.marca.nome + "_" + switch.modelo.nome hostgroups = foreman.hostgroups.index(search = hostgroup_name) if len(hostgroups['results']) == 0: raise RackConfigError(None, rack.nome, "Could not create entry for %s. Could not find hostgroup %s " "in foreman." % (switch.nome, hostgroup_name)) elif len(hostgroups['results'])>1: raise RackConfigError(None, rack.nome, "Could not create entry for %s. Multiple hostgroups %s found" " in Foreman." % (switch.nome, hostgroup_name)) else: hostgroup_id = hostgroups['results'][0]['id'] foreman.hosts.create(host = {'name': switch.nome, 'ip': ip, 'mac': mac, 'environment_id': FOREMAN_HOSTS_ENVIRONMENT_ID, 'hostgroup_id': hostgroup_id, 'subnet_id': subnet_id, 'build': 'true', 'overwrite': 'true'}) switch_cadastrado = 1 if not switch_cadastrado: raise RackConfigError(None, rack.nome, "Unknown error. Could not create entry for %s in foreman." % switch.nome)