blob: 7317fd0c4e6b73d2993ba1a77386edb16dd0dc3b [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']
koder aka kdanilov6c491062015-04-09 22:33:13 +030091
gstepanov023c1e42015-04-08 15:50:19 +030092 # if creds != 'ENV':
93 # raise ValueError("Only 'ENV' creds are supported")
koder aka kdanilovda45e882015-04-06 02:24:42 +030094
95 logger.debug("Starting new nodes on openstack")
96 conn = nova_connect()
97 params = config['vm_params'].copy()
98 count = params.pop('count')
99
100 if isinstance(count, basestring):
101 assert count.startswith("x")
102 lst = conn.services.list(binary='nova-compute')
103 srv_count = len([srv for srv in lst if srv.status == 'enabled'])
104 count = srv_count * int(count[1:])
105
106 creds = params.pop('creds')
107
108 for ip, _ in create_vms_mt(conn, count, **params):
109 yield Node(creds.format(ip), [])
110
111
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800112def create_vms_mt(nova, amount, keypair_name, img_name,
113 flavor_name, vol_sz=None, network_zone_name=None,
koder aka kdanilov7dec9df2015-02-15 21:35:19 -0800114 flt_ip_pool=None, name_templ='ceph-test-{0}',
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800115 scheduler_hints=None):
116
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800117 with ThreadPoolExecutor(max_workers=16) as executor:
koder aka kdanilov97644f92015-02-13 11:11:08 -0800118 if network_zone_name is not None:
119 network_future = executor.submit(nova.networks.find,
120 label=network_zone_name)
121 else:
122 network_future = None
123
124 fl_future = executor.submit(nova.flavors.find, name=flavor_name)
125 img_future = executor.submit(nova.images.find, name=img_name)
126
127 if flt_ip_pool is not None:
128 ips_future = executor.submit(get_floating_ips,
129 nova, flt_ip_pool, amount)
koder aka kdanilove21d7472015-02-14 19:02:04 -0800130 logger.debug("Wait for floating ip")
koder aka kdanilov97644f92015-02-13 11:11:08 -0800131 ips = ips_future.result()
132 ips += [Allocate] * (amount - len(ips))
133 else:
134 ips = [None] * amount
135
koder aka kdanilov7dec9df2015-02-15 21:35:19 -0800136 logger.debug("Getting flavor object")
koder aka kdanilov97644f92015-02-13 11:11:08 -0800137 fl = fl_future.result()
koder aka kdanilov7dec9df2015-02-15 21:35:19 -0800138 logger.debug("Getting image object")
koder aka kdanilov97644f92015-02-13 11:11:08 -0800139 img = img_future.result()
140
141 if network_future is not None:
koder aka kdanilove21d7472015-02-14 19:02:04 -0800142 logger.debug("Waiting for network results")
koder aka kdanilov97644f92015-02-13 11:11:08 -0800143 nics = [{'net-id': network_future.result().id}]
144 else:
145 nics = None
146
koder aka kdanilov97644f92015-02-13 11:11:08 -0800147 names = map(name_templ.format, range(amount))
148
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800149 futures = []
koder aka kdanilov6e2ae792015-03-04 18:02:24 -0800150 logger.debug("Requesting new vms")
151
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800152 for name, flt_ip in zip(names, ips):
153 params = (nova, name, keypair_name, img, fl,
154 nics, vol_sz, flt_ip, scheduler_hints,
155 flt_ip_pool)
156
157 futures.append(executor.submit(create_vm, *params))
koder aka kdanilove21d7472015-02-14 19:02:04 -0800158 res = [future.result() for future in futures]
159 logger.debug("Done spawning")
160 return res
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800161
162
163def create_vm(nova, name, keypair_name, img,
164 fl, nics, vol_sz=None,
165 flt_ip=False,
166 scheduler_hints=None,
167 pool=None):
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800168 for i in range(3):
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800169 srv = nova.servers.create(name,
170 flavor=fl, image=img, nics=nics,
171 key_name=keypair_name,
172 scheduler_hints=scheduler_hints)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800173
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800174 if not wait_for_server_active(nova, srv):
175 msg = "Server {0} fails to start. Kill it and try again"
koder aka kdanilove21d7472015-02-14 19:02:04 -0800176 logger.debug(msg.format(srv))
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800177 nova.servers.delete(srv)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800178
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800179 while True:
koder aka kdanilov3f356262015-02-13 08:06:14 -0800180 # print "wait till server deleted"
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800181 all_id = set(alive_srv.id for alive_srv in nova.servers.list())
182 if srv.id not in all_id:
183 break
184 time.sleep(1)
185 else:
186 break
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800187
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800188 if vol_sz is not None:
koder aka kdanilov3f356262015-02-13 08:06:14 -0800189 # print "creating volume"
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800190 vol = create_volume(vol_sz, name)
koder aka kdanilov3f356262015-02-13 08:06:14 -0800191 # print "attach volume to server"
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800192 nova.volumes.create_server_volume(srv.id, vol.id, None)
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800193
194 if flt_ip is Allocate:
195 flt_ip = nova.floating_ips.create(pool)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300196
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800197 if flt_ip is not None:
koder aka kdanilov3f356262015-02-13 08:06:14 -0800198 # print "attaching ip to server"
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800199 srv.add_floating_ip(flt_ip)
Yulia Portnova0e64ea22015-03-20 17:27:22 +0200200
koder aka kdanilovda45e882015-04-06 02:24:42 +0300201 return flt_ip.ip, nova.servers.get(srv.id)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800202
203
gstepanov023c1e42015-04-08 15:50:19 +0300204def clear_nodes():
205 nova = nova_connect()
206 clear_all(nova)
207
208
koder aka kdanilov7dec9df2015-02-15 21:35:19 -0800209def clear_all(nova, name_templ="ceph-test-{0}"):
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800210 deleted_srvs = set()
211 for srv in nova.servers.list():
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800212 if re.match(name_templ.format("\\d+"), srv.name):
koder aka kdanilove21d7472015-02-14 19:02:04 -0800213 logger.debug("Deleting server {0}".format(srv.name))
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800214 nova.servers.delete(srv)
215 deleted_srvs.add(srv.id)
216
217 while deleted_srvs != set():
koder aka kdanilove21d7472015-02-14 19:02:04 -0800218 logger.debug("Waiting till all servers are actually deleted")
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800219 all_id = set(srv.id for srv in nova.servers.list())
220 if all_id.intersection(deleted_srvs) == set():
koder aka kdanilove21d7472015-02-14 19:02:04 -0800221 logger.debug("Done, deleting volumes")
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800222 break
223 time.sleep(1)
224
225 # wait till vm actually deleted
226
227 cinder = c_client(*ostack_get_creds())
228 for vol in cinder.volumes.list():
229 if isinstance(vol.display_name, basestring):
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800230 if re.match(name_templ.format("\\d+"), vol.display_name):
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800231 if vol.status in ('available', 'error'):
koder aka kdanilov2c473092015-03-29 17:12:13 +0300232 logger.debug("Deleting volume " + vol.display_name)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800233 cinder.volumes.delete(vol)
234
koder aka kdanilove21d7472015-02-14 19:02:04 -0800235 logger.debug("Clearing done (yet some volumes may still deleting)")
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800236
237
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800238# def prepare_host(key_file, ip, fio_path, dst_fio_path, user='cirros'):
239# print "Wait till ssh ready...."
240# wait_ssh_ready(ip, user, key_file)
241
242# print "Preparing host >"
243# print " Coping fio"
244# copy_fio(key_file, ip, fio_path, user, dst_fio_path)
245
246# key_opts = '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
247# args = (key_file, user, ip, key_opts)
248# cmd_format = "ssh {3} -i {0} {1}@{2} '{{0}}'".format(*args).format
249
250# def exec_on_host(cmd):
251# print " " + cmd
252# subprocess.check_call(cmd_format(cmd), shell=True)
253
254# exec_on_host("sudo /usr/sbin/mkfs.ext4 /dev/vdb")
255# exec_on_host("sudo /bin/mkdir /media/ceph")
256# exec_on_host("sudo /bin/mount /dev/vdb /media/ceph")
257# exec_on_host("sudo /bin/chmod a+rwx /media/ceph")
258
259
koder aka kdanilove21d7472015-02-14 19:02:04 -0800260# def main():
261# image_name = 'TestVM'
262# flavor_name = 'ceph'
263# vol_sz = 50
264# network_zone_name = 'net04'
265# amount = 10
266# keypair_name = 'ceph-test'
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800267
koder aka kdanilove21d7472015-02-14 19:02:04 -0800268# nova = nova_connect()
269# clear_all(nova)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800270
koder aka kdanilove21d7472015-02-14 19:02:04 -0800271# try:
272# ips = []
273# params = dict(vol_sz=vol_sz)
274# params['image_name'] = image_name
275# params['flavor_name'] = flavor_name
276# params['network_zone_name'] = network_zone_name
277# params['amount'] = amount
278# params['keypair_name'] = keypair_name
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800279
koder aka kdanilove21d7472015-02-14 19:02:04 -0800280# for ip, host in create_vms(nova, **params):
281# ips.append(ip)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800282
koder aka kdanilove21d7472015-02-14 19:02:04 -0800283# print "All setup done! Ips =", " ".join(ips)
284# print "Starting tests"
285# finally:
286# clear_all(nova)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800287
koder aka kdanilove21d7472015-02-14 19:02:04 -0800288# if __name__ == "__main__":
289# exit(main())