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