blob: a4720cf76041b9e600dcc71a1636e89491d7a980 [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)
172
173 if flt_ip is not None:
koder aka kdanilov3f356262015-02-13 08:06:14 -0800174 # print "attaching ip to server"
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800175 srv.add_floating_ip(flt_ip)
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800176 return (flt_ip.ip, srv)
177 else:
178 return (None, srv)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800179
180
koder aka kdanilov7dec9df2015-02-15 21:35:19 -0800181def clear_all(nova, name_templ="ceph-test-{0}"):
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800182 deleted_srvs = set()
183 for srv in nova.servers.list():
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800184 if re.match(name_templ.format("\\d+"), srv.name):
koder aka kdanilove21d7472015-02-14 19:02:04 -0800185 logger.debug("Deleting server {0}".format(srv.name))
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800186 nova.servers.delete(srv)
187 deleted_srvs.add(srv.id)
188
189 while deleted_srvs != set():
koder aka kdanilove21d7472015-02-14 19:02:04 -0800190 logger.debug("Waiting till all servers are actually deleted")
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800191 all_id = set(srv.id for srv in nova.servers.list())
192 if all_id.intersection(deleted_srvs) == set():
koder aka kdanilove21d7472015-02-14 19:02:04 -0800193 logger.debug("Done, deleting volumes")
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800194 break
195 time.sleep(1)
196
197 # wait till vm actually deleted
198
199 cinder = c_client(*ostack_get_creds())
200 for vol in cinder.volumes.list():
201 if isinstance(vol.display_name, basestring):
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800202 if re.match(name_templ.format("\\d+"), vol.display_name):
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800203 if vol.status in ('available', 'error'):
204 print "Deleting volume", vol.display_name
205 cinder.volumes.delete(vol)
206
koder aka kdanilove21d7472015-02-14 19:02:04 -0800207 logger.debug("Clearing done (yet some volumes may still deleting)")
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800208
209
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800210# def prepare_host(key_file, ip, fio_path, dst_fio_path, user='cirros'):
211# print "Wait till ssh ready...."
212# wait_ssh_ready(ip, user, key_file)
213
214# print "Preparing host >"
215# print " Coping fio"
216# copy_fio(key_file, ip, fio_path, user, dst_fio_path)
217
218# key_opts = '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
219# args = (key_file, user, ip, key_opts)
220# cmd_format = "ssh {3} -i {0} {1}@{2} '{{0}}'".format(*args).format
221
222# def exec_on_host(cmd):
223# print " " + cmd
224# subprocess.check_call(cmd_format(cmd), shell=True)
225
226# exec_on_host("sudo /usr/sbin/mkfs.ext4 /dev/vdb")
227# exec_on_host("sudo /bin/mkdir /media/ceph")
228# exec_on_host("sudo /bin/mount /dev/vdb /media/ceph")
229# exec_on_host("sudo /bin/chmod a+rwx /media/ceph")
230
231
koder aka kdanilove21d7472015-02-14 19:02:04 -0800232# def main():
233# image_name = 'TestVM'
234# flavor_name = 'ceph'
235# vol_sz = 50
236# network_zone_name = 'net04'
237# amount = 10
238# keypair_name = 'ceph-test'
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800239
koder aka kdanilove21d7472015-02-14 19:02:04 -0800240# nova = nova_connect()
241# clear_all(nova)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800242
koder aka kdanilove21d7472015-02-14 19:02:04 -0800243# try:
244# ips = []
245# params = dict(vol_sz=vol_sz)
246# params['image_name'] = image_name
247# params['flavor_name'] = flavor_name
248# params['network_zone_name'] = network_zone_name
249# params['amount'] = amount
250# params['keypair_name'] = keypair_name
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800251
koder aka kdanilove21d7472015-02-14 19:02:04 -0800252# for ip, host in create_vms(nova, **params):
253# ips.append(ip)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800254
koder aka kdanilove21d7472015-02-14 19:02:04 -0800255# print "All setup done! Ips =", " ".join(ips)
256# print "Starting tests"
257# finally:
258# clear_all(nova)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800259
koder aka kdanilove21d7472015-02-14 19:02:04 -0800260# if __name__ == "__main__":
261# exit(main())