blob: 81f3e81963fb93d981f0bdfc5a85e866ee473973 [file] [log] [blame]
koder aka kdanilov4643fd62015-02-10 16:20:13 -08001import re
2import os
3import time
koder aka kdanilove21d7472015-02-14 19:02:04 -08004import logging
koder aka kdanilov4643fd62015-02-10 16:20:13 -08005
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -08006from concurrent.futures import ThreadPoolExecutor
7
koder aka kdanilov4643fd62015-02-10 16:20:13 -08008from novaclient.client import Client as n_client
9from cinderclient.v1.client import Client as c_client
10
koder aka kdanilovda45e882015-04-06 02:24:42 +030011from nodes.node import Node
12from nodes.openstack import get_floating_ip
koder aka kdanilov4643fd62015-02-10 16:20:13 -080013
koder aka kdanilove21d7472015-02-14 19:02:04 -080014logger = logging.getLogger("io-perf-tool")
15
16
koder aka kdanilov4643fd62015-02-10 16:20:13 -080017def ostack_get_creds():
18 env = os.environ.get
19 name = env('OS_USERNAME')
20 passwd = env('OS_PASSWORD')
21 tenant = env('OS_TENANT_NAME')
22 auth_url = env('OS_AUTH_URL')
23 return name, passwd, tenant, auth_url
24
25
26def nova_connect():
27 return n_client('1.1', *ostack_get_creds())
28
29
30def create_keypair(nova, name, key_path):
31 with open(key_path) as key:
32 return nova.keypairs.create(name, key.read())
33
34
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080035def create_volume(size, name):
koder aka kdanilov4643fd62015-02-10 16:20:13 -080036 cinder = c_client(*ostack_get_creds())
koder aka kdanilov4643fd62015-02-10 16:20:13 -080037 vol = cinder.volumes.create(size=size, display_name=name)
38 err_count = 0
39 while vol.status != 'available':
40 if vol.status == 'error':
41 if err_count == 3:
koder aka kdanilove21d7472015-02-14 19:02:04 -080042 logger.critical("Fail to create volume")
koder aka kdanilov4643fd62015-02-10 16:20:13 -080043 raise RuntimeError("Fail to create volume")
44 else:
45 err_count += 1
46 cinder.volumes.delete(vol)
47 time.sleep(1)
48 vol = cinder.volumes.create(size=size, display_name=name)
49 continue
50 time.sleep(1)
51 vol = cinder.volumes.get(vol.id)
52 return vol
53
54
55def wait_for_server_active(nova, server, timeout=240):
56 t = time.time()
57 while True:
koder aka kdanilov3f356262015-02-13 08:06:14 -080058 time.sleep(1)
koder aka kdanilov4643fd62015-02-10 16:20:13 -080059 sstate = getattr(server, 'OS-EXT-STS:vm_state').lower()
60
61 if sstate == 'active':
62 return True
63
koder aka kdanilov4643fd62015-02-10 16:20:13 -080064 if sstate == 'error':
65 return False
66
67 if time.time() - t > timeout:
68 return False
69
70 server = nova.servers.get(server)
71
72
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080073class Allocate(object):
74 pass
75
76
77def get_floating_ips(nova, pool, amount):
koder aka kdanilov4643fd62015-02-10 16:20:13 -080078 ip_list = nova.floating_ips.list()
79
80 if pool is not None:
81 ip_list = [ip for ip in ip_list if ip.pool == pool]
82
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080083 return [ip for ip in ip_list if ip.instance_id is None][:amount]
koder aka kdanilov4643fd62015-02-10 16:20:13 -080084
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080085
koder aka kdanilovda45e882015-04-06 02:24:42 +030086def launch_vms(config):
87 creds = config['creds']
88 if creds != 'ENV':
89 raise ValueError("Only 'ENV' creds are supported")
90
91 logger.debug("Starting new nodes on openstack")
92 conn = nova_connect()
93 params = config['vm_params'].copy()
94 count = params.pop('count')
95
96 if isinstance(count, basestring):
97 assert count.startswith("x")
98 lst = conn.services.list(binary='nova-compute')
99 srv_count = len([srv for srv in lst if srv.status == 'enabled'])
100 count = srv_count * int(count[1:])
101
102 creds = params.pop('creds')
103
104 for ip, _ in create_vms_mt(conn, count, **params):
105 yield Node(creds.format(ip), [])
106
107
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800108def create_vms_mt(nova, amount, keypair_name, img_name,
109 flavor_name, vol_sz=None, network_zone_name=None,
koder aka kdanilov7dec9df2015-02-15 21:35:19 -0800110 flt_ip_pool=None, name_templ='ceph-test-{0}',
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800111 scheduler_hints=None):
112
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800113 with ThreadPoolExecutor(max_workers=16) as executor:
koder aka kdanilov97644f92015-02-13 11:11:08 -0800114 if network_zone_name is not None:
115 network_future = executor.submit(nova.networks.find,
116 label=network_zone_name)
117 else:
118 network_future = None
119
120 fl_future = executor.submit(nova.flavors.find, name=flavor_name)
121 img_future = executor.submit(nova.images.find, name=img_name)
122
123 if flt_ip_pool is not None:
124 ips_future = executor.submit(get_floating_ips,
125 nova, flt_ip_pool, amount)
koder aka kdanilove21d7472015-02-14 19:02:04 -0800126 logger.debug("Wait for floating ip")
koder aka kdanilov97644f92015-02-13 11:11:08 -0800127 ips = ips_future.result()
128 ips += [Allocate] * (amount - len(ips))
129 else:
130 ips = [None] * amount
131
koder aka kdanilov7dec9df2015-02-15 21:35:19 -0800132 logger.debug("Getting flavor object")
koder aka kdanilov97644f92015-02-13 11:11:08 -0800133 fl = fl_future.result()
koder aka kdanilov7dec9df2015-02-15 21:35:19 -0800134 logger.debug("Getting image object")
koder aka kdanilov97644f92015-02-13 11:11:08 -0800135 img = img_future.result()
136
137 if network_future is not None:
koder aka kdanilove21d7472015-02-14 19:02:04 -0800138 logger.debug("Waiting for network results")
koder aka kdanilov97644f92015-02-13 11:11:08 -0800139 nics = [{'net-id': network_future.result().id}]
140 else:
141 nics = None
142
koder aka kdanilov97644f92015-02-13 11:11:08 -0800143 names = map(name_templ.format, range(amount))
144
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800145 futures = []
koder aka kdanilov6e2ae792015-03-04 18:02:24 -0800146 logger.debug("Requesting new vms")
147
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800148 for name, flt_ip in zip(names, ips):
149 params = (nova, name, keypair_name, img, fl,
150 nics, vol_sz, flt_ip, scheduler_hints,
151 flt_ip_pool)
152
153 futures.append(executor.submit(create_vm, *params))
koder aka kdanilove21d7472015-02-14 19:02:04 -0800154 res = [future.result() for future in futures]
155 logger.debug("Done spawning")
156 return res
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800157
158
159def create_vm(nova, name, keypair_name, img,
160 fl, nics, vol_sz=None,
161 flt_ip=False,
162 scheduler_hints=None,
163 pool=None):
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800164 for i in range(3):
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800165 srv = nova.servers.create(name,
166 flavor=fl, image=img, nics=nics,
167 key_name=keypair_name,
168 scheduler_hints=scheduler_hints)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800169
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800170 if not wait_for_server_active(nova, srv):
171 msg = "Server {0} fails to start. Kill it and try again"
koder aka kdanilove21d7472015-02-14 19:02:04 -0800172 logger.debug(msg.format(srv))
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800173 nova.servers.delete(srv)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800174
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800175 while True:
koder aka kdanilov3f356262015-02-13 08:06:14 -0800176 # print "wait till server deleted"
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800177 all_id = set(alive_srv.id for alive_srv in nova.servers.list())
178 if srv.id not in all_id:
179 break
180 time.sleep(1)
181 else:
182 break
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800183
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800184 if vol_sz is not None:
koder aka kdanilov3f356262015-02-13 08:06:14 -0800185 # print "creating volume"
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800186 vol = create_volume(vol_sz, name)
koder aka kdanilov3f356262015-02-13 08:06:14 -0800187 # print "attach volume to server"
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800188 nova.volumes.create_server_volume(srv.id, vol.id, None)
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800189
190 if flt_ip is Allocate:
191 flt_ip = nova.floating_ips.create(pool)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300192
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800193 if flt_ip is not None:
koder aka kdanilov3f356262015-02-13 08:06:14 -0800194 # print "attaching ip to server"
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800195 srv.add_floating_ip(flt_ip)
Yulia Portnova0e64ea22015-03-20 17:27:22 +0200196
koder aka kdanilovda45e882015-04-06 02:24:42 +0300197 return flt_ip.ip, nova.servers.get(srv.id)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800198
199
koder aka kdanilov7dec9df2015-02-15 21:35:19 -0800200def clear_all(nova, name_templ="ceph-test-{0}"):
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800201 deleted_srvs = set()
202 for srv in nova.servers.list():
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800203 if re.match(name_templ.format("\\d+"), srv.name):
koder aka kdanilove21d7472015-02-14 19:02:04 -0800204 logger.debug("Deleting server {0}".format(srv.name))
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800205 nova.servers.delete(srv)
206 deleted_srvs.add(srv.id)
207
208 while deleted_srvs != set():
koder aka kdanilove21d7472015-02-14 19:02:04 -0800209 logger.debug("Waiting till all servers are actually deleted")
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800210 all_id = set(srv.id for srv in nova.servers.list())
211 if all_id.intersection(deleted_srvs) == set():
koder aka kdanilove21d7472015-02-14 19:02:04 -0800212 logger.debug("Done, deleting volumes")
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800213 break
214 time.sleep(1)
215
216 # wait till vm actually deleted
217
218 cinder = c_client(*ostack_get_creds())
219 for vol in cinder.volumes.list():
220 if isinstance(vol.display_name, basestring):
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800221 if re.match(name_templ.format("\\d+"), vol.display_name):
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800222 if vol.status in ('available', 'error'):
koder aka kdanilov2c473092015-03-29 17:12:13 +0300223 logger.debug("Deleting volume " + vol.display_name)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800224 cinder.volumes.delete(vol)
225
koder aka kdanilove21d7472015-02-14 19:02:04 -0800226 logger.debug("Clearing done (yet some volumes may still deleting)")
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800227
228
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800229# def prepare_host(key_file, ip, fio_path, dst_fio_path, user='cirros'):
230# print "Wait till ssh ready...."
231# wait_ssh_ready(ip, user, key_file)
232
233# print "Preparing host >"
234# print " Coping fio"
235# copy_fio(key_file, ip, fio_path, user, dst_fio_path)
236
237# key_opts = '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
238# args = (key_file, user, ip, key_opts)
239# cmd_format = "ssh {3} -i {0} {1}@{2} '{{0}}'".format(*args).format
240
241# def exec_on_host(cmd):
242# print " " + cmd
243# subprocess.check_call(cmd_format(cmd), shell=True)
244
245# exec_on_host("sudo /usr/sbin/mkfs.ext4 /dev/vdb")
246# exec_on_host("sudo /bin/mkdir /media/ceph")
247# exec_on_host("sudo /bin/mount /dev/vdb /media/ceph")
248# exec_on_host("sudo /bin/chmod a+rwx /media/ceph")
249
250
koder aka kdanilove21d7472015-02-14 19:02:04 -0800251# def main():
252# image_name = 'TestVM'
253# flavor_name = 'ceph'
254# vol_sz = 50
255# network_zone_name = 'net04'
256# amount = 10
257# keypair_name = 'ceph-test'
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800258
koder aka kdanilove21d7472015-02-14 19:02:04 -0800259# nova = nova_connect()
260# clear_all(nova)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800261
koder aka kdanilove21d7472015-02-14 19:02:04 -0800262# try:
263# ips = []
264# params = dict(vol_sz=vol_sz)
265# params['image_name'] = image_name
266# params['flavor_name'] = flavor_name
267# params['network_zone_name'] = network_zone_name
268# params['amount'] = amount
269# params['keypair_name'] = keypair_name
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800270
koder aka kdanilove21d7472015-02-14 19:02:04 -0800271# for ip, host in create_vms(nova, **params):
272# ips.append(ip)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800273
koder aka kdanilove21d7472015-02-14 19:02:04 -0800274# print "All setup done! Ips =", " ".join(ips)
275# print "Starting tests"
276# finally:
277# clear_all(nova)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800278
koder aka kdanilove21d7472015-02-14 19:02:04 -0800279# if __name__ == "__main__":
280# exit(main())