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 |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 13 | from utils import parse_creds |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 14 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 15 | logger = logging.getLogger("io-perf-tool") |
| 16 | |
| 17 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 18 | def 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') |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 24 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 25 | return name, passwd, tenant, auth_url |
| 26 | |
| 27 | |
| 28 | def nova_connect(): |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 29 | return n_client('1.1', *ostack_get_creds() |
| 30 | ) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 31 | |
| 32 | |
| 33 | def 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 kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 38 | def create_volume(size, name): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 39 | cinder = c_client(*ostack_get_creds()) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 40 | 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 kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 45 | logger.critical("Fail to create volume") |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 46 | 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 | |
| 58 | def wait_for_server_active(nova, server, timeout=240): |
| 59 | t = time.time() |
| 60 | while True: |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 61 | time.sleep(1) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 62 | sstate = getattr(server, 'OS-EXT-STS:vm_state').lower() |
| 63 | |
| 64 | if sstate == 'active': |
| 65 | return True |
| 66 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 67 | 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 kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 76 | class Allocate(object): |
| 77 | pass |
| 78 | |
| 79 | |
| 80 | def get_floating_ips(nova, pool, amount): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 81 | 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 kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 86 | 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] | 87 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 88 | |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 89 | def launch_vms(config): |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 90 | creds = config['vm_params']['creds'] |
koder aka kdanilov | 6c49106 | 2015-04-09 22:33:13 +0300 | [diff] [blame] | 91 | |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 92 | # if creds != 'ENV': |
| 93 | # raise ValueError("Only 'ENV' creds are supported") |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 94 | |
| 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 kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 112 | def create_vms_mt(nova, amount, keypair_name, img_name, |
| 113 | flavor_name, vol_sz=None, network_zone_name=None, |
koder aka kdanilov | 7dec9df | 2015-02-15 21:35:19 -0800 | [diff] [blame] | 114 | flt_ip_pool=None, name_templ='ceph-test-{0}', |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 115 | scheduler_hints=None): |
| 116 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 117 | with ThreadPoolExecutor(max_workers=16) as executor: |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 118 | 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 kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 130 | logger.debug("Wait for floating ip") |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 131 | ips = ips_future.result() |
| 132 | ips += [Allocate] * (amount - len(ips)) |
| 133 | else: |
| 134 | ips = [None] * amount |
| 135 | |
koder aka kdanilov | 7dec9df | 2015-02-15 21:35:19 -0800 | [diff] [blame] | 136 | logger.debug("Getting flavor object") |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 137 | fl = fl_future.result() |
koder aka kdanilov | 7dec9df | 2015-02-15 21:35:19 -0800 | [diff] [blame] | 138 | logger.debug("Getting image object") |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 139 | img = img_future.result() |
| 140 | |
| 141 | if network_future is not None: |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 142 | logger.debug("Waiting for network results") |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 143 | nics = [{'net-id': network_future.result().id}] |
| 144 | else: |
| 145 | nics = None |
| 146 | |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 147 | names = map(name_templ.format, range(amount)) |
| 148 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 149 | futures = [] |
koder aka kdanilov | 6e2ae79 | 2015-03-04 18:02:24 -0800 | [diff] [blame] | 150 | logger.debug("Requesting new vms") |
| 151 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 152 | 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 kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 158 | res = [future.result() for future in futures] |
| 159 | logger.debug("Done spawning") |
| 160 | return res |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 161 | |
| 162 | |
| 163 | def 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 kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 168 | for i in range(3): |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 169 | srv = nova.servers.create(name, |
| 170 | flavor=fl, image=img, nics=nics, |
| 171 | key_name=keypair_name, |
| 172 | scheduler_hints=scheduler_hints) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 173 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 174 | if not wait_for_server_active(nova, srv): |
| 175 | msg = "Server {0} fails to start. Kill it and try again" |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 176 | logger.debug(msg.format(srv)) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 177 | nova.servers.delete(srv) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 178 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 179 | while True: |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 180 | # print "wait till server deleted" |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 181 | 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 kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 187 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 188 | if vol_sz is not None: |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 189 | # print "creating volume" |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 190 | vol = create_volume(vol_sz, name) |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 191 | # print "attach volume to server" |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 192 | nova.volumes.create_server_volume(srv.id, vol.id, None) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 193 | |
| 194 | if flt_ip is Allocate: |
| 195 | flt_ip = nova.floating_ips.create(pool) |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 196 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 197 | if flt_ip is not None: |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 198 | # print "attaching ip to server" |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 199 | srv.add_floating_ip(flt_ip) |
Yulia Portnova | 0e64ea2 | 2015-03-20 17:27:22 +0200 | [diff] [blame] | 200 | |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 201 | return flt_ip.ip, nova.servers.get(srv.id) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 202 | |
| 203 | |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 204 | def clear_nodes(): |
| 205 | nova = nova_connect() |
| 206 | clear_all(nova) |
| 207 | |
| 208 | |
koder aka kdanilov | 7dec9df | 2015-02-15 21:35:19 -0800 | [diff] [blame] | 209 | def clear_all(nova, name_templ="ceph-test-{0}"): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 210 | deleted_srvs = set() |
| 211 | for srv in nova.servers.list(): |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 212 | if re.match(name_templ.format("\\d+"), srv.name): |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 213 | logger.debug("Deleting server {0}".format(srv.name)) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 214 | nova.servers.delete(srv) |
| 215 | deleted_srvs.add(srv.id) |
| 216 | |
| 217 | while deleted_srvs != set(): |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 218 | logger.debug("Waiting till all servers are actually deleted") |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 219 | all_id = set(srv.id for srv in nova.servers.list()) |
| 220 | if all_id.intersection(deleted_srvs) == set(): |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 221 | logger.debug("Done, deleting volumes") |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 222 | 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 kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 230 | if re.match(name_templ.format("\\d+"), vol.display_name): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 231 | if vol.status in ('available', 'error'): |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 232 | logger.debug("Deleting volume " + vol.display_name) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 233 | cinder.volumes.delete(vol) |
| 234 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 235 | logger.debug("Clearing done (yet some volumes may still deleting)") |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 236 | |
| 237 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 238 | # 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 kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 260 | # 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 kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 267 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 268 | # nova = nova_connect() |
| 269 | # clear_all(nova) |
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 | # 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 kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 279 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 280 | # for ip, host in create_vms(nova, **params): |
| 281 | # ips.append(ip) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 282 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 283 | # print "All setup done! Ips =", " ".join(ips) |
| 284 | # print "Starting tests" |
| 285 | # finally: |
| 286 | # clear_all(nova) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 287 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 288 | # if __name__ == "__main__": |
| 289 | # exit(main()) |