blob: 16e7d08ce6af3b762700d57b733996fdaecd88d7 [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
11
koder aka kdanilove21d7472015-02-14 19:02:04 -080012logger = logging.getLogger("io-perf-tool")
13
14
koder aka kdanilov4643fd62015-02-10 16:20:13 -080015def ostack_get_creds():
16 env = os.environ.get
17 name = env('OS_USERNAME')
18 passwd = env('OS_PASSWORD')
19 tenant = env('OS_TENANT_NAME')
20 auth_url = env('OS_AUTH_URL')
21 return name, passwd, tenant, auth_url
22
23
24def nova_connect():
25 return n_client('1.1', *ostack_get_creds())
26
27
28def create_keypair(nova, name, key_path):
29 with open(key_path) as key:
30 return nova.keypairs.create(name, key.read())
31
32
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080033def create_volume(size, name):
koder aka kdanilov4643fd62015-02-10 16:20:13 -080034 cinder = c_client(*ostack_get_creds())
koder aka kdanilov4643fd62015-02-10 16:20:13 -080035 vol = cinder.volumes.create(size=size, display_name=name)
36 err_count = 0
37 while vol.status != 'available':
38 if vol.status == 'error':
39 if err_count == 3:
koder aka kdanilove21d7472015-02-14 19:02:04 -080040 logger.critical("Fail to create volume")
koder aka kdanilov4643fd62015-02-10 16:20:13 -080041 raise RuntimeError("Fail to create volume")
42 else:
43 err_count += 1
44 cinder.volumes.delete(vol)
45 time.sleep(1)
46 vol = cinder.volumes.create(size=size, display_name=name)
47 continue
48 time.sleep(1)
49 vol = cinder.volumes.get(vol.id)
50 return vol
51
52
53def wait_for_server_active(nova, server, timeout=240):
54 t = time.time()
55 while True:
koder aka kdanilov3f356262015-02-13 08:06:14 -080056 time.sleep(1)
koder aka kdanilov4643fd62015-02-10 16:20:13 -080057 sstate = getattr(server, 'OS-EXT-STS:vm_state').lower()
58
59 if sstate == 'active':
60 return True
61
koder aka kdanilov4643fd62015-02-10 16:20:13 -080062 if sstate == 'error':
63 return False
64
65 if time.time() - t > timeout:
66 return False
67
68 server = nova.servers.get(server)
69
70
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080071class Allocate(object):
72 pass
73
74
75def get_floating_ips(nova, pool, amount):
koder aka kdanilov4643fd62015-02-10 16:20:13 -080076 ip_list = nova.floating_ips.list()
77
78 if pool is not None:
79 ip_list = [ip for ip in ip_list if ip.pool == pool]
80
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080081 return [ip for ip in ip_list if ip.instance_id is None][:amount]
koder aka kdanilov4643fd62015-02-10 16:20:13 -080082
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080083
84def create_vms_mt(nova, amount, keypair_name, img_name,
85 flavor_name, vol_sz=None, network_zone_name=None,
koder aka kdanilov7dec9df2015-02-15 21:35:19 -080086 flt_ip_pool=None, name_templ='ceph-test-{0}',
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080087 scheduler_hints=None):
88
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080089 with ThreadPoolExecutor(max_workers=16) as executor:
koder aka kdanilov97644f92015-02-13 11:11:08 -080090 if network_zone_name is not None:
91 network_future = executor.submit(nova.networks.find,
92 label=network_zone_name)
93 else:
94 network_future = None
95
96 fl_future = executor.submit(nova.flavors.find, name=flavor_name)
97 img_future = executor.submit(nova.images.find, name=img_name)
98
99 if flt_ip_pool is not None:
100 ips_future = executor.submit(get_floating_ips,
101 nova, flt_ip_pool, amount)
102 else:
103 ips_future = None
104
105 if ips_future is not None:
koder aka kdanilove21d7472015-02-14 19:02:04 -0800106 logger.debug("Wait for floating ip")
koder aka kdanilov97644f92015-02-13 11:11:08 -0800107 ips = ips_future.result()
108 ips += [Allocate] * (amount - len(ips))
109 else:
110 ips = [None] * amount
111
koder aka kdanilov7dec9df2015-02-15 21:35:19 -0800112 logger.debug("Getting flavor object")
koder aka kdanilov97644f92015-02-13 11:11:08 -0800113 fl = fl_future.result()
koder aka kdanilov7dec9df2015-02-15 21:35:19 -0800114 logger.debug("Getting image object")
koder aka kdanilov97644f92015-02-13 11:11:08 -0800115 img = img_future.result()
116
117 if network_future is not None:
koder aka kdanilove21d7472015-02-14 19:02:04 -0800118 logger.debug("Waiting for network results")
koder aka kdanilov97644f92015-02-13 11:11:08 -0800119 nics = [{'net-id': network_future.result().id}]
120 else:
121 nics = None
122
koder aka kdanilov97644f92015-02-13 11:11:08 -0800123 names = map(name_templ.format, range(amount))
124
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800125 futures = []
koder aka kdanilov6e2ae792015-03-04 18:02:24 -0800126 logger.debug("Requesting new vms")
127
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800128 for name, flt_ip in zip(names, ips):
129 params = (nova, name, keypair_name, img, fl,
130 nics, vol_sz, flt_ip, scheduler_hints,
131 flt_ip_pool)
132
133 futures.append(executor.submit(create_vm, *params))
koder aka kdanilove21d7472015-02-14 19:02:04 -0800134 res = [future.result() for future in futures]
135 logger.debug("Done spawning")
136 return res
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800137
138
139def create_vm(nova, name, keypair_name, img,
140 fl, nics, vol_sz=None,
141 flt_ip=False,
142 scheduler_hints=None,
143 pool=None):
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800144 for i in range(3):
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800145 srv = nova.servers.create(name,
146 flavor=fl, image=img, nics=nics,
147 key_name=keypair_name,
148 scheduler_hints=scheduler_hints)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800149
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800150 if not wait_for_server_active(nova, srv):
151 msg = "Server {0} fails to start. Kill it and try again"
koder aka kdanilove21d7472015-02-14 19:02:04 -0800152 logger.debug(msg.format(srv))
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800153 nova.servers.delete(srv)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800154
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800155 while True:
koder aka kdanilov3f356262015-02-13 08:06:14 -0800156 # print "wait till server deleted"
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800157 all_id = set(alive_srv.id for alive_srv in nova.servers.list())
158 if srv.id not in all_id:
159 break
160 time.sleep(1)
161 else:
162 break
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800163
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800164 if vol_sz is not None:
koder aka kdanilov3f356262015-02-13 08:06:14 -0800165 # print "creating volume"
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800166 vol = create_volume(vol_sz, name)
koder aka kdanilov3f356262015-02-13 08:06:14 -0800167 # print "attach volume to server"
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800168 nova.volumes.create_server_volume(srv.id, vol.id, None)
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800169
170 if flt_ip is Allocate:
171 flt_ip = nova.floating_ips.create(pool)
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800172 if flt_ip is not None:
koder aka kdanilov3f356262015-02-13 08:06:14 -0800173 # print "attaching ip to server"
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800174 srv.add_floating_ip(flt_ip)
Yulia Portnova0e64ea22015-03-20 17:27:22 +0200175
176 return nova.servers.get(srv.id)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800177
178
koder aka kdanilov7dec9df2015-02-15 21:35:19 -0800179def clear_all(nova, name_templ="ceph-test-{0}"):
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800180 deleted_srvs = set()
181 for srv in nova.servers.list():
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800182 if re.match(name_templ.format("\\d+"), srv.name):
koder aka kdanilove21d7472015-02-14 19:02:04 -0800183 logger.debug("Deleting server {0}".format(srv.name))
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800184 nova.servers.delete(srv)
185 deleted_srvs.add(srv.id)
186
187 while deleted_srvs != set():
koder aka kdanilove21d7472015-02-14 19:02:04 -0800188 logger.debug("Waiting till all servers are actually deleted")
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800189 all_id = set(srv.id for srv in nova.servers.list())
190 if all_id.intersection(deleted_srvs) == set():
koder aka kdanilove21d7472015-02-14 19:02:04 -0800191 logger.debug("Done, deleting volumes")
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800192 break
193 time.sleep(1)
194
195 # wait till vm actually deleted
196
197 cinder = c_client(*ostack_get_creds())
198 for vol in cinder.volumes.list():
199 if isinstance(vol.display_name, basestring):
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800200 if re.match(name_templ.format("\\d+"), vol.display_name):
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800201 if vol.status in ('available', 'error'):
koder aka kdanilov2c473092015-03-29 17:12:13 +0300202 logger.debug("Deleting volume " + vol.display_name)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800203 cinder.volumes.delete(vol)
204
koder aka kdanilove21d7472015-02-14 19:02:04 -0800205 logger.debug("Clearing done (yet some volumes may still deleting)")
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800206
207
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800208# def prepare_host(key_file, ip, fio_path, dst_fio_path, user='cirros'):
209# print "Wait till ssh ready...."
210# wait_ssh_ready(ip, user, key_file)
211
212# print "Preparing host >"
213# print " Coping fio"
214# copy_fio(key_file, ip, fio_path, user, dst_fio_path)
215
216# key_opts = '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
217# args = (key_file, user, ip, key_opts)
218# cmd_format = "ssh {3} -i {0} {1}@{2} '{{0}}'".format(*args).format
219
220# def exec_on_host(cmd):
221# print " " + cmd
222# subprocess.check_call(cmd_format(cmd), shell=True)
223
224# exec_on_host("sudo /usr/sbin/mkfs.ext4 /dev/vdb")
225# exec_on_host("sudo /bin/mkdir /media/ceph")
226# exec_on_host("sudo /bin/mount /dev/vdb /media/ceph")
227# exec_on_host("sudo /bin/chmod a+rwx /media/ceph")
228
229
koder aka kdanilove21d7472015-02-14 19:02:04 -0800230# def main():
231# image_name = 'TestVM'
232# flavor_name = 'ceph'
233# vol_sz = 50
234# network_zone_name = 'net04'
235# amount = 10
236# keypair_name = 'ceph-test'
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800237
koder aka kdanilove21d7472015-02-14 19:02:04 -0800238# nova = nova_connect()
239# clear_all(nova)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800240
koder aka kdanilove21d7472015-02-14 19:02:04 -0800241# try:
242# ips = []
243# params = dict(vol_sz=vol_sz)
244# params['image_name'] = image_name
245# params['flavor_name'] = flavor_name
246# params['network_zone_name'] = network_zone_name
247# params['amount'] = amount
248# params['keypair_name'] = keypair_name
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800249
koder aka kdanilove21d7472015-02-14 19:02:04 -0800250# for ip, host in create_vms(nova, **params):
251# ips.append(ip)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800252
koder aka kdanilove21d7472015-02-14 19:02:04 -0800253# print "All setup done! Ips =", " ".join(ips)
254# print "Starting tests"
255# finally:
256# clear_all(nova)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800257
koder aka kdanilove21d7472015-02-14 19:02:04 -0800258# if __name__ == "__main__":
259# exit(main())