def up(self): """ Set up the topology. """ nova = novaclient.Client('2', session=self.session) neutron = neutronclient.Client(endpoint_url=self.neutron_endpoint, token=self.token) neutron_if = NeutronIF() # Create nets logging.info('Creating networks...') try: for i in range(len(self.instances)): self.nets.append(self._create_net(neutron=neutron, net_name=self.net_names[i])) except Exception as e: logging.error('ERROR at creating networks:') logging.error(e) else: logging.info('Success!') # Create subnets into the created nets logging.info('Creating subnetworks...') try: for i in range(len(self.instances)): self.subnets.append(self._create_subnet(neutron=neutron, subnet_name=self.subnet_names[i], subnet_prefix=self.net_prefixes[i], net_id=self.nets[i]['id'])) except Exception as e: logging.error('ERROR at creating subnetworks') logging.error(e) else: logging.info('Success!') # Create router and connect to net logging.info('Creating router...') try: self._create_router(neutron=neutron, nova=nova, net_name=self.net_names[0], router_name=self.router_name[0], port_names=self.router_ports) self._create_router(neutron=neutron, nova=nova, net_name=self.net_names[1:], router_name=self.router_name[1]) except Exception as e: logging.error('ERROR at creating router') logging.error(e) else: logging.info('Success!') # Boot the load balancer instance logging.info('Booting load balancer instance') self._boot_instance(nova=nova, image=self.images[0], flavor=self.flavors[0], nets=self.net_names[:2], key_name=self.key_names[0], secgroups=self.secgroups[0], name='loadbalancer', userdata=self.userdata[0], count=self.instances[0]) logging.info('Success!') # Boot the server instances logging.info("Booting server instances") try: self._boot_instance(nova=nova, image=self.images[1], flavor=self.flavors[1], nets=self.net_names[1:], key_name=self.key_names[1], secgroups=self.secgroups[1], name='server', userdata=self.userdata[1], count=self.instances[1] ) except Exception as e: logging.error('ERROR when creating servers') logging.error(e) else: logging.info('Success!') # Boot the storage instances logging.info('Booting storage instances') try: self._boot_instance(nova=nova, image=self.images[2], flavor=self.flavors[2], nets=self.net_names[2], key_name=self.key_names[2], secgroups=self.secgroups[2], name='persist_1', count=1, userdata=self.userdata[2], fixed_ip=[self.fixed_ips[2]]) self._boot_instance(nova=nova, image=self.images[2], flavor=self.flavors[2], nets=self.net_names[2], key_name=self.key_names[2], secgroups=self.secgroups[2], name='persist_2', count=1, userdata=self.userdata[3], fixed_ip=[self.fixed_ips[3]]) except Exception as e: logging.error('ERROR when creating storage instances') logging.error(e) else: logging.info('Success!') # Allocate a floating IP and associate to balancer instance logging.info('Allocating IP') try: for server in nova.servers.list(): if server.name == 'loadbalancer': id_ = server.id nova.floating_ips.create(pool='ExtNet') floating_ips = nova.floating_ips.list() floating_ip = floating_ips[0].ip nova.servers.add_floating_ip(server=id_, address=floating_ip) except Exception as e: logging.error('ERROR when allocating IP') logging.error(e) else: logging.info('Success!') # Create Firewall logging.info('Creating FWaaS') try: for fw_rule in self.fw_opts['rules']: neutron_if.firewall_rule_create(fw_rule) neutron_if.firewall_policy_create(name=self.fw_opts['policy_name'], fw_rules=self.fw_opts['policy_rules']) neutron_if.firewall_create(name=self.fw_opts['fw_name'], fw_policy=self.fw_opts['policy_name'], router=self.fw_opts['fw_router']) except Exception as e: logging.error('ERROR at creating FWaaS') logging.error(e) else: logging.info('Success!')
def drop(self): """ Drops the All Topology, which is the topology of all the elements in the tenant, """ nova = novaclient.Client('2', session=self.session) neutron = neutronclient.Client(endpoint_url=self.neutron_endpoint, token=self.token) neutron_if = NeutronIF() # Remove FWaaS logging.info('Removing FWaaS') try: for fw in neutron_if.firewall_list(): neutron_if.firewall_delete(fw.get('id')) for policy in neutron_if.firewall_policy_list(): neutron_if.firewall_policy_delete(policy.get('id')) for rule in neutron_if.firewall_rule_list(): neutron_if.firewall_rule_delete(id_=rule.get('id')) except Exception as e: logging.error('ERROR at removing FWaaS') logging.error(e) else: logging.info('Success!') # Deallocate floating IPs logging.info('Deallocating floating IPs') try: floating_ips = nova.floating_ips.list() for f_ip in floating_ips: nova.servers.remove_floating_ip(server=f_ip.instance_id, address=f_ip.ip) nova.floating_ips.delete(f_ip.id) except Exception as e: logging.error('ERROR at deallocating floating IPs') logging.error(e) else: logging.info('Success!') # Delete routers logging.info('Deleting routers...') try: routers = neutron_if.router_list() for router in routers: id_ = router['id'] # Delete gateway neutron_if.router_gateway_clear(router_id=id_) # Get router ports interfaces = neutron_if.router_port_list(router_id=id_) # Delete interfaces the router is connected to for interface in interfaces: port_id = interface.get('id') neutron_if.router_interface_delete(router_id=id_, port_id=port_id) # Delete router neutron_if.router_delete(router_id=id_) except Exception as e: logging.error('Error at deleting routers') logging.error(e) else: logging.info('Success!') # Delete instances logging.info('Deleting instances...') try: servers = nova.servers.list() for server in servers: server.delete() except Exception as e: logging.error('Error at deleting the instances') logging.error(e) else: logging.info('Success!') time.sleep(5) # Delete networks logging.info('Deleting subnets and nets...') try: networks = neutron.list_networks()['networks'] for network in networks: if network['name'] == 'ExtNet': continue id_ = network['id'] for subnet in network['subnets']: neutron.delete_subnet(subnet) neutron.delete_network(id_) except Exception as e: logging.error('Error at deleting the subnets and nets') logging.error(e) else: logging.info('Success!')