def _delete_floating_ip(self, server, fip): with logging.ExceptionLogger(LOG, "Unable to delete IP: %s" % fip["ip"]): if self.check_ip_address(fip["ip"])(server): self._dissociate_floating_ip(server, fip) with atomic.ActionTimer(self, "neutron.delete_floating_ip"): network_wrapper.wrap(self.clients, self).delete_floating_ip(fip["id"], wait=True)
def setup(self): # NOTE(rkiran): Some clients are not thread-safe. Thus during # multithreading/multiprocessing, it is likely the # sockets are left open. This problem is eliminated by # creating a connection in setup and cleanup separately. net_wrapper = network_wrapper.wrap(osclients.Clients( self.context["admin"]["credential"]), self, config=self.config) kwargs = {} if self.config["dns_nameservers"] is not None: kwargs["dns_nameservers"] = self.config["dns_nameservers"] for user, tenant_id in self._iterate_per_tenants(): self.context["tenants"][tenant_id]["networks"] = [] self.context["tenants"][tenant_id]["subnets"] = [] for i in range(self.config["networks_per_tenant"]): network_create_args = self.config["network_create_args"].copy() net_infra = net_wrapper._create_network_infrastructure( tenant_id, dualstack=self.config["dualstack"], subnets_num=self.config["subnets_per_network"], network_create_args=network_create_args, router_create_args=self.config["router"], **kwargs) self.context["tenants"][tenant_id]["networks"].append( net_infra["network"]) self.context["tenants"][tenant_id]["subnets"].extend( net_infra["subnets"])
def _remove_default_security_group(self): """Delete default security group for tenants.""" clients = osclients.Clients(self.credential) if consts.Service.NEUTRON not in clients.services().values(): return use_sg, msg = network.wrap(clients, self).supports_extension("security-group") if not use_sg: LOG.debug("Security group context is disabled: %s" % msg) return for user, tenant_id in self._iterate_per_tenants(): with logging.ExceptionLogger( LOG, "Unable to delete default security group"): uclients = osclients.Clients(user["credential"]) security_groups = uclients.neutron()\ .list_security_groups(tenant_id=tenant_id) default = [ sg for sg in security_groups["security_groups"] if sg["name"] == "default" ] if default: clients.neutron().delete_security_group(default[0]["id"])
def test_wrap(self): mock_clients = mock.Mock() config = {"fakearg": "fake"} owner = Owner() mock_clients.services.return_value = {"foo": consts.Service.NEUTRON} wrapper = network.wrap(mock_clients, owner, config) self.assertIsInstance(wrapper, network.NeutronWrapper) self.assertEqual(wrapper.owner, owner) self.assertEqual(wrapper.config, config)
def cleanup(self): net_wrapper = network_wrapper.wrap(osclients.Clients( self.context["admin"]["credential"]), self, config=self.config) for tenant_id, tenant_ctx in self.context["tenants"].items(): for network in tenant_ctx.get("networks", []): with logging.ExceptionLogger( LOG, "Failed to delete network for tenant %s" % tenant_id): net_wrapper.delete_network(network)
def _attach_floating_ip(self, server, floating_network): internal_network = list(server.networks)[0] fixed_ip = server.addresses[internal_network][0]["addr"] with atomic.ActionTimer(self, "neutron.create_floating_ip"): fip = network_wrapper.wrap(self.clients, self).create_floating_ip( ext_network=floating_network, tenant_id=server.tenant_id, fixed_ip=fixed_ip) self._associate_floating_ip(server, fip, fixed_address=fixed_ip) return fip
def cleanup(self): net_wrapper = network_wrapper.wrap(osclients.Clients( self.context["admin"]["credential"]), self, config=self.config) for tenant_id, tenant_ctx in self.context["tenants"].items(): for network in tenant_ctx.get("networks", []): for pool in network.get("lb_pools", []): with logging.ExceptionLogger( LOG, "Failed to delete pool %(pool)s for tenant " "%(tenant)s" % { "pool": pool["pool"]["id"], "tenant": tenant_id }): if self.config["lbaas_version"] == 1: net_wrapper.delete_v1_pool(pool["pool"]["id"])
def setup(self): admin_or_user = (self.context.get("admin") or self.context.get("users")[0]) net_wrapper = network.wrap(osclients.Clients( admin_or_user["credential"]), self, config=self.config) use_sg, msg = net_wrapper.supports_extension("security-group") if not use_sg: LOG.info("Security group context is disabled: %s" % msg) return secgroup_name = self.generate_random_name() for user in self.context["users"]: user["secgroup"] = _prepare_open_secgroup(user["credential"], secgroup_name)
def setup(self): net_wrapper = network_wrapper.wrap(osclients.Clients( self.context["admin"]["credential"]), self, config=self.config) use_lb, msg = net_wrapper.supports_extension("lbaas") if not use_lb: LOG.info(msg) return # Creates a lb-pool for every subnet created in network context. for user, tenant_id in self._iterate_per_tenants(): for network in self.context["tenants"][tenant_id]["networks"]: for subnet in network.get("subnets", []): if self.config["lbaas_version"] == 1: network.setdefault("lb_pools", []).append( net_wrapper.create_v1_pool(tenant_id, subnet, **self.config["pool"])) else: raise NotImplementedError( "Context for LBaaS version %s not implemented." % self.config["lbaas_version"])
def run(self, image, flavor, username, password=None, floating_net=None, port=22, use_floating_ip=True, description=None, admin_state=True, listeners=None, flavor_id=None, provider=None, vip_qos_policy_id=None, **kwargs): """Create a loadbalancer in front of two apache servers. :param image: The image to boot from :param flavor: Flavor used to boot instance :param username: ssh username on server :param password: Password on SSH authentication :param floating_net: external network name, for floating ip :param port: ssh port for SSH connection :param use_floating_ip: bool, floating or fixed IP for SSH connection :param description: Human-readable description of the loadbalancer :param admin_state: The administrative state of the loadbalancer, which is up(true) or down(false) :param listeners: The associated listener id, if any :param flavor_id: The ID of the flavor :param provider: Provider name for the loadbalancer :param vip_qos_policy_id: The ID of the QoS policy :param kwargs: Optional additional arguments for instance creation - Create a loadbalancer - Create a listener for protocal HTTP on port 80 - Create a pool using ROUND_ROBIN and protocal HTTP - Create two VMs, installing apache2 from cloud-init - Wait for loadbalancer and server resources to become ACTIVE - Create a loadbalancer member for each VM - Assign a floating IP to the loadbalancer port - Verify that a connection can be established to port 80 at the FIP """ project_id = self.context["tenant"]["id"] network = self.context["tenant"]["networks"][0] subnet = network["subnets"][0] security_group_create_args = {"name": self.generate_random_name()} security_group = self.clients("neutron").create_security_group( {"security_group": security_group_create_args}) sg_id = security_group["security_group"]["id"] security_group_rule_args = { "security_group_id": sg_id, "direction": "ingress", "port_range_max": 80, "port_range_min": 80, "protocol": "tcp", "remote_ip_prefix": "0.0.0.0/0", } self.clients("neutron").create_security_group_rule( {"security_group_rule": security_group_rule_args}) fnet = {"id": floating_net} lp_fip = network_wrapper.wrap(self.clients, self).create_floating_ip( ext_network=fnet, tenant_id=project_id) lb = self.octavia.load_balancer_create( subnet_id=subnet, description=description, admin_state=admin_state, project_id=project_id, listeners=listeners, flavor_id=flavor_id, provider=provider, vip_qos_policy_id=vip_qos_policy_id ) LOG.info("Waiting for Octavia loadbalancer to become ready.") self.octavia.wait_for_loadbalancer_prov_status(lb) lb_id = lb["id"] largs = { "protocol": "HTTP", "protocol_port": 80, "loadbalancer_id": lb_id, } listener = self.octavia.listener_create(json={"listener": largs}) l1 = listener["listener"] self.octavia.wait_for_listener_prov_status(l1) pool = self.octavia.pool_create( lb_id=lb_id, protocol="HTTP", lb_algorithm="ROUND_ROBIN", listener_id=l1["id"], project_id=project_id, ) # Assign the FIP to the loadbalancer fip_update_dict = {"port_id": lb["vip_port_id"]} self.clients("neutron").update_floatingip( lp_fip["id"], {"floatingip": fip_update_dict}) # Place a couple http servers behind the loadbalancer for s in range(2): server, fip = self._boot_server_with_fip( image, flavor, use_floating_ip=use_floating_ip, floating_net=floating_net, key_name=self.context["user"]["keypair"]["name"], userdata="#cloud-config\npackages:\n - apache2", **kwargs) server_address = server.addresses[network["name"]][0]["addr"] margs = { "address": server_address, "protocol_port": 80, } self.octavia.member_create( pool["id"], json={"member": margs}) # Allow http access to the server self._add_server_secgroups(server, sg_id) LOG.info("Added server with IP %s as a member to pool: %s" % (server_address, pool["id"])) # It takes some time for the server to become ready. # Be sure the server network is available before proceeding. script = "cloud-init status -w || exit 1" command = { "script_inline": script, "interpreter": "/bin/bash" } LOG.info("Waiting for server instance to become ready.") rally_utils.wait_for_status( server, ready_statuses=["ACTIVE"], update_resource=rally_utils.get_from_manager(), ) code, out, err = self._run_command( fip["ip"], port, username, password, command=command) if code: raise exceptions.ScriptError( "Error running command %(command)s. " "Error %(code)s: %(error)s" % { "command": command, "code": code, "error": err}) # Verifiy the http servers can be reached on the loadbalancer FIP. response = urlopen("http://%s/" % lp_fip["ip"]) r_code = response.getcode() if r_code != 200: raise exceptions.RallyException( "Received unexpected error when connecting to %s: %d" % (lp_fip["ip"], r_code)) LOG.info("Successfully connected to Octavia loadbalancer at http://%s/" % lp_fip["ip"])