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 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
Beispiel #3
0
def create_port_multi_ip(security_group_id=None):
    """Create port in external network, with 5 IPs (this is the maximum allowed
    with the default neutron configuration).

    Be aware that this port is not deleted when the VM is deleted!

    Another point is that security group may be different than the security
    group of the VM.

    :param security_group_id: The security group
    :return: a pre-allocated port
    """
    neutron = osclients.get_neutronclient()
    subnet_external = None
    for subnet in neutron.list_subnets()['subnets']:
        if subnet['network_id'] == network['external']:
            subnet_external = subnet['id']

    fixed_ips = list()
    for i in range(200, 205):
        fixed_ip = dict()
        fixed_ip['subnet_id'] = subnet_external
        fixed_ip['ip_address'] = '192.168.58.%d' % i
        fixed_ips.append(fixed_ip)

    p = {'port': {'network_id': network['external'], 'fixed_ips': fixed_ips}}
    if security_group_id:
        p['port']['security_groups'] = [security_group_id]
    return neutron.create_port(p)
 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))
Beispiel #5
0
 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))
Beispiel #6
0
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}})
Beispiel #7
0
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
Beispiel #8
0
def prepare_networks():
    network = dict()
    neutron = osclients.get_neutronclient()
    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}})
    return network
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)
Beispiel #10
0
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)