-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_instance_launch.py
More file actions
executable file
·83 lines (77 loc) · 2.87 KB
/
python_instance_launch.py
File metadata and controls
executable file
·83 lines (77 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/python2
import os
import sys
import time
import timeit
import argparse
from keystoneauth1 import loading
from keystoneauth1 import session
from novaclient import client as nv_client
from glanceclient import client as gc_client
#import keystoneclient.v2_0.client as ks_client
from keystoneclient.v2_0 import client as ks_client
from neutronclient.v2_0 import client as nt_client
# Define variables
flavor_name = 'm1.nano'
key_name = 'demotestkey1'
sec_group = ['default'] # Must be a list
network_id = [{'net-id': '638ae64c-53af-41f1-bda6-4ef5430f4b12' }] # Must be a dict
# Define image IDs
cirros_image_ID = '8ecbbd50-86a0-4948-9a38-e7d978b8e3d3'
win10_image_ID = '35b5f896-54b7-429f-b3d6-346c10898f58'
winServer_image_ID = '7d6c8e94-be48-47db-aea4-7eaf27a266a0'
ubuntu18_image_ID = 'ba010d4f-74ae-447a-8d15-beecc2a55ba1'
# Define functions
def get_keystone_cred():
d = {}
d['username'] = os.environ['OS_USERNAME']
d['password'] = os.environ['OS_PASSWORD']
d['auth_url'] = os.environ['OS_AUTH_URL']
d['project_name'] = os.environ['OS_PROJECT_NAME']
d['user_domain_name'] = os.environ['OS_USER_DOMAIN_NAME']
d['project_domain_name'] = os.environ['OS_PROJECT_DOMAIN_NAME']
#d['tenant_name'] = os.environ['OS_TENANT_NAME']
return d
if __name__ == "__main__":
# Set up argparser
parser = argparse.ArgumentParser()
parser.add_argument('--trials' ,help='The number of instances you would like to launch', type=int,default=0)
parser.add_argument('--output_file', help='The name of the output file to save run time data', default='unnamed_data.txt')
args = parser.parse_args()
# Get keystone credentials
creds = get_keystone_cred()
#keystone = ksclient.Client(**creds)
# Load credentials
loader = loading.get_plugin_loader('password')
auth = loader.load_from_options(**creds)
sess = session.Session(auth=auth)
# nova client initialization
nova = nv_client.Client(2, session=sess)
# Glance client
glance = gc_client.Client(2, session=sess)
# Neutron client initialization
neutron = nt_client.Client(session=sess)
# Launching instances
# Set all parameters for instance launching
# Set flavor
try:
fl_obj = nova.flavors.find(name=flavor_name)
except:
print("The flavor could not be found.")
sys.exit(1)
# Set Image
try:
im_obj = glance.images.get(cirros_image_ID)
except:
print("The image could not be found.")
sys.exit(1)
# Open a file for printing
f = open(args.output_file,"w")
# loop for number of trials
for i in range(args.trials):
instance_name = 'py_test_'+str(i)
tic = time.time()
nova.servers.create(instance_name, im_obj, fl_obj, security_groups=sec_group, key_name=key_name, nics=network_id)
toc = time.time()
t = toc - tic
f.write('Trial %d: %0.4f \n' % (i , t))