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 | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 7 | import tempfile |
| 8 | import subprocess |
| 9 | import urllib.request |
| 10 | from typing import Dict, Any, Iterable, Iterator, NamedTuple, Optional, List, Tuple |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame] | 11 | from concurrent.futures import ThreadPoolExecutor |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 12 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 13 | from keystoneauth1 import loading, session |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 14 | from novaclient.exceptions import NotFound |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 15 | from novaclient.client import Client as NovaClient |
| 16 | from cinderclient.client import Client as CinderClient |
| 17 | from glanceclient import Client as GlanceClient |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 18 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 19 | |
| 20 | from .utils import Timeout |
| 21 | from .node_interfaces import NodeInfo |
| 22 | |
koder aka kdanilov | 3405201 | 2015-08-27 18:32:11 +0300 | [diff] [blame] | 23 | |
koder aka kdanilov | a94dfe1 | 2015-08-19 13:04:51 +0300 | [diff] [blame] | 24 | __doc__ = """ |
| 25 | Module used to reliably spawn set of VM's, evenly distributed across |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 26 | compute servers in openstack cluster. Main functions: |
koder aka kdanilov | a94dfe1 | 2015-08-19 13:04:51 +0300 | [diff] [blame] | 27 | |
| 28 | get_OS_credentials - extract openstack credentials from different sources |
| 29 | nova_connect - connect to nova api |
| 30 | cinder_connect - connect to cinder api |
| 31 | find - find VM with given prefix in name |
| 32 | prepare_OS - prepare tenant for usage |
| 33 | launch_vms - reliably start set of VM in parallel with volumes and floating IP |
| 34 | clear_all - clear VM and volumes |
| 35 | """ |
| 36 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 37 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 38 | logger = logging.getLogger("wally.vms") |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 39 | |
| 40 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 41 | OSCreds = NamedTuple("OSCreds", |
| 42 | [("name", str), |
| 43 | ("passwd", str), |
| 44 | ("tenant", str), |
| 45 | ("auth_url", str), |
| 46 | ("insecure", bool)]) |
koder aka kdanilov | b719743 | 2015-07-15 00:40:43 +0300 | [diff] [blame] | 47 | |
| 48 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 49 | # TODO(koder): should correctly process different sources, not only env???? |
| 50 | def get_openstack_credentials() -> OSCreds: |
| 51 | is_insecure = os.environ.get('OS_INSECURE', 'false').lower() in ('true', 'yes') |
| 52 | |
| 53 | return OSCreds(os.environ.get('OS_USERNAME'), |
| 54 | os.environ.get('OS_PASSWORD'), |
| 55 | os.environ.get('OS_TENANT_NAME'), |
| 56 | os.environ.get('OS_AUTH_URL'), |
| 57 | is_insecure) |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 58 | |
| 59 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 60 | class OSConnection: |
| 61 | def __init__(self, nova: NovaClient, cinder: CinderClient, glance: GlanceClient) -> None: |
| 62 | self.nova = nova |
| 63 | self.cinder = cinder |
| 64 | self.glance = glance |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 65 | |
| 66 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 67 | def os_connect(os_creds: OSCreds, version: str = "2") -> OSConnection: |
| 68 | loader = loading.get_plugin_loader('password') |
| 69 | auth = loader.load_from_options(auth_url=os_creds.auth_url, |
| 70 | username=os_creds.name, |
| 71 | password=os_creds.passwd, |
| 72 | project_id=os_creds.tenant) |
| 73 | auth_sess = session.Session(auth=auth) |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 74 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 75 | glance = GlanceClient(version, session=auth_sess) |
| 76 | nova = NovaClient(version, session=auth_sess) |
| 77 | cinder = CinderClient(os_creds.name, os_creds.passwd, os_creds.tenant, os_creds.auth_url, |
| 78 | insecure=os_creds.insecure, api_version=version) |
| 79 | return OSConnection(nova, cinder, glance) |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 80 | |
| 81 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 82 | def find_vms(conn: OSConnection, name_prefix: str) -> Iterable[str, int]: |
| 83 | for srv in conn.nova.servers.list(): |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 84 | if srv.name.startswith(name_prefix): |
| 85 | for ips in srv.addresses.values(): |
| 86 | for ip in ips: |
| 87 | if ip.get("OS-EXT-IPS:type", None) == 'floating': |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 88 | yield ip['addr'], srv.id |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 89 | break |
| 90 | |
| 91 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 92 | def pause(conn: OSConnection, ids: Iterable[int], executor: ThreadPoolExecutor) -> None: |
| 93 | def pause_vm(vm_id: str) -> None: |
| 94 | vm = conn.nova.servers.get(vm_id) |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 95 | if vm.status == 'ACTIVE': |
| 96 | vm.pause() |
| 97 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 98 | for future in executor.map(pause_vm, ids): |
| 99 | future.result() |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 100 | |
| 101 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 102 | def unpause(conn: OSConnection, ids: Iterable[int], executor: ThreadPoolExecutor, max_resume_time=10) -> None: |
| 103 | def unpause(vm_id: str) -> None: |
| 104 | vm = conn.nova.servers.get(vm_id) |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 105 | if vm.status == 'PAUSED': |
| 106 | vm.unpause() |
| 107 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 108 | for _ in Timeout(max_resume_time): |
| 109 | vm = conn.nova.servers.get(vm_id) |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 110 | if vm.status != 'PAUSED': |
| 111 | return |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 112 | raise RuntimeError("Can't unpause vm {0}".format(vm_id)) |
| 113 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 114 | for future in executor.map(unpause, ids): |
| 115 | future.result() |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 116 | |
| 117 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 118 | def prepare_os(conn: OSConnection, params: Dict[str, Any], max_vm_per_node: int = 8) -> None: |
koder aka kdanilov | 3405201 | 2015-08-27 18:32:11 +0300 | [diff] [blame] | 119 | """prepare openstack for futher usage |
| 120 | |
| 121 | Creates server groups, security rules, keypair, flavor |
| 122 | and upload VM image from web. In case if object with |
| 123 | given name already exists, skip preparation part. |
| 124 | Don't check, that existing object has required attributes |
| 125 | |
| 126 | params: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 127 | nova: OSConnection |
koder aka kdanilov | 3405201 | 2015-08-27 18:32:11 +0300 | [diff] [blame] | 128 | params: dict { |
| 129 | security_group:str - security group name with allowed ssh and ping |
| 130 | aa_group_name:str - template for anti-affinity group names. Should |
| 131 | receive one integer parameter, like "cbt_aa_{0}" |
| 132 | keypair_name: str - OS keypair name |
| 133 | keypair_file_public: str - path to public key file |
| 134 | keypair_file_private: str - path to private key file |
| 135 | |
| 136 | flavor:dict - flavor params |
| 137 | name, ram_size, hdd_size, cpu_count |
| 138 | as for novaclient.Client.flavor.create call |
| 139 | |
| 140 | image:dict - image params |
| 141 | 'name': image name |
| 142 | 'url': image url |
| 143 | } |
| 144 | os_creds: OSCreds |
| 145 | max_vm_per_compute: int=8 maximum expected amount of VM, per |
| 146 | compute host. Used to create appropriate |
| 147 | count of server groups for even placement |
koder aka kdanilov | 3405201 | 2015-08-27 18:32:11 +0300 | [diff] [blame] | 148 | """ |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 149 | allow_ssh_and_ping(conn, params['security_group']) |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 150 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 151 | for idx in range(max_vm_per_node): |
| 152 | get_or_create_aa_group(conn, params['aa_group_name'].format(idx)) |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 153 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 154 | create_keypair(conn, params['keypair_name'], params['keypair_file_public'], params['keypair_file_private']) |
| 155 | create_image(conn, params['image']['name'], params['image']['url']) |
| 156 | create_flavor(conn, **params['flavor']) |
koder aka kdanilov | 4e9f3ed | 2015-04-14 11:26:12 +0300 | [diff] [blame] | 157 | |
| 158 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 159 | def create_keypair(conn: OSConnection, name: str, pub_key_path: str, priv_key_path: str): |
koder aka kdanilov | a94dfe1 | 2015-08-19 13:04:51 +0300 | [diff] [blame] | 160 | """create and upload keypair into nova, if doesn't exists yet |
| 161 | |
| 162 | Create and upload keypair into nova, if keypair with given bane |
| 163 | doesn't exists yet. Uses key from files, if file doesn't exists - |
| 164 | create new keys, and store'em into files. |
| 165 | |
| 166 | parameters: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 167 | conn: OSConnection |
koder aka kdanilov | a94dfe1 | 2015-08-19 13:04:51 +0300 | [diff] [blame] | 168 | name: str - ketpair name |
| 169 | pub_key_path: str - path for public key |
| 170 | priv_key_path: str - path for private key |
koder aka kdanilov | a94dfe1 | 2015-08-19 13:04:51 +0300 | [diff] [blame] | 171 | """ |
| 172 | |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 173 | pub_key_exists = os.path.exists(pub_key_path) |
| 174 | priv_key_exists = os.path.exists(priv_key_path) |
| 175 | |
| 176 | try: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 177 | kpair = conn.nova.keypairs.find(name=name) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 178 | # if file not found- delete and recreate |
| 179 | except NotFound: |
| 180 | kpair = None |
| 181 | |
| 182 | if pub_key_exists and not priv_key_exists: |
| 183 | raise EnvironmentError("Private key file doesn't exists") |
| 184 | |
| 185 | if not pub_key_exists and priv_key_exists: |
| 186 | raise EnvironmentError("Public key file doesn't exists") |
| 187 | |
| 188 | if kpair is None: |
| 189 | if pub_key_exists: |
| 190 | with open(pub_key_path) as pub_key_fd: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 191 | return conn.nova.keypairs.create(name, pub_key_fd.read()) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 192 | else: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 193 | key = conn.nova.keypairs.create(name) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 194 | |
| 195 | with open(priv_key_path, "w") as priv_key_fd: |
| 196 | priv_key_fd.write(key.private_key) |
| 197 | os.chmod(priv_key_path, stat.S_IREAD | stat.S_IWRITE) |
| 198 | |
| 199 | with open(pub_key_path, "w") as pub_key_fd: |
| 200 | pub_key_fd.write(key.public_key) |
| 201 | elif not priv_key_exists: |
| 202 | raise EnvironmentError("Private key file doesn't exists," + |
| 203 | " but key uploaded openstack." + |
| 204 | " Either set correct path to private key" + |
| 205 | " or remove key from openstack") |
| 206 | |
| 207 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 208 | def get_or_create_aa_group(conn: OSConnection, name: str) -> int: |
koder aka kdanilov | a94dfe1 | 2015-08-19 13:04:51 +0300 | [diff] [blame] | 209 | """create anti-affinity server group, if doesn't exists yet |
| 210 | |
| 211 | parameters: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 212 | conn: OSConnection |
koder aka kdanilov | a94dfe1 | 2015-08-19 13:04:51 +0300 | [diff] [blame] | 213 | name: str - group name |
| 214 | |
| 215 | returns: str - group id |
| 216 | """ |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 217 | try: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 218 | return conn.nova.server_groups.find(name=name).id |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 219 | except NotFound: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 220 | return conn.nova.server_groups.create(name=name, policies=['anti-affinity']).id |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 221 | |
| 222 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 223 | def allow_ssh_and_ping(conn: OSConnection, group_name: str) -> int: |
koder aka kdanilov | a94dfe1 | 2015-08-19 13:04:51 +0300 | [diff] [blame] | 224 | """create sequrity group for ping and ssh |
| 225 | |
| 226 | parameters: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 227 | conn: |
koder aka kdanilov | a94dfe1 | 2015-08-19 13:04:51 +0300 | [diff] [blame] | 228 | group_name: str - group name |
| 229 | |
| 230 | returns: str - group id |
| 231 | """ |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 232 | try: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 233 | secgroup = conn.nova.security_groups.find(name=group_name) |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 234 | except NotFound: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 235 | secgroup = conn.nova.security_groups.create(group_name, "allow ssh/ping to node") |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 236 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 237 | conn.nova.security_group_rules.create(secgroup.id, |
| 238 | ip_protocol="tcp", |
| 239 | from_port="22", |
| 240 | to_port="22", |
| 241 | cidr="0.0.0.0/0") |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 242 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 243 | conn.nova.security_group_rules.create(secgroup.id, |
| 244 | ip_protocol="icmp", |
| 245 | from_port=-1, |
| 246 | cidr="0.0.0.0/0", |
| 247 | to_port=-1) |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 248 | return secgroup.id |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 249 | |
| 250 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 251 | def create_image(conn: OSConnection, name: str, url: str) -> None: |
koder aka kdanilov | a94dfe1 | 2015-08-19 13:04:51 +0300 | [diff] [blame] | 252 | """upload image into glance from given URL, if given image doesn't exisis yet |
| 253 | |
| 254 | parameters: |
| 255 | nova: nova connection |
| 256 | os_creds: OSCreds object - openstack credentials, should be same, |
| 257 | as used when connectiong given novaclient |
| 258 | name: str - image name |
| 259 | url: str - image download url |
| 260 | |
| 261 | returns: None |
| 262 | """ |
| 263 | try: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 264 | conn.nova.images.find(name=name) |
koder aka kdanilov | a94dfe1 | 2015-08-19 13:04:51 +0300 | [diff] [blame] | 265 | return |
| 266 | except NotFound: |
| 267 | pass |
| 268 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 269 | ok = False |
| 270 | with tempfile.NamedTemporaryFile() as temp_fd: |
| 271 | try: |
| 272 | cmd = "wget --dns-timeout=30 --connect-timeout=30 --read-timeout=30 -o {} {}" |
| 273 | subprocess.check_call(cmd.format(temp_fd.name, url)) |
| 274 | ok = True |
koder aka kdanilov | a94dfe1 | 2015-08-19 13:04:51 +0300 | [diff] [blame] | 275 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 276 | # TODO(koder): add proper error handling |
| 277 | except Exception: |
| 278 | pass |
koder aka kdanilov | a94dfe1 | 2015-08-19 13:04:51 +0300 | [diff] [blame] | 279 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 280 | if not ok: |
| 281 | urllib.request.urlretrieve(url, temp_fd.name) |
koder aka kdanilov | a94dfe1 | 2015-08-19 13:04:51 +0300 | [diff] [blame] | 282 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 283 | image = conn.glance.images.create(name=name) |
| 284 | with open(temp_fd.name, 'rb') as fd: |
| 285 | conn.glance.images.upload(image.id, fd) |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 286 | |
| 287 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 288 | def create_flavor(conn: OSConnection, name: str, ram_size: int, hdd_size: int, cpu_count: int) -> None: |
koder aka kdanilov | a94dfe1 | 2015-08-19 13:04:51 +0300 | [diff] [blame] | 289 | """create flavor, if doesn't exisis yet |
| 290 | |
| 291 | parameters: |
| 292 | nova: nova connection |
| 293 | name: str - flavor name |
| 294 | ram_size: int - ram size (UNIT?) |
| 295 | hdd_size: int - root hdd size (UNIT?) |
| 296 | cpu_count: int - cpu cores |
| 297 | |
| 298 | returns: None |
| 299 | """ |
| 300 | try: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 301 | conn.nova.flavors.find(name) |
koder aka kdanilov | a94dfe1 | 2015-08-19 13:04:51 +0300 | [diff] [blame] | 302 | return |
| 303 | except NotFound: |
| 304 | pass |
| 305 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 306 | conn.nova.flavors.create(name, cpu_count, ram_size, hdd_size) |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 307 | |
| 308 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 309 | def create_volume(conn: OSConnection, size: int, name: str) -> Any: |
| 310 | vol = conn.cinder.volumes.create(size=size, display_name=name) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 311 | err_count = 0 |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 312 | |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 313 | while vol.status != 'available': |
| 314 | if vol.status == 'error': |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 315 | if err_count == 3: |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 316 | logger.critical("Fail to create volume") |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 317 | raise RuntimeError("Fail to create volume") |
| 318 | else: |
| 319 | err_count += 1 |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 320 | conn.cinder.volumes.delete(vol) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 321 | time.sleep(1) |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 322 | vol = conn.cinder.volumes.create(size=size, display_name=name) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 323 | continue |
| 324 | time.sleep(1) |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 325 | vol = conn.cinder.volumes.get(vol.id) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 326 | return vol |
| 327 | |
| 328 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 329 | def wait_for_server_active(conn: OSConnection, server: Any, timeout: int = 300)-> None: |
koder aka kdanilov | a94dfe1 | 2015-08-19 13:04:51 +0300 | [diff] [blame] | 330 | """waiting till server became active |
| 331 | |
| 332 | parameters: |
| 333 | nova: nova connection |
| 334 | server: server object |
| 335 | timeout: int - seconds to wait till raise an exception |
| 336 | |
| 337 | returns: None |
| 338 | """ |
| 339 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 340 | for _ in Timeout(timeout, no_exc=True): |
| 341 | server_state = getattr(server, 'OS-EXT-STS:vm_state').lower() |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 342 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 343 | if server_state == 'active': |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 344 | return True |
| 345 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 346 | if server_state == 'error': |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 347 | return False |
| 348 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 349 | server = conn.nova.servers.get(server) |
| 350 | return False |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 351 | |
| 352 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 353 | class Allocate(object): |
| 354 | pass |
| 355 | |
| 356 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 357 | def get_floating_ips(conn: OSConnection, pool: Optional[str], amount: int) -> List[str]: |
| 358 | """allocate floating ips |
koder aka kdanilov | a94dfe1 | 2015-08-19 13:04:51 +0300 | [diff] [blame] | 359 | |
| 360 | parameters: |
| 361 | nova: nova connection |
| 362 | pool:str floating ip pool name |
| 363 | amount:int - ip count |
| 364 | |
| 365 | returns: [ip object] |
| 366 | """ |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 367 | ip_list = conn.nova.floating_ips.list() |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 368 | |
| 369 | if pool is not None: |
| 370 | ip_list = [ip for ip in ip_list if ip.pool == pool] |
| 371 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 372 | 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] | 373 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 374 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 375 | def launch_vms(conn: OSConnection, |
| 376 | params: Dict[str, Any], |
| 377 | executor: ThreadPoolExecutor, |
| 378 | already_has_count: int = 0) -> Iterator[NodeInfo]: |
koder aka kdanilov | 3405201 | 2015-08-27 18:32:11 +0300 | [diff] [blame] | 379 | """launch virtual servers |
| 380 | |
| 381 | Parameters: |
| 382 | nova: nova client |
| 383 | params: dict { |
| 384 | count: str or int - server count. If count is string it should be in |
| 385 | one of bext forms: "=INT" or "xINT". First mean |
| 386 | to spawn (INT - already_has_count) servers, and |
| 387 | all should be evenly distributed across all compute |
| 388 | nodes. xINT mean spawn COMPUTE_COUNT * INT servers. |
| 389 | image: dict {'name': str - image name} |
| 390 | flavor: dict {'name': str - flavor name} |
| 391 | group_name: str - group name, used to create uniq server name |
| 392 | keypair_name: str - ssh keypais name |
| 393 | keypair_file_private: str - path to private key |
| 394 | user: str - vm user name |
| 395 | vol_sz: int or None - volume size, or None, if no volume |
| 396 | network_zone_name: str - network zone name |
| 397 | flt_ip_pool: str - floating ip pool |
| 398 | name_templ: str - server name template, should receive two parameters |
| 399 | 'group and id, like 'cbt-{group}-{id}' |
| 400 | aa_group_name: str scheduler group name |
| 401 | security_group: str - security group name |
| 402 | } |
| 403 | already_has_count: int=0 - how many servers already exists. Used to distribute |
| 404 | new servers evenly across all compute nodes, taking |
| 405 | old server in accout |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 406 | returns: generator of NodeInfo - server credentials, in format USER@IP:KEY_PATH |
koder aka kdanilov | 3405201 | 2015-08-27 18:32:11 +0300 | [diff] [blame] | 407 | |
| 408 | """ |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 409 | logger.debug("Calculating new vm count") |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 410 | count = params['count'] # type: int |
| 411 | lst = conn.nova.services.list(binary='nova-compute') |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 412 | 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] | 413 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 414 | if isinstance(count, str): |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 415 | if count.startswith("x"): |
| 416 | count = srv_count * int(count[1:]) |
| 417 | else: |
| 418 | assert count.startswith('=') |
| 419 | count = int(count[1:]) - already_has_count |
| 420 | |
| 421 | if count <= 0: |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 422 | logger.debug("Not need new vms") |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 423 | return |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 424 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 425 | logger.debug("Starting new nodes on openstack") |
| 426 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 427 | assert isinstance(count, int) |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 428 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 429 | srv_params = "img: {image[name]}, flavor: {flavor[name]}".format(**params) |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 430 | msg_templ = "Will start {0} servers with next params: {1}" |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 431 | logger.info(msg_templ.format(count, srv_params)) |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 432 | |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 433 | vm_params = dict( |
| 434 | img_name=params['image']['name'], |
| 435 | flavor_name=params['flavor']['name'], |
| 436 | group_name=params['group_name'], |
| 437 | keypair_name=params['keypair_name'], |
| 438 | vol_sz=params.get('vol_sz'), |
| 439 | network_zone_name=params.get("network_zone_name"), |
| 440 | flt_ip_pool=params.get('flt_ip_pool'), |
| 441 | name_templ=params.get('name_templ'), |
| 442 | scheduler_hints={"group": params['aa_group_name']}, |
| 443 | security_group=params['security_group'], |
| 444 | sec_group_size=srv_count |
| 445 | ) |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 446 | |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 447 | # precache all errors before start creating vms |
| 448 | private_key_path = params['keypair_file_private'] |
| 449 | creds = params['image']['creds'] |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 450 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 451 | for ip, os_node in create_vms_mt(conn, count, executor, **vm_params): |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 452 | conn_uri = creds.format(ip=ip, private_key_path=private_key_path) |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 453 | info = NodeInfo(conn_uri, set()) |
| 454 | info.os_vm_id = os_node.id |
| 455 | yield info |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 456 | |
| 457 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 458 | def get_free_server_groups(conn: OSConnection, template: str) -> Iterator[str]: |
koder aka kdanilov | 3405201 | 2015-08-27 18:32:11 +0300 | [diff] [blame] | 459 | """get fre server groups, that match given name template |
| 460 | |
| 461 | parameters: |
| 462 | nova: nova connection |
| 463 | template:str - name template |
| 464 | amount:int - ip count |
| 465 | |
| 466 | returns: generator or str - server group names |
| 467 | """ |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 468 | for server_group in conn.nova.server_groups.list(): |
| 469 | if not server_group.members: |
| 470 | if re.match(template, server_group.name): |
| 471 | yield str(server_group.id) |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 472 | |
| 473 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 474 | def create_vms_mt(conn: OSConnection, |
| 475 | amount: int, |
| 476 | executor: ThreadPoolExecutor, |
| 477 | group_name: str, |
| 478 | keypair_name: str, |
| 479 | img_name: str, |
| 480 | flavor_name: str, |
| 481 | vol_sz: int = None, |
| 482 | network_zone_name: str = None, |
| 483 | flt_ip_pool: str = None, |
| 484 | name_templ: str ='wally-{id}', |
| 485 | scheduler_hints: Dict = None, |
| 486 | security_group: str = None, |
| 487 | sec_group_size: int = None) -> List[Tuple[str, Any]]: |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 488 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 489 | if network_zone_name is not None: |
| 490 | network_future = executor.submit(conn.nova.networks.find, |
| 491 | label=network_zone_name) |
| 492 | else: |
| 493 | network_future = None |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 494 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 495 | fl_future = executor.submit(conn.nova.flavors.find, name=flavor_name) |
| 496 | img_future = executor.submit(conn.nova.images.find, name=img_name) |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 497 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 498 | if flt_ip_pool is not None: |
| 499 | ips_future = executor.submit(get_floating_ips, |
| 500 | conn, flt_ip_pool, amount) |
| 501 | logger.debug("Wait for floating ip") |
| 502 | ips = ips_future.result() |
| 503 | ips += [Allocate] * (amount - len(ips)) |
| 504 | else: |
| 505 | ips = [None] * amount |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 506 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 507 | logger.debug("Getting flavor object") |
| 508 | fl = fl_future.result() |
| 509 | logger.debug("Getting image object") |
| 510 | img = img_future.result() |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 511 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 512 | if network_future is not None: |
| 513 | logger.debug("Waiting for network results") |
| 514 | nics = [{'net-id': network_future.result().id}] |
| 515 | else: |
| 516 | nics = None |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 517 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 518 | names = [] # type: List[str] |
| 519 | for i in range(amount): |
| 520 | names.append(name_templ.format(group=group_name, id=i)) |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 521 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 522 | futures = [] |
| 523 | logger.debug("Requesting new vm's") |
koder aka kdanilov | 6e2ae79 | 2015-03-04 18:02:24 -0800 | [diff] [blame] | 524 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 525 | orig_scheduler_hints = scheduler_hints.copy() |
| 526 | group_name_template = scheduler_hints['group'].format("\\d+") |
| 527 | groups = list(get_free_server_groups(conn, group_name_template + "$")) |
| 528 | groups.sort() |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 529 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 530 | for idx, (name, flt_ip) in enumerate(zip(names, ips), 2): |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 531 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 532 | scheduler_hints = None |
| 533 | if orig_scheduler_hints is not None and sec_group_size is not None: |
| 534 | if "group" in orig_scheduler_hints: |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 535 | scheduler_hints = orig_scheduler_hints.copy() |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 536 | scheduler_hints['group'] = groups[idx // sec_group_size] |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 537 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 538 | if scheduler_hints is None: |
| 539 | scheduler_hints = orig_scheduler_hints.copy() |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 540 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 541 | params = (conn, name, keypair_name, img, fl, |
| 542 | nics, vol_sz, flt_ip, scheduler_hints, |
| 543 | flt_ip_pool, [security_group]) |
| 544 | |
| 545 | futures.append(executor.submit(create_vm, *params)) |
| 546 | res = [future.result() for future in futures] |
| 547 | logger.debug("Done spawning") |
| 548 | return res |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 549 | |
| 550 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 551 | def create_vm(conn: OSConnection, |
| 552 | name: str, |
| 553 | keypair_name: str, |
| 554 | img: Any, |
| 555 | flavor: Any, |
| 556 | nics: List, |
| 557 | vol_sz: int = None, |
| 558 | flt_ip: Any = False, |
| 559 | scheduler_hints: Dict = None, |
| 560 | pool: str = None, |
| 561 | security_groups=None, |
| 562 | max_retry: int = 3, |
| 563 | delete_timeout: int = 120) -> Tuple[str, Any]: |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 564 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 565 | # make mypy/pylint happy |
| 566 | srv = None # type: Any |
| 567 | for i in range(max_retry): |
| 568 | srv = conn.nova.servers.create(name, flavor=flavor, image=img, nics=nics, key_name=keypair_name, |
| 569 | scheduler_hints=scheduler_hints, security_groups=security_groups) |
| 570 | |
| 571 | if not wait_for_server_active(conn, srv): |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 572 | msg = "Server {0} fails to start. Kill it and try again" |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 573 | logger.debug(msg.format(srv)) |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 574 | conn.nova.servers.delete(srv) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 575 | |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 576 | try: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 577 | for _ in Timeout(delete_timeout, "Server {0} delete timeout".format(srv.id)): |
| 578 | srv = conn.nova.servers.get(srv.id) |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 579 | except NotFound: |
| 580 | pass |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 581 | else: |
| 582 | break |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 583 | else: |
| 584 | raise RuntimeError("Failed to start server".format(srv.id)) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 585 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 586 | if vol_sz is not None: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 587 | vol = create_volume(conn, vol_sz, name) |
| 588 | conn.nova.volumes.create_server_volume(srv.id, vol.id, None) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 589 | |
| 590 | if flt_ip is Allocate: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 591 | flt_ip = conn.nova.floating_ips.create(pool) |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 592 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 593 | if flt_ip is not None: |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 594 | srv.add_floating_ip(flt_ip) |
Yulia Portnova | 0e64ea2 | 2015-03-20 17:27:22 +0200 | [diff] [blame] | 595 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 596 | return flt_ip.ip, conn.nova.servers.get(srv.id) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 597 | |
| 598 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 599 | def clear_nodes(conn: OSConnection, |
| 600 | ids: List[int] = None, |
| 601 | name_templ: str = None, |
| 602 | max_server_delete_time: int = 120): |
koder aka kdanilov | 765920a | 2016-04-12 00:35:48 +0300 | [diff] [blame] | 603 | try: |
| 604 | def need_delete(srv): |
| 605 | if name_templ is not None: |
| 606 | return re.match(name_templ.format("\\d+"), srv.name) is not None |
| 607 | else: |
| 608 | return srv.id in ids |
| 609 | |
| 610 | volumes_to_delete = [] |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 611 | for vol in conn.cinder.volumes.list(): |
koder aka kdanilov | 765920a | 2016-04-12 00:35:48 +0300 | [diff] [blame] | 612 | for attachment in vol.attachments: |
| 613 | if attachment['server_id'] in ids: |
| 614 | volumes_to_delete.append(vol) |
| 615 | break |
| 616 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 617 | still_alive = set() |
| 618 | for srv in conn.nova.servers.list(): |
koder aka kdanilov | 765920a | 2016-04-12 00:35:48 +0300 | [diff] [blame] | 619 | if need_delete(srv): |
| 620 | logger.debug("Deleting server {0}".format(srv.name)) |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 621 | conn.nova.servers.delete(srv) |
| 622 | still_alive.add(srv.id) |
koder aka kdanilov | 765920a | 2016-04-12 00:35:48 +0300 | [diff] [blame] | 623 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 624 | if still_alive: |
| 625 | logger.debug("Waiting till all servers are actually deleted") |
| 626 | tout = Timeout(max_server_delete_time, no_exc=True) |
| 627 | while tout.tick() and still_alive: |
| 628 | all_id = set(srv.id for srv in conn.nova.servers.list()) |
| 629 | still_alive = still_alive.intersection(all_id) |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 630 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 631 | if still_alive: |
| 632 | logger.warning("Failed to remove servers {}. ".format(",".join(still_alive)) + |
| 633 | "You, probably, need to remove them manually (and volumes as well)") |
| 634 | return |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 635 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 636 | if volumes_to_delete: |
| 637 | logger.debug("Deleting volumes") |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 638 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 639 | # wait till vm actually deleted |
| 640 | |
| 641 | # logger.warning("Volume deletion commented out") |
| 642 | for vol in volumes_to_delete: |
| 643 | logger.debug("Deleting volume " + vol.display_name) |
| 644 | conn.cinder.volumes.delete(vol) |
| 645 | |
| 646 | logger.debug("Clearing complete (yet some volumes may still be deleting)") |
| 647 | except Exception: |
koder aka kdanilov | 765920a | 2016-04-12 00:35:48 +0300 | [diff] [blame] | 648 | logger.exception("During removing servers. " + |
| 649 | "You, probably, need to remove them manually") |