blob: 9ee26a6edba166e2d724e6cb1dd7865aec7a231b [file] [log] [blame]
Dennis Dmitrievf5f2e602017-11-03 15:36:19 +02001# Copyright 2019 Mirantis, Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15import os
16import netaddr
17import yaml
18
19from devops.helpers import helpers
20from devops.helpers.helpers import ssh_client
21from retry import retry
22
23from cached_property import cached_property
24
25from heatclient import client as heatclient
26from heatclient import exc as heat_exceptions
27from heatclient.common import template_utils
28from keystoneauth1.identity import v3 as keystone_v3
29from keystoneauth1 import session as keystone_session
30
31import requests
32from requests.packages.urllib3.exceptions import InsecureRequestWarning
33
34from oslo_config import cfg
35from paramiko.ssh_exception import (
36 AuthenticationException,
37 BadAuthenticationType)
38
39from tcp_tests import settings
40from tcp_tests import settings_oslo
41from tcp_tests.helpers import exceptions
42from tcp_tests import logger
43
44LOG = logger.logger
45
46EXPECTED_STACK_STATUS = "CREATE_COMPLETE"
47BAD_STACK_STATUSES = ["CREATE_FAILED"]
48
49# Disable multiple notifications like:
50# "InsecureRequestWarning: Unverified HTTPS request is being made."
51requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
52
53
54class EnvironmentManagerHeat(object):
55 """Class-helper for creating VMs via devops environments"""
56
57 __config = None
58
59 # Do not use self.__heatclient directly! Use properties
60 # for necessary resources with catching HTTPUnauthorized exception
61 __heatclient = None
62
63 def __init__(self, config=None):
64 """Create/connect to the Heat stack with test environment
65
66 :param config: oslo.config object
67 :param config.hardware.heat_version: Heat version
68 :param config.hardware.os_auth_url: OS auth URL to access heat
69 :param config.hardware.os_username: OS username
70 :param config.hardware.os_password: OS password
71 :param config.hardware.os_project_name: OS tenant name
72 """
73 self.__config = config
74
75 if not self.__config.hardware.heat_stack_name:
76 self.__config.hardware.heat_stack_name = settings.ENV_NAME
77
78 self.__init_heatclient()
79
80 try:
81 stack_status = self._current_stack.stack_status
82 if stack_status != EXPECTED_STACK_STATUS:
83 raise exceptions.EnvironmentWrongStatus(
84 self.__config.hardware.heat_stack_name,
85 EXPECTED_STACK_STATUS,
86 stack_status
87 )
88 LOG.info("Heat stack '{0}' already exists".format(
89 self.__config.hardware.heat_stack_name))
90 except heat_exceptions.HTTPNotFound:
91 self._create_environment()
92 LOG.info("Heat stack '{0}' created".format(
93 self.__config.hardware.heat_stack_name))
94
95 self.set_address_pools_config()
96 self.set_dhcp_ranges_config()
97
98 @cached_property
99 def _keystone_session(self):
100 keystone_auth = keystone_v3.Password(
101 auth_url=settings.OS_AUTH_URL,
102 username=settings.OS_USERNAME,
103 password=settings.OS_PASSWORD,
104 project_name=settings.OS_PROJECT_NAME,
Dennis Dmitrievc902ad82019-04-12 13:41:30 +0300105 user_domain_name=settings.OS_USER_DOMAIN_NAME,
Dennis Dmitrievf5f2e602017-11-03 15:36:19 +0200106 project_domain_name='Default')
107 return keystone_session.Session(auth=keystone_auth, verify=False)
108
109 def __init_heatclient(self):
110 token = self._keystone_session.get_token()
111 endpoint_url = self._keystone_session.get_endpoint(
112 service_type='orchestration', endpoint_type='publicURL')
113 self.__heatclient = heatclient.Client(
114 version=settings.OS_HEAT_VERSION, endpoint=endpoint_url,
115 token=token, insecure=True)
116
117 @property
118 def _current_stack(self):
119 return self.__stacks.get(
120 self.__config.hardware.heat_stack_name)
121
122 @property
123 def __stacks(self):
124 try:
125 return self.__heatclient.stacks
126 except heat_exceptions.HTTPUnauthorized:
127 LOG.warning("Authorization token outdated, refreshing")
128 self.__init_heatclient()
129 return self.__heatclient.stacks
130
131 @property
132 def __resources(self):
133 try:
134 return self.__heatclient.resources
135 except heat_exceptions.HTTPUnauthorized:
136 LOG.warning("Authorization token outdated, refreshing")
137 self.__init_heatclient()
138 return self.__heatclient.resources
139
Dennis Dmitriev4015adc2019-04-15 18:33:44 +0300140 def __get_stack_parent(self, stack_id, stacks):
141 """Find the parent ID of the specified stack_id in the 'stacks' list"""
142 for stack in stacks:
143 if stack_id == stack.id:
144 if stack.parent:
145 return self.__get_stack_parent(stack.parent, stacks)
146 else:
147 return stack.id
148 raise Exception("stack with ID {} not found!".format(stack_id))
149
150 @property
151 def __nested_resources(self):
152 resources = []
153 stacks = [s for s in self.__stacks.list(show_nested=True)]
154 current_stack_id = self._current_stack.id
155 for stack in stacks:
156 parent_stack_id = self.__get_stack_parent(stack.id, stacks)
157 if parent_stack_id == current_stack_id:
158 # Add resources to list
159 LOG.info("Get resources from stack {0}"
160 .format(stack.stack_name))
161 resources.extend([
162 res for res in self.__resources.list(stack.id)
163 ])
164 LOG.info("Found {0} resources".format(len(resources)))
165 return resources
166
Dennis Dmitrievf5f2e602017-11-03 15:36:19 +0200167 def _get_resources_by_type(self, resource_type):
168 res = []
Dennis Dmitriev4015adc2019-04-15 18:33:44 +0300169 for item in self.__nested_resources:
Dennis Dmitrievf5f2e602017-11-03 15:36:19 +0200170 if item.resource_type == resource_type:
171 resource = self.__resources.get(
Dennis Dmitriev4015adc2019-04-15 18:33:44 +0300172 item.stack_name,
Dennis Dmitrievf5f2e602017-11-03 15:36:19 +0200173 item.resource_name)
174 res.append(resource)
175 return res
176
177 @cached_property
178 def _nodes(self):
179 """Get list of nodenames from heat
180
181 Returns list of dicts.
182 Example:
183 - name: cfg01
184 roles:
185 - salt_master
186 addresses: # Optional. May be an empty dict
187 admin-pool01: p.p.p.202
188 - name: ctl01
189 roles:
190 - salt_minion
191 - openstack_controller
192 - openstack_messaging
193 - openstack_database
194 addresses: {} # Optional. May be an empty dict
195
196 'name': taken from heat template resource's ['name'] parameter
197 'roles': a list taken from resource's ['metadata']['roles'] parameter
198 """
199 address_pools = self._address_pools
200 nodes = []
201 for heat_node in self._get_resources_by_type("OS::Nova::Server"):
202 # addresses will have the following dict structure:
203 # {'admin-pool01': <floating_ip1>,
204 # 'private-pool01': <floating_ip2>,
205 # 'external-pool01': <floating_ip3>
206 # }
207 # , where key is one of roles from OS::Neutron::Subnet,
208 # and value is a floating IP associated to the fixed IP
209 # in this subnet (if exists).
210 # If no floating IPs associated to the server,
211 # then addresses will be an empty list.
212 addresses = {}
213 for network in heat_node.attributes['addresses']:
214 fixed = None
215 floating = None
216 for address in heat_node.attributes['addresses'][network]:
217 addr_type = address['OS-EXT-IPS:type']
218 if addr_type == 'fixed':
219 fixed = address['addr']
220 elif addr_type == 'floating':
221 floating = address['addr']
222 else:
223 LOG.error("Unexpected OS-EXT-IPS:type={0} "
224 "in node '{1}' for network '{2}'"
225 .format(addr_type,
226 heat_node.attributes['name'],
227 network))
Dennis Dmitriev4015adc2019-04-15 18:33:44 +0300228 if fixed is None and floating is None:
Dennis Dmitrievf5f2e602017-11-03 15:36:19 +0200229 LOG.error("Unable to determine the correct IP address "
230 "in node '{0}' for network '{1}'"
231 .format(heat_node.attributes['name'], network))
232 continue
233 # Check which address pool has the fixed address, and set
234 # the floating address as the access to this address pool.
235 for address_pool in address_pools:
236 pool_net = netaddr.IPNetwork(address_pool['cidr'])
237 if fixed in pool_net:
238 for role in address_pool['roles']:
Dennis Dmitrievc902ad82019-04-12 13:41:30 +0300239 # addresses[role] = floating
240 # Use fixed addresses for SSH access
241 addresses[role] = fixed
Dennis Dmitriev4015adc2019-04-15 18:33:44 +0300242 if 'metadata' not in heat_node.attributes or \
243 'roles' not in heat_node.attributes['metadata']:
244 raise Exception("Node {} doesn't have metadata:roles:[...,...]"
245 .format(heat_node.attributes['name']))
Dennis Dmitrievf5f2e602017-11-03 15:36:19 +0200246
247 nodes.append({
248 'name': heat_node.attributes['name'],
249 'roles': yaml.load(heat_node.attributes['metadata']['roles']),
250 'addresses': addresses,
251 })
252 return nodes
253
254 @cached_property
255 def _address_pools(self):
256 """Get address pools from subnets OS::Neutron::Subnet
257
258 Returns list of dicts.
259 Example:
260 - roles:
261 - admin-pool01
262 cidr: x.x.x.x/y
263 start: x.x.x.2
264 end: x.x.x.254
265 gateway: x.x.x.1 # or None
266 """
267 pools = []
268 for heat_subnet in self._get_resources_by_type("OS::Neutron::Subnet"):
269 pools.append({
270 'roles': heat_subnet.attributes['tags'],
271 'cidr': heat_subnet.attributes['cidr'],
272 'gateway': heat_subnet.attributes['gateway_ip'],
273 'start': heat_subnet.attributes[
274 'allocation_pools'][0]['start'],
275 'end': heat_subnet.attributes['allocation_pools'][0]['end'],
276 })
277 return pools
278
279 def _get_nodes_by_roles(self, roles=None):
280 nodes = []
281 if roles is None:
282 return self._nodes
283
284 for node in self._nodes:
285 if set(node['roles']).intersection(set(roles)):
286 nodes.append(node)
287 return nodes
288
289 def get_ssh_data(self, roles=None):
290 """Generate ssh config for Underlay
291
292 :param roles: list of strings
293 """
294 if roles is None:
295 raise Exception("No roles specified for the environment!")
296
297 config_ssh = []
298 for d_node in self._get_nodes_by_roles(roles=roles):
299 for pool_name in d_node['addresses']:
300 ssh_data = {
301 'node_name': d_node['name'],
302 'minion_id': d_node['name'],
303 'roles': d_node['roles'],
304 'address_pool': pool_name,
305 'host': d_node['addresses'][pool_name],
306 'login': settings.SSH_NODE_CREDENTIALS['login'],
307 'password': settings.SSH_NODE_CREDENTIALS['password'],
308 'keys': [k['private']
309 for k in self.__config.underlay.ssh_keys]
310 }
311 config_ssh.append(ssh_data)
312 return config_ssh
313
314 def _get_resources_with_wrong_status(self):
315 res = []
Dennis Dmitriev4015adc2019-04-15 18:33:44 +0300316 for item in self.__nested_resources:
Dennis Dmitrievf5f2e602017-11-03 15:36:19 +0200317 if item.resource_status in BAD_STACK_STATUSES:
318 res.append({
319 'resource_name': item.resource_name,
320 'resource_status': item.resource_status,
321 'resource_status_reason': item.resource_status_reason,
322 'resource_type': item.resource_type
323 })
324 wrong_resources = '\n'.join([
325 "*** Heat stack resource '{0}' ({1}) has wrong status '{2}': {3}"
326 .format(item['resource_name'],
327 item['resource_type'],
328 item['resource_status'],
329 item['resource_status_reason'])
330 for item in res
331 ])
332 return wrong_resources
333
334 def wait_of_stack_status(self, status, delay=30, tries=60):
335
336 @retry(exceptions.EnvironmentWrongStatus, delay=delay, tries=tries)
337 def wait():
338 st = self._current_stack.stack_status
339 if st == status:
340 return
341 elif st in BAD_STACK_STATUSES:
342 wrong_resources = self._get_resources_with_wrong_status()
343 raise exceptions.EnvironmentBadStatus(
344 self.__config.hardware.heat_stack_name,
345 status,
346 st,
347 wrong_resources
348 )
349 else:
350 LOG.info("Stack {0} status: {1}".format(
351 self.__config.hardware.heat_stack_name, st))
352 raise exceptions.EnvironmentWrongStatus(
353 self.__config.hardware.heat_stack_name,
354 status,
355 st
356 )
357 LOG.info("Waiting for stack '{0}' status <{1}>".format(
358 self.__config.hardware.heat_stack_name, status))
359 wait()
360
361 def revert_snapshot(self, name):
362 """Revert snapshot by name
363
364 - Revert the heat snapshot in the environment
365 - Try to reload 'config' object from a file 'config_<name>.ini'
366 If the file not found, then pass with defaults.
367 - Set <name> as the current state of the environment after reload
368
369 :param name: string
370 """
371 LOG.info("Reading INI config (without reverting env to snapshot) "
372 "named '{0}'".format(name))
373
374 try:
375 test_config_path = self._get_snapshot_config_name(name)
376 settings_oslo.reload_snapshot_config(self.__config,
377 test_config_path)
378 except cfg.ConfigFilesNotFoundError as conf_err:
379 LOG.error("Config file(s) {0} not found!".format(
380 conf_err.config_files))
381
382 self.__config.hardware.current_snapshot = name
383
384 def create_snapshot(self, name, *args, **kwargs):
385 """Create named snapshot of current env.
386
387 - Create a snapshot for the environment
388 - Save 'config' object to a file 'config_<name>.ini'
389
390 :name: string
391 """
Dennis Dmitrievfa1774a2019-05-28 15:27:44 +0300392 if not settings.MAKE_SNAPSHOT_STAGES:
393 msg = ("[ SKIP snapshot '{0}' because MAKE_SNAPSHOT_STAGES=false ]"
394 .format(name))
395 LOG.info("\n\n{0}\n{1}".format(msg, '*' * len(msg)))
396 return
397
Dennis Dmitrievf5f2e602017-11-03 15:36:19 +0200398 LOG.info("Store INI config (without env snapshot) named '{0}'"
399 .format(name))
400 self.__config.hardware.current_snapshot = name
401 settings_oslo.save_config(self.__config,
402 name,
403 self.__config.hardware.heat_stack_name)
404
405 def _get_snapshot_config_name(self, snapshot_name):
406 """Get config name for the environment"""
407 env_name = self.__config.hardware.heat_stack_name
408 if env_name is None:
409 env_name = 'config'
410 test_config_path = os.path.join(
411 settings.LOGS_DIR, '{0}_{1}.ini'.format(env_name, snapshot_name))
412 return test_config_path
413
414 def has_snapshot(self, name):
415 # Heat doesn't support live snapshots, so just
416 # check if an INI file was created for this environment,
417 # assuming that the environment has the configuration
418 # described in this INI.
419 return self.has_snapshot_config(name)
420
421 def has_snapshot_config(self, name):
422 test_config_path = self._get_snapshot_config_name(name)
423 return os.path.isfile(test_config_path)
424
425 def start(self, underlay_node_roles, timeout=480):
426 """Start environment"""
427 LOG.warning("HEAT Manager doesn't support start environment feature. "
428 "Waiting for finish the bootstrap process on the nodes "
429 "with accessible SSH")
430
431 check_cloudinit_started = '[ -f /is_cloud_init_started ]'
432 check_cloudinit_finished = ('[ -f /is_cloud_init_finished ] || '
433 '[ -f /var/log/mcp/.bootstrap_done ]')
434 check_cloudinit_failed = 'cat /is_cloud_init_failed'
435 passed = {}
436 for node in self._get_nodes_by_roles(roles=underlay_node_roles):
437
438 try:
439 node_ip = self.node_ip(node)
440 except exceptions.EnvironmentNodeAccessError:
441 LOG.warning("Node {0} doesn't have accessible IP address"
442 ", skipping".format(node['name']))
443 continue
444
445 LOG.info("Waiting for SSH on node '{0}' / {1} ...".format(
446 node['name'], node_ip))
447
448 def _ssh_check(host,
449 port,
450 username=settings.SSH_NODE_CREDENTIALS['login'],
451 password=settings.SSH_NODE_CREDENTIALS['password'],
452 timeout=0):
453 try:
454 ssh = ssh_client.SSHClient(
455 host=host, port=port,
456 auth=ssh_client.SSHAuth(
457 username=username,
458 password=password))
459
460 # If '/is_cloud_init_started' exists, then wait for
461 # the flag /is_cloud_init_finished
462 if ssh.execute(check_cloudinit_started)['exit_code'] == 0:
463 result = ssh.execute(check_cloudinit_failed)
464 if result['exit_code'] == 0:
465 raise exceptions.EnvironmentNodeIsNotStarted(
466 "{0}:{1}".format(host, port),
467 result.stdout_str)
468
469 status = ssh.execute(
470 check_cloudinit_finished)['exit_code'] == 0
471 # Else, just wait for SSH
472 else:
473 status = ssh.execute('echo ok')['exit_code'] == 0
474 return status
475
476 except (AuthenticationException, BadAuthenticationType):
477 return True
478 except Exception:
479 return False
480
481 def _ssh_wait(host,
482 port,
483 username=settings.SSH_NODE_CREDENTIALS['login'],
484 password=settings.SSH_NODE_CREDENTIALS['password'],
485 timeout=0):
486
487 if host in passed and passed[host] >= 2:
488 # host already passed the check
489 return True
490
491 for node in self._get_nodes_by_roles(
492 roles=underlay_node_roles):
493 ip = node_ip
494 if ip not in passed:
495 passed[ip] = 0
496 if _ssh_check(ip, port):
497 passed[ip] += 1
498 else:
499 passed[ip] = 0
500
501 helpers.wait(
502 lambda: _ssh_wait(node_ip, 22),
503 timeout=timeout,
504 timeout_msg="Node '{}' didn't open SSH in {} sec".format(
505 node['name'], timeout
506 )
507 )
508 LOG.info('Heat stack "{0}" ready'
509 .format(self.__config.hardware.heat_stack_name))
510
511 def _create_environment(self):
512 tpl_files, template = template_utils.get_template_contents(
513 self.__config.hardware.heat_conf_path)
514 env_files_list = []
515 env_files, env = (
516 template_utils.process_multiple_environments_and_files(
517 env_paths=[self.__config.hardware.heat_env_path],
518 env_list_tracker=env_files_list))
519
520 fields = {
521 'stack_name': self.__config.hardware.heat_stack_name,
522 'template': template,
523 'files': dict(list(tpl_files.items()) + list(env_files.items())),
524 'environment': env,
Dennis Dmitrievc902ad82019-04-12 13:41:30 +0300525 'parameters': {
526 'mcp_version': settings.MCP_VERSION,
527 'env_name': settings.ENV_NAME,
528 }
Dennis Dmitrievf5f2e602017-11-03 15:36:19 +0200529 }
530
531 if env_files_list:
532 fields['environment_files'] = env_files_list
533
534 self.__stacks.create(**fields)
Dmitry Tyzhnenko89728632019-05-23 14:22:59 +0300535 self.wait_of_stack_status(EXPECTED_STACK_STATUS, tries=140)
Dennis Dmitrievf5f2e602017-11-03 15:36:19 +0200536 LOG.info("Stack '{0}' created"
537 .format(self.__config.hardware.heat_stack_name))
538
539 def stop(self):
540 """Stop environment"""
541 LOG.warning("HEAT Manager doesn't support stop environment feature")
542 pass
543
544# TODO(ddmitriev): add all Environment methods
545 @staticmethod
546 def node_ip(node, address_pool_name='admin-pool01'):
547 """Determine node's IP
548
549 :param node: a dict element from the self._nodes
550 :return: string
551 """
552 if address_pool_name in node['addresses']:
553 addr = node['addresses'][address_pool_name]
554 LOG.debug('{0} IP= {1}'.format(node['name'], addr))
555 return addr
556 else:
557 raise exceptions.EnvironmentNodeAccessError(
558 node['name'],
559 "No addresses available for the subnet {0}"
560 .format(address_pool_name))
561
562 def set_address_pools_config(self):
563 """Store address pools CIDRs in config object"""
564 for ap in self._address_pools:
565 for role in ap['roles']:
566 self.__config.underlay.address_pools[role] = ap['cidr']
567
568 def set_dhcp_ranges_config(self):
569 """Store DHCP ranges in config object"""
570 for ap in self._address_pools:
571 for role in ap['roles']:
572 self.__config.underlay.dhcp_ranges[role] = {
573 "cidr": ap['cidr'],
574 "start": ap['start'],
575 "end": ap['end'],
576 "gateway": ap['gateway'],
577 }
578
579 def wait_for_node_state(self, node_name, state, timeout):
580 raise NotImplementedError()
581
582 def warm_shutdown_nodes(self, underlay, nodes_prefix, timeout=600):
583 raise NotImplementedError()
584
585 def warm_restart_nodes(self, underlay, nodes_prefix, timeout=600):
586 raise NotImplementedError()
587
588 @property
589 def slave_nodes(self):
590 raise NotImplementedError()