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