koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 1 | import re |
| 2 | import os |
| 3 | import time |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 4 | import os.path |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 5 | import logging |
koder aka kdanilov | 4e9f3ed | 2015-04-14 11:26:12 +0300 | [diff] [blame] | 6 | import subprocess |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 7 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 8 | from concurrent.futures import ThreadPoolExecutor |
| 9 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 10 | from novaclient.exceptions import NotFound |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 11 | from novaclient.client import Client as n_client |
| 12 | from cinderclient.v1.client import Client as c_client |
| 13 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 14 | import wally |
| 15 | from wally.discover import Node |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 16 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 17 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 18 | logger = logging.getLogger("wally.vms") |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 19 | |
| 20 | |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 21 | STORED_OPENSTACK_CREDS = None |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 22 | NOVA_CONNECTION = None |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 23 | CINDER_CONNECTION = None |
| 24 | |
| 25 | |
| 26 | def ostack_get_creds(): |
| 27 | if STORED_OPENSTACK_CREDS is None: |
| 28 | env = os.environ.get |
| 29 | name = env('OS_USERNAME') |
| 30 | passwd = env('OS_PASSWORD') |
| 31 | tenant = env('OS_TENANT_NAME') |
| 32 | auth_url = env('OS_AUTH_URL') |
| 33 | return name, passwd, tenant, auth_url |
| 34 | else: |
| 35 | return STORED_OPENSTACK_CREDS |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 36 | |
| 37 | |
| 38 | def nova_connect(name=None, passwd=None, tenant=None, auth_url=None): |
| 39 | global NOVA_CONNECTION |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 40 | global STORED_OPENSTACK_CREDS |
| 41 | |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 42 | if NOVA_CONNECTION is None: |
| 43 | if name is None: |
| 44 | name, passwd, tenant, auth_url = ostack_get_creds() |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 45 | else: |
| 46 | STORED_OPENSTACK_CREDS = (name, passwd, tenant, auth_url) |
| 47 | |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 48 | NOVA_CONNECTION = n_client('1.1', name, passwd, tenant, auth_url) |
| 49 | return NOVA_CONNECTION |
| 50 | |
| 51 | |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 52 | def cinder_connect(name=None, passwd=None, tenant=None, auth_url=None): |
| 53 | global CINDER_CONNECTION |
| 54 | global STORED_OPENSTACK_CREDS |
| 55 | |
| 56 | if CINDER_CONNECTION is None: |
| 57 | if name is None: |
| 58 | name, passwd, tenant, auth_url = ostack_get_creds() |
| 59 | else: |
| 60 | STORED_OPENSTACK_CREDS = (name, passwd, tenant, auth_url) |
| 61 | CINDER_CONNECTION = c_client(name, passwd, tenant, auth_url) |
| 62 | return CINDER_CONNECTION |
| 63 | |
| 64 | |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 65 | def nova_disconnect(): |
| 66 | global NOVA_CONNECTION |
| 67 | if NOVA_CONNECTION is not None: |
| 68 | NOVA_CONNECTION.close() |
| 69 | NOVA_CONNECTION = None |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 70 | |
| 71 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 72 | def prepare_os_subpr(name=None, passwd=None, tenant=None, auth_url=None): |
koder aka kdanilov | 4e9f3ed | 2015-04-14 11:26:12 +0300 | [diff] [blame] | 73 | if name is None: |
| 74 | name, passwd, tenant, auth_url = ostack_get_creds() |
| 75 | |
| 76 | params = { |
| 77 | 'OS_USERNAME': name, |
| 78 | 'OS_PASSWORD': passwd, |
| 79 | 'OS_TENANT_NAME': tenant, |
| 80 | 'OS_AUTH_URL': auth_url |
| 81 | } |
| 82 | |
koder aka kdanilov | 6b1341a | 2015-04-21 22:44:21 +0300 | [diff] [blame^] | 83 | params_s = " ".join("{0}={1}".format(k, v) for k, v in params.items()) |
koder aka kdanilov | 4e9f3ed | 2015-04-14 11:26:12 +0300 | [diff] [blame] | 84 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 85 | spath = os.path.dirname(wally.__file__) |
| 86 | spath = os.path.dirname(spath) |
| 87 | spath = os.path.join(spath, 'scripts/prepare.sh') |
| 88 | |
| 89 | cmd_templ = "env {params} bash {spath} >/dev/null" |
| 90 | cmd = cmd_templ.format(params=params_s, spath=spath) |
koder aka kdanilov | 4e9f3ed | 2015-04-14 11:26:12 +0300 | [diff] [blame] | 91 | subprocess.call(cmd, shell=True) |
| 92 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 93 | |
| 94 | def prepare_os(nova, params): |
| 95 | allow_ssh(nova, params['security_group']) |
| 96 | |
| 97 | shed_ids = [] |
| 98 | for shed_group in params['schedulers_groups']: |
| 99 | shed_ids.append(get_or_create_aa_group(nova, shed_group)) |
| 100 | |
| 101 | create_keypair(nova, |
| 102 | params['keypair_name'], |
| 103 | params['pub_key_path'], |
| 104 | params['priv_key_path']) |
| 105 | |
| 106 | create_image(nova, params['image']['name'], |
| 107 | params['image']['url']) |
| 108 | |
| 109 | create_flavor(nova, **params['flavor']) |
koder aka kdanilov | 4e9f3ed | 2015-04-14 11:26:12 +0300 | [diff] [blame] | 110 | |
| 111 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 112 | def get_or_create_aa_group(nova, name): |
| 113 | try: |
| 114 | group = nova.server_groups.find(name=name) |
| 115 | except NotFound: |
| 116 | group = nova.server_groups.create({'name': name, |
| 117 | 'policies': ['anti-affinity']}) |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 118 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 119 | return group.id |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 120 | |
| 121 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 122 | def allow_ssh(nova, group_name): |
| 123 | try: |
| 124 | secgroup = nova.security_groups.find(name=group_name) |
| 125 | except NotFound: |
| 126 | secgroup = nova.security_groups.create(group_name, |
| 127 | "allow ssh/ping to node") |
| 128 | |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 129 | nova.security_group_rules.create(secgroup.id, |
| 130 | ip_protocol="tcp", |
| 131 | from_port="22", |
| 132 | to_port="22", |
| 133 | cidr="0.0.0.0/0") |
| 134 | |
| 135 | nova.security_group_rules.create(secgroup.id, |
| 136 | ip_protocol="icmp", |
| 137 | from_port=-1, |
| 138 | cidr="0.0.0.0/0", |
| 139 | to_port=-1) |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 140 | return secgroup.id |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 141 | |
| 142 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 143 | def create_image(nova, name, url): |
| 144 | pass |
| 145 | |
| 146 | |
| 147 | def create_flavor(nova, name, **params): |
| 148 | pass |
| 149 | |
| 150 | |
| 151 | def create_keypair(nova, name, pub_key_path, priv_key_path): |
| 152 | try: |
| 153 | nova.keypairs.find(name=name) |
| 154 | except NotFound: |
| 155 | if os.path.exists(pub_key_path): |
| 156 | with open(pub_key_path) as pub_key_fd: |
| 157 | return nova.keypairs.create(name, pub_key_fd.read()) |
| 158 | else: |
| 159 | key = nova.keypairs.create(name) |
| 160 | |
| 161 | with open(priv_key_path, "w") as priv_key_fd: |
| 162 | priv_key_fd.write(key.private_key) |
| 163 | |
| 164 | with open(pub_key_path, "w") as pub_key_fd: |
| 165 | pub_key_fd.write(key.public_key) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 166 | |
| 167 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 168 | def create_volume(size, name): |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 169 | cinder = cinder_connect() |
| 170 | # vol_id = "2974f227-8755-4333-bcae-cd9693cd5d04" |
| 171 | # logger.warning("Reusing volume {0}".format(vol_id)) |
| 172 | # vol = cinder.volumes.get(vol_id) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 173 | vol = cinder.volumes.create(size=size, display_name=name) |
| 174 | err_count = 0 |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 175 | |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 176 | while vol.status != 'available': |
| 177 | if vol.status == 'error': |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 178 | if err_count == 3: |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 179 | logger.critical("Fail to create volume") |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 180 | raise RuntimeError("Fail to create volume") |
| 181 | else: |
| 182 | err_count += 1 |
| 183 | cinder.volumes.delete(vol) |
| 184 | time.sleep(1) |
| 185 | vol = cinder.volumes.create(size=size, display_name=name) |
| 186 | continue |
| 187 | time.sleep(1) |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 188 | vol = cinder.volumes.get(vol.id) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 189 | return vol |
| 190 | |
| 191 | |
| 192 | def wait_for_server_active(nova, server, timeout=240): |
| 193 | t = time.time() |
| 194 | while True: |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 195 | time.sleep(1) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 196 | sstate = getattr(server, 'OS-EXT-STS:vm_state').lower() |
| 197 | |
| 198 | if sstate == 'active': |
| 199 | return True |
| 200 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 201 | if sstate == 'error': |
| 202 | return False |
| 203 | |
| 204 | if time.time() - t > timeout: |
| 205 | return False |
| 206 | |
| 207 | server = nova.servers.get(server) |
| 208 | |
| 209 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 210 | class Allocate(object): |
| 211 | pass |
| 212 | |
| 213 | |
| 214 | def get_floating_ips(nova, pool, amount): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 215 | ip_list = nova.floating_ips.list() |
| 216 | |
| 217 | if pool is not None: |
| 218 | ip_list = [ip for ip in ip_list if ip.pool == pool] |
| 219 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 220 | 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] | 221 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 222 | |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 223 | def launch_vms(params): |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 224 | logger.debug("Starting new nodes on openstack") |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 225 | params = params.copy() |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 226 | count = params.pop('count') |
| 227 | |
| 228 | if isinstance(count, basestring): |
| 229 | assert count.startswith("x") |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 230 | lst = NOVA_CONNECTION.services.list(binary='nova-compute') |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 231 | srv_count = len([srv for srv in lst if srv.status == 'enabled']) |
| 232 | count = srv_count * int(count[1:]) |
| 233 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 234 | srv_params = "img: {image[name]}, flavor: {flavor[name]}".format(**params) |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 235 | msg_templ = "Will start {0} servers with next params: {1}" |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 236 | logger.info(msg_templ.format(count, srv_params)) |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 237 | vm_creds = params.pop('creds') |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 238 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 239 | params = params.copy() |
| 240 | |
| 241 | params['img_name'] = params['image']['name'] |
| 242 | params['flavor_name'] = params['flavor']['name'] |
| 243 | |
| 244 | del params['image'] |
| 245 | del params['flavor'] |
| 246 | del params['scheduler_group_name'] |
| 247 | private_key_path = params.pop('private_key_path') |
| 248 | |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 249 | for ip, os_node in create_vms_mt(NOVA_CONNECTION, count, **params): |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 250 | conn_uri = vm_creds.format(ip=ip, private_key_path=private_key_path) |
| 251 | yield Node(conn_uri, []), os_node.id |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 252 | |
| 253 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 254 | 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] | 255 | flavor_name, vol_sz=None, network_zone_name=None, |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 256 | flt_ip_pool=None, name_templ='wally-{id}', |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 257 | scheduler_hints=None, security_group=None): |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 258 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 259 | with ThreadPoolExecutor(max_workers=16) as executor: |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 260 | if network_zone_name is not None: |
| 261 | network_future = executor.submit(nova.networks.find, |
| 262 | label=network_zone_name) |
| 263 | else: |
| 264 | network_future = None |
| 265 | |
| 266 | fl_future = executor.submit(nova.flavors.find, name=flavor_name) |
| 267 | img_future = executor.submit(nova.images.find, name=img_name) |
| 268 | |
| 269 | if flt_ip_pool is not None: |
| 270 | ips_future = executor.submit(get_floating_ips, |
| 271 | nova, flt_ip_pool, amount) |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 272 | logger.debug("Wait for floating ip") |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 273 | ips = ips_future.result() |
| 274 | ips += [Allocate] * (amount - len(ips)) |
| 275 | else: |
| 276 | ips = [None] * amount |
| 277 | |
koder aka kdanilov | 7dec9df | 2015-02-15 21:35:19 -0800 | [diff] [blame] | 278 | logger.debug("Getting flavor object") |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 279 | fl = fl_future.result() |
koder aka kdanilov | 7dec9df | 2015-02-15 21:35:19 -0800 | [diff] [blame] | 280 | logger.debug("Getting image object") |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 281 | img = img_future.result() |
| 282 | |
| 283 | if network_future is not None: |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 284 | logger.debug("Waiting for network results") |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 285 | nics = [{'net-id': network_future.result().id}] |
| 286 | else: |
| 287 | nics = None |
| 288 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 289 | names = [] |
| 290 | for i in range(amount): |
| 291 | names.append(name_templ.format(group=group_name, id=i)) |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 292 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 293 | futures = [] |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 294 | logger.debug("Requesting new vm's") |
koder aka kdanilov | 6e2ae79 | 2015-03-04 18:02:24 -0800 | [diff] [blame] | 295 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 296 | for name, flt_ip in zip(names, ips): |
| 297 | params = (nova, name, keypair_name, img, fl, |
| 298 | nics, vol_sz, flt_ip, scheduler_hints, |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 299 | flt_ip_pool, [security_group]) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 300 | |
| 301 | futures.append(executor.submit(create_vm, *params)) |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 302 | res = [future.result() for future in futures] |
| 303 | logger.debug("Done spawning") |
| 304 | return res |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 305 | |
| 306 | |
| 307 | def create_vm(nova, name, keypair_name, img, |
| 308 | fl, nics, vol_sz=None, |
| 309 | flt_ip=False, |
| 310 | scheduler_hints=None, |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 311 | pool=None, |
| 312 | security_groups=None): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 313 | for i in range(3): |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 314 | srv = nova.servers.create(name, |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 315 | flavor=fl, |
| 316 | image=img, |
| 317 | nics=nics, |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 318 | key_name=keypair_name, |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 319 | scheduler_hints=scheduler_hints, |
| 320 | security_groups=security_groups) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 321 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 322 | if not wait_for_server_active(nova, srv): |
| 323 | msg = "Server {0} fails to start. Kill it and try again" |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 324 | logger.debug(msg.format(srv)) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 325 | nova.servers.delete(srv) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 326 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 327 | for j in range(120): |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 328 | all_id = set(alive_srv.id for alive_srv in nova.servers.list()) |
| 329 | if srv.id not in all_id: |
| 330 | break |
| 331 | time.sleep(1) |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 332 | else: |
| 333 | raise RuntimeError("Server {0} delete timeout".format(srv.id)) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 334 | else: |
| 335 | break |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 336 | else: |
| 337 | raise RuntimeError("Failed to start server".format(srv.id)) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 338 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 339 | if vol_sz is not None: |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 340 | vol = create_volume(vol_sz, name) |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 341 | nova.volumes.create_server_volume(srv.id, vol.id, None) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 342 | |
| 343 | if flt_ip is Allocate: |
| 344 | flt_ip = nova.floating_ips.create(pool) |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 345 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 346 | if flt_ip is not None: |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 347 | srv.add_floating_ip(flt_ip) |
Yulia Portnova | 0e64ea2 | 2015-03-20 17:27:22 +0200 | [diff] [blame] | 348 | |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 349 | return flt_ip.ip, nova.servers.get(srv.id) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 350 | |
| 351 | |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 352 | def clear_nodes(nodes_ids): |
| 353 | clear_all(NOVA_CONNECTION, nodes_ids, None) |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 354 | |
| 355 | |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 356 | def clear_all(nova, ids=None, name_templ=None): |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 357 | |
| 358 | def need_delete(srv): |
| 359 | if name_templ is not None: |
| 360 | return re.match(name_templ.format("\\d+"), srv.name) is not None |
| 361 | else: |
| 362 | return srv.id in ids |
| 363 | |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 364 | volumes_to_delete = [] |
| 365 | cinder = cinder_connect() |
| 366 | for vol in cinder.volumes.list(): |
| 367 | for attachment in vol.attachments: |
| 368 | if attachment['server_id'] in ids: |
| 369 | volumes_to_delete.append(vol) |
| 370 | break |
| 371 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 372 | deleted_srvs = set() |
| 373 | for srv in nova.servers.list(): |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 374 | if need_delete(srv): |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 375 | logger.debug("Deleting server {0}".format(srv.name)) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 376 | nova.servers.delete(srv) |
| 377 | deleted_srvs.add(srv.id) |
| 378 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 379 | count = 0 |
| 380 | while True: |
| 381 | if count % 60 == 0: |
| 382 | logger.debug("Waiting till all servers are actually deleted") |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 383 | all_id = set(srv.id for srv in nova.servers.list()) |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 384 | if len(all_id.intersection(deleted_srvs)) == 0: |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 385 | break |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 386 | count += 1 |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 387 | time.sleep(1) |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 388 | logger.debug("Done, deleting volumes") |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 389 | |
| 390 | # wait till vm actually deleted |
| 391 | |
koder aka kdanilov | 6b1341a | 2015-04-21 22:44:21 +0300 | [diff] [blame^] | 392 | # logger.warning("Volume deletion commented out") |
| 393 | for vol in volumes_to_delete: |
| 394 | logger.debug("Deleting volume " + vol.display_name) |
| 395 | cinder.volumes.delete(vol) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 396 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 397 | logger.debug("Clearing done (yet some volumes may still deleting)") |