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 | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 4 | import os.path |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 5 | import logging |
koder aka kdanilov | 4e9f3ed | 2015-04-14 11:26:12 +0300 | [diff] [blame] | 6 | import subprocess |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 7 | |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame] | 8 | from concurrent.futures import ThreadPoolExecutor |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 9 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 10 | from novaclient.exceptions import NotFound |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 11 | from novaclient.client import Client as n_client |
| 12 | from cinderclient.v1.client import Client as c_client |
| 13 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 14 | import wally |
| 15 | from wally.discover import Node |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 16 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 17 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 18 | logger = logging.getLogger("wally.vms") |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 19 | |
| 20 | |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 21 | STORED_OPENSTACK_CREDS = None |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 22 | NOVA_CONNECTION = None |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 23 | CINDER_CONNECTION = None |
| 24 | |
| 25 | |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 26 | def is_connected(): |
| 27 | return NOVA_CONNECTION is not None |
| 28 | |
| 29 | |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 30 | def ostack_get_creds(): |
| 31 | if STORED_OPENSTACK_CREDS is None: |
| 32 | env = os.environ.get |
| 33 | name = env('OS_USERNAME') |
| 34 | passwd = env('OS_PASSWORD') |
| 35 | tenant = env('OS_TENANT_NAME') |
| 36 | auth_url = env('OS_AUTH_URL') |
| 37 | return name, passwd, tenant, auth_url |
| 38 | else: |
| 39 | return STORED_OPENSTACK_CREDS |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 40 | |
| 41 | |
| 42 | def nova_connect(name=None, passwd=None, tenant=None, auth_url=None): |
| 43 | global NOVA_CONNECTION |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 44 | global STORED_OPENSTACK_CREDS |
| 45 | |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 46 | if NOVA_CONNECTION is None: |
| 47 | if name is None: |
| 48 | name, passwd, tenant, auth_url = ostack_get_creds() |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 49 | else: |
| 50 | STORED_OPENSTACK_CREDS = (name, passwd, tenant, auth_url) |
| 51 | |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 52 | NOVA_CONNECTION = n_client('1.1', name, passwd, tenant, auth_url) |
| 53 | return NOVA_CONNECTION |
| 54 | |
| 55 | |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 56 | def cinder_connect(name=None, passwd=None, tenant=None, auth_url=None): |
| 57 | global CINDER_CONNECTION |
| 58 | global STORED_OPENSTACK_CREDS |
| 59 | |
| 60 | if CINDER_CONNECTION is None: |
| 61 | if name is None: |
| 62 | name, passwd, tenant, auth_url = ostack_get_creds() |
| 63 | else: |
| 64 | STORED_OPENSTACK_CREDS = (name, passwd, tenant, auth_url) |
| 65 | CINDER_CONNECTION = c_client(name, passwd, tenant, auth_url) |
| 66 | return CINDER_CONNECTION |
| 67 | |
| 68 | |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 69 | def prepare_os_subpr(params, name=None, passwd=None, tenant=None, |
| 70 | auth_url=None): |
koder aka kdanilov | 4e9f3ed | 2015-04-14 11:26:12 +0300 | [diff] [blame] | 71 | if name is None: |
| 72 | name, passwd, tenant, auth_url = ostack_get_creds() |
| 73 | |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 74 | MAX_VM_PER_NODE = 8 |
| 75 | serv_groups = " ".join(map(params['aa_group_name'].format, |
| 76 | range(MAX_VM_PER_NODE))) |
koder aka kdanilov | 4e9f3ed | 2015-04-14 11:26:12 +0300 | [diff] [blame] | 77 | |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 78 | env = os.environ.copy() |
| 79 | env.update(dict( |
| 80 | OS_USERNAME=name, |
| 81 | OS_PASSWORD=passwd, |
| 82 | OS_TENANT_NAME=tenant, |
| 83 | OS_AUTH_URL=auth_url, |
koder aka kdanilov | 4e9f3ed | 2015-04-14 11:26:12 +0300 | [diff] [blame] | 84 | |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 85 | FLAVOR_NAME=params['flavor']['name'], |
| 86 | FLAVOR_RAM=str(params['flavor']['ram_size']), |
| 87 | FLAVOR_HDD=str(params['flavor']['hdd_size']), |
| 88 | FLAVOR_CPU_COUNT=str(params['flavor']['cpu_count']), |
| 89 | |
| 90 | SERV_GROUPS=serv_groups, |
| 91 | KEYPAIR_NAME=params['keypair_name'], |
| 92 | |
| 93 | SECGROUP=params['security_group'], |
| 94 | |
| 95 | IMAGE_NAME=params['image']['name'], |
| 96 | KEY_FILE_NAME=params['keypair_file_private'], |
| 97 | IMAGE_URL=params['image']['url'], |
| 98 | )) |
| 99 | |
| 100 | spath = os.path.dirname(os.path.dirname(wally.__file__)) |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 101 | spath = os.path.join(spath, 'scripts/prepare.sh') |
| 102 | |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 103 | cmd = "bash {spath} >/dev/null".format(spath=spath) |
| 104 | subprocess.check_call(cmd, shell=True, env=env) |
koder aka kdanilov | 4e9f3ed | 2015-04-14 11:26:12 +0300 | [diff] [blame] | 105 | |
koder aka kdanilov | 7e0f7cf | 2015-05-01 17:24:35 +0300 | [diff] [blame] | 106 | conn = nova_connect(name, passwd, tenant, auth_url) |
| 107 | while True: |
| 108 | status = conn.images.find(name='wally_ubuntu').status |
| 109 | if status == 'ACTIVE': |
| 110 | break |
| 111 | msg = "Image {0} is still in {1} state. Waiting 10 more seconds" |
| 112 | logger.info(msg.format('wally_ubuntu', status)) |
| 113 | time.sleep(10) |
| 114 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 115 | |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 116 | def find_vms(nova, name_prefix): |
| 117 | for srv in nova.servers.list(): |
| 118 | if srv.name.startswith(name_prefix): |
| 119 | for ips in srv.addresses.values(): |
| 120 | for ip in ips: |
| 121 | if ip.get("OS-EXT-IPS:type", None) == 'floating': |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 122 | yield ip['addr'], srv.id |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 123 | break |
| 124 | |
| 125 | |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 126 | def pause(ids): |
| 127 | def pause_vm(conn, vm_id): |
| 128 | vm = conn.servers.get(vm_id) |
| 129 | if vm.status == 'ACTIVE': |
| 130 | vm.pause() |
| 131 | |
| 132 | conn = nova_connect() |
| 133 | with ThreadPoolExecutor(max_workers=16) as executor: |
| 134 | futures = [executor.submit(pause_vm, conn, vm_id) |
| 135 | for vm_id in ids] |
| 136 | for future in futures: |
| 137 | future.result() |
| 138 | |
| 139 | |
| 140 | def unpause(ids, max_resume_time=10): |
| 141 | def unpause(conn, vm_id): |
| 142 | vm = conn.servers.get(vm_id) |
| 143 | if vm.status == 'PAUSED': |
| 144 | vm.unpause() |
| 145 | |
| 146 | for i in range(max_resume_time * 10): |
| 147 | vm = conn.servers.get(vm_id) |
| 148 | if vm.status != 'PAUSED': |
| 149 | return |
| 150 | time.sleep(0.1) |
| 151 | raise RuntimeError("Can't unpause vm {0}".format(vm_id)) |
| 152 | |
| 153 | conn = nova_connect() |
| 154 | with ThreadPoolExecutor(max_workers=16) as executor: |
| 155 | futures = [executor.submit(unpause, conn, vm_id) |
| 156 | for vm_id in ids] |
| 157 | |
| 158 | for future in futures: |
| 159 | future.result() |
| 160 | |
| 161 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 162 | def prepare_os(nova, params): |
| 163 | allow_ssh(nova, params['security_group']) |
| 164 | |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 165 | MAX_VM_PER_NODE = 8 |
| 166 | serv_groups = " ".join(map(params['aa_group_name'].format, |
| 167 | range(MAX_VM_PER_NODE))) |
| 168 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 169 | shed_ids = [] |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 170 | for shed_group in serv_groups: |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 171 | shed_ids.append(get_or_create_aa_group(nova, shed_group)) |
| 172 | |
| 173 | create_keypair(nova, |
| 174 | params['keypair_name'], |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 175 | params['keypair_name'] + ".pub", |
| 176 | params['keypair_name'] + ".pem") |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 177 | |
| 178 | create_image(nova, params['image']['name'], |
| 179 | params['image']['url']) |
| 180 | |
| 181 | create_flavor(nova, **params['flavor']) |
koder aka kdanilov | 4e9f3ed | 2015-04-14 11:26:12 +0300 | [diff] [blame] | 182 | |
| 183 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 184 | def get_or_create_aa_group(nova, name): |
| 185 | try: |
| 186 | group = nova.server_groups.find(name=name) |
| 187 | except NotFound: |
| 188 | group = nova.server_groups.create({'name': name, |
| 189 | 'policies': ['anti-affinity']}) |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 190 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 191 | return group.id |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 192 | |
| 193 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 194 | def allow_ssh(nova, group_name): |
| 195 | try: |
| 196 | secgroup = nova.security_groups.find(name=group_name) |
| 197 | except NotFound: |
| 198 | secgroup = nova.security_groups.create(group_name, |
| 199 | "allow ssh/ping to node") |
| 200 | |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 201 | nova.security_group_rules.create(secgroup.id, |
| 202 | ip_protocol="tcp", |
| 203 | from_port="22", |
| 204 | to_port="22", |
| 205 | cidr="0.0.0.0/0") |
| 206 | |
| 207 | nova.security_group_rules.create(secgroup.id, |
| 208 | ip_protocol="icmp", |
| 209 | from_port=-1, |
| 210 | cidr="0.0.0.0/0", |
| 211 | to_port=-1) |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 212 | return secgroup.id |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 213 | |
| 214 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 215 | def create_image(nova, name, url): |
| 216 | pass |
| 217 | |
| 218 | |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 219 | def create_flavor(nova, name, ram_size, hdd_size, cpu_count): |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 220 | pass |
| 221 | |
| 222 | |
| 223 | def create_keypair(nova, name, pub_key_path, priv_key_path): |
| 224 | try: |
| 225 | nova.keypairs.find(name=name) |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 226 | # if file not found- delete and recreate |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 227 | except NotFound: |
| 228 | if os.path.exists(pub_key_path): |
| 229 | with open(pub_key_path) as pub_key_fd: |
| 230 | return nova.keypairs.create(name, pub_key_fd.read()) |
| 231 | else: |
| 232 | key = nova.keypairs.create(name) |
| 233 | |
| 234 | with open(priv_key_path, "w") as priv_key_fd: |
| 235 | priv_key_fd.write(key.private_key) |
| 236 | |
| 237 | with open(pub_key_path, "w") as pub_key_fd: |
| 238 | pub_key_fd.write(key.public_key) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 239 | |
| 240 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 241 | def create_volume(size, name): |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 242 | cinder = cinder_connect() |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 243 | vol = cinder.volumes.create(size=size, display_name=name) |
| 244 | err_count = 0 |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 245 | |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 246 | while vol.status != 'available': |
| 247 | if vol.status == 'error': |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 248 | if err_count == 3: |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 249 | logger.critical("Fail to create volume") |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 250 | raise RuntimeError("Fail to create volume") |
| 251 | else: |
| 252 | err_count += 1 |
| 253 | cinder.volumes.delete(vol) |
| 254 | time.sleep(1) |
| 255 | vol = cinder.volumes.create(size=size, display_name=name) |
| 256 | continue |
| 257 | time.sleep(1) |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 258 | vol = cinder.volumes.get(vol.id) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 259 | return vol |
| 260 | |
| 261 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 262 | def wait_for_server_active(nova, server, timeout=300): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 263 | t = time.time() |
| 264 | while True: |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 265 | time.sleep(1) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 266 | sstate = getattr(server, 'OS-EXT-STS:vm_state').lower() |
| 267 | |
| 268 | if sstate == 'active': |
| 269 | return True |
| 270 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 271 | if sstate == 'error': |
| 272 | return False |
| 273 | |
| 274 | if time.time() - t > timeout: |
| 275 | return False |
| 276 | |
| 277 | server = nova.servers.get(server) |
| 278 | |
| 279 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 280 | class Allocate(object): |
| 281 | pass |
| 282 | |
| 283 | |
| 284 | def get_floating_ips(nova, pool, amount): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 285 | ip_list = nova.floating_ips.list() |
| 286 | |
| 287 | if pool is not None: |
| 288 | ip_list = [ip for ip in ip_list if ip.pool == pool] |
| 289 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 290 | 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] | 291 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 292 | |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 293 | def launch_vms(params, already_has_count=0): |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 294 | logger.debug("Calculating new vm count") |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 295 | count = params['count'] |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 296 | nova = nova_connect() |
| 297 | lst = nova.services.list(binary='nova-compute') |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 298 | srv_count = len([srv for srv in lst if srv.status == 'enabled']) |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 299 | |
| 300 | if isinstance(count, basestring): |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 301 | if count.startswith("x"): |
| 302 | count = srv_count * int(count[1:]) |
| 303 | else: |
| 304 | assert count.startswith('=') |
| 305 | count = int(count[1:]) - already_has_count |
| 306 | |
| 307 | if count <= 0: |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 308 | logger.debug("Not need new vms") |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 309 | return |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 310 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 311 | logger.debug("Starting new nodes on openstack") |
| 312 | |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 313 | assert isinstance(count, (int, long)) |
| 314 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 315 | srv_params = "img: {image[name]}, flavor: {flavor[name]}".format(**params) |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 316 | msg_templ = "Will start {0} servers with next params: {1}" |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 317 | logger.info(msg_templ.format(count, srv_params)) |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 318 | |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 319 | vm_params = dict( |
| 320 | img_name=params['image']['name'], |
| 321 | flavor_name=params['flavor']['name'], |
| 322 | group_name=params['group_name'], |
| 323 | keypair_name=params['keypair_name'], |
| 324 | vol_sz=params.get('vol_sz'), |
| 325 | network_zone_name=params.get("network_zone_name"), |
| 326 | flt_ip_pool=params.get('flt_ip_pool'), |
| 327 | name_templ=params.get('name_templ'), |
| 328 | scheduler_hints={"group": params['aa_group_name']}, |
| 329 | security_group=params['security_group'], |
| 330 | sec_group_size=srv_count |
| 331 | ) |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 332 | |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 333 | # precache all errors before start creating vms |
| 334 | private_key_path = params['keypair_file_private'] |
| 335 | creds = params['image']['creds'] |
| 336 | creds.format(ip="1.1.1.1", private_key_path="/some_path/xx") |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 337 | |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 338 | for ip, os_node in create_vms_mt(NOVA_CONNECTION, count, **vm_params): |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 339 | |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 340 | conn_uri = creds.format(ip=ip, private_key_path=private_key_path) |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 341 | yield Node(conn_uri, []), os_node.id |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 342 | |
| 343 | |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 344 | def get_free_server_grpoups(nova, template=None): |
| 345 | for g in nova.server_groups.list(): |
| 346 | if g.members == []: |
| 347 | if re.match(template, g.name): |
| 348 | yield str(g.name) |
| 349 | |
| 350 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 351 | def create_vms_mt(nova, amount, group_name, keypair_name, img_name, |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 352 | flavor_name, vol_sz=None, network_zone_name=None, |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 353 | flt_ip_pool=None, name_templ='wally-{id}', |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 354 | scheduler_hints=None, security_group=None, |
| 355 | sec_group_size=None): |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 356 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 357 | with ThreadPoolExecutor(max_workers=16) as executor: |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 358 | if network_zone_name is not None: |
| 359 | network_future = executor.submit(nova.networks.find, |
| 360 | label=network_zone_name) |
| 361 | else: |
| 362 | network_future = None |
| 363 | |
| 364 | fl_future = executor.submit(nova.flavors.find, name=flavor_name) |
| 365 | img_future = executor.submit(nova.images.find, name=img_name) |
| 366 | |
| 367 | if flt_ip_pool is not None: |
| 368 | ips_future = executor.submit(get_floating_ips, |
| 369 | nova, flt_ip_pool, amount) |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 370 | logger.debug("Wait for floating ip") |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 371 | ips = ips_future.result() |
| 372 | ips += [Allocate] * (amount - len(ips)) |
| 373 | else: |
| 374 | ips = [None] * amount |
| 375 | |
koder aka kdanilov | 7dec9df | 2015-02-15 21:35:19 -0800 | [diff] [blame] | 376 | logger.debug("Getting flavor object") |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 377 | fl = fl_future.result() |
koder aka kdanilov | 7dec9df | 2015-02-15 21:35:19 -0800 | [diff] [blame] | 378 | logger.debug("Getting image object") |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 379 | img = img_future.result() |
| 380 | |
| 381 | if network_future is not None: |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 382 | logger.debug("Waiting for network results") |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 383 | nics = [{'net-id': network_future.result().id}] |
| 384 | else: |
| 385 | nics = None |
| 386 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 387 | names = [] |
| 388 | for i in range(amount): |
| 389 | names.append(name_templ.format(group=group_name, id=i)) |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 390 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 391 | futures = [] |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 392 | logger.debug("Requesting new vm's") |
koder aka kdanilov | 6e2ae79 | 2015-03-04 18:02:24 -0800 | [diff] [blame] | 393 | |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 394 | orig_scheduler_hints = scheduler_hints.copy() |
| 395 | |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 396 | MAX_SHED_GROUPS = 32 |
| 397 | for start_idx in range(MAX_SHED_GROUPS): |
| 398 | pass |
| 399 | |
| 400 | group_name_template = scheduler_hints['group'].format("\\d+") |
| 401 | groups = list(get_free_server_grpoups(nova, group_name_template + "$")) |
| 402 | groups.sort() |
| 403 | |
| 404 | for idx, (name, flt_ip) in enumerate(zip(names, ips), 2): |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 405 | |
| 406 | scheduler_hints = None |
| 407 | if orig_scheduler_hints is not None and sec_group_size is not None: |
| 408 | if "group" in orig_scheduler_hints: |
| 409 | scheduler_hints = orig_scheduler_hints.copy() |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 410 | scheduler_hints['group'] = groups[idx // sec_group_size] |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 411 | |
| 412 | if scheduler_hints is None: |
| 413 | scheduler_hints = orig_scheduler_hints.copy() |
| 414 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 415 | params = (nova, name, keypair_name, img, fl, |
| 416 | nics, vol_sz, flt_ip, scheduler_hints, |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 417 | flt_ip_pool, [security_group]) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 418 | |
| 419 | futures.append(executor.submit(create_vm, *params)) |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 420 | res = [future.result() for future in futures] |
| 421 | logger.debug("Done spawning") |
| 422 | return res |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 423 | |
| 424 | |
| 425 | def create_vm(nova, name, keypair_name, img, |
| 426 | fl, nics, vol_sz=None, |
| 427 | flt_ip=False, |
| 428 | scheduler_hints=None, |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 429 | pool=None, |
| 430 | security_groups=None): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 431 | for i in range(3): |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 432 | srv = nova.servers.create(name, |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 433 | flavor=fl, |
| 434 | image=img, |
| 435 | nics=nics, |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 436 | key_name=keypair_name, |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 437 | scheduler_hints=scheduler_hints, |
| 438 | security_groups=security_groups) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 439 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 440 | if not wait_for_server_active(nova, srv): |
| 441 | msg = "Server {0} fails to start. Kill it and try again" |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 442 | logger.debug(msg.format(srv)) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 443 | nova.servers.delete(srv) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 444 | |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 445 | try: |
| 446 | for j in range(120): |
| 447 | srv = nova.servers.get(srv.id) |
| 448 | time.sleep(1) |
| 449 | else: |
| 450 | msg = "Server {0} delete timeout".format(srv.id) |
| 451 | raise RuntimeError(msg) |
| 452 | except NotFound: |
| 453 | pass |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 454 | else: |
| 455 | break |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 456 | else: |
| 457 | raise RuntimeError("Failed to start server".format(srv.id)) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 458 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 459 | if vol_sz is not None: |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 460 | vol = create_volume(vol_sz, name) |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 461 | nova.volumes.create_server_volume(srv.id, vol.id, None) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 462 | |
| 463 | if flt_ip is Allocate: |
| 464 | flt_ip = nova.floating_ips.create(pool) |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 465 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 466 | if flt_ip is not None: |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 467 | srv.add_floating_ip(flt_ip) |
Yulia Portnova | 0e64ea2 | 2015-03-20 17:27:22 +0200 | [diff] [blame] | 468 | |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 469 | return flt_ip.ip, nova.servers.get(srv.id) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 470 | |
| 471 | |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 472 | def clear_nodes(nodes_ids): |
| 473 | clear_all(NOVA_CONNECTION, nodes_ids, None) |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 474 | |
| 475 | |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 476 | def clear_all(nova, ids=None, name_templ=None): |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 477 | |
| 478 | def need_delete(srv): |
| 479 | if name_templ is not None: |
| 480 | return re.match(name_templ.format("\\d+"), srv.name) is not None |
| 481 | else: |
| 482 | return srv.id in ids |
| 483 | |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 484 | volumes_to_delete = [] |
| 485 | cinder = cinder_connect() |
| 486 | for vol in cinder.volumes.list(): |
| 487 | for attachment in vol.attachments: |
| 488 | if attachment['server_id'] in ids: |
| 489 | volumes_to_delete.append(vol) |
| 490 | break |
| 491 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 492 | deleted_srvs = set() |
| 493 | for srv in nova.servers.list(): |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 494 | if need_delete(srv): |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 495 | logger.debug("Deleting server {0}".format(srv.name)) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 496 | nova.servers.delete(srv) |
| 497 | deleted_srvs.add(srv.id) |
| 498 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 499 | count = 0 |
| 500 | while True: |
| 501 | if count % 60 == 0: |
| 502 | logger.debug("Waiting till all servers are actually deleted") |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 503 | all_id = set(srv.id for srv in nova.servers.list()) |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 504 | if len(all_id.intersection(deleted_srvs)) == 0: |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 505 | break |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 506 | count += 1 |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 507 | time.sleep(1) |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 508 | logger.debug("Done, deleting volumes") |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 509 | |
| 510 | # wait till vm actually deleted |
| 511 | |
koder aka kdanilov | 6b1341a | 2015-04-21 22:44:21 +0300 | [diff] [blame] | 512 | # logger.warning("Volume deletion commented out") |
| 513 | for vol in volumes_to_delete: |
| 514 | logger.debug("Deleting volume " + vol.display_name) |
| 515 | cinder.volumes.delete(vol) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 516 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 517 | logger.debug("Clearing done (yet some volumes may still deleting)") |