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