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 | |
| 11 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 12 | logger = logging.getLogger("io-perf-tool") |
| 13 | |
| 14 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 15 | def 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 | |
| 24 | def nova_connect(): |
| 25 | return n_client('1.1', *ostack_get_creds()) |
| 26 | |
| 27 | |
| 28 | def 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 kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 33 | def create_volume(size, name): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 34 | cinder = c_client(*ostack_get_creds()) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 35 | 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 kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 40 | logger.critical("Fail to create volume") |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 41 | 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 | |
| 53 | def wait_for_server_active(nova, server, timeout=240): |
| 54 | t = time.time() |
| 55 | while True: |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 56 | time.sleep(1) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 57 | sstate = getattr(server, 'OS-EXT-STS:vm_state').lower() |
| 58 | |
| 59 | if sstate == 'active': |
| 60 | return True |
| 61 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 62 | 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 kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 71 | class Allocate(object): |
| 72 | pass |
| 73 | |
| 74 | |
| 75 | def get_floating_ips(nova, pool, amount): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 76 | 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 kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 81 | 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] | 82 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 83 | |
| 84 | def create_vms_mt(nova, amount, keypair_name, img_name, |
| 85 | flavor_name, vol_sz=None, network_zone_name=None, |
koder aka kdanilov | 7dec9df | 2015-02-15 21:35:19 -0800 | [diff] [blame] | 86 | flt_ip_pool=None, name_templ='ceph-test-{0}', |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 87 | scheduler_hints=None): |
| 88 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 89 | with ThreadPoolExecutor(max_workers=16) as executor: |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 90 | 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 kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 106 | logger.debug("Wait for floating ip") |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 107 | ips = ips_future.result() |
| 108 | ips += [Allocate] * (amount - len(ips)) |
| 109 | else: |
| 110 | ips = [None] * amount |
| 111 | |
koder aka kdanilov | 7dec9df | 2015-02-15 21:35:19 -0800 | [diff] [blame] | 112 | logger.debug("Getting flavor object") |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 113 | fl = fl_future.result() |
koder aka kdanilov | 7dec9df | 2015-02-15 21:35:19 -0800 | [diff] [blame] | 114 | logger.debug("Getting image object") |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 115 | img = img_future.result() |
| 116 | |
| 117 | if network_future is not None: |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 118 | logger.debug("Waiting for network results") |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 119 | nics = [{'net-id': network_future.result().id}] |
| 120 | else: |
| 121 | nics = None |
| 122 | |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 123 | names = map(name_templ.format, range(amount)) |
| 124 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 125 | futures = [] |
koder aka kdanilov | 6e2ae79 | 2015-03-04 18:02:24 -0800 | [diff] [blame] | 126 | logger.debug("Requesting new vms") |
| 127 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 128 | for name, flt_ip in zip(names, ips): |
| 129 | params = (nova, name, keypair_name, img, fl, |
| 130 | nics, vol_sz, flt_ip, scheduler_hints, |
| 131 | flt_ip_pool) |
| 132 | |
| 133 | futures.append(executor.submit(create_vm, *params)) |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 134 | res = [future.result() for future in futures] |
| 135 | logger.debug("Done spawning") |
| 136 | return res |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 137 | |
| 138 | |
| 139 | def create_vm(nova, name, keypair_name, img, |
| 140 | fl, nics, vol_sz=None, |
| 141 | flt_ip=False, |
| 142 | scheduler_hints=None, |
| 143 | pool=None): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 144 | for i in range(3): |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 145 | srv = nova.servers.create(name, |
| 146 | flavor=fl, image=img, nics=nics, |
| 147 | key_name=keypair_name, |
| 148 | scheduler_hints=scheduler_hints) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 149 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 150 | if not wait_for_server_active(nova, srv): |
| 151 | msg = "Server {0} fails to start. Kill it and try again" |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 152 | logger.debug(msg.format(srv)) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 153 | nova.servers.delete(srv) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 154 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 155 | while True: |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 156 | # print "wait till server deleted" |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 157 | all_id = set(alive_srv.id for alive_srv in nova.servers.list()) |
| 158 | if srv.id not in all_id: |
| 159 | break |
| 160 | time.sleep(1) |
| 161 | else: |
| 162 | break |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 163 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 164 | if vol_sz is not None: |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 165 | # print "creating volume" |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 166 | vol = create_volume(vol_sz, name) |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 167 | # print "attach volume to server" |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 168 | nova.volumes.create_server_volume(srv.id, vol.id, None) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 169 | |
| 170 | if flt_ip is Allocate: |
| 171 | flt_ip = nova.floating_ips.create(pool) |
| 172 | |
| 173 | if flt_ip is not None: |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 174 | # print "attaching ip to server" |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 175 | srv.add_floating_ip(flt_ip) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 176 | return (flt_ip.ip, srv) |
| 177 | else: |
| 178 | return (None, srv) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 179 | |
| 180 | |
koder aka kdanilov | 7dec9df | 2015-02-15 21:35:19 -0800 | [diff] [blame] | 181 | def clear_all(nova, name_templ="ceph-test-{0}"): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 182 | deleted_srvs = set() |
| 183 | for srv in nova.servers.list(): |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 184 | if re.match(name_templ.format("\\d+"), srv.name): |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 185 | logger.debug("Deleting server {0}".format(srv.name)) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 186 | nova.servers.delete(srv) |
| 187 | deleted_srvs.add(srv.id) |
| 188 | |
| 189 | while deleted_srvs != set(): |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 190 | logger.debug("Waiting till all servers are actually deleted") |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 191 | all_id = set(srv.id for srv in nova.servers.list()) |
| 192 | if all_id.intersection(deleted_srvs) == set(): |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 193 | logger.debug("Done, deleting volumes") |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 194 | break |
| 195 | time.sleep(1) |
| 196 | |
| 197 | # wait till vm actually deleted |
| 198 | |
| 199 | cinder = c_client(*ostack_get_creds()) |
| 200 | for vol in cinder.volumes.list(): |
| 201 | if isinstance(vol.display_name, basestring): |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 202 | if re.match(name_templ.format("\\d+"), vol.display_name): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 203 | if vol.status in ('available', 'error'): |
| 204 | print "Deleting volume", vol.display_name |
| 205 | cinder.volumes.delete(vol) |
| 206 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 207 | logger.debug("Clearing done (yet some volumes may still deleting)") |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 208 | |
| 209 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 210 | # def prepare_host(key_file, ip, fio_path, dst_fio_path, user='cirros'): |
| 211 | # print "Wait till ssh ready...." |
| 212 | # wait_ssh_ready(ip, user, key_file) |
| 213 | |
| 214 | # print "Preparing host >" |
| 215 | # print " Coping fio" |
| 216 | # copy_fio(key_file, ip, fio_path, user, dst_fio_path) |
| 217 | |
| 218 | # key_opts = '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' |
| 219 | # args = (key_file, user, ip, key_opts) |
| 220 | # cmd_format = "ssh {3} -i {0} {1}@{2} '{{0}}'".format(*args).format |
| 221 | |
| 222 | # def exec_on_host(cmd): |
| 223 | # print " " + cmd |
| 224 | # subprocess.check_call(cmd_format(cmd), shell=True) |
| 225 | |
| 226 | # exec_on_host("sudo /usr/sbin/mkfs.ext4 /dev/vdb") |
| 227 | # exec_on_host("sudo /bin/mkdir /media/ceph") |
| 228 | # exec_on_host("sudo /bin/mount /dev/vdb /media/ceph") |
| 229 | # exec_on_host("sudo /bin/chmod a+rwx /media/ceph") |
| 230 | |
| 231 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 232 | # def main(): |
| 233 | # image_name = 'TestVM' |
| 234 | # flavor_name = 'ceph' |
| 235 | # vol_sz = 50 |
| 236 | # network_zone_name = 'net04' |
| 237 | # amount = 10 |
| 238 | # keypair_name = 'ceph-test' |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 239 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 240 | # nova = nova_connect() |
| 241 | # clear_all(nova) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 242 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 243 | # try: |
| 244 | # ips = [] |
| 245 | # params = dict(vol_sz=vol_sz) |
| 246 | # params['image_name'] = image_name |
| 247 | # params['flavor_name'] = flavor_name |
| 248 | # params['network_zone_name'] = network_zone_name |
| 249 | # params['amount'] = amount |
| 250 | # params['keypair_name'] = keypair_name |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 251 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 252 | # for ip, host in create_vms(nova, **params): |
| 253 | # ips.append(ip) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 254 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 255 | # print "All setup done! Ips =", " ".join(ips) |
| 256 | # print "Starting tests" |
| 257 | # finally: |
| 258 | # clear_all(nova) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 259 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 260 | # if __name__ == "__main__": |
| 261 | # exit(main()) |