def get_network_from_ip(ip, database_environment): net_api_credentials = get_credentials_for( environment=database_environment, credential_type=CredentialType.NETWORKAPI ) ip_client = Ip.Ip( net_api_credentials.endpoint, net_api_credentials.user, net_api_credentials.password ) ips = ip_client.get_ipv4_or_ipv6(ip) ips = ips['ips'] if type(ips) != list: ips = [ips] net_ip = ips[0] network_client = Network.Network( net_api_credentials.endpoint, net_api_credentials.user, net_api_credentials.password ) network = network_client.get_network_ipv4(net_ip['networkipv4']) network = network['network'] return network['oct1'] + '.' + network['oct2'] + '.' + network['oct3'] + '.' + network['oct4'] + '/' + network['block']
def _create_vip(self, name, vip_config): vip_id = None client_evip = EnvironmentVIP.EnvironmentVIP(self.networkapi_endpoint, self.networkapi_user, self.networkapi_password) evip = client_evip.search(ambiente_p44_txt=vip_config.environment_p44, cliente_txt=vip_config.client, finalidade_txt=vip_config.finality) client_ip = Ip.Ip(self.networkapi_endpoint, self.networkapi_user, self.networkapi_password) vip_ip = client_ip.get_available_ip4_for_vip( evip["environment_vip"]["id"], u"tsuru hm {0}".format(name)) try: client_vip = Vip.Vip(self.networkapi_endpoint, self.networkapi_user, self.networkapi_password) request = client_vip.add( id_ipv4=vip_ip["ip"]["id"], id_ipv6=None, id_healthcheck_expect=vip_config.healthcheck_expect, finality=vip_config.finality, client=vip_config.client, environment=vip_config.environment_p44, cache=vip_config.cache, method_bal=vip_config.lb_method, persistence=vip_config.persistence, healthcheck_type=u"HTTP", healthcheck=vip_config.healthcheck, timeout="5", host="{0}.hm.tsuru".format(name), maxcon=vip_config.maxconn, areanegocio=vip_config.business_area, nome_servico=vip_config.service_name, l7_filter=None, reals=[], reals_prioritys=[], reals_weights=[], ports=vip_config.port_mapping) vip_id = request["requisicao_vip"]["id"] log.debug(u"VIP request %s successfully created." % vip_id) client_vip.validate(vip_id) client_vip.criar(vip_id) address = "{oct1}.{oct2}.{oct3}.{oct4}".format(**vip_ip["ip"]) return vip_id, vip_ip["ip"]["id"], address except Exception as e: if vip_id: try: client_vip.remove_script(vip_id) client_vip.remover(vip_id) except Exception as exc: args = [str(a) for a in exc.args] error = "".join(args) log.error("Failed to remove the VIP {0}: {1}".format( vip_id, error)) client_ip.delete_ip4(vip_ip["ip"]["id"]) raise e
def _remove_vip(self, lb): client_vip = Vip.Vip(self.networkapi_endpoint, self.networkapi_user, self.networkapi_password) client_vip.remove_script(lb.id) client_vip.remover(lb.id) try: client_ip = Ip.Ip(self.networkapi_endpoint, self.networkapi_user, self.networkapi_password) client_ip.delete_ip4(lb.ip_id) except IpNaoExisteError: pass
def __init__(self, config, storage, lock_manager): self.storage = storage self.service_name = config.get("RPAAS_SERVICE_NAME", "rpaas") self.acl_api_host = config.get("ACL_API_HOST") self.acl_api_user = config.get("ACL_API_USER") self.acl_api_password = config.get("ACL_API_PASSWORD") self.acl_api_timeout = int(config.get("ACL_API_TIMEOUT", 30)) self.acl_port_range_start = config.get("ACL_PORT_RANGE_START", "30000") self.acl_port_range_end = config.get("ACL_PORT_RANGE_END", "61000") self.network_api_url = config.get("NETWORK_API_URL", None) self.acl_auth_basic = HTTPBasicAuth(self.acl_api_user, self.acl_api_password) self.lock_manager = lock_manager self.lock_name = "acl_manager:{}".format(self.service_name) if self.network_api_url: self.network_api_username = config.get("NETWORK_API_USERNAME") self.network_api_password = config.get("NETWORK_API_PASSWORD") self.ip_client = Ip.Ip(self.network_api_url, self.network_api_username, self.network_api_password) self.network_client = Network.Network(self.network_api_url, self.network_api_username, self.network_api_password)