blob: f7cb09fac2883b112e846cd702b9d77ee4d1bb59 [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
gstepanov023c1e42015-04-08 15:50:19 +030013from utils import parse_creds
koder aka kdanilov4643fd62015-02-10 16:20:13 -080014
koder aka kdanilove21d7472015-02-14 19:02:04 -080015logger = logging.getLogger("io-perf-tool")
16
17
koder aka kdanilov4643fd62015-02-10 16:20:13 -080018def ostack_get_creds():
19 env = os.environ.get
20 name = env('OS_USERNAME')
21 passwd = env('OS_PASSWORD')
22 tenant = env('OS_TENANT_NAME')
23 auth_url = env('OS_AUTH_URL')
gstepanov023c1e42015-04-08 15:50:19 +030024
koder aka kdanilov4643fd62015-02-10 16:20:13 -080025 return name, passwd, tenant, auth_url
26
27
28def nova_connect():
gstepanov023c1e42015-04-08 15:50:19 +030029 return n_client('1.1', *ostack_get_creds()
30 )
koder aka kdanilov4643fd62015-02-10 16:20:13 -080031
32
33def create_keypair(nova, name, key_path):
34 with open(key_path) as key:
35 return nova.keypairs.create(name, key.read())
36
37
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080038def create_volume(size, name):
koder aka kdanilov4643fd62015-02-10 16:20:13 -080039 cinder = c_client(*ostack_get_creds())
koder aka kdanilov4643fd62015-02-10 16:20:13 -080040 vol = cinder.volumes.create(size=size, display_name=name)
41 err_count = 0
42 while vol.status != 'available':
43 if vol.status == 'error':
44 if err_count == 3:
koder aka kdanilove21d7472015-02-14 19:02:04 -080045 logger.critical("Fail to create volume")
koder aka kdanilov4643fd62015-02-10 16:20:13 -080046 raise RuntimeError("Fail to create volume")
47 else:
48 err_count += 1
49 cinder.volumes.delete(vol)
50 time.sleep(1)
51 vol = cinder.volumes.create(size=size, display_name=name)
52 continue
53 time.sleep(1)
54 vol = cinder.volumes.get(vol.id)
55 return vol
56
57
58def wait_for_server_active(nova, server, timeout=240):
59 t = time.time()
60 while True:
koder aka kdanilov3f356262015-02-13 08:06:14 -080061 time.sleep(1)
koder aka kdanilov4643fd62015-02-10 16:20:13 -080062 sstate = getattr(server, 'OS-EXT-STS:vm_state').lower()
63
64 if sstate == 'active':
65 return True
66
koder aka kdanilov4643fd62015-02-10 16:20:13 -080067 if sstate == 'error':
68 return False
69
70 if time.time() - t > timeout:
71 return False
72
73 server = nova.servers.get(server)
74
75
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080076class Allocate(object):
77 pass
78
79
80def get_floating_ips(nova, pool, amount):
koder aka kdanilov4643fd62015-02-10 16:20:13 -080081 ip_list = nova.floating_ips.list()
82
83 if pool is not None:
84 ip_list = [ip for ip in ip_list if ip.pool == pool]
85
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080086 return [ip for ip in ip_list if ip.instance_id is None][:amount]
koder aka kdanilov4643fd62015-02-10 16:20:13 -080087
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080088
koder aka kdanilovda45e882015-04-06 02:24:42 +030089def launch_vms(config):
gstepanov023c1e42015-04-08 15:50:19 +030090 creds = config['vm_params']['creds']
91 # if creds != 'ENV':
92 # raise ValueError("Only 'ENV' creds are supported")
koder aka kdanilovda45e882015-04-06 02:24:42 +030093
94 logger.debug("Starting new nodes on openstack")
95 conn = nova_connect()
96 params = config['vm_params'].copy()
97 count = params.pop('count')
98
99 if isinstance(count, basestring):
100 assert count.startswith("x")
101 lst = conn.services.list(binary='nova-compute')
102 srv_count = len([srv for srv in lst if srv.status == 'enabled'])
103 count = srv_count * int(count[1:])
104
105 creds = params.pop('creds')
106
107 for ip, _ in create_vms_mt(conn, count, **params):
108 yield Node(creds.format(ip), [])
109
110
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800111def create_vms_mt(nova, amount, keypair_name, img_name,
112 flavor_name, vol_sz=None, network_zone_name=None,
koder aka kdanilov7dec9df2015-02-15 21:35:19 -0800113 flt_ip_pool=None, name_templ='ceph-test-{0}',
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800114 scheduler_hints=None):
115
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800116 with ThreadPoolExecutor(max_workers=16) as executor:
koder aka kdanilov97644f92015-02-13 11:11:08 -0800117 if network_zone_name is not None:
118 network_future = executor.submit(nova.networks.find,
119 label=network_zone_name)
120 else:
121 network_future = None
122
123 fl_future = executor.submit(nova.flavors.find, name=flavor_name)
124 img_future = executor.submit(nova.images.find, name=img_name)
125
126 if flt_ip_pool is not None:
127 ips_future = executor.submit(get_floating_ips,
128 nova, flt_ip_pool, amount)
koder aka kdanilove21d7472015-02-14 19:02:04 -0800129 logger.debug("Wait for floating ip")
koder aka kdanilov97644f92015-02-13 11:11:08 -0800130 ips = ips_future.result()
131 ips += [Allocate] * (amount - len(ips))
132 else:
133 ips = [None] * amount
134
koder aka kdanilov7dec9df2015-02-15 21:35:19 -0800135 logger.debug("Getting flavor object")
koder aka kdanilov97644f92015-02-13 11:11:08 -0800136 fl = fl_future.result()
koder aka kdanilov7dec9df2015-02-15 21:35:19 -0800137 logger.debug("Getting image object")
koder aka kdanilov97644f92015-02-13 11:11:08 -0800138 img = img_future.result()
139
140 if network_future is not None:
koder aka kdanilove21d7472015-02-14 19:02:04 -0800141 logger.debug("Waiting for network results")
koder aka kdanilov97644f92015-02-13 11:11:08 -0800142 nics = [{'net-id': network_future.result().id}]
143 else:
144 nics = None
145
koder aka kdanilov97644f92015-02-13 11:11:08 -0800146 names = map(name_templ.format, range(amount))
147
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800148 futures = []
koder aka kdanilov6e2ae792015-03-04 18:02:24 -0800149 logger.debug("Requesting new vms")
150
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800151 for name, flt_ip in zip(names, ips):
152 params = (nova, name, keypair_name, img, fl,
153 nics, vol_sz, flt_ip, scheduler_hints,
154 flt_ip_pool)
155
156 futures.append(executor.submit(create_vm, *params))
koder aka kdanilove21d7472015-02-14 19:02:04 -0800157 res = [future.result() for future in futures]
158 logger.debug("Done spawning")
159 return res
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800160
161
162def create_vm(nova, name, keypair_name, img,
163 fl, nics, vol_sz=None,
164 flt_ip=False,
165 scheduler_hints=None,
166 pool=None):
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800167 for i in range(3):
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800168 srv = nova.servers.create(name,
169 flavor=fl, image=img, nics=nics,
170 key_name=keypair_name,
171 scheduler_hints=scheduler_hints)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800172
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800173 if not wait_for_server_active(nova, srv):
174 msg = "Server {0} fails to start. Kill it and try again"
koder aka kdanilove21d7472015-02-14 19:02:04 -0800175 logger.debug(msg.format(srv))
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800176 nova.servers.delete(srv)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800177
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800178 while True:
koder aka kdanilov3f356262015-02-13 08:06:14 -0800179 # print "wait till server deleted"
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800180 all_id = set(alive_srv.id for alive_srv in nova.servers.list())
181 if srv.id not in all_id:
182 break
183 time.sleep(1)
184 else:
185 break
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800186
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800187 if vol_sz is not None:
koder aka kdanilov3f356262015-02-13 08:06:14 -0800188 # print "creating volume"
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800189 vol = create_volume(vol_sz, name)
koder aka kdanilov3f356262015-02-13 08:06:14 -0800190 # print "attach volume to server"
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800191 nova.volumes.create_server_volume(srv.id, vol.id, None)
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800192
193 if flt_ip is Allocate:
194 flt_ip = nova.floating_ips.create(pool)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300195
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800196 if flt_ip is not None:
koder aka kdanilov3f356262015-02-13 08:06:14 -0800197 # print "attaching ip to server"
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800198 srv.add_floating_ip(flt_ip)
Yulia Portnova0e64ea22015-03-20 17:27:22 +0200199
koder aka kdanilovda45e882015-04-06 02:24:42 +0300200 return flt_ip.ip, nova.servers.get(srv.id)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800201
202
gstepanov023c1e42015-04-08 15:50:19 +0300203def clear_nodes():
204 nova = nova_connect()
205 clear_all(nova)
206
207
koder aka kdanilov7dec9df2015-02-15 21:35:19 -0800208def clear_all(nova, name_templ="ceph-test-{0}"):
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800209 deleted_srvs = set()
210 for srv in nova.servers.list():
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800211 if re.match(name_templ.format("\\d+"), srv.name):
koder aka kdanilove21d7472015-02-14 19:02:04 -0800212 logger.debug("Deleting server {0}".format(srv.name))
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800213 nova.servers.delete(srv)
214 deleted_srvs.add(srv.id)
215
216 while deleted_srvs != set():
koder aka kdanilove21d7472015-02-14 19:02:04 -0800217 logger.debug("Waiting till all servers are actually deleted")
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800218 all_id = set(srv.id for srv in nova.servers.list())
219 if all_id.intersection(deleted_srvs) == set():
koder aka kdanilove21d7472015-02-14 19:02:04 -0800220 logger.debug("Done, deleting volumes")
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800221 break
222 time.sleep(1)
223
224 # wait till vm actually deleted
225
226 cinder = c_client(*ostack_get_creds())
227 for vol in cinder.volumes.list():
228 if isinstance(vol.display_name, basestring):
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800229 if re.match(name_templ.format("\\d+"), vol.display_name):
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800230 if vol.status in ('available', 'error'):
koder aka kdanilov2c473092015-03-29 17:12:13 +0300231 logger.debug("Deleting volume " + vol.display_name)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800232 cinder.volumes.delete(vol)
233
koder aka kdanilove21d7472015-02-14 19:02:04 -0800234 logger.debug("Clearing done (yet some volumes may still deleting)")
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800235
236
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800237# def prepare_host(key_file, ip, fio_path, dst_fio_path, user='cirros'):
238# print "Wait till ssh ready...."
239# wait_ssh_ready(ip, user, key_file)
240
241# print "Preparing host >"
242# print " Coping fio"
243# copy_fio(key_file, ip, fio_path, user, dst_fio_path)
244
245# key_opts = '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
246# args = (key_file, user, ip, key_opts)
247# cmd_format = "ssh {3} -i {0} {1}@{2} '{{0}}'".format(*args).format
248
249# def exec_on_host(cmd):
250# print " " + cmd
251# subprocess.check_call(cmd_format(cmd), shell=True)
252
253# exec_on_host("sudo /usr/sbin/mkfs.ext4 /dev/vdb")
254# exec_on_host("sudo /bin/mkdir /media/ceph")
255# exec_on_host("sudo /bin/mount /dev/vdb /media/ceph")
256# exec_on_host("sudo /bin/chmod a+rwx /media/ceph")
257
258
koder aka kdanilove21d7472015-02-14 19:02:04 -0800259# def main():
260# image_name = 'TestVM'
261# flavor_name = 'ceph'
262# vol_sz = 50
263# network_zone_name = 'net04'
264# amount = 10
265# keypair_name = 'ceph-test'
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800266
koder aka kdanilove21d7472015-02-14 19:02:04 -0800267# nova = nova_connect()
268# clear_all(nova)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800269
koder aka kdanilove21d7472015-02-14 19:02:04 -0800270# try:
271# ips = []
272# params = dict(vol_sz=vol_sz)
273# params['image_name'] = image_name
274# params['flavor_name'] = flavor_name
275# params['network_zone_name'] = network_zone_name
276# params['amount'] = amount
277# params['keypair_name'] = keypair_name
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800278
koder aka kdanilove21d7472015-02-14 19:02:04 -0800279# for ip, host in create_vms(nova, **params):
280# ips.append(ip)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800281
koder aka kdanilove21d7472015-02-14 19:02:04 -0800282# print "All setup done! Ips =", " ".join(ips)
283# print "Starting tests"
284# finally:
285# clear_all(nova)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800286
koder aka kdanilove21d7472015-02-14 19:02:04 -0800287# if __name__ == "__main__":
288# exit(main())