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