koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 1 | import re |
| 2 | import os |
| 3 | import time |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 4 | import logging |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 5 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 6 | from concurrent.futures import ThreadPoolExecutor |
| 7 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 8 | from novaclient.client import Client as n_client |
| 9 | from cinderclient.v1.client import Client as c_client |
| 10 | |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 11 | from nodes.node import Node |
| 12 | from nodes.openstack import get_floating_ip |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 13 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 14 | logger = logging.getLogger("io-perf-tool") |
| 15 | |
| 16 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 17 | def 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') |
| 23 | return name, passwd, tenant, auth_url |
| 24 | |
| 25 | |
| 26 | def nova_connect(): |
| 27 | return n_client('1.1', *ostack_get_creds()) |
| 28 | |
| 29 | |
| 30 | def create_keypair(nova, name, key_path): |
| 31 | with open(key_path) as key: |
| 32 | return nova.keypairs.create(name, key.read()) |
| 33 | |
| 34 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 35 | def create_volume(size, name): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 36 | cinder = c_client(*ostack_get_creds()) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 37 | vol = cinder.volumes.create(size=size, display_name=name) |
| 38 | err_count = 0 |
| 39 | while vol.status != 'available': |
| 40 | if vol.status == 'error': |
| 41 | if err_count == 3: |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 42 | logger.critical("Fail to create volume") |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 43 | raise RuntimeError("Fail to create volume") |
| 44 | else: |
| 45 | err_count += 1 |
| 46 | cinder.volumes.delete(vol) |
| 47 | time.sleep(1) |
| 48 | vol = cinder.volumes.create(size=size, display_name=name) |
| 49 | continue |
| 50 | time.sleep(1) |
| 51 | vol = cinder.volumes.get(vol.id) |
| 52 | return vol |
| 53 | |
| 54 | |
| 55 | def wait_for_server_active(nova, server, timeout=240): |
| 56 | t = time.time() |
| 57 | while True: |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 58 | time.sleep(1) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 59 | sstate = getattr(server, 'OS-EXT-STS:vm_state').lower() |
| 60 | |
| 61 | if sstate == 'active': |
| 62 | return True |
| 63 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 64 | if sstate == 'error': |
| 65 | return False |
| 66 | |
| 67 | if time.time() - t > timeout: |
| 68 | return False |
| 69 | |
| 70 | server = nova.servers.get(server) |
| 71 | |
| 72 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 73 | class Allocate(object): |
| 74 | pass |
| 75 | |
| 76 | |
| 77 | def get_floating_ips(nova, pool, amount): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 78 | ip_list = nova.floating_ips.list() |
| 79 | |
| 80 | if pool is not None: |
| 81 | ip_list = [ip for ip in ip_list if ip.pool == pool] |
| 82 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 83 | return [ip for ip in ip_list if ip.instance_id is None][:amount] |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 84 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 85 | |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 86 | def launch_vms(config): |
| 87 | creds = config['creds'] |
| 88 | if creds != 'ENV': |
| 89 | raise ValueError("Only 'ENV' creds are supported") |
| 90 | |
| 91 | logger.debug("Starting new nodes on openstack") |
| 92 | conn = nova_connect() |
| 93 | params = config['vm_params'].copy() |
| 94 | count = params.pop('count') |
| 95 | |
| 96 | if isinstance(count, basestring): |
| 97 | assert count.startswith("x") |
| 98 | lst = conn.services.list(binary='nova-compute') |
| 99 | srv_count = len([srv for srv in lst if srv.status == 'enabled']) |
| 100 | count = srv_count * int(count[1:]) |
| 101 | |
| 102 | creds = params.pop('creds') |
| 103 | |
| 104 | for ip, _ in create_vms_mt(conn, count, **params): |
| 105 | yield Node(creds.format(ip), []) |
| 106 | |
| 107 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 108 | def create_vms_mt(nova, amount, keypair_name, img_name, |
| 109 | flavor_name, vol_sz=None, network_zone_name=None, |
koder aka kdanilov | 7dec9df | 2015-02-15 21:35:19 -0800 | [diff] [blame] | 110 | flt_ip_pool=None, name_templ='ceph-test-{0}', |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 111 | scheduler_hints=None): |
| 112 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 113 | with ThreadPoolExecutor(max_workers=16) as executor: |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 114 | if network_zone_name is not None: |
| 115 | network_future = executor.submit(nova.networks.find, |
| 116 | label=network_zone_name) |
| 117 | else: |
| 118 | network_future = None |
| 119 | |
| 120 | fl_future = executor.submit(nova.flavors.find, name=flavor_name) |
| 121 | img_future = executor.submit(nova.images.find, name=img_name) |
| 122 | |
| 123 | if flt_ip_pool is not None: |
| 124 | ips_future = executor.submit(get_floating_ips, |
| 125 | nova, flt_ip_pool, amount) |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 126 | logger.debug("Wait for floating ip") |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 127 | ips = ips_future.result() |
| 128 | ips += [Allocate] * (amount - len(ips)) |
| 129 | else: |
| 130 | ips = [None] * amount |
| 131 | |
koder aka kdanilov | 7dec9df | 2015-02-15 21:35:19 -0800 | [diff] [blame] | 132 | logger.debug("Getting flavor object") |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 133 | fl = fl_future.result() |
koder aka kdanilov | 7dec9df | 2015-02-15 21:35:19 -0800 | [diff] [blame] | 134 | logger.debug("Getting image object") |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 135 | img = img_future.result() |
| 136 | |
| 137 | if network_future is not None: |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 138 | logger.debug("Waiting for network results") |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 139 | nics = [{'net-id': network_future.result().id}] |
| 140 | else: |
| 141 | nics = None |
| 142 | |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 143 | names = map(name_templ.format, range(amount)) |
| 144 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 145 | futures = [] |
koder aka kdanilov | 6e2ae79 | 2015-03-04 18:02:24 -0800 | [diff] [blame] | 146 | logger.debug("Requesting new vms") |
| 147 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 148 | for name, flt_ip in zip(names, ips): |
| 149 | params = (nova, name, keypair_name, img, fl, |
| 150 | nics, vol_sz, flt_ip, scheduler_hints, |
| 151 | flt_ip_pool) |
| 152 | |
| 153 | futures.append(executor.submit(create_vm, *params)) |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 154 | res = [future.result() for future in futures] |
| 155 | logger.debug("Done spawning") |
| 156 | return res |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 157 | |
| 158 | |
| 159 | def create_vm(nova, name, keypair_name, img, |
| 160 | fl, nics, vol_sz=None, |
| 161 | flt_ip=False, |
| 162 | scheduler_hints=None, |
| 163 | pool=None): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 164 | for i in range(3): |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 165 | srv = nova.servers.create(name, |
| 166 | flavor=fl, image=img, nics=nics, |
| 167 | key_name=keypair_name, |
| 168 | scheduler_hints=scheduler_hints) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 169 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 170 | if not wait_for_server_active(nova, srv): |
| 171 | msg = "Server {0} fails to start. Kill it and try again" |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 172 | logger.debug(msg.format(srv)) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 173 | nova.servers.delete(srv) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 174 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 175 | while True: |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 176 | # print "wait till server deleted" |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 177 | all_id = set(alive_srv.id for alive_srv in nova.servers.list()) |
| 178 | if srv.id not in all_id: |
| 179 | break |
| 180 | time.sleep(1) |
| 181 | else: |
| 182 | break |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 183 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 184 | if vol_sz is not None: |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 185 | # print "creating volume" |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 186 | vol = create_volume(vol_sz, name) |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 187 | # print "attach volume to server" |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 188 | nova.volumes.create_server_volume(srv.id, vol.id, None) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 189 | |
| 190 | if flt_ip is Allocate: |
| 191 | flt_ip = nova.floating_ips.create(pool) |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 192 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 193 | if flt_ip is not None: |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 194 | # print "attaching ip to server" |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 195 | srv.add_floating_ip(flt_ip) |
Yulia Portnova | 0e64ea2 | 2015-03-20 17:27:22 +0200 | [diff] [blame] | 196 | |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 197 | return flt_ip.ip, nova.servers.get(srv.id) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 198 | |
| 199 | |
koder aka kdanilov | 7dec9df | 2015-02-15 21:35:19 -0800 | [diff] [blame] | 200 | def clear_all(nova, name_templ="ceph-test-{0}"): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 201 | deleted_srvs = set() |
| 202 | for srv in nova.servers.list(): |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 203 | if re.match(name_templ.format("\\d+"), srv.name): |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 204 | logger.debug("Deleting server {0}".format(srv.name)) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 205 | nova.servers.delete(srv) |
| 206 | deleted_srvs.add(srv.id) |
| 207 | |
| 208 | while deleted_srvs != set(): |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 209 | logger.debug("Waiting till all servers are actually deleted") |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 210 | all_id = set(srv.id for srv in nova.servers.list()) |
| 211 | if all_id.intersection(deleted_srvs) == set(): |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 212 | logger.debug("Done, deleting volumes") |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 213 | break |
| 214 | time.sleep(1) |
| 215 | |
| 216 | # wait till vm actually deleted |
| 217 | |
| 218 | cinder = c_client(*ostack_get_creds()) |
| 219 | for vol in cinder.volumes.list(): |
| 220 | if isinstance(vol.display_name, basestring): |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 221 | if re.match(name_templ.format("\\d+"), vol.display_name): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 222 | if vol.status in ('available', 'error'): |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 223 | logger.debug("Deleting volume " + vol.display_name) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 224 | cinder.volumes.delete(vol) |
| 225 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 226 | logger.debug("Clearing done (yet some volumes may still deleting)") |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 227 | |
| 228 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 229 | # def prepare_host(key_file, ip, fio_path, dst_fio_path, user='cirros'): |
| 230 | # print "Wait till ssh ready...." |
| 231 | # wait_ssh_ready(ip, user, key_file) |
| 232 | |
| 233 | # print "Preparing host >" |
| 234 | # print " Coping fio" |
| 235 | # copy_fio(key_file, ip, fio_path, user, dst_fio_path) |
| 236 | |
| 237 | # key_opts = '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' |
| 238 | # args = (key_file, user, ip, key_opts) |
| 239 | # cmd_format = "ssh {3} -i {0} {1}@{2} '{{0}}'".format(*args).format |
| 240 | |
| 241 | # def exec_on_host(cmd): |
| 242 | # print " " + cmd |
| 243 | # subprocess.check_call(cmd_format(cmd), shell=True) |
| 244 | |
| 245 | # exec_on_host("sudo /usr/sbin/mkfs.ext4 /dev/vdb") |
| 246 | # exec_on_host("sudo /bin/mkdir /media/ceph") |
| 247 | # exec_on_host("sudo /bin/mount /dev/vdb /media/ceph") |
| 248 | # exec_on_host("sudo /bin/chmod a+rwx /media/ceph") |
| 249 | |
| 250 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 251 | # def main(): |
| 252 | # image_name = 'TestVM' |
| 253 | # flavor_name = 'ceph' |
| 254 | # vol_sz = 50 |
| 255 | # network_zone_name = 'net04' |
| 256 | # amount = 10 |
| 257 | # keypair_name = 'ceph-test' |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 258 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 259 | # nova = nova_connect() |
| 260 | # clear_all(nova) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 261 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 262 | # try: |
| 263 | # ips = [] |
| 264 | # params = dict(vol_sz=vol_sz) |
| 265 | # params['image_name'] = image_name |
| 266 | # params['flavor_name'] = flavor_name |
| 267 | # params['network_zone_name'] = network_zone_name |
| 268 | # params['amount'] = amount |
| 269 | # params['keypair_name'] = keypair_name |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 270 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 271 | # for ip, host in create_vms(nova, **params): |
| 272 | # ips.append(ip) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 273 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 274 | # print "All setup done! Ips =", " ".join(ips) |
| 275 | # print "Starting tests" |
| 276 | # finally: |
| 277 | # clear_all(nova) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 278 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 279 | # if __name__ == "__main__": |
| 280 | # exit(main()) |