blob: 2d52e00b9d10ffe3ecfabd81b25a60b5f48c5ba2 [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,
105 user_domain_name='Default',
106 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
140 def _get_resources_by_type(self, resource_type):
141 res = []
142 for item in self.__resources.list(
143 self.__config.hardware.heat_stack_name):
144 if item.resource_type == resource_type:
145 resource = self.__resources.get(
146 self.__config.hardware.heat_stack_name,
147 item.resource_name)
148 res.append(resource)
149 return res
150
151 @cached_property
152 def _nodes(self):
153 """Get list of nodenames from heat
154
155 Returns list of dicts.
156 Example:
157 - name: cfg01
158 roles:
159 - salt_master
160 addresses: # Optional. May be an empty dict
161 admin-pool01: p.p.p.202
162 - name: ctl01
163 roles:
164 - salt_minion
165 - openstack_controller
166 - openstack_messaging
167 - openstack_database
168 addresses: {} # Optional. May be an empty dict
169
170 'name': taken from heat template resource's ['name'] parameter
171 'roles': a list taken from resource's ['metadata']['roles'] parameter
172 """
173 address_pools = self._address_pools
174 nodes = []
175 for heat_node in self._get_resources_by_type("OS::Nova::Server"):
176 # addresses will have the following dict structure:
177 # {'admin-pool01': <floating_ip1>,
178 # 'private-pool01': <floating_ip2>,
179 # 'external-pool01': <floating_ip3>
180 # }
181 # , where key is one of roles from OS::Neutron::Subnet,
182 # and value is a floating IP associated to the fixed IP
183 # in this subnet (if exists).
184 # If no floating IPs associated to the server,
185 # then addresses will be an empty list.
186 addresses = {}
187 for network in heat_node.attributes['addresses']:
188 fixed = None
189 floating = None
190 for address in heat_node.attributes['addresses'][network]:
191 addr_type = address['OS-EXT-IPS:type']
192 if addr_type == 'fixed':
193 fixed = address['addr']
194 elif addr_type == 'floating':
195 floating = address['addr']
196 else:
197 LOG.error("Unexpected OS-EXT-IPS:type={0} "
198 "in node '{1}' for network '{2}'"
199 .format(addr_type,
200 heat_node.attributes['name'],
201 network))
202 if fixed is None or floating is None:
203 LOG.error("Unable to determine the correct IP address "
204 "in node '{0}' for network '{1}'"
205 .format(heat_node.attributes['name'], network))
206 continue
207 # Check which address pool has the fixed address, and set
208 # the floating address as the access to this address pool.
209 for address_pool in address_pools:
210 pool_net = netaddr.IPNetwork(address_pool['cidr'])
211 if fixed in pool_net:
212 for role in address_pool['roles']:
213 addresses[role] = floating
214
215 nodes.append({
216 'name': heat_node.attributes['name'],
217 'roles': yaml.load(heat_node.attributes['metadata']['roles']),
218 'addresses': addresses,
219 })
220 return nodes
221
222 @cached_property
223 def _address_pools(self):
224 """Get address pools from subnets OS::Neutron::Subnet
225
226 Returns list of dicts.
227 Example:
228 - roles:
229 - admin-pool01
230 cidr: x.x.x.x/y
231 start: x.x.x.2
232 end: x.x.x.254
233 gateway: x.x.x.1 # or None
234 """
235 pools = []
236 for heat_subnet in self._get_resources_by_type("OS::Neutron::Subnet"):
237 pools.append({
238 'roles': heat_subnet.attributes['tags'],
239 'cidr': heat_subnet.attributes['cidr'],
240 'gateway': heat_subnet.attributes['gateway_ip'],
241 'start': heat_subnet.attributes[
242 'allocation_pools'][0]['start'],
243 'end': heat_subnet.attributes['allocation_pools'][0]['end'],
244 })
245 return pools
246
247 def _get_nodes_by_roles(self, roles=None):
248 nodes = []
249 if roles is None:
250 return self._nodes
251
252 for node in self._nodes:
253 if set(node['roles']).intersection(set(roles)):
254 nodes.append(node)
255 return nodes
256
257 def get_ssh_data(self, roles=None):
258 """Generate ssh config for Underlay
259
260 :param roles: list of strings
261 """
262 if roles is None:
263 raise Exception("No roles specified for the environment!")
264
265 config_ssh = []
266 for d_node in self._get_nodes_by_roles(roles=roles):
267 for pool_name in d_node['addresses']:
268 ssh_data = {
269 'node_name': d_node['name'],
270 'minion_id': d_node['name'],
271 'roles': d_node['roles'],
272 'address_pool': pool_name,
273 'host': d_node['addresses'][pool_name],
274 'login': settings.SSH_NODE_CREDENTIALS['login'],
275 'password': settings.SSH_NODE_CREDENTIALS['password'],
276 'keys': [k['private']
277 for k in self.__config.underlay.ssh_keys]
278 }
279 config_ssh.append(ssh_data)
280 return config_ssh
281
282 def _get_resources_with_wrong_status(self):
283 res = []
284 for item in self.__resources.list(
285 self.__config.hardware.heat_stack_name):
286 if item.resource_status in BAD_STACK_STATUSES:
287 res.append({
288 'resource_name': item.resource_name,
289 'resource_status': item.resource_status,
290 'resource_status_reason': item.resource_status_reason,
291 'resource_type': item.resource_type
292 })
293 wrong_resources = '\n'.join([
294 "*** Heat stack resource '{0}' ({1}) has wrong status '{2}': {3}"
295 .format(item['resource_name'],
296 item['resource_type'],
297 item['resource_status'],
298 item['resource_status_reason'])
299 for item in res
300 ])
301 return wrong_resources
302
303 def wait_of_stack_status(self, status, delay=30, tries=60):
304
305 @retry(exceptions.EnvironmentWrongStatus, delay=delay, tries=tries)
306 def wait():
307 st = self._current_stack.stack_status
308 if st == status:
309 return
310 elif st in BAD_STACK_STATUSES:
311 wrong_resources = self._get_resources_with_wrong_status()
312 raise exceptions.EnvironmentBadStatus(
313 self.__config.hardware.heat_stack_name,
314 status,
315 st,
316 wrong_resources
317 )
318 else:
319 LOG.info("Stack {0} status: {1}".format(
320 self.__config.hardware.heat_stack_name, st))
321 raise exceptions.EnvironmentWrongStatus(
322 self.__config.hardware.heat_stack_name,
323 status,
324 st
325 )
326 LOG.info("Waiting for stack '{0}' status <{1}>".format(
327 self.__config.hardware.heat_stack_name, status))
328 wait()
329
330 def revert_snapshot(self, name):
331 """Revert snapshot by name
332
333 - Revert the heat snapshot in the environment
334 - Try to reload 'config' object from a file 'config_<name>.ini'
335 If the file not found, then pass with defaults.
336 - Set <name> as the current state of the environment after reload
337
338 :param name: string
339 """
340 LOG.info("Reading INI config (without reverting env to snapshot) "
341 "named '{0}'".format(name))
342
343 try:
344 test_config_path = self._get_snapshot_config_name(name)
345 settings_oslo.reload_snapshot_config(self.__config,
346 test_config_path)
347 except cfg.ConfigFilesNotFoundError as conf_err:
348 LOG.error("Config file(s) {0} not found!".format(
349 conf_err.config_files))
350
351 self.__config.hardware.current_snapshot = name
352
353 def create_snapshot(self, name, *args, **kwargs):
354 """Create named snapshot of current env.
355
356 - Create a snapshot for the environment
357 - Save 'config' object to a file 'config_<name>.ini'
358
359 :name: string
360 """
361 LOG.info("Store INI config (without env snapshot) named '{0}'"
362 .format(name))
363 self.__config.hardware.current_snapshot = name
364 settings_oslo.save_config(self.__config,
365 name,
366 self.__config.hardware.heat_stack_name)
367
368 def _get_snapshot_config_name(self, snapshot_name):
369 """Get config name for the environment"""
370 env_name = self.__config.hardware.heat_stack_name
371 if env_name is None:
372 env_name = 'config'
373 test_config_path = os.path.join(
374 settings.LOGS_DIR, '{0}_{1}.ini'.format(env_name, snapshot_name))
375 return test_config_path
376
377 def has_snapshot(self, name):
378 # Heat doesn't support live snapshots, so just
379 # check if an INI file was created for this environment,
380 # assuming that the environment has the configuration
381 # described in this INI.
382 return self.has_snapshot_config(name)
383
384 def has_snapshot_config(self, name):
385 test_config_path = self._get_snapshot_config_name(name)
386 return os.path.isfile(test_config_path)
387
388 def start(self, underlay_node_roles, timeout=480):
389 """Start environment"""
390 LOG.warning("HEAT Manager doesn't support start environment feature. "
391 "Waiting for finish the bootstrap process on the nodes "
392 "with accessible SSH")
393
394 check_cloudinit_started = '[ -f /is_cloud_init_started ]'
395 check_cloudinit_finished = ('[ -f /is_cloud_init_finished ] || '
396 '[ -f /var/log/mcp/.bootstrap_done ]')
397 check_cloudinit_failed = 'cat /is_cloud_init_failed'
398 passed = {}
399 for node in self._get_nodes_by_roles(roles=underlay_node_roles):
400
401 try:
402 node_ip = self.node_ip(node)
403 except exceptions.EnvironmentNodeAccessError:
404 LOG.warning("Node {0} doesn't have accessible IP address"
405 ", skipping".format(node['name']))
406 continue
407
408 LOG.info("Waiting for SSH on node '{0}' / {1} ...".format(
409 node['name'], node_ip))
410
411 def _ssh_check(host,
412 port,
413 username=settings.SSH_NODE_CREDENTIALS['login'],
414 password=settings.SSH_NODE_CREDENTIALS['password'],
415 timeout=0):
416 try:
417 ssh = ssh_client.SSHClient(
418 host=host, port=port,
419 auth=ssh_client.SSHAuth(
420 username=username,
421 password=password))
422
423 # If '/is_cloud_init_started' exists, then wait for
424 # the flag /is_cloud_init_finished
425 if ssh.execute(check_cloudinit_started)['exit_code'] == 0:
426 result = ssh.execute(check_cloudinit_failed)
427 if result['exit_code'] == 0:
428 raise exceptions.EnvironmentNodeIsNotStarted(
429 "{0}:{1}".format(host, port),
430 result.stdout_str)
431
432 status = ssh.execute(
433 check_cloudinit_finished)['exit_code'] == 0
434 # Else, just wait for SSH
435 else:
436 status = ssh.execute('echo ok')['exit_code'] == 0
437 return status
438
439 except (AuthenticationException, BadAuthenticationType):
440 return True
441 except Exception:
442 return False
443
444 def _ssh_wait(host,
445 port,
446 username=settings.SSH_NODE_CREDENTIALS['login'],
447 password=settings.SSH_NODE_CREDENTIALS['password'],
448 timeout=0):
449
450 if host in passed and passed[host] >= 2:
451 # host already passed the check
452 return True
453
454 for node in self._get_nodes_by_roles(
455 roles=underlay_node_roles):
456 ip = node_ip
457 if ip not in passed:
458 passed[ip] = 0
459 if _ssh_check(ip, port):
460 passed[ip] += 1
461 else:
462 passed[ip] = 0
463
464 helpers.wait(
465 lambda: _ssh_wait(node_ip, 22),
466 timeout=timeout,
467 timeout_msg="Node '{}' didn't open SSH in {} sec".format(
468 node['name'], timeout
469 )
470 )
471 LOG.info('Heat stack "{0}" ready'
472 .format(self.__config.hardware.heat_stack_name))
473
474 def _create_environment(self):
475 tpl_files, template = template_utils.get_template_contents(
476 self.__config.hardware.heat_conf_path)
477 env_files_list = []
478 env_files, env = (
479 template_utils.process_multiple_environments_and_files(
480 env_paths=[self.__config.hardware.heat_env_path],
481 env_list_tracker=env_files_list))
482
483 fields = {
484 'stack_name': self.__config.hardware.heat_stack_name,
485 'template': template,
486 'files': dict(list(tpl_files.items()) + list(env_files.items())),
487 'environment': env,
488 }
489
490 if env_files_list:
491 fields['environment_files'] = env_files_list
492
493 self.__stacks.create(**fields)
494 self.wait_of_stack_status(EXPECTED_STACK_STATUS)
495 LOG.info("Stack '{0}' created"
496 .format(self.__config.hardware.heat_stack_name))
497
498 def stop(self):
499 """Stop environment"""
500 LOG.warning("HEAT Manager doesn't support stop environment feature")
501 pass
502
503# TODO(ddmitriev): add all Environment methods
504 @staticmethod
505 def node_ip(node, address_pool_name='admin-pool01'):
506 """Determine node's IP
507
508 :param node: a dict element from the self._nodes
509 :return: string
510 """
511 if address_pool_name in node['addresses']:
512 addr = node['addresses'][address_pool_name]
513 LOG.debug('{0} IP= {1}'.format(node['name'], addr))
514 return addr
515 else:
516 raise exceptions.EnvironmentNodeAccessError(
517 node['name'],
518 "No addresses available for the subnet {0}"
519 .format(address_pool_name))
520
521 def set_address_pools_config(self):
522 """Store address pools CIDRs in config object"""
523 for ap in self._address_pools:
524 for role in ap['roles']:
525 self.__config.underlay.address_pools[role] = ap['cidr']
526
527 def set_dhcp_ranges_config(self):
528 """Store DHCP ranges in config object"""
529 for ap in self._address_pools:
530 for role in ap['roles']:
531 self.__config.underlay.dhcp_ranges[role] = {
532 "cidr": ap['cidr'],
533 "start": ap['start'],
534 "end": ap['end'],
535 "gateway": ap['gateway'],
536 }
537
538 def wait_for_node_state(self, node_name, state, timeout):
539 raise NotImplementedError()
540
541 def warm_shutdown_nodes(self, underlay, nodes_prefix, timeout=600):
542 raise NotImplementedError()
543
544 def warm_restart_nodes(self, underlay, nodes_prefix, timeout=600):
545 raise NotImplementedError()
546
547 @property
548 def slave_nodes(self):
549 raise NotImplementedError()