blob: db2566528e4e6bf4d8437c985c93f12323d7151d [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
koder aka kdanilov1c2b5112015-04-10 16:53:51 +030012
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')
gstepanov023c1e42015-04-08 15:50:19 +030023
koder aka kdanilov4643fd62015-02-10 16:20:13 -080024 return name, passwd, tenant, auth_url
25
26
koder aka kdanilov1c2b5112015-04-10 16:53:51 +030027NOVA_CONNECTION = None
28
29
30def nova_connect(name=None, passwd=None, tenant=None, auth_url=None):
31 global NOVA_CONNECTION
32 if NOVA_CONNECTION is None:
33 if name is None:
34 name, passwd, tenant, auth_url = ostack_get_creds()
35 NOVA_CONNECTION = n_client('1.1', name, passwd, tenant, auth_url)
36 return NOVA_CONNECTION
37
38
39def nova_disconnect():
40 global NOVA_CONNECTION
41 if NOVA_CONNECTION is not None:
42 NOVA_CONNECTION.close()
43 NOVA_CONNECTION = None
koder aka kdanilov4643fd62015-02-10 16:20:13 -080044
45
46def create_keypair(nova, name, key_path):
47 with open(key_path) as key:
48 return nova.keypairs.create(name, key.read())
49
50
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080051def create_volume(size, name):
koder aka kdanilov4643fd62015-02-10 16:20:13 -080052 cinder = c_client(*ostack_get_creds())
koder aka kdanilov4643fd62015-02-10 16:20:13 -080053 vol = cinder.volumes.create(size=size, display_name=name)
54 err_count = 0
55 while vol.status != 'available':
56 if vol.status == 'error':
57 if err_count == 3:
koder aka kdanilove21d7472015-02-14 19:02:04 -080058 logger.critical("Fail to create volume")
koder aka kdanilov4643fd62015-02-10 16:20:13 -080059 raise RuntimeError("Fail to create volume")
60 else:
61 err_count += 1
62 cinder.volumes.delete(vol)
63 time.sleep(1)
64 vol = cinder.volumes.create(size=size, display_name=name)
65 continue
66 time.sleep(1)
67 vol = cinder.volumes.get(vol.id)
68 return vol
69
70
71def wait_for_server_active(nova, server, timeout=240):
72 t = time.time()
73 while True:
koder aka kdanilov3f356262015-02-13 08:06:14 -080074 time.sleep(1)
koder aka kdanilov4643fd62015-02-10 16:20:13 -080075 sstate = getattr(server, 'OS-EXT-STS:vm_state').lower()
76
77 if sstate == 'active':
78 return True
79
koder aka kdanilov4643fd62015-02-10 16:20:13 -080080 if sstate == 'error':
81 return False
82
83 if time.time() - t > timeout:
84 return False
85
86 server = nova.servers.get(server)
87
88
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080089class Allocate(object):
90 pass
91
92
93def get_floating_ips(nova, pool, amount):
koder aka kdanilov4643fd62015-02-10 16:20:13 -080094 ip_list = nova.floating_ips.list()
95
96 if pool is not None:
97 ip_list = [ip for ip in ip_list if ip.pool == pool]
98
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080099 return [ip for ip in ip_list if ip.instance_id is None][:amount]
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800100
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800101
koder aka kdanilovda45e882015-04-06 02:24:42 +0300102def launch_vms(config):
koder aka kdanilovda45e882015-04-06 02:24:42 +0300103 logger.debug("Starting new nodes on openstack")
koder aka kdanilovda45e882015-04-06 02:24:42 +0300104 params = config['vm_params'].copy()
105 count = params.pop('count')
106
107 if isinstance(count, basestring):
108 assert count.startswith("x")
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300109 lst = NOVA_CONNECTION.services.list(binary='nova-compute')
koder aka kdanilovda45e882015-04-06 02:24:42 +0300110 srv_count = len([srv for srv in lst if srv.status == 'enabled'])
111 count = srv_count * int(count[1:])
112
koder aka kdanilov66839a92015-04-11 13:22:31 +0300113 msg_templ = "Will start {0} servers with next params: {1}"
114 logger.debug(msg_templ.format(count, ""))
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300115 # vm_creds = config['vm_params']['creds'] ?????
116 vm_creds = params.pop('creds')
koder aka kdanilovda45e882015-04-06 02:24:42 +0300117
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300118 for ip, os_node in create_vms_mt(NOVA_CONNECTION, count, **params):
119 yield Node(vm_creds.format(ip), []), os_node.id
koder aka kdanilovda45e882015-04-06 02:24:42 +0300120
121
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800122def create_vms_mt(nova, amount, keypair_name, img_name,
123 flavor_name, vol_sz=None, network_zone_name=None,
koder aka kdanilov7dec9df2015-02-15 21:35:19 -0800124 flt_ip_pool=None, name_templ='ceph-test-{0}',
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800125 scheduler_hints=None):
126
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800127 with ThreadPoolExecutor(max_workers=16) as executor:
koder aka kdanilov97644f92015-02-13 11:11:08 -0800128 if network_zone_name is not None:
129 network_future = executor.submit(nova.networks.find,
130 label=network_zone_name)
131 else:
132 network_future = None
133
134 fl_future = executor.submit(nova.flavors.find, name=flavor_name)
135 img_future = executor.submit(nova.images.find, name=img_name)
136
137 if flt_ip_pool is not None:
138 ips_future = executor.submit(get_floating_ips,
139 nova, flt_ip_pool, amount)
koder aka kdanilove21d7472015-02-14 19:02:04 -0800140 logger.debug("Wait for floating ip")
koder aka kdanilov97644f92015-02-13 11:11:08 -0800141 ips = ips_future.result()
142 ips += [Allocate] * (amount - len(ips))
143 else:
144 ips = [None] * amount
145
koder aka kdanilov7dec9df2015-02-15 21:35:19 -0800146 logger.debug("Getting flavor object")
koder aka kdanilov97644f92015-02-13 11:11:08 -0800147 fl = fl_future.result()
koder aka kdanilov7dec9df2015-02-15 21:35:19 -0800148 logger.debug("Getting image object")
koder aka kdanilov97644f92015-02-13 11:11:08 -0800149 img = img_future.result()
150
151 if network_future is not None:
koder aka kdanilove21d7472015-02-14 19:02:04 -0800152 logger.debug("Waiting for network results")
koder aka kdanilov97644f92015-02-13 11:11:08 -0800153 nics = [{'net-id': network_future.result().id}]
154 else:
155 nics = None
156
koder aka kdanilov97644f92015-02-13 11:11:08 -0800157 names = map(name_templ.format, range(amount))
158
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800159 futures = []
koder aka kdanilov6e2ae792015-03-04 18:02:24 -0800160 logger.debug("Requesting new vms")
161
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800162 for name, flt_ip in zip(names, ips):
163 params = (nova, name, keypair_name, img, fl,
164 nics, vol_sz, flt_ip, scheduler_hints,
165 flt_ip_pool)
166
167 futures.append(executor.submit(create_vm, *params))
koder aka kdanilove21d7472015-02-14 19:02:04 -0800168 res = [future.result() for future in futures]
169 logger.debug("Done spawning")
170 return res
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800171
172
173def create_vm(nova, name, keypair_name, img,
174 fl, nics, vol_sz=None,
175 flt_ip=False,
176 scheduler_hints=None,
177 pool=None):
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800178 for i in range(3):
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800179 srv = nova.servers.create(name,
180 flavor=fl, image=img, nics=nics,
181 key_name=keypair_name,
182 scheduler_hints=scheduler_hints)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800183
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800184 if not wait_for_server_active(nova, srv):
185 msg = "Server {0} fails to start. Kill it and try again"
koder aka kdanilove21d7472015-02-14 19:02:04 -0800186 logger.debug(msg.format(srv))
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800187 nova.servers.delete(srv)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800188
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800189 while True:
koder aka kdanilov3f356262015-02-13 08:06:14 -0800190 # print "wait till server deleted"
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800191 all_id = set(alive_srv.id for alive_srv in nova.servers.list())
192 if srv.id not in all_id:
193 break
194 time.sleep(1)
195 else:
196 break
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800197
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800198 if vol_sz is not None:
koder aka kdanilov3f356262015-02-13 08:06:14 -0800199 # print "creating volume"
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800200 vol = create_volume(vol_sz, name)
koder aka kdanilov3f356262015-02-13 08:06:14 -0800201 # print "attach volume to server"
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800202 nova.volumes.create_server_volume(srv.id, vol.id, None)
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800203
204 if flt_ip is Allocate:
205 flt_ip = nova.floating_ips.create(pool)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300206
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800207 if flt_ip is not None:
koder aka kdanilov3f356262015-02-13 08:06:14 -0800208 # print "attaching ip to server"
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800209 srv.add_floating_ip(flt_ip)
Yulia Portnova0e64ea22015-03-20 17:27:22 +0200210
koder aka kdanilovda45e882015-04-06 02:24:42 +0300211 return flt_ip.ip, nova.servers.get(srv.id)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800212
213
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300214def clear_nodes(nodes_ids):
215 clear_all(NOVA_CONNECTION, nodes_ids, None)
gstepanov023c1e42015-04-08 15:50:19 +0300216
217
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300218def clear_all(nova, ids=None, name_templ="ceph-test-{0}"):
219
220 def need_delete(srv):
221 if name_templ is not None:
222 return re.match(name_templ.format("\\d+"), srv.name) is not None
223 else:
224 return srv.id in ids
225
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800226 deleted_srvs = set()
227 for srv in nova.servers.list():
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300228 if need_delete(srv):
koder aka kdanilove21d7472015-02-14 19:02:04 -0800229 logger.debug("Deleting server {0}".format(srv.name))
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800230 nova.servers.delete(srv)
231 deleted_srvs.add(srv.id)
232
233 while deleted_srvs != set():
koder aka kdanilove21d7472015-02-14 19:02:04 -0800234 logger.debug("Waiting till all servers are actually deleted")
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800235 all_id = set(srv.id for srv in nova.servers.list())
236 if all_id.intersection(deleted_srvs) == set():
koder aka kdanilove21d7472015-02-14 19:02:04 -0800237 logger.debug("Done, deleting volumes")
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800238 break
239 time.sleep(1)
240
241 # wait till vm actually deleted
242
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300243 if name_templ is not None:
244 cinder = c_client(*ostack_get_creds())
245 for vol in cinder.volumes.list():
246 if isinstance(vol.display_name, basestring):
247 if re.match(name_templ.format("\\d+"), vol.display_name):
248 if vol.status in ('available', 'error'):
249 logger.debug("Deleting volume " + vol.display_name)
250 cinder.volumes.delete(vol)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800251
koder aka kdanilove21d7472015-02-14 19:02:04 -0800252 logger.debug("Clearing done (yet some volumes may still deleting)")
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800253
254
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800255# def prepare_host(key_file, ip, fio_path, dst_fio_path, user='cirros'):
256# print "Wait till ssh ready...."
257# wait_ssh_ready(ip, user, key_file)
258
259# print "Preparing host >"
260# print " Coping fio"
261# copy_fio(key_file, ip, fio_path, user, dst_fio_path)
262
263# key_opts = '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
264# args = (key_file, user, ip, key_opts)
265# cmd_format = "ssh {3} -i {0} {1}@{2} '{{0}}'".format(*args).format
266
267# def exec_on_host(cmd):
268# print " " + cmd
269# subprocess.check_call(cmd_format(cmd), shell=True)
270
271# exec_on_host("sudo /usr/sbin/mkfs.ext4 /dev/vdb")
272# exec_on_host("sudo /bin/mkdir /media/ceph")
273# exec_on_host("sudo /bin/mount /dev/vdb /media/ceph")
274# exec_on_host("sudo /bin/chmod a+rwx /media/ceph")
275
276
koder aka kdanilove21d7472015-02-14 19:02:04 -0800277# def main():
278# image_name = 'TestVM'
279# flavor_name = 'ceph'
280# vol_sz = 50
281# network_zone_name = 'net04'
282# amount = 10
283# keypair_name = 'ceph-test'
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800284
koder aka kdanilove21d7472015-02-14 19:02:04 -0800285# nova = nova_connect()
286# clear_all(nova)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800287
koder aka kdanilove21d7472015-02-14 19:02:04 -0800288# try:
289# ips = []
290# params = dict(vol_sz=vol_sz)
291# params['image_name'] = image_name
292# params['flavor_name'] = flavor_name
293# params['network_zone_name'] = network_zone_name
294# params['amount'] = amount
295# params['keypair_name'] = keypair_name
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800296
koder aka kdanilove21d7472015-02-14 19:02:04 -0800297# for ip, host in create_vms(nova, **params):
298# ips.append(ip)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800299
koder aka kdanilove21d7472015-02-14 19:02:04 -0800300# print "All setup done! Ips =", " ".join(ips)
301# print "Starting tests"
302# finally:
303# clear_all(nova)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800304
koder aka kdanilove21d7472015-02-14 19:02:04 -0800305# if __name__ == "__main__":
306# exit(main())