blob: de6c005b07049d3c3a79d4ca7fe1984dc11364d2 [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 kdanilove21d7472015-02-14 19:02:04 -0800126 logger.debug("Requesting new vm")
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800127 for name, flt_ip in zip(names, ips):
128 params = (nova, name, keypair_name, img, fl,
129 nics, vol_sz, flt_ip, scheduler_hints,
130 flt_ip_pool)
131
132 futures.append(executor.submit(create_vm, *params))
koder aka kdanilove21d7472015-02-14 19:02:04 -0800133 res = [future.result() for future in futures]
134 logger.debug("Done spawning")
135 return res
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800136
137
138def create_vm(nova, name, keypair_name, img,
139 fl, nics, vol_sz=None,
140 flt_ip=False,
141 scheduler_hints=None,
142 pool=None):
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800143 for i in range(3):
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800144 srv = nova.servers.create(name,
145 flavor=fl, image=img, nics=nics,
146 key_name=keypair_name,
147 scheduler_hints=scheduler_hints)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800148
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800149 if not wait_for_server_active(nova, srv):
150 msg = "Server {0} fails to start. Kill it and try again"
koder aka kdanilove21d7472015-02-14 19:02:04 -0800151 logger.debug(msg.format(srv))
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800152 nova.servers.delete(srv)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800153
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800154 while True:
koder aka kdanilov3f356262015-02-13 08:06:14 -0800155 # print "wait till server deleted"
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800156 all_id = set(alive_srv.id for alive_srv in nova.servers.list())
157 if srv.id not in all_id:
158 break
159 time.sleep(1)
160 else:
161 break
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800162
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800163 if vol_sz is not None:
koder aka kdanilov3f356262015-02-13 08:06:14 -0800164 # print "creating volume"
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800165 vol = create_volume(vol_sz, name)
koder aka kdanilov3f356262015-02-13 08:06:14 -0800166 # print "attach volume to server"
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800167 nova.volumes.create_server_volume(srv.id, vol.id, None)
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800168
169 if flt_ip is Allocate:
170 flt_ip = nova.floating_ips.create(pool)
171
172 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)
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800175 return (flt_ip.ip, srv)
176 else:
177 return (None, srv)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800178
179
koder aka kdanilov7dec9df2015-02-15 21:35:19 -0800180def clear_all(nova, name_templ="ceph-test-{0}"):
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800181 deleted_srvs = set()
182 for srv in nova.servers.list():
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800183 if re.match(name_templ.format("\\d+"), srv.name):
koder aka kdanilove21d7472015-02-14 19:02:04 -0800184 logger.debug("Deleting server {0}".format(srv.name))
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800185 nova.servers.delete(srv)
186 deleted_srvs.add(srv.id)
187
188 while deleted_srvs != set():
koder aka kdanilove21d7472015-02-14 19:02:04 -0800189 logger.debug("Waiting till all servers are actually deleted")
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800190 all_id = set(srv.id for srv in nova.servers.list())
191 if all_id.intersection(deleted_srvs) == set():
koder aka kdanilove21d7472015-02-14 19:02:04 -0800192 logger.debug("Done, deleting volumes")
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800193 break
194 time.sleep(1)
195
196 # wait till vm actually deleted
197
198 cinder = c_client(*ostack_get_creds())
199 for vol in cinder.volumes.list():
200 if isinstance(vol.display_name, basestring):
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800201 if re.match(name_templ.format("\\d+"), vol.display_name):
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800202 if vol.status in ('available', 'error'):
203 print "Deleting volume", vol.display_name
204 cinder.volumes.delete(vol)
205
koder aka kdanilove21d7472015-02-14 19:02:04 -0800206 logger.debug("Clearing done (yet some volumes may still deleting)")
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800207
208
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800209# def prepare_host(key_file, ip, fio_path, dst_fio_path, user='cirros'):
210# print "Wait till ssh ready...."
211# wait_ssh_ready(ip, user, key_file)
212
213# print "Preparing host >"
214# print " Coping fio"
215# copy_fio(key_file, ip, fio_path, user, dst_fio_path)
216
217# key_opts = '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
218# args = (key_file, user, ip, key_opts)
219# cmd_format = "ssh {3} -i {0} {1}@{2} '{{0}}'".format(*args).format
220
221# def exec_on_host(cmd):
222# print " " + cmd
223# subprocess.check_call(cmd_format(cmd), shell=True)
224
225# exec_on_host("sudo /usr/sbin/mkfs.ext4 /dev/vdb")
226# exec_on_host("sudo /bin/mkdir /media/ceph")
227# exec_on_host("sudo /bin/mount /dev/vdb /media/ceph")
228# exec_on_host("sudo /bin/chmod a+rwx /media/ceph")
229
230
koder aka kdanilove21d7472015-02-14 19:02:04 -0800231# def main():
232# image_name = 'TestVM'
233# flavor_name = 'ceph'
234# vol_sz = 50
235# network_zone_name = 'net04'
236# amount = 10
237# keypair_name = 'ceph-test'
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800238
koder aka kdanilove21d7472015-02-14 19:02:04 -0800239# nova = nova_connect()
240# clear_all(nova)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800241
koder aka kdanilove21d7472015-02-14 19:02:04 -0800242# try:
243# ips = []
244# params = dict(vol_sz=vol_sz)
245# params['image_name'] = image_name
246# params['flavor_name'] = flavor_name
247# params['network_zone_name'] = network_zone_name
248# params['amount'] = amount
249# params['keypair_name'] = keypair_name
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800250
koder aka kdanilove21d7472015-02-14 19:02:04 -0800251# for ip, host in create_vms(nova, **params):
252# ips.append(ip)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800253
koder aka kdanilove21d7472015-02-14 19:02:04 -0800254# print "All setup done! Ips =", " ".join(ips)
255# print "Starting tests"
256# finally:
257# clear_all(nova)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800258
koder aka kdanilove21d7472015-02-14 19:02:04 -0800259# if __name__ == "__main__":
260# exit(main())