def make_networks(conf): conn = os_client_config.make_sdk() net = IPv4Network(u'10.0.0.0/8') snets = net.subnets(prefixlen_diff=16) ext_gateway_net = conn.network.find_network( conf["external_gateway_network"], ignore_missing=False ) for number, cidr in enumerate(snets): network = conn.network.create_network( name="test_network_{}".format(number), ) subnet = conn.network.create_subnet( network_id=network.id, ip_version=4, cidr=str(cidr), name="test_subnet_{}".format(number), ) router = conn.network.create_router( name="test_router_{}".format(number), external_gateway_info={ "network_id": ext_gateway_net.id, } ) conn.network.add_interface_to_router( router, subnet_id=subnet.id, ) yield {"uuid": network.id}
def verify_active(building, timeout=600): conn = os_client_config.make_sdk() active = [] delay = 10 timeout_ = datetime.utcnow() + timedelta(seconds=timeout) while building: if datetime.utcnow() < timeout_: sleep(delay) else: raise Exception( "{} servers failed to build within {} seconds.".format( len(building), timeout, ) ) still_building = [] for server in building: server = conn.compute.get_server(server) if server.status == "ACTIVE": active.append(server) elif server.status == "BUILD": still_building.append(server) else: raise Exception( "The resource {} is in an invalid state.".format( server.id ) ) building = still_building return active
def sync_vms(): openstack = os_client_config.make_sdk() with open(OPENSTACK_SERVERS) as f: openstack_servers = json.load(f) vm_state = {} for chall, vm_uuids in openstack_servers.items(): for vm_uuid in vm_uuids: vm = openstack.compute.get_server(vm_uuid) if vm.status == "SHUTOFF": status = "off" elif vm.status == "ACTIVE": status = "on" else: logging.critical('VM %s IN UNKNOWN STATE, CHECK OPENSTACK', vm_uuid) status = "off" # assume off def get_addr(constraints): try: return next(addrinfo["addr"] for addrinfo in vm.addresses[CTF_NETWORK] if constraints.issubset(addrinfo.items())) except StopIteration: return None addr = get_addr(ADDR_CONSTRAINTS) extaddr = get_addr(EXTADDR_CONSTRAINTS) vm_state[vm_uuid] = VMState(status, addr, extaddr) return openstack_servers, vm_state
def create_floating_ips(network, count): conn = os_client_config.make_sdk() ext_gateway_net = conn.network.find_network(network, ignore_missing=False) ips = [] for _ in range(count): fip = conn.network.create_ip(floating_network_id=ext_gateway_net.id) ips.append(fip.floating_ip_address) return ips
def stop_vm(vm_uuid): try: logging.info("Stopping VM %s", vm_uuid) openstack = os_client_config.make_sdk() openstack.compute.stop_server(vm_uuid) return ("stop_vm", (vm_uuid, )) except: logging.exception("Got exception on stop_vm(%r)", vm_uuid) return None
def generate_resources(conf): conn = os_client_config.make_sdk() image = conn.image.find_image(conf["servers"]["image"], ignore_missing=False) flavor = conn.compute.find_flavor(conf["servers"]["flavor"], ignore_missing=False) sec_group = conn.network.find_security_group( conf["security_group"]["name"], ignore_missing=False) server_names = ("test_server_{}".format(id_) for id_ in xrange(conf["servers"]["count"])) networks = make_networks(conf["networks"]) counts = izip( server_names, get_child_counts(conf["servers"]["count"], conf["volumes"]["count"]), get_child_counts(conf["servers"]["count"], conf["networks"]["count"]), ) building = [ conn.compute.create_server( name=name, image_id=image.id, flavor_id=flavor.id, block_device_mapping_v2=[ { "source_type": "image", "destination_type": "local", "delete_on_termination": True, "uuid": image.id, "boot_index": 0, }, ] + [ { "source_type": "blank", "destination_type": "volume", "volume_size": conf["volumes"]["size"], "delete_on_termination": True, }, ] * v_count, networks=list(islice(networks, 0, n_count)), security_groups=[{ "name": sec_group.name }], ) for name, v_count, n_count in counts ] floating_ips = create_floating_ips( conf["networks"]["external_gateway_network"], len(building)) active = verify_active(building) for server, floating_ip in zip(active, floating_ips): conn.compute.add_floating_ip_to_server(server, floating_ip) return floating_ips
def set_quotas(): conn = os_client_config.make_sdk() project_id = conn.session.get_project_id() nova = nova_client.Client("2", session=conn.session) try: cinder = cinder_client.Client("2", session=conn.session) cinder.quotas.update( project_id, gigabytes=-1, snapshots=-1, volumes=-1, ) except exceptions.SDKException: cinder = cinder_client.Client("1", session=conn.session) cinder.quotas.update( project_id, gigabytes=-1, snapshots=-1, volumes=-1, ) nova.quotas.update( project_id, cores=-1, fixed_ips=-1, floating_ips=-1, injected_file_content_bytes=-1, injected_file_path_bytes=-1, injected_files=-1, instances=-1, key_pairs=-1, metadata_items=-1, ram=-1, security_group_rules=-1, security_groups=-1, server_groups=-1, server_group_members=-1, ) conn.network.update_quota( project_id, floatingip=-1, network=-1, port=-1, router=-1, security_group=-1, security_group_rule=-1, subnet=-1, )
def start_vm(vm_uuid): try: logging.info("Starting VM %s", vm_uuid) openstack = os_client_config.make_sdk() while True: vm = openstack.compute.get_server(vm_uuid) if vm.status == "ACTIVE": break if vm.task_state != "powering-on": openstack.compute.start_server(vm_uuid) time.sleep(2) return ("start_vm", (vm_uuid, )) except: logging.exception("Got exception on start_vm(%r)", vm_uuid) return None
def ensure_sec_group(name): conn = os_client_config.make_sdk() try: sg = conn.network.find_security_group(name, ignore_missing=False) except exceptions.ResourceNotFound: sg = conn.network.create_security_group(name=name) try: conn.network.create_security_group_rule(security_group_id=sg.id, direction='ingress', remote_ip_prefix='0.0.0.0/0', protocol='icmp', port_range_max=None, port_range_min=None, ethertype='IPv4') except exceptions.HttpException as e: if e.http_status != 409: raise e return sg
s['NetworkGateway'], host_routes, allocation_pool, name, segment.id, CONF['nameservers']) else: subnet = _neutron_subnet_create(sdk, ctlplane_id, s['NetworkCidr'], s['NetworkGateway'], host_routes, allocation_pool, name, None, CONF['nameservers']) # If the subnet is IPv6 we need to start a router so that router # advertisments are sent out for stateless IP addressing to work. if netaddr.IPNetwork(s['NetworkCidr']).version == 6: _ensure_neutron_router(sdk, name, subnet.id) if 'true' not in _run_command(['hiera', 'neutron_api_enabled'], name='hiera').lower(): print('WARNING: UndercloudCtlplaneNetworkDeployment : The Neutron API ' 'is disabled. The ctlplane network cannot be configured.') else: sdk = os_client_config.make_sdk(auth_url=AUTH_URL, project_name='admin', username='******', password=ADMIN_PASSWORD, project_domain_name='Default', user_domain_name='Default') network = _ensure_neutron_network(sdk) config_neutron_segments_and_subnets(sdk, network.id)