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