def launch_vm(vmname, imagename, ip, key, flavorname, cloudinit=False, configdrive=False): nova = osclients.get_novaclient() glance = osclients.get_glanceclient() neutron = osclients.get_neutronclient() image_id = glance.images.find(name=imagename).id netid = neutron.list_networks(name='shared-net')['networks'][0]['id'] flavor = nova.flavors.find(name=flavorname) nic = {'net-id': netid} extra_params = dict() if cloudinit: with open('fake_test_key.pub') as fileh: sshkey = fileh.read() with open('public-fake.gpg') as fileh: lines = fileh.readlines() lines.insert(0, '\n') gpgkey = ' '.join(lines) cloud_init_meta = cloud_init_meta.format(sshkey, gpgkey) extra_params['userdata'] = cloud_init_meta if configdrive: extra_params['files'] = {'/etc/fiware-support/defaultssh.pub': open('fake_test_key.pub'), '/etc/fiware-support/defaultgpg.pub': open('public-fake.gpg')} extra_params['config_drive'] = True extra_params['meta'] = {'encrypt': 'True'} server = nova.servers.create( vmname, flavor=flavor, image=image_id, key_name=key, security_groups=['default'], nics=[nic], **extra_params) time.sleep(2) if ip: server.add_floating_ip(ip) return server.id
def destroy_testbeds(): """ It delete all the testbed deployed automatically :return: nothing() """ nova = osclients.get_novaclient() servers = nova.servers.findall(name=settings.vm_name) # Delete vms for server in servers: if server.name == settings.vm_name: print "Deleting VM " + server.id nova.servers.delete(server) while vm_exists(nova, server): time.sleep(5) # Delete securing groups sg_name = settings.security_group sec_groups = nova.security_groups.findall(name=sg_name) for sec in sec_groups: print "Deleting sec group " + sec.name nova.security_groups.delete(sec) # Delete keypair keys = nova.keypairs.findall(name=settings.key_name) for key in keys: print "Deleting keypair " + key.name nova.keypairs.delete(key) filename = os.path.expanduser('~/.ssh/' + settings.key_name) if os.path.exists(filename): os.remove(filename)
def update_quota(self, user, role): """ It updates the quota for the user according to role requirements :param user: the user :param role: the role :return: nothing """ nova_c = osclients.get_novaclient() neutron_c = osclients.get_neutronclient() kargs = self.get_nova_quota(user, role) nova_c.quotas.update(user.cloud_project_id, **kargs) neutron_c.update_quota(user.cloud_project_id, self.get_neutron_quota(role))
def create_key_pair(): # It creates the keypair nova = osclients.get_novaclient() keys = nova.keypairs.findall(name=settings.key_name) if not keys: new_key = nova.keypairs.create(settings.key_name) filename = os.path.expanduser('~/.ssh/' + settings.key_name) with open(filename, 'w') as f: f.write(new_key.private_key) # make the file only readable by the owner os.chmod(filename, 0600)
def launch_vm(vm_n, flavor_n, securityg_n, image_n, ifaces, user_data=None, keystone_ip=None, region=None, region_keystone=None): """ Launch a VM and wait until it is ready. :param vm_n: virtual machine name :param flavor_n: flavor name :param securityg_n: securigy group name :param image_n: image name :param ifaces: array with the interfaces :param user_data: optional init script or cloud init data :return: a VM object """ # Get image image_id = osclients.get_glanceclient().images.find(name=image_n).id nova_c = osclients.get_novaclient() flavor = nova_c.flavors.find(name=flavor_n) extra_params = dict() # load script to launch with nova.servers.create() if user_data: data = open(user_data).read() extra_params['userdata'] = data extra_params['meta'] = {"Region": region, "keystone_ip": keystone_ip, "region_keystone": region_keystone} server = nova_c.servers.create( vm_n, flavor=flavor.id, image=image_id, key_name=settings.key_name, security_groups=[securityg_n], nics=ifaces, **extra_params) print('{0}: VM with UUID {1}'.format(region, server.id)) # wait until the vm is active tries = 1 while server.status != 'ACTIVE' and tries < 40: print('Waiting for ACTIVE status. (Try ' + str(tries) + '/30)') time.sleep(5) try: server = nova_c.servers.get(server.id) except: pass tries += 1 if server.status != 'ACTIVE': sys.stderr.write('Failed waiting the VM is active\n') sys.exit(-1) return server
def deploy_security_groups(sg_name, ports): # Create security group if it does not exist nova = osclients.get_novaclient() neutron = osclients.get_neutronclient() sec_groups = nova.security_groups.findall(name=sg_name) if not sec_groups: g = nova.security_groups.create(name=sg_name, description=sg_name) # Open nova.security_group_rules.create( g.id, ip_protocol='icmp', from_port=-1, to_port=-1, cidr=settings.ingress_icmp_ip_range) # Open SSH (port TCP 22) nova.security_group_rules.create( g.id, ip_protocol='tcp', from_port=22, to_port=22, cidr=settings.ingress_ssh_ip_range) for port in ports: nova.security_group_rules.create( g.id, ip_protocol='tcp', from_port=port, to_port=port, cidr=settings.ingress_ssh_ip_range) neutron.create_security_group_rule( {'security_group_rule': {'direction': 'ingress', 'security_group_id': g.id, 'remote_group_id': g.id}})
def obtain_floating_ips(num_floating_ips, booked_ips_num): neutron = osclients.get_neutronclient() nova = osclients.get_novaclient() # Get a floating IP booked_ips = [] if booked_ips_num == 1: if "BOOKED_IP" in env: booked_ips.append(env["BOOKED_IP"]) elif booked_ips_num == 2: if "BOOKED_IP1" in env: booked_ips.append(env["BOOKED_IP1"]) if "BOOKED_IP2" in env: booked_ips.append(env["BOOKED_IP2"]) floating_ips = [] available_floating_ips = [] for ip in neutron.list_floatingips()['floatingips']: if not ip["fixed_ip_address"]: available_floating_ips.append(ip["floating_ip_address"]) if booked_ips: for booked_ip in booked_ips: if booked_ip in available_floating_ips: floating_ips.append(booked_ip) else: print 'ERROR. The booked ip {0} is not available'.format(booked_ip) exit() for ip in available_floating_ips: if ip not in floating_ips: floating_ips.append(ip) if len(floating_ips) == num_floating_ips: break while len(floating_ips) < num_floating_ips: # allocate floating ip if it does not exist new_floating_ip = nova.floating_ips.create('public-ext-net-01').ip floating_ips.append(new_floating_ip) return floating_ips
def deploy_testbeds(): """ It deploy the testbed :return: """ network = dict() neutron = osclients.get_neutronclient() nova = osclients.get_novaclient() for net in neutron.list_networks()['networks']: for n in settings.network_names: if net['name'] == settings.network_names[n]: network[n] = net['id'] for n in settings.network_names: if n not in network: if n == 'management': sys.stderr.write('Fatal error: network ' + settings.network_names[n] + 'not found.\n') sys.exit(-1) network[n] = neutron.create_network( {'network': {'name': settings.network_names[n], 'admin_state_up': True}})['network']['id'] # create subnetwork. It is not possible to assign a network # to a VM without a subnetwork. neutron.create_subnet({'subnet': {'network_id': network[n], 'ip_version': 4, 'cidr': settings.subnet[n], 'gateway_ip': None}}) floating_ips = launch_vm.obtain_floating_ips(2, 1) launch_vm.create_key_pair() sg_name = settings.security_group ports = [5000, 35357, 8774, 9696, 8080, 9292] launch_vm.deploy_security_groups(sg_name, ports) # Launch testbed VM if settings.multinetwork: security_group_id = nova.security_groups.find(name=sg_name).id port = launch_vm.create_port_multi_ip(security_group_id) nics = [{'net-id': network['management']}, {'net-id': network['tunnel']}, {'port-id': port['port']['id']}] else: nics = [{'net-id': network['management']}] keystone_ip = floating_ips[0] if env["Region1"]: region = env["Region1"] else: region = 'RegionOne' print region print "Keystone IP {0}".format(keystone_ip) print "Region1 IP: {0} {1}".format(region, keystone_ip) region_keystone = region init_script = os.path.join(os.path.split(sys.argv[0])[0], settings.init_script) server = launch_vm.launch_vm(settings.vm_name, settings.flavor_name, sg_name, settings.image_name, nics, init_script, keystone_ip, region, region_keystone) # assign the floating ip if floating_ips: print('Assigning floating IP ' + floating_ips[0]) server.add_floating_ip(floating_ips[0]) if settings.multinetwork: # Launch test VM nics = [{'net-id': network['management']}, {'net-id': network['external']}] launch_vm.launch_vm(settings.vm_name_test, settings.flavor_name_test, sg_name, settings.image_name_test, nics, init_script, keystone_ip, region, region_keystone) time.sleep(120) if env["Region2"]: region = env["Region2"] else: region = 'RegionTwo' print region init_script = os.path.join(os.path.split(sys.argv[0])[0], settings.init_script) server = launch_vm.launch_vm(settings.vm_name, settings.flavor_name, sg_name, settings.image_name, nics, init_script, keystone_ip, region, region_keystone) print "Region2 IP: {0} {1}".format(region, floating_ips[1]) # assign the floating ip if floating_ips: print('Assigning floating IP ' + floating_ips[1]) server.add_floating_ip(floating_ips[1]) if settings.multinetwork: # Launch test VM nics = [{'net-id': network['management']}, {'net-id': network['external']}] launch_vm.launch_vm(settings.vm_name_test, settings.flavor_name_test, sg_name, settings.image_name_test, nics, init_script, keystone_ip, region, region_keystone)
def deploy_testbeds(): """ It deploy the testbed :return: """ network = dict() neutron = osclients.get_neutronclient() nova = osclients.get_novaclient() for net in neutron.list_networks()['networks']: for n in settings.network_names: if net['name'] == settings.network_names[n]: network[n] = net['id'] for n in settings.network_names: if n not in network: if n == 'management': sys.stderr.write('Fatal error: network ' + settings.network_names[n] + 'not found.\n') sys.exit(-1) network[n] = neutron.create_network({ 'network': { 'name': settings.network_names[n], 'admin_state_up': True } })['network']['id'] # create subnetwork. It is not possible to assign a network # to a VM without a subnetwork. neutron.create_subnet({ 'subnet': { 'network_id': network[n], 'ip_version': 4, 'cidr': settings.subnet[n], 'gateway_ip': None } }) floating_ips = launch_vm.obtain_floating_ips(2, 1) launch_vm.create_key_pair() sg_name = settings.security_group ports = [5000, 35357, 8774, 9696, 8080, 9292] launch_vm.deploy_security_groups(sg_name, ports) # Launch testbed VM if settings.multinetwork: security_group_id = nova.security_groups.find(name=sg_name).id port = launch_vm.create_port_multi_ip(security_group_id) nics = [{ 'net-id': network['management'] }, { 'net-id': network['tunnel'] }, { 'port-id': port['port']['id'] }] else: nics = [{'net-id': network['management']}] keystone_ip = floating_ips[0] if env["Region1"]: region = env["Region1"] else: region = 'RegionOne' print region print "Keystone IP {0}".format(keystone_ip) print "Region1 IP: {0} {1}".format(region, keystone_ip) region_keystone = region init_script = os.path.join( os.path.split(sys.argv[0])[0], settings.init_script) server = launch_vm.launch_vm(settings.vm_name, settings.flavor_name, sg_name, settings.image_name, nics, init_script, keystone_ip, region, region_keystone) # assign the floating ip if floating_ips: print('Assigning floating IP ' + floating_ips[0]) server.add_floating_ip(floating_ips[0]) if settings.multinetwork: # Launch test VM nics = [{ 'net-id': network['management'] }, { 'net-id': network['external'] }] launch_vm.launch_vm(settings.vm_name_test, settings.flavor_name_test, sg_name, settings.image_name_test, nics, init_script, keystone_ip, region, region_keystone) time.sleep(120) if env["Region2"]: region = env["Region2"] else: region = 'RegionTwo' print region init_script = os.path.join( os.path.split(sys.argv[0])[0], settings.init_script) server = launch_vm.launch_vm(settings.vm_name, settings.flavor_name, sg_name, settings.image_name, nics, init_script, keystone_ip, region, region_keystone) print "Region2 IP: {0} {1}".format(region, floating_ips[1]) # assign the floating ip if floating_ips: print('Assigning floating IP ' + floating_ips[1]) server.add_floating_ip(floating_ips[1]) if settings.multinetwork: # Launch test VM nics = [{ 'net-id': network['management'] }, { 'net-id': network['external'] }] launch_vm.launch_vm(settings.vm_name_test, settings.flavor_name_test, sg_name, settings.image_name_test, nics, init_script, keystone_ip, region, region_keystone)
def deploy_phone_home_vm(): network = dict() neutron = osclients.get_neutronclient() nova = osclients.get_novaclient() for net in neutron.list_networks()['networks']: for n in settings.network_names: if net['name'] == settings.network_names[n]: network[n] = net['id'] for n in settings.network_names: if n not in network: if n == 'management': sys.stderr.write('Fatal error: network ' + settings.network_names[n] + 'not found.\n') sys.exit(-1) network[n] = neutron.create_network({ 'network': { 'name': settings.network_names[n], 'admin_state_up': True } })['network']['id'] # create subnetwork. It is not possible to assign a network # to a VM without a subnetwork. neutron.create_subnet({ 'subnet': { 'network_id': network[n], 'ip_version': 4, 'cidr': settings.subnet[n], 'gateway_ip': None } }) # Get a floating IP floating_ip = launch_vm.obtain_floating_ips(1, 1)[0] launch_vm.create_key_pair() sg_name = "phonehome" vm_name = "phonehome" ports = [8081] launch_vm.deploy_security_groups(sg_name, ports) if settings.multinetwork: security_group_id = nova.security_groups.find(name=sg_name).id port = launch_vm.create_port_multi_ip(security_group_id) nics = [{ 'net-id': network['management'] }, { 'net-id': network['tunnel'] }, { 'port-id': port['port']['id'] }] else: nics = [{'net-id': network['management']}] keystone_ip = floating_ip init_script = os.path.join( os.path.split(sys.argv[0])[0], settings.init_script_phone_home) server = launch_vm.launch_vm(vm_name, settings.flavor_name_phone_home, sg_name, settings.image_phone_home, nics, init_script, keystone_ip, '', '') print "IP: {0}".format(keystone_ip) # assign the floating ip if floating_ip: print('Assigning floating IP ' + floating_ip) server.add_floating_ip(floating_ip)
def deploy_three_glances(): """ It deploy three glance services. :return: """ nova = osclients.get_novaclient() network = launch_vm.prepare_networks() floating_ips = launch_vm.obtain_floating_ips(3, 2) launch_vm.create_key_pair() sg_name = settings.security_group ports = [5000, 35357, 8774, 9696, 8080, 9292] launch_vm.deploy_security_groups(sg_name, ports) # Launch testbed VM if settings.multinetwork: security_group_id = nova.security_groups.find(name=sg_name).id port = launch_vm.create_port_multi_ip(security_group_id) nics = [{'net-id': network['management']}, {'net-id': network['tunnel']}, {'port-id': port['port']['id']}] else: nics = [{'net-id': network['management']}] keystone_ip = floating_ips[0] if "Region1" in env: region = env["Region1"] else: region = 'RegionOne' print region print "Keystone IP {0}".format(keystone_ip) print "Region1 IP: {0} {1}".format(region, keystone_ip) region_keystone = region deploy_glance(nics, keystone_ip, region, region_keystone, floating_ips[0], network) time.sleep(120) if "Region2" in env: region = env["Region2"] else: region = 'RegionTwo' print region print "Region2 IP: {0} {1}".format(region, floating_ips[2]) deploy_glance(nics, keystone_ip, region, region_keystone, floating_ips[2], network) if "Region3" in env: region = env["Region3"] else: region = 'RegionThree' if "Region2keystone" in env: region_keystone = env["Region2keystone"] keystone_ip = floating_ips[1] print region print "Region3 IP: {0} {1}".format(region, floating_ips[1]) deploy_glance(nics, keystone_ip, region, region_keystone, floating_ips[1], network)