blob: 17ad45226f454195df25463227465362a550c06a [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 Dmitrievb6bcc5c2018-09-26 11:07:53 +000074 self.set_dhcp_ranges_config()
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030075
76 @property
77 def _devops_config(self):
78 return self.__devops_config
79
80 @_devops_config.setter
81 def _devops_config(self, conf):
82 """Setter for self.__devops_config
83
84 :param conf: tcp_tests.helpers.env_config.EnvironmentConfig
85 """
86 if not isinstance(conf, env_config.EnvironmentConfig):
87 msg = ("Unexpected type of devops config. Got '{0}' " +
88 "instead of '{1}'")
89 raise TypeError(
90 msg.format(
91 type(conf).__name__,
92 env_config.EnvironmentConfig.__name__
93 )
94 )
95 self.__devops_config = conf
96
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030097 @property
98 def _d_env_name(self):
99 """Get environment name from fuel devops config
100
101 :rtype: string
102 """
103 return self._devops_config['env_name']
104
105 def _get_env_by_name(self, name):
106 """Set existing environment by name
107
108 :param name: string
109 """
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300110 self.__env = models.Environment.get(name=name)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300111
112 def _get_default_node_group(self):
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300113 return self.__env.get_group(name='default')
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300114
115 def _get_network_pool(self, net_pool_name):
116 default_node_group = self._get_default_node_group()
117 network_pool = default_node_group.get_network_pool(name=net_pool_name)
118 return network_pool
119
120 def get_ssh_data(self, roles=None):
121 """Generate ssh config for Underlay
122
123 :param roles: list of strings
124 """
125 if roles is None:
126 raise Exception("No roles specified for the environment!")
127
128 config_ssh = []
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300129 for d_node in self.__env.get_nodes(role__in=roles):
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300130 ssh_data = {
131 'node_name': d_node.name,
Dennis Dmitriev83cc1d52018-11-09 15:35:30 +0200132 'minion_id': d_node.name,
Dennis Dmitriev474e3f72016-10-21 16:46:09 +0300133 'roles': [d_node.role],
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300134 'address_pool': self._get_network_pool(
disc5298382016-11-23 16:03:33 +0200135 ext.NETWORK_TYPE.admin).address_pool.name,
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300136 'host': self.node_ip(d_node),
137 'login': settings.SSH_NODE_CREDENTIALS['login'],
138 'password': settings.SSH_NODE_CREDENTIALS['password'],
Artem Panchenkodb0a97f2017-06-27 19:09:13 +0300139 'keys': [k['private'] for k in self.__config.underlay.ssh_keys]
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300140 }
141 config_ssh.append(ssh_data)
142 return config_ssh
143
Dennis Dmitriev411dd102017-09-15 16:04:47 +0300144 def create_snapshot(self, name, description=None, force=False):
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300145 """Create named snapshot of current env.
146
147 - Create a libvirt snapshots for all nodes in the environment
148 - Save 'config' object to a file 'config_<name>.ini'
149
150 :name: string
151 """
Dennis Dmitriev411dd102017-09-15 16:04:47 +0300152 if not settings.MAKE_SNAPSHOT_STAGES and not force:
153 msg = ("[ SKIP snapshot '{0}' because MAKE_SNAPSHOT_STAGES=false ]"
154 " {1}".format(name, description or ''))
155 LOG.info("\n\n{0}\n{1}".format(msg, '*' * len(msg)))
156 return
Dennis Dmitriev99b26fe2017-04-26 12:34:44 +0300157 msg = "[ Create snapshot '{0}' ] {1}".format(name, description or '')
158 LOG.info("\n\n{0}\n{1}".format(msg, '*' * len(msg)))
159
Dmitry Tyzhnenko1fb041c2017-04-28 16:07:48 +0300160 self.__config.hardware.current_snapshot = name
Dennis Dmitriev99b26fe2017-04-26 12:34:44 +0300161 LOG.info("Set current snapshot in config to '{0}'".format(
Dmitry Tyzhnenko1fb041c2017-04-28 16:07:48 +0300162 self.__config.hardware.current_snapshot))
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300163 if self.__env is not None:
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300164 LOG.info('trying to suspend ....')
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300165 self.__env.suspend()
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300166 LOG.info('trying to snapshot ....')
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300167 self.__env.snapshot(name, description=description, force=True)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300168 LOG.info('trying to resume ....')
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300169 self.__env.resume()
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300170 else:
171 raise exceptions.EnvironmentIsNotSet()
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300172 settings_oslo.save_config(self.__config, name, self.__env.name)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300173
Dennis Dmitriev99b26fe2017-04-26 12:34:44 +0300174 if settings.VIRTUAL_ENV:
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +0300175 venv_msg = "source {0}/bin/activate;\n".format(
176 settings.VIRTUAL_ENV)
Dennis Dmitriev99b26fe2017-04-26 12:34:44 +0300177 else:
178 venv_msg = ""
179 LOG.info("To revert the snapshot:\n\n"
180 "************************************\n"
181 "{venv_msg}"
182 "dos.py revert {env_name} {snapshot_name};\n"
183 "dos.py resume {env_name};\n"
184 "# dos.py time-sync {env_name}; # Optional\n"
Artem Panchenkodb0a97f2017-06-27 19:09:13 +0300185 "ssh -i {key_file} {login}@{salt_master_host} "
186 "# Optional password: {password}\n"
Dennis Dmitriev99b26fe2017-04-26 12:34:44 +0300187 "************************************\n"
188 .format(venv_msg=venv_msg,
Dennis Dmitriev68671a62017-05-13 16:40:32 +0300189 env_name=self._d_env_name,
Dennis Dmitriev99b26fe2017-04-26 12:34:44 +0300190 snapshot_name=name,
191 login=settings.SSH_NODE_CREDENTIALS['login'],
192 password=settings.SSH_NODE_CREDENTIALS['password'],
Artem Panchenkodb0a97f2017-06-27 19:09:13 +0300193 salt_master_host=self.__config.salt.salt_master_host,
194 key_file=self.__config.underlay.ssh_key_file))
Dennis Dmitriev99b26fe2017-04-26 12:34:44 +0300195
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300196 def _get_snapshot_config_name(self, snapshot_name):
197 """Get config name for the environment"""
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300198 env_name = self.__env.name
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300199 if env_name is None:
200 env_name = 'config'
201 test_config_path = os.path.join(
202 settings.LOGS_DIR, '{0}_{1}.ini'.format(env_name, snapshot_name))
203 return test_config_path
204
205 def revert_snapshot(self, name):
206 """Revert snapshot by name
207
208 - Revert a libvirt snapshots for all nodes in the environment
209 - Try to reload 'config' object from a file 'config_<name>.ini'
210 If the file not found, then pass with defaults.
211 - Set <name> as the current state of the environment after reload
212
213 :param name: string
214 """
Dennis Dmitriev411dd102017-09-15 16:04:47 +0300215 if not settings.MAKE_SNAPSHOT_STAGES:
216 LOG.info("SKIP reverting from snapshot '{0}' "
217 "because MAKE_SNAPSHOT_STAGES=false".format(name))
218 return
219
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300220 if self.__env is not None:
Dennis Dmitrieva5978eb2018-02-21 10:12:33 +0200221 LOG.info("Suspending environment to stop IO")
222 self.__env.suspend()
223 LOG.info("Reverting from snapshot named '{0}'".format(name))
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300224 self.__env.revert(name=name)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300225 LOG.info("Resuming environment after revert")
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300226 self.__env.resume()
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300227 else:
228 raise exceptions.EnvironmentIsNotSet()
229
230 try:
231 test_config_path = self._get_snapshot_config_name(name)
Dmitry Tyzhnenko1fb041c2017-04-28 16:07:48 +0300232 settings_oslo.reload_snapshot_config(self.__config,
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300233 test_config_path)
234 except cfg.ConfigFilesNotFoundError as conf_err:
235 LOG.error("Config file(s) {0} not found!".format(
236 conf_err.config_files))
237
Dmitry Tyzhnenko1fb041c2017-04-28 16:07:48 +0300238 self.__config.hardware.current_snapshot = name
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300239
240 def _create_environment(self):
241 """Create environment and start VMs.
242
243 If config was provided earlier, we simply create and start VMs,
244 otherwise we tries to generate config from self.config_file,
245 """
246 if self._devops_config.config is None:
247 raise exceptions.DevopsConfigPathIsNotSet()
248 settings = self._devops_config
249 env_name = settings['env_name']
250 LOG.debug(
251 'Preparing to create environment named "{0}"'.format(env_name)
252 )
253 if env_name is None:
254 LOG.error('Environment name is not set!')
255 raise exceptions.EnvironmentNameIsNotSet()
256 try:
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300257 self.__env = models.Environment.create_environment(
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300258 settings.config
259 )
260 except db.IntegrityError:
261 LOG.error(
dis2b2d8632016-12-08 17:56:57 +0200262 'Seems like environment {0} already exists or contain errors'
263 ' in template.'.format(env_name)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300264 )
dis2b2d8632016-12-08 17:56:57 +0200265 raise
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300266 self.__env.define()
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300267 LOG.info(
Dennis Dmitriev53d3b772016-10-18 14:31:58 +0300268 'Environment "{0}" created'.format(env_name)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300269 )
270
Dennis Dmitriev7b9538f2017-05-15 17:01:34 +0300271 def start(self, underlay_node_roles, timeout=480):
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300272 """Method for start environment
273
274 """
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300275 if self.__env is None:
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300276 raise exceptions.EnvironmentIsNotSet()
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300277 self.__env.start()
278 LOG.info('Environment "{0}" started'.format(self.__env.name))
Dennis Dmitrievb01b90e2018-06-07 14:57:53 +0300279 check_cloudinit_started = '[ -f /is_cloud_init_started ]'
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300280 check_cloudinit_finished = ('[ -f /is_cloud_init_finished ] || '
281 '[ -f /var/log/mcp/.bootstrap_done ]')
Dennis Dmitriev86085b42018-07-02 14:14:25 +0300282 check_cloudinit_failed = 'cat /is_cloud_init_failed'
Dennis Dmitrievb01b90e2018-06-07 14:57:53 +0300283 passed = {}
Dennis Dmitriev7b9538f2017-05-15 17:01:34 +0300284 for node in self.__env.get_nodes(role__in=underlay_node_roles):
Dennis Dmitrieva63bac62017-05-15 18:36:26 +0300285 LOG.info("Waiting for SSH on node '{0}' / {1} ...".format(
286 node.name, self.node_ip(node)))
Dmitry Tyzhnenkob610afd2018-02-19 15:43:45 +0200287
Dennis Dmitrievb01b90e2018-06-07 14:57:53 +0300288 def _ssh_check(host,
289 port,
290 username=settings.SSH_NODE_CREDENTIALS['login'],
291 password=settings.SSH_NODE_CREDENTIALS['password'],
292 timeout=0):
Dmitry Tyzhnenkob610afd2018-02-19 15:43:45 +0200293 try:
294 ssh = ssh_client.SSHClient(
295 host=host, port=port,
296 auth=ssh_client.SSHAuth(
297 username=username,
298 password=password))
Dennis Dmitrievb01b90e2018-06-07 14:57:53 +0300299
300 # If '/is_cloud_init_started' exists, then wait for
301 # the flag /is_cloud_init_finished
302 if ssh.execute(check_cloudinit_started)['exit_code'] == 0:
Dennis Dmitriev86085b42018-07-02 14:14:25 +0300303 result = ssh.execute(check_cloudinit_failed)
304 if result['exit_code'] == 0:
305 raise exceptions.EnvironmentNodeIsNotStarted(
306 "{0}:{1}".format(host, port),
307 result.stdout_str)
308
Dennis Dmitrievb01b90e2018-06-07 14:57:53 +0300309 status = ssh.execute(
310 check_cloudinit_finished)['exit_code'] == 0
311 # Else, just wait for SSH
312 else:
313 status = ssh.execute('echo ok')['exit_code'] == 0
314 return status
315
316 except (AuthenticationException, BadAuthenticationType):
Dmitry Tyzhnenkob610afd2018-02-19 15:43:45 +0200317 return True
318 except Exception:
319 return False
320
Dennis Dmitrievb01b90e2018-06-07 14:57:53 +0300321 def _ssh_wait(host,
322 port,
323 username=settings.SSH_NODE_CREDENTIALS['login'],
324 password=settings.SSH_NODE_CREDENTIALS['password'],
325 timeout=0):
326
327 if host in passed and passed[host] >= 2:
328 # host already passed the check
329 return True
330
331 for node in self.__env.get_nodes(role__in=underlay_node_roles):
332 ip = self.node_ip(node)
333 if ip not in passed:
334 passed[ip] = 0
335 if _ssh_check(ip, port):
336 passed[ip] += 1
337 else:
338 passed[ip] = 0
Dmitry Tyzhnenkob610afd2018-02-19 15:43:45 +0200339
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300340 helpers.wait(
Dmitry Tyzhnenkob610afd2018-02-19 15:43:45 +0200341 lambda: _ssh_wait(self.node_ip(node), 22),
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300342 timeout=timeout,
343 timeout_msg="Node '{}' didn't open SSH in {} sec".format(
344 node.name, timeout
345 )
346 )
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300347 LOG.info('Environment "{0}" ready'.format(self.__env.name))
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300348
349 def resume(self):
350 """Resume environment"""
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300351 if self.__env is None:
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300352 raise exceptions.EnvironmentIsNotSet()
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300353 self.__env.resume()
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300354
355 def suspend(self):
356 """Suspend environment"""
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300357 if self.__env is None:
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300358 raise exceptions.EnvironmentIsNotSet()
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300359 self.__env.suspend()
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300360
361 def stop(self):
362 """Stop environment"""
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300363 if self.__env is None:
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300364 raise exceptions.EnvironmentIsNotSet()
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300365 self.__env.destroy()
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300366
Tatyana Leontoviche5ccdb32017-10-09 20:10:43 +0300367 def destroy_node(self, node_name):
368 """Destroy node"""
369 node = self.__env.get_node(name=node_name)
370 node.destroy()
371
372 def start_node(self, node_name):
373 """Start node"""
374 node = self.__env.get_node(name=node_name)
375 node.start()
376
377 def reboot_node(self, node_name):
378 """Reboot node"""
379 node = self.__env.get_node(name=node_name)
380 node.reboot()
381
382 def remove_node(self, node_name):
383 """Remove node"""
384 node = self.__env.get_node(name=node_name)
385 node.remove()
386
387 def wait_for_node_state(self, node_name, state, timeout):
388 node = self.__env.get_node(name=node_name)
389 if 'active' in state:
390 helpers.wait(lambda: node.is_active(),
391 timeout=timeout,
392 timeout_msg=('Node {0} failed '
393 'to become active'.format(node)))
394 else:
395 helpers.wait(lambda: not node.is_active(),
396 timeout=timeout,
397 timeout_msg=('Node {0} failed '
398 'to become active'.format(node)))
399
Vladimir Jigulinee1faa52018-06-25 13:00:51 +0400400 def warm_shutdown_nodes(self, underlay, nodes_prefix, timeout=600):
401 node_names = underlay.get_target_node_names(nodes_prefix)
402 for node in node_names:
403 LOG.debug('Shutdown node {0}'.format(node))
404 underlay.check_call(cmd="shutdown +1", node_name=node)
405 for node in node_names:
406 self.wait_for_node_state(node, state='offline', timeout=timeout)
407
408 def warm_restart_nodes(self, underlay, nodes_prefix, timeout=600):
409 self.warm_shutdown_nodes(underlay, nodes_prefix, timeout)
410 node_names = underlay.get_target_node_names(nodes_prefix)
411 for node in node_names:
412 LOG.debug('Starting node {0}'.format(node))
413 self.start_node(node)
414 self.wait_for_node_state(node, state='active', timeout=timeout)
415
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300416 def has_snapshot(self, name):
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300417 return self.__env.has_snapshot(name)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300418
419 def has_snapshot_config(self, name):
420 test_config_path = self._get_snapshot_config_name(name)
421 return os.path.isfile(test_config_path)
422
423 def delete_environment(self):
424 """Delete environment
425
426 """
427 LOG.debug("Deleting environment")
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300428 self.__env.erase()
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300429
430 def __get_nodes_by_role(self, node_role):
431 """Get node by given role name
432
433 :param node_role: string
434 :rtype: devops.models.Node
435 """
436 LOG.debug('Trying to get nodes by role {0}'.format(node_role))
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300437 return self.__env.get_nodes(role=node_role)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300438
Tatyana Leontoviche5ccdb32017-10-09 20:10:43 +0300439 def __get_nodes_by_name(self, node_name):
440 """Get node by given role name
441
442 :param node_name: string
443 :rtype: devops.models.Node
444 """
445 LOG.debug('Trying to get nodes by role {0}'.format(node_name))
446 return self.__env.get_nodes(name=node_name)
447
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300448 @property
449 def master_nodes(self):
450 """Get all master nodes
451
452 :rtype: list
453 """
454 nodes = self.__get_nodes_by_role(
Dennis Dmitriev53d3b772016-10-18 14:31:58 +0300455 node_role=ext.UNDERLAY_NODE_ROLES.salt_master)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300456 return nodes
457
458 @property
459 def slave_nodes(self):
460 """Get all slave nodes
461
462 :rtype: list
463 """
464 nodes = self.__get_nodes_by_role(
Dennis Dmitriev53d3b772016-10-18 14:31:58 +0300465 node_role=ext.UNDERLAY_NODE_ROLES.salt_minion)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300466 return nodes
467
Dennis Dmitriev53d3b772016-10-18 14:31:58 +0300468 @staticmethod
469 def node_ip(node):
470 """Determine node's IP
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300471
Dennis Dmitriev53d3b772016-10-18 14:31:58 +0300472 :param node: devops.models.Node
473 :return: string
474 """
475 LOG.debug('Trying to determine {0} ip.'.format(node.name))
476 return node.get_ip_address_by_network_name(
disc5298382016-11-23 16:03:33 +0200477 ext.NETWORK_TYPE.admin
Dennis Dmitriev53d3b772016-10-18 14:31:58 +0300478 )
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300479
480 @property
481 def nameserver(self):
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300482 return self.__env.router(ext.NETWORK_TYPE.admin)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300483
484 def set_dns_config(self):
485 # Set local nameserver to use by default
Dmitry Tyzhnenko1fb041c2017-04-28 16:07:48 +0300486 if not self.__config.underlay.nameservers:
487 self.__config.underlay.nameservers = [self.nameserver]
488 if not self.__config.underlay.upstream_dns_servers:
489 self.__config.underlay.upstream_dns_servers = [self.nameserver]
Dennis Dmitriev99b26fe2017-04-26 12:34:44 +0300490
491 def set_address_pools_config(self):
492 """Store address pools CIDRs in config object"""
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300493 for ap in self.__env.get_address_pools():
Dmitry Tyzhnenko1fb041c2017-04-28 16:07:48 +0300494 self.__config.underlay.address_pools[ap.name] = ap.net
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000495
496 def set_dhcp_ranges_config(self):
497 """Store DHCP ranges in config object"""
498 for ap in self.__env.get_address_pools():
499 if "gateway" in ap.ip_reserved and "dhcp" in ap.ip_ranges:
500 self.__config.underlay.dhcp_ranges[ap.name] = {
501 "cidr": ap.net,
502 "start": ap.ip_range_start("dhcp"),
503 "end": ap.ip_range_end("dhcp"),
504 "gateway": ap.gateway,
505 }