Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 1 | # 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 | |
| 15 | import os |
| 16 | |
| 17 | from devops import error |
| 18 | from devops.helpers import helpers |
Dmitry Tyzhnenko | b610afd | 2018-02-19 15:43:45 +0200 | [diff] [blame] | 19 | from devops.helpers.helpers import ssh_client |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 20 | from devops import models |
| 21 | from django import db |
| 22 | from oslo_config import cfg |
| 23 | |
Dmitry Tyzhnenko | b610afd | 2018-02-19 15:43:45 +0200 | [diff] [blame] | 24 | from paramiko.ssh_exception import ( |
| 25 | AuthenticationException, |
| 26 | BadAuthenticationType) |
| 27 | |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 28 | from tcp_tests import settings |
| 29 | from tcp_tests import settings_oslo |
Dmitry Tyzhnenko | 35413c0 | 2018-03-05 14:12:37 +0200 | [diff] [blame] | 30 | from tcp_tests.helpers import env_config |
| 31 | from tcp_tests.helpers import ext |
| 32 | from tcp_tests.helpers import exceptions |
| 33 | from tcp_tests import logger |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 34 | |
| 35 | LOG = logger.logger |
| 36 | |
| 37 | |
| 38 | class EnvironmentManager(object): |
| 39 | """Class-helper for creating VMs via devops environments""" |
| 40 | |
Dmitry Tyzhnenko | 1fb041c | 2017-04-28 16:07:48 +0300 | [diff] [blame] | 41 | __config = None |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 42 | |
| 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 Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 52 | self.__env = None |
Dmitry Tyzhnenko | 1fb041c | 2017-04-28 16:07:48 +0300 | [diff] [blame] | 53 | self.__config = config |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 54 | |
| 55 | if config.hardware.conf_path is not None: |
Artem Panchenko | db0a97f | 2017-06-27 19:09:13 +0300 | [diff] [blame] | 56 | options = { |
| 57 | 'config': self.__config, |
| 58 | } |
| 59 | self._devops_config.load_template(config.hardware.conf_path, |
| 60 | options=options) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 61 | 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 Dmitriev | 99b26fe | 2017-04-26 12:34:44 +0300 | [diff] [blame] | 73 | self.set_address_pools_config() |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 74 | |
| 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 Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 115 | for node in self.__env.get_nodes(role__in=ext.UNDERLAY_NODE_ROLES): |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 116 | 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 Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 142 | self.__env = models.Environment.get(name=name) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 143 | |
| 144 | def _get_default_node_group(self): |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 145 | return self.__env.get_group(name='default') |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 146 | |
| 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 Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 161 | for d_node in self.__env.get_nodes(role__in=roles): |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 162 | ssh_data = { |
| 163 | 'node_name': d_node.name, |
Dennis Dmitriev | 474e3f7 | 2016-10-21 16:46:09 +0300 | [diff] [blame] | 164 | 'roles': [d_node.role], |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 165 | 'address_pool': self._get_network_pool( |
dis | c529838 | 2016-11-23 16:03:33 +0200 | [diff] [blame] | 166 | ext.NETWORK_TYPE.admin).address_pool.name, |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 167 | 'host': self.node_ip(d_node), |
| 168 | 'login': settings.SSH_NODE_CREDENTIALS['login'], |
| 169 | 'password': settings.SSH_NODE_CREDENTIALS['password'], |
Artem Panchenko | db0a97f | 2017-06-27 19:09:13 +0300 | [diff] [blame] | 170 | 'keys': [k['private'] for k in self.__config.underlay.ssh_keys] |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 171 | } |
| 172 | config_ssh.append(ssh_data) |
| 173 | return config_ssh |
| 174 | |
Dennis Dmitriev | 411dd10 | 2017-09-15 16:04:47 +0300 | [diff] [blame] | 175 | def create_snapshot(self, name, description=None, force=False): |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 176 | """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 Dmitriev | 411dd10 | 2017-09-15 16:04:47 +0300 | [diff] [blame] | 183 | 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 Dmitriev | 99b26fe | 2017-04-26 12:34:44 +0300 | [diff] [blame] | 188 | msg = "[ Create snapshot '{0}' ] {1}".format(name, description or '') |
| 189 | LOG.info("\n\n{0}\n{1}".format(msg, '*' * len(msg))) |
| 190 | |
Dmitry Tyzhnenko | 1fb041c | 2017-04-28 16:07:48 +0300 | [diff] [blame] | 191 | self.__config.hardware.current_snapshot = name |
Dennis Dmitriev | 99b26fe | 2017-04-26 12:34:44 +0300 | [diff] [blame] | 192 | LOG.info("Set current snapshot in config to '{0}'".format( |
Dmitry Tyzhnenko | 1fb041c | 2017-04-28 16:07:48 +0300 | [diff] [blame] | 193 | self.__config.hardware.current_snapshot)) |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 194 | if self.__env is not None: |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 195 | LOG.info('trying to suspend ....') |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 196 | self.__env.suspend() |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 197 | LOG.info('trying to snapshot ....') |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 198 | self.__env.snapshot(name, description=description, force=True) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 199 | LOG.info('trying to resume ....') |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 200 | self.__env.resume() |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 201 | else: |
| 202 | raise exceptions.EnvironmentIsNotSet() |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 203 | settings_oslo.save_config(self.__config, name, self.__env.name) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 204 | |
Dennis Dmitriev | 99b26fe | 2017-04-26 12:34:44 +0300 | [diff] [blame] | 205 | if settings.VIRTUAL_ENV: |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 206 | venv_msg = "source {0}/bin/activate;\n".format( |
| 207 | settings.VIRTUAL_ENV) |
Dennis Dmitriev | 99b26fe | 2017-04-26 12:34:44 +0300 | [diff] [blame] | 208 | 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 Panchenko | db0a97f | 2017-06-27 19:09:13 +0300 | [diff] [blame] | 216 | "ssh -i {key_file} {login}@{salt_master_host} " |
| 217 | "# Optional password: {password}\n" |
Dennis Dmitriev | 99b26fe | 2017-04-26 12:34:44 +0300 | [diff] [blame] | 218 | "************************************\n" |
| 219 | .format(venv_msg=venv_msg, |
Dennis Dmitriev | 68671a6 | 2017-05-13 16:40:32 +0300 | [diff] [blame] | 220 | env_name=self._d_env_name, |
Dennis Dmitriev | 99b26fe | 2017-04-26 12:34:44 +0300 | [diff] [blame] | 221 | snapshot_name=name, |
| 222 | login=settings.SSH_NODE_CREDENTIALS['login'], |
| 223 | password=settings.SSH_NODE_CREDENTIALS['password'], |
Artem Panchenko | db0a97f | 2017-06-27 19:09:13 +0300 | [diff] [blame] | 224 | salt_master_host=self.__config.salt.salt_master_host, |
| 225 | key_file=self.__config.underlay.ssh_key_file)) |
Dennis Dmitriev | 99b26fe | 2017-04-26 12:34:44 +0300 | [diff] [blame] | 226 | |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 227 | def _get_snapshot_config_name(self, snapshot_name): |
| 228 | """Get config name for the environment""" |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 229 | env_name = self.__env.name |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 230 | 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 Dmitriev | 411dd10 | 2017-09-15 16:04:47 +0300 | [diff] [blame] | 246 | 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 Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 251 | if self.__env is not None: |
Dennis Dmitriev | a5978eb | 2018-02-21 10:12:33 +0200 | [diff] [blame] | 252 | LOG.info("Suspending environment to stop IO") |
| 253 | self.__env.suspend() |
| 254 | LOG.info("Reverting from snapshot named '{0}'".format(name)) |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 255 | self.__env.revert(name=name) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 256 | LOG.info("Resuming environment after revert") |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 257 | self.__env.resume() |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 258 | else: |
| 259 | raise exceptions.EnvironmentIsNotSet() |
| 260 | |
| 261 | try: |
| 262 | test_config_path = self._get_snapshot_config_name(name) |
Dmitry Tyzhnenko | 1fb041c | 2017-04-28 16:07:48 +0300 | [diff] [blame] | 263 | settings_oslo.reload_snapshot_config(self.__config, |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 264 | 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 Tyzhnenko | 1fb041c | 2017-04-28 16:07:48 +0300 | [diff] [blame] | 269 | self.__config.hardware.current_snapshot = name |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 270 | |
| 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 Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 288 | self.__env = models.Environment.create_environment( |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 289 | settings.config |
| 290 | ) |
| 291 | except db.IntegrityError: |
| 292 | LOG.error( |
dis | 2b2d863 | 2016-12-08 17:56:57 +0200 | [diff] [blame] | 293 | 'Seems like environment {0} already exists or contain errors' |
| 294 | ' in template.'.format(env_name) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 295 | ) |
dis | 2b2d863 | 2016-12-08 17:56:57 +0200 | [diff] [blame] | 296 | raise |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 297 | self.__env.define() |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 298 | LOG.info( |
Dennis Dmitriev | 53d3b77 | 2016-10-18 14:31:58 +0300 | [diff] [blame] | 299 | 'Environment "{0}" created'.format(env_name) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 300 | ) |
| 301 | |
Dennis Dmitriev | 7b9538f | 2017-05-15 17:01:34 +0300 | [diff] [blame] | 302 | def start(self, underlay_node_roles, timeout=480): |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 303 | """Method for start environment |
| 304 | |
| 305 | """ |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 306 | if self.__env is None: |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 307 | raise exceptions.EnvironmentIsNotSet() |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 308 | self.__env.start() |
| 309 | LOG.info('Environment "{0}" started'.format(self.__env.name)) |
Dennis Dmitriev | b01b90e | 2018-06-07 14:57:53 +0300 | [diff] [blame] | 310 | check_cloudinit_started = '[ -f /is_cloud_init_started ]' |
| 311 | check_cloudinit_finished = '[ -f /is_cloud_init_finished ]' |
| 312 | passed = {} |
Dennis Dmitriev | 7b9538f | 2017-05-15 17:01:34 +0300 | [diff] [blame] | 313 | for node in self.__env.get_nodes(role__in=underlay_node_roles): |
Dennis Dmitriev | a63bac6 | 2017-05-15 18:36:26 +0300 | [diff] [blame] | 314 | LOG.info("Waiting for SSH on node '{0}' / {1} ...".format( |
| 315 | node.name, self.node_ip(node))) |
Dmitry Tyzhnenko | b610afd | 2018-02-19 15:43:45 +0200 | [diff] [blame] | 316 | |
Dennis Dmitriev | b01b90e | 2018-06-07 14:57:53 +0300 | [diff] [blame] | 317 | def _ssh_check(host, |
| 318 | port, |
| 319 | username=settings.SSH_NODE_CREDENTIALS['login'], |
| 320 | password=settings.SSH_NODE_CREDENTIALS['password'], |
| 321 | timeout=0): |
Dmitry Tyzhnenko | b610afd | 2018-02-19 15:43:45 +0200 | [diff] [blame] | 322 | try: |
| 323 | ssh = ssh_client.SSHClient( |
| 324 | host=host, port=port, |
| 325 | auth=ssh_client.SSHAuth( |
| 326 | username=username, |
| 327 | password=password)) |
Dennis Dmitriev | b01b90e | 2018-06-07 14:57:53 +0300 | [diff] [blame] | 328 | |
| 329 | # If '/is_cloud_init_started' exists, then wait for |
| 330 | # the flag /is_cloud_init_finished |
| 331 | if ssh.execute(check_cloudinit_started)['exit_code'] == 0: |
| 332 | status = ssh.execute( |
| 333 | check_cloudinit_finished)['exit_code'] == 0 |
| 334 | # Else, just wait for SSH |
| 335 | else: |
| 336 | status = ssh.execute('echo ok')['exit_code'] == 0 |
| 337 | return status |
| 338 | |
| 339 | except (AuthenticationException, BadAuthenticationType): |
Dmitry Tyzhnenko | b610afd | 2018-02-19 15:43:45 +0200 | [diff] [blame] | 340 | return True |
| 341 | except Exception: |
| 342 | return False |
| 343 | |
Dennis Dmitriev | b01b90e | 2018-06-07 14:57:53 +0300 | [diff] [blame] | 344 | def _ssh_wait(host, |
| 345 | port, |
| 346 | username=settings.SSH_NODE_CREDENTIALS['login'], |
| 347 | password=settings.SSH_NODE_CREDENTIALS['password'], |
| 348 | timeout=0): |
| 349 | |
| 350 | if host in passed and passed[host] >= 2: |
| 351 | # host already passed the check |
| 352 | return True |
| 353 | |
| 354 | for node in self.__env.get_nodes(role__in=underlay_node_roles): |
| 355 | ip = self.node_ip(node) |
| 356 | if ip not in passed: |
| 357 | passed[ip] = 0 |
| 358 | if _ssh_check(ip, port): |
| 359 | passed[ip] += 1 |
| 360 | else: |
| 361 | passed[ip] = 0 |
Dmitry Tyzhnenko | b610afd | 2018-02-19 15:43:45 +0200 | [diff] [blame] | 362 | |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 363 | helpers.wait( |
Dmitry Tyzhnenko | b610afd | 2018-02-19 15:43:45 +0200 | [diff] [blame] | 364 | lambda: _ssh_wait(self.node_ip(node), 22), |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 365 | timeout=timeout, |
| 366 | timeout_msg="Node '{}' didn't open SSH in {} sec".format( |
| 367 | node.name, timeout |
| 368 | ) |
| 369 | ) |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 370 | LOG.info('Environment "{0}" ready'.format(self.__env.name)) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 371 | |
| 372 | def resume(self): |
| 373 | """Resume environment""" |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 374 | if self.__env is None: |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 375 | raise exceptions.EnvironmentIsNotSet() |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 376 | self.__env.resume() |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 377 | |
| 378 | def suspend(self): |
| 379 | """Suspend environment""" |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 380 | if self.__env is None: |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 381 | raise exceptions.EnvironmentIsNotSet() |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 382 | self.__env.suspend() |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 383 | |
| 384 | def stop(self): |
| 385 | """Stop environment""" |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 386 | if self.__env is None: |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 387 | raise exceptions.EnvironmentIsNotSet() |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 388 | self.__env.destroy() |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 389 | |
Tatyana Leontovich | e5ccdb3 | 2017-10-09 20:10:43 +0300 | [diff] [blame] | 390 | def destroy_node(self, node_name): |
| 391 | """Destroy node""" |
| 392 | node = self.__env.get_node(name=node_name) |
| 393 | node.destroy() |
| 394 | |
| 395 | def start_node(self, node_name): |
| 396 | """Start node""" |
| 397 | node = self.__env.get_node(name=node_name) |
| 398 | node.start() |
| 399 | |
| 400 | def reboot_node(self, node_name): |
| 401 | """Reboot node""" |
| 402 | node = self.__env.get_node(name=node_name) |
| 403 | node.reboot() |
| 404 | |
| 405 | def remove_node(self, node_name): |
| 406 | """Remove node""" |
| 407 | node = self.__env.get_node(name=node_name) |
| 408 | node.remove() |
| 409 | |
| 410 | def wait_for_node_state(self, node_name, state, timeout): |
| 411 | node = self.__env.get_node(name=node_name) |
| 412 | if 'active' in state: |
| 413 | helpers.wait(lambda: node.is_active(), |
| 414 | timeout=timeout, |
| 415 | timeout_msg=('Node {0} failed ' |
| 416 | 'to become active'.format(node))) |
| 417 | else: |
| 418 | helpers.wait(lambda: not node.is_active(), |
| 419 | timeout=timeout, |
| 420 | timeout_msg=('Node {0} failed ' |
| 421 | 'to become active'.format(node))) |
| 422 | |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 423 | def has_snapshot(self, name): |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 424 | return self.__env.has_snapshot(name) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 425 | |
| 426 | def has_snapshot_config(self, name): |
| 427 | test_config_path = self._get_snapshot_config_name(name) |
| 428 | return os.path.isfile(test_config_path) |
| 429 | |
| 430 | def delete_environment(self): |
| 431 | """Delete environment |
| 432 | |
| 433 | """ |
| 434 | LOG.debug("Deleting environment") |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 435 | self.__env.erase() |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 436 | |
| 437 | def __get_nodes_by_role(self, node_role): |
| 438 | """Get node by given role name |
| 439 | |
| 440 | :param node_role: string |
| 441 | :rtype: devops.models.Node |
| 442 | """ |
| 443 | LOG.debug('Trying to get nodes by role {0}'.format(node_role)) |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 444 | return self.__env.get_nodes(role=node_role) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 445 | |
Tatyana Leontovich | e5ccdb3 | 2017-10-09 20:10:43 +0300 | [diff] [blame] | 446 | def __get_nodes_by_name(self, node_name): |
| 447 | """Get node by given role name |
| 448 | |
| 449 | :param node_name: string |
| 450 | :rtype: devops.models.Node |
| 451 | """ |
| 452 | LOG.debug('Trying to get nodes by role {0}'.format(node_name)) |
| 453 | return self.__env.get_nodes(name=node_name) |
| 454 | |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 455 | @property |
| 456 | def master_nodes(self): |
| 457 | """Get all master nodes |
| 458 | |
| 459 | :rtype: list |
| 460 | """ |
| 461 | nodes = self.__get_nodes_by_role( |
Dennis Dmitriev | 53d3b77 | 2016-10-18 14:31:58 +0300 | [diff] [blame] | 462 | node_role=ext.UNDERLAY_NODE_ROLES.salt_master) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 463 | return nodes |
| 464 | |
| 465 | @property |
| 466 | def slave_nodes(self): |
| 467 | """Get all slave nodes |
| 468 | |
| 469 | :rtype: list |
| 470 | """ |
| 471 | nodes = self.__get_nodes_by_role( |
Dennis Dmitriev | 53d3b77 | 2016-10-18 14:31:58 +0300 | [diff] [blame] | 472 | node_role=ext.UNDERLAY_NODE_ROLES.salt_minion) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 473 | return nodes |
| 474 | |
Dennis Dmitriev | 53d3b77 | 2016-10-18 14:31:58 +0300 | [diff] [blame] | 475 | @staticmethod |
| 476 | def node_ip(node): |
| 477 | """Determine node's IP |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 478 | |
Dennis Dmitriev | 53d3b77 | 2016-10-18 14:31:58 +0300 | [diff] [blame] | 479 | :param node: devops.models.Node |
| 480 | :return: string |
| 481 | """ |
| 482 | LOG.debug('Trying to determine {0} ip.'.format(node.name)) |
| 483 | return node.get_ip_address_by_network_name( |
dis | c529838 | 2016-11-23 16:03:33 +0200 | [diff] [blame] | 484 | ext.NETWORK_TYPE.admin |
Dennis Dmitriev | 53d3b77 | 2016-10-18 14:31:58 +0300 | [diff] [blame] | 485 | ) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 486 | |
| 487 | @property |
| 488 | def nameserver(self): |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 489 | return self.__env.router(ext.NETWORK_TYPE.admin) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 490 | |
| 491 | def set_dns_config(self): |
| 492 | # Set local nameserver to use by default |
Dmitry Tyzhnenko | 1fb041c | 2017-04-28 16:07:48 +0300 | [diff] [blame] | 493 | if not self.__config.underlay.nameservers: |
| 494 | self.__config.underlay.nameservers = [self.nameserver] |
| 495 | if not self.__config.underlay.upstream_dns_servers: |
| 496 | self.__config.underlay.upstream_dns_servers = [self.nameserver] |
Dennis Dmitriev | 99b26fe | 2017-04-26 12:34:44 +0300 | [diff] [blame] | 497 | |
| 498 | def set_address_pools_config(self): |
| 499 | """Store address pools CIDRs in config object""" |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 500 | for ap in self.__env.get_address_pools(): |
Dmitry Tyzhnenko | 1fb041c | 2017-04-28 16:07:48 +0300 | [diff] [blame] | 501 | self.__config.underlay.address_pools[ap.name] = ap.net |