Created
July 26, 2016 16:54
-
-
Save tamurray/be404e6d62da77e30bbc9fdf26a60172 to your computer and use it in GitHub Desktop.
simple test script to spin up 16 vms in Openstack using python API clients
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
import sys | |
import time | |
from pprint import pprint | |
from concurrent import futures | |
from keystoneclient.v2_0 import client as ks_client | |
from novaclient import client as nova_client | |
from neutronclient.neutron import client as neutron_client | |
TENANT='scalTest-01' | |
NEUTRON_ENDPOINT = "http://10.93.3.59:9696" | |
FLAVOR_ID = "3" # "m1.medium" | |
CIDR = "20.10.1.0/24" | |
IMAGE_ID = "2838ff3e-a4b6-4caa-b8dc-592fde88efc3" # "cirros-0.3.4-x86_64" | |
EXTNET_ID = '60d232ff-2b33-4484-814c-de93e7bcce22' # ext-net | |
def create_tenant_port(name): | |
print "create_tenant_port %s starting\n" % (name) | |
neutron = neutron_client.Client('2.0', username=ks_name, password=ks_pass, tenant_name=ks_tenant, auth_url=ks_auth, token=ks_token, endpoint_url=NEUTRON_ENDPOINT) | |
networks = neutron.list_networks(name=TENANT + '-net') | |
net_id = network_id = networks['networks'][0]['id'] | |
port_dict = {'network_id': net_id, 'name': name + "_tenant_port"} | |
response = neutron.create_port({'port': port_dict}) | |
port_id = response['port']['id'] | |
fip_dict = {'port_id': port_id, 'floating_network_id': EXTNET_ID } | |
fip_id = neutron.create_floatingip({'floatingip': fip_dict}) | |
return port_id | |
def create_private_port(name): | |
print "create_private_port %s starting\n" % (name) | |
neutron = neutron_client.Client('2.0', username=ks_name, password=ks_pass, tenant_name=ks_tenant, auth_url=ks_auth, token=ks_token, endpoint_url=NEUTRON_ENDPOINT) | |
vn_dict = {'name': name + "_private_net"} | |
response = neutron.create_network({'network': vn_dict}) | |
net_id = response['network']['id'] | |
subnet_dict = {'name': name + "_subnet", 'cidr': CIDR, 'ip_version': 4, 'network_id': net_id } | |
response = neutron.create_subnet({'subnet': subnet_dict}) | |
subnet_uuid = response['subnet']['id'] | |
port_dict = {'network_id': net_id, 'name': name + "_port"} | |
response = neutron.create_port({'port': port_dict}) | |
port_id = response['port']['id'] | |
return port_id | |
def create_topology(name): | |
print "create_topology %s starting\n" % (name) | |
executor = futures.ThreadPoolExecutor(max_workers=2) | |
tenant_port_job = executor.submit(create_tenant_port, name) | |
private_port_job = executor.submit(create_private_port, name) | |
nova = nova_client.Client('2', auth_token_=ks_token, username=ks_name, api_key=ks_pass, project_id=ks_tenant, auth_url=ks_auth) | |
tenant_port_id = tenant_port_job.result() | |
private_port_id = private_port_job.result() | |
nics = [{'port-id': tenant_port_id}, {'port-id': private_port_id}] | |
vm_name = name + "_unix" | |
response = nova.servers.create(name=vm_name, flavor=FLAVOR_ID, image=IMAGE_ID, nics=nics) | |
# Poll at 5 second intervals, until the status is no longer 'BUILD' | |
status = response.status | |
while status == 'BUILD': | |
time.sleep(5) | |
# Retrieve the instance again so the status field updates | |
response = nova.servers.get(response.id) | |
status = response.status | |
print "VM created: %s: %s" % (response, status) | |
print "create_topology %s complete\n" % (name) | |
ks_name = os.environ['OS_USERNAME'] | |
print "ks_name = '%s'" % ks_name | |
ks_pass = os.environ['OS_PASSWORD'] | |
print "ks_pass = '%s'" % ks_pass | |
ks_tenant = os.environ['OS_TENANT_NAME'] | |
print "ks_tenant = '%s'" % ks_tenant | |
ks_auth = os.environ['OS_AUTH_URL'] | |
print "ks_auth = '%s'" % ks_auth | |
keystone = ks_client.Client(username=ks_name, password=ks_pass, tenant_name=ks_tenant, auth_url=ks_auth) | |
ks_token = keystone.auth_token | |
# Fire off concurrent topology creates | |
jobs = [] | |
with futures.ThreadPoolExecutor(max_workers=16) as executor: | |
for i in range(1,17): | |
inst = "%s%s" % ("r",i) | |
jobs.append(executor.submit(create_topology,inst)) | |
for future in futures.as_completed(jobs): | |
print "Jobs completed %s" % (future.result()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
List networks is a very costly operation, in scaled scenarios like 3K VNs, we have observed that it takes ~20 seconds.