blob: 7424a493e7faee960f08d7ae3f43b933b60b1159 [file] [log] [blame]
Dennis Dmitriev6f59add2016-10-18 13:45:27 +03001# Copyright 2016 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
16
17from devops import error
18from devops.helpers import helpers
Dmitry Tyzhnenkob610afd2018-02-19 15:43:45 +020019from devops.helpers.helpers import ssh_client
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030020from devops import models
21from django import db
22from oslo_config import cfg
23
Dmitry Tyzhnenkob610afd2018-02-19 15:43:45 +020024from paramiko.ssh_exception import (
25 AuthenticationException,
26 BadAuthenticationType)
27
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030028from tcp_tests import settings
29from tcp_tests import settings_oslo
Dmitry Tyzhnenko35413c02018-03-05 14:12:37 +020030from tcp_tests.helpers import env_config
31from tcp_tests.helpers import ext
32from tcp_tests.helpers import exceptions
33from tcp_tests import logger
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030034
35LOG = logger.logger
36
37
38class EnvironmentManager(object):
39 """Class-helper for creating VMs via devops environments"""
40
Dmitry Tyzhnenko1fb041c2017-04-28 16:07:48 +030041 __config = None
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030042
43 def __init__(self, config=None):
44 """Initializing class instance and create the environment
45
46 :param config: oslo.config object
47 :param config.hardware.conf_path: path to devops YAML template
48 :param config.hardware.current_snapshot: name of the snapshot that
49 descriebe environment status.
50 """
51 self.__devops_config = env_config.EnvironmentConfig()
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +030052 self.__env = None
Dmitry Tyzhnenko1fb041c2017-04-28 16:07:48 +030053 self.__config = config
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030054
55 if config.hardware.conf_path is not None:
Artem Panchenkodb0a97f2017-06-27 19:09:13 +030056 options = {
57 'config': self.__config,
58 }
59 self._devops_config.load_template(config.hardware.conf_path,
60 options=options)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030061 else:
62 raise Exception("Devops YAML template is not set in config object")
63
64 try:
65 self._get_env_by_name(self._d_env_name)
66 if not self.has_snapshot(config.hardware.current_snapshot):
67 raise exceptions.EnvironmentSnapshotMissing(
68 self._d_env_name, config.hardware.current_snapshot)
69 except error.DevopsObjNotFound:
70 LOG.info("Environment doesn't exist, creating a new one")
71 self._create_environment()
72 self.set_dns_config()
Dennis Dmitriev99b26fe2017-04-26 12:34:44 +030073 self.set_address_pools_config()
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030074
75 @property
76 def _devops_config(self):
77 return self.__devops_config
78
79 @_devops_config.setter
80 def _devops_config(self, conf):
81 """Setter for self.__devops_config
82
83 :param conf: tcp_tests.helpers.env_config.EnvironmentConfig
84 """
85 if not isinstance(conf, env_config.EnvironmentConfig):
86 msg = ("Unexpected type of devops config. Got '{0}' " +
87 "instead of '{1}'")
88 raise TypeError(
89 msg.format(
90 type(conf).__name__,
91 env_config.EnvironmentConfig.__name__
92 )
93 )
94 self.__devops_config = conf
95
96 def lvm_storages(self):
97 """Returns a dict object of lvm storages in current environment
98
99 returned data example:
100 {
101 "master": {
102 "id": "virtio-bff72959d1a54cb19d08"
103 },
104 "slave-0": {
105 "id": "virtio-5e33affc8fe44503839f"
106 },
107 "slave-1": {
108 "id": "virtio-10b6a262f1ec4341a1ba"
109 },
110 }
111
112 :rtype: dict
113 """
114 result = {}
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300115 for node in self.__env.get_nodes(role__in=ext.UNDERLAY_NODE_ROLES):
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300116 lvm = filter(lambda x: x.volume.name == 'lvm', node.disk_devices)
117 if len(lvm) == 0:
118 continue
119 lvm = lvm[0]
120 result[node.name] = {}
121 result_node = result[node.name]
122 result_node['id'] = "{bus}-{serial}".format(
123 bus=lvm.bus,
124 serial=lvm.volume.serial[:20])
125 LOG.info("Got disk-id '{}' for node '{}'".format(
126 result_node['id'], node.name))
127 return result
128
129 @property
130 def _d_env_name(self):
131 """Get environment name from fuel devops config
132
133 :rtype: string
134 """
135 return self._devops_config['env_name']
136
137 def _get_env_by_name(self, name):
138 """Set existing environment by name
139
140 :param name: string
141 """
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300142 self.__env = models.Environment.get(name=name)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300143
144 def _get_default_node_group(self):
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300145 return self.__env.get_group(name='default')
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300146
147 def _get_network_pool(self, net_pool_name):
148 default_node_group = self._get_default_node_group()
149 network_pool = default_node_group.get_network_pool(name=net_pool_name)
150 return network_pool
151
152 def get_ssh_data(self, roles=None):
153 """Generate ssh config for Underlay
154
155 :param roles: list of strings
156 """
157 if roles is None:
158 raise Exception("No roles specified for the environment!")
159
160 config_ssh = []
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300161 for d_node in self.__env.get_nodes(role__in=roles):
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300162 ssh_data = {
163 'node_name': d_node.name,
Dennis Dmitriev474e3f72016-10-21 16:46:09 +0300164 'roles': [d_node.role],
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300165 'address_pool': self._get_network_pool(
disc5298382016-11-23 16:03:33 +0200166 ext.NETWORK_TYPE.admin).address_pool.name,
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300167 'host': self.node_ip(d_node),
168 'login': settings.SSH_NODE_CREDENTIALS['login'],
169 'password': settings.SSH_NODE_CREDENTIALS['password'],
Artem Panchenkodb0a97f2017-06-27 19:09:13 +0300170 'keys': [k['private'] for k in self.__config.underlay.ssh_keys]
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300171 }
172 config_ssh.append(ssh_data)
173 return config_ssh
174
Dennis Dmitriev411dd102017-09-15 16:04:47 +0300175 def create_snapshot(self, name, description=None, force=False):
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300176 """Create named snapshot of current env.
177
178 - Create a libvirt snapshots for all nodes in the environment
179 - Save 'config' object to a file 'config_<name>.ini'
180
181 :name: string
182 """
Dennis Dmitriev411dd102017-09-15 16:04:47 +0300183 if not settings.MAKE_SNAPSHOT_STAGES and not force:
184 msg = ("[ SKIP snapshot '{0}' because MAKE_SNAPSHOT_STAGES=false ]"
185 " {1}".format(name, description or ''))
186 LOG.info("\n\n{0}\n{1}".format(msg, '*' * len(msg)))
187 return
Dennis Dmitriev99b26fe2017-04-26 12:34:44 +0300188 msg = "[ Create snapshot '{0}' ] {1}".format(name, description or '')
189 LOG.info("\n\n{0}\n{1}".format(msg, '*' * len(msg)))
190
Dmitry Tyzhnenko1fb041c2017-04-28 16:07:48 +0300191 self.__config.hardware.current_snapshot = name
Dennis Dmitriev99b26fe2017-04-26 12:34:44 +0300192 LOG.info("Set current snapshot in config to '{0}'".format(
Dmitry Tyzhnenko1fb041c2017-04-28 16:07:48 +0300193 self.__config.hardware.current_snapshot))
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300194 if self.__env is not None:
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300195 LOG.info('trying to suspend ....')
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300196 self.__env.suspend()
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300197 LOG.info('trying to snapshot ....')
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300198 self.__env.snapshot(name, description=description, force=True)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300199 LOG.info('trying to resume ....')
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300200 self.__env.resume()
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300201 else:
202 raise exceptions.EnvironmentIsNotSet()
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300203 settings_oslo.save_config(self.__config, name, self.__env.name)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300204
Dennis Dmitriev99b26fe2017-04-26 12:34:44 +0300205 if settings.VIRTUAL_ENV:
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +0300206 venv_msg = "source {0}/bin/activate;\n".format(
207 settings.VIRTUAL_ENV)
Dennis Dmitriev99b26fe2017-04-26 12:34:44 +0300208 else:
209 venv_msg = ""
210 LOG.info("To revert the snapshot:\n\n"
211 "************************************\n"
212 "{venv_msg}"
213 "dos.py revert {env_name} {snapshot_name};\n"
214 "dos.py resume {env_name};\n"
215 "# dos.py time-sync {env_name}; # Optional\n"
Artem Panchenkodb0a97f2017-06-27 19:09:13 +0300216 "ssh -i {key_file} {login}@{salt_master_host} "
217 "# Optional password: {password}\n"
Dennis Dmitriev99b26fe2017-04-26 12:34:44 +0300218 "************************************\n"
219 .format(venv_msg=venv_msg,
Dennis Dmitriev68671a62017-05-13 16:40:32 +0300220 env_name=self._d_env_name,
Dennis Dmitriev99b26fe2017-04-26 12:34:44 +0300221 snapshot_name=name,
222 login=settings.SSH_NODE_CREDENTIALS['login'],
223 password=settings.SSH_NODE_CREDENTIALS['password'],
Artem Panchenkodb0a97f2017-06-27 19:09:13 +0300224 salt_master_host=self.__config.salt.salt_master_host,
225 key_file=self.__config.underlay.ssh_key_file))
Dennis Dmitriev99b26fe2017-04-26 12:34:44 +0300226
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300227 def _get_snapshot_config_name(self, snapshot_name):
228 """Get config name for the environment"""
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300229 env_name = self.__env.name
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300230 if env_name is None:
231 env_name = 'config'
232 test_config_path = os.path.join(
233 settings.LOGS_DIR, '{0}_{1}.ini'.format(env_name, snapshot_name))
234 return test_config_path
235
236 def revert_snapshot(self, name):
237 """Revert snapshot by name
238
239 - Revert a libvirt snapshots for all nodes in the environment
240 - Try to reload 'config' object from a file 'config_<name>.ini'
241 If the file not found, then pass with defaults.
242 - Set <name> as the current state of the environment after reload
243
244 :param name: string
245 """
Dennis Dmitriev411dd102017-09-15 16:04:47 +0300246 if not settings.MAKE_SNAPSHOT_STAGES:
247 LOG.info("SKIP reverting from snapshot '{0}' "
248 "because MAKE_SNAPSHOT_STAGES=false".format(name))
249 return
250
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300251 if self.__env is not None:
Dennis Dmitrieva5978eb2018-02-21 10:12:33 +0200252 LOG.info("Suspending environment to stop IO")
253 self.__env.suspend()
254 LOG.info("Reverting from snapshot named '{0}'".format(name))
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300255 self.__env.revert(name=name)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300256 LOG.info("Resuming environment after revert")
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300257 self.__env.resume()
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300258 else:
259 raise exceptions.EnvironmentIsNotSet()
260
261 try:
262 test_config_path = self._get_snapshot_config_name(name)
Dmitry Tyzhnenko1fb041c2017-04-28 16:07:48 +0300263 settings_oslo.reload_snapshot_config(self.__config,
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300264 test_config_path)
265 except cfg.ConfigFilesNotFoundError as conf_err:
266 LOG.error("Config file(s) {0} not found!".format(
267 conf_err.config_files))
268
Dmitry Tyzhnenko1fb041c2017-04-28 16:07:48 +0300269 self.__config.hardware.current_snapshot = name
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300270
271 def _create_environment(self):
272 """Create environment and start VMs.
273
274 If config was provided earlier, we simply create and start VMs,
275 otherwise we tries to generate config from self.config_file,
276 """
277 if self._devops_config.config is None:
278 raise exceptions.DevopsConfigPathIsNotSet()
279 settings = self._devops_config
280 env_name = settings['env_name']
281 LOG.debug(
282 'Preparing to create environment named "{0}"'.format(env_name)
283 )
284 if env_name is None:
285 LOG.error('Environment name is not set!')
286 raise exceptions.EnvironmentNameIsNotSet()
287 try:
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300288 self.__env = models.Environment.create_environment(
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300289 settings.config
290 )
291 except db.IntegrityError:
292 LOG.error(
dis2b2d8632016-12-08 17:56:57 +0200293 'Seems like environment {0} already exists or contain errors'
294 ' in template.'.format(env_name)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300295 )
dis2b2d8632016-12-08 17:56:57 +0200296 raise
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300297 self.__env.define()
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300298 LOG.info(
Dennis Dmitriev53d3b772016-10-18 14:31:58 +0300299 'Environment "{0}" created'.format(env_name)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300300 )
301
Dennis Dmitriev7b9538f2017-05-15 17:01:34 +0300302 def start(self, underlay_node_roles, timeout=480):
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300303 """Method for start environment
304
305 """
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300306 if self.__env is None:
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300307 raise exceptions.EnvironmentIsNotSet()
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300308 self.__env.start()
309 LOG.info('Environment "{0}" started'.format(self.__env.name))
Dennis Dmitrievb01b90e2018-06-07 14:57:53 +0300310 check_cloudinit_started = '[ -f /is_cloud_init_started ]'
311 check_cloudinit_finished = '[ -f /is_cloud_init_finished ]'
Dennis Dmitriev86085b42018-07-02 14:14:25 +0300312 check_cloudinit_failed = 'cat /is_cloud_init_failed'
Dennis Dmitrievb01b90e2018-06-07 14:57:53 +0300313 passed = {}
Dennis Dmitriev7b9538f2017-05-15 17:01:34 +0300314 for node in self.__env.get_nodes(role__in=underlay_node_roles):
Dennis Dmitrieva63bac62017-05-15 18:36:26 +0300315 LOG.info("Waiting for SSH on node '{0}' / {1} ...".format(
316 node.name, self.node_ip(node)))
Dmitry Tyzhnenkob610afd2018-02-19 15:43:45 +0200317
Dennis Dmitrievb01b90e2018-06-07 14:57:53 +0300318 def _ssh_check(host,
319 port,
320 username=settings.SSH_NODE_CREDENTIALS['login'],
321 password=settings.SSH_NODE_CREDENTIALS['password'],
322 timeout=0):
Dmitry Tyzhnenkob610afd2018-02-19 15:43:45 +0200323 try:
324 ssh = ssh_client.SSHClient(
325 host=host, port=port,
326 auth=ssh_client.SSHAuth(
327 username=username,
328 password=password))
Dennis Dmitrievb01b90e2018-06-07 14:57:53 +0300329
330 # If '/is_cloud_init_started' exists, then wait for
331 # the flag /is_cloud_init_finished
332 if ssh.execute(check_cloudinit_started)['exit_code'] == 0:
Dennis Dmitriev86085b42018-07-02 14:14:25 +0300333 result = ssh.execute(check_cloudinit_failed)
334 if result['exit_code'] == 0:
335 raise exceptions.EnvironmentNodeIsNotStarted(
336 "{0}:{1}".format(host, port),
337 result.stdout_str)
338
Dennis Dmitrievb01b90e2018-06-07 14:57:53 +0300339 status = ssh.execute(
340 check_cloudinit_finished)['exit_code'] == 0
341 # Else, just wait for SSH
342 else:
343 status = ssh.execute('echo ok')['exit_code'] == 0
344 return status
345
346 except (AuthenticationException, BadAuthenticationType):
Dmitry Tyzhnenkob610afd2018-02-19 15:43:45 +0200347 return True
348 except Exception:
349 return False
350
Dennis Dmitrievb01b90e2018-06-07 14:57:53 +0300351 def _ssh_wait(host,
352 port,
353 username=settings.SSH_NODE_CREDENTIALS['login'],
354 password=settings.SSH_NODE_CREDENTIALS['password'],
355 timeout=0):
356
357 if host in passed and passed[host] >= 2:
358 # host already passed the check
359 return True
360
361 for node in self.__env.get_nodes(role__in=underlay_node_roles):
362 ip = self.node_ip(node)
363 if ip not in passed:
364 passed[ip] = 0
365 if _ssh_check(ip, port):
366 passed[ip] += 1
367 else:
368 passed[ip] = 0
Dmitry Tyzhnenkob610afd2018-02-19 15:43:45 +0200369
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300370 helpers.wait(
Dmitry Tyzhnenkob610afd2018-02-19 15:43:45 +0200371 lambda: _ssh_wait(self.node_ip(node), 22),
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300372 timeout=timeout,
373 timeout_msg="Node '{}' didn't open SSH in {} sec".format(
374 node.name, timeout
375 )
376 )
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300377 LOG.info('Environment "{0}" ready'.format(self.__env.name))
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300378
379 def resume(self):
380 """Resume environment"""
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300381 if self.__env is None:
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300382 raise exceptions.EnvironmentIsNotSet()
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300383 self.__env.resume()
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300384
385 def suspend(self):
386 """Suspend environment"""
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300387 if self.__env is None:
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300388 raise exceptions.EnvironmentIsNotSet()
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300389 self.__env.suspend()
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300390
391 def stop(self):
392 """Stop environment"""
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300393 if self.__env is None:
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300394 raise exceptions.EnvironmentIsNotSet()
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300395 self.__env.destroy()
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300396
Tatyana Leontoviche5ccdb32017-10-09 20:10:43 +0300397 def destroy_node(self, node_name):
398 """Destroy node"""
399 node = self.__env.get_node(name=node_name)
400 node.destroy()
401
402 def start_node(self, node_name):
403 """Start node"""
404 node = self.__env.get_node(name=node_name)
405 node.start()
406
407 def reboot_node(self, node_name):
408 """Reboot node"""
409 node = self.__env.get_node(name=node_name)
410 node.reboot()
411
412 def remove_node(self, node_name):
413 """Remove node"""
414 node = self.__env.get_node(name=node_name)
415 node.remove()
416
417 def wait_for_node_state(self, node_name, state, timeout):
418 node = self.__env.get_node(name=node_name)
419 if 'active' in state:
420 helpers.wait(lambda: node.is_active(),
421 timeout=timeout,
422 timeout_msg=('Node {0} failed '
423 'to become active'.format(node)))
424 else:
425 helpers.wait(lambda: not node.is_active(),
426 timeout=timeout,
427 timeout_msg=('Node {0} failed '
428 'to become active'.format(node)))
429
Vladimir Jigulinee1faa52018-06-25 13:00:51 +0400430 def warm_shutdown_nodes(self, underlay, nodes_prefix, timeout=600):
431 node_names = underlay.get_target_node_names(nodes_prefix)
432 for node in node_names:
433 LOG.debug('Shutdown node {0}'.format(node))
434 underlay.check_call(cmd="shutdown +1", node_name=node)
435 for node in node_names:
436 self.wait_for_node_state(node, state='offline', timeout=timeout)
437
438 def warm_restart_nodes(self, underlay, nodes_prefix, timeout=600):
439 self.warm_shutdown_nodes(underlay, nodes_prefix, timeout)
440 node_names = underlay.get_target_node_names(nodes_prefix)
441 for node in node_names:
442 LOG.debug('Starting node {0}'.format(node))
443 self.start_node(node)
444 self.wait_for_node_state(node, state='active', timeout=timeout)
445
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300446 def has_snapshot(self, name):
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300447 return self.__env.has_snapshot(name)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300448
449 def has_snapshot_config(self, name):
450 test_config_path = self._get_snapshot_config_name(name)
451 return os.path.isfile(test_config_path)
452
453 def delete_environment(self):
454 """Delete environment
455
456 """
457 LOG.debug("Deleting environment")
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300458 self.__env.erase()
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300459
460 def __get_nodes_by_role(self, node_role):
461 """Get node by given role name
462
463 :param node_role: string
464 :rtype: devops.models.Node
465 """
466 LOG.debug('Trying to get nodes by role {0}'.format(node_role))
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300467 return self.__env.get_nodes(role=node_role)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300468
Tatyana Leontoviche5ccdb32017-10-09 20:10:43 +0300469 def __get_nodes_by_name(self, node_name):
470 """Get node by given role name
471
472 :param node_name: string
473 :rtype: devops.models.Node
474 """
475 LOG.debug('Trying to get nodes by role {0}'.format(node_name))
476 return self.__env.get_nodes(name=node_name)
477
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300478 @property
479 def master_nodes(self):
480 """Get all master nodes
481
482 :rtype: list
483 """
484 nodes = self.__get_nodes_by_role(
Dennis Dmitriev53d3b772016-10-18 14:31:58 +0300485 node_role=ext.UNDERLAY_NODE_ROLES.salt_master)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300486 return nodes
487
488 @property
489 def slave_nodes(self):
490 """Get all slave nodes
491
492 :rtype: list
493 """
494 nodes = self.__get_nodes_by_role(
Dennis Dmitriev53d3b772016-10-18 14:31:58 +0300495 node_role=ext.UNDERLAY_NODE_ROLES.salt_minion)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300496 return nodes
497
Dennis Dmitriev53d3b772016-10-18 14:31:58 +0300498 @staticmethod
499 def node_ip(node):
500 """Determine node's IP
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300501
Dennis Dmitriev53d3b772016-10-18 14:31:58 +0300502 :param node: devops.models.Node
503 :return: string
504 """
505 LOG.debug('Trying to determine {0} ip.'.format(node.name))
506 return node.get_ip_address_by_network_name(
disc5298382016-11-23 16:03:33 +0200507 ext.NETWORK_TYPE.admin
Dennis Dmitriev53d3b772016-10-18 14:31:58 +0300508 )
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300509
510 @property
511 def nameserver(self):
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300512 return self.__env.router(ext.NETWORK_TYPE.admin)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300513
514 def set_dns_config(self):
515 # Set local nameserver to use by default
Dmitry Tyzhnenko1fb041c2017-04-28 16:07:48 +0300516 if not self.__config.underlay.nameservers:
517 self.__config.underlay.nameservers = [self.nameserver]
518 if not self.__config.underlay.upstream_dns_servers:
519 self.__config.underlay.upstream_dns_servers = [self.nameserver]
Dennis Dmitriev99b26fe2017-04-26 12:34:44 +0300520
521 def set_address_pools_config(self):
522 """Store address pools CIDRs in config object"""
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300523 for ap in self.__env.get_address_pools():
Dmitry Tyzhnenko1fb041c2017-04-28 16:07:48 +0300524 self.__config.underlay.address_pools[ap.name] = ap.net