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 | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 74 | self.set_dhcp_ranges_config() |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 75 | |
| 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 | |
| 97 | def lvm_storages(self): |
| 98 | """Returns a dict object of lvm storages in current environment |
| 99 | |
| 100 | returned data example: |
| 101 | { |
| 102 | "master": { |
| 103 | "id": "virtio-bff72959d1a54cb19d08" |
| 104 | }, |
| 105 | "slave-0": { |
| 106 | "id": "virtio-5e33affc8fe44503839f" |
| 107 | }, |
| 108 | "slave-1": { |
| 109 | "id": "virtio-10b6a262f1ec4341a1ba" |
| 110 | }, |
| 111 | } |
| 112 | |
| 113 | :rtype: dict |
| 114 | """ |
| 115 | result = {} |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 116 | 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] | 117 | lvm = filter(lambda x: x.volume.name == 'lvm', node.disk_devices) |
| 118 | if len(lvm) == 0: |
| 119 | continue |
| 120 | lvm = lvm[0] |
| 121 | result[node.name] = {} |
| 122 | result_node = result[node.name] |
| 123 | result_node['id'] = "{bus}-{serial}".format( |
| 124 | bus=lvm.bus, |
| 125 | serial=lvm.volume.serial[:20]) |
| 126 | LOG.info("Got disk-id '{}' for node '{}'".format( |
| 127 | result_node['id'], node.name)) |
| 128 | return result |
| 129 | |
| 130 | @property |
| 131 | def _d_env_name(self): |
| 132 | """Get environment name from fuel devops config |
| 133 | |
| 134 | :rtype: string |
| 135 | """ |
| 136 | return self._devops_config['env_name'] |
| 137 | |
| 138 | def _get_env_by_name(self, name): |
| 139 | """Set existing environment by name |
| 140 | |
| 141 | :param name: string |
| 142 | """ |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 143 | self.__env = models.Environment.get(name=name) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 144 | |
| 145 | def _get_default_node_group(self): |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 146 | return self.__env.get_group(name='default') |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 147 | |
| 148 | def _get_network_pool(self, net_pool_name): |
| 149 | default_node_group = self._get_default_node_group() |
| 150 | network_pool = default_node_group.get_network_pool(name=net_pool_name) |
| 151 | return network_pool |
| 152 | |
| 153 | def get_ssh_data(self, roles=None): |
| 154 | """Generate ssh config for Underlay |
| 155 | |
| 156 | :param roles: list of strings |
| 157 | """ |
| 158 | if roles is None: |
| 159 | raise Exception("No roles specified for the environment!") |
| 160 | |
| 161 | config_ssh = [] |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 162 | for d_node in self.__env.get_nodes(role__in=roles): |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 163 | ssh_data = { |
| 164 | 'node_name': d_node.name, |
Dennis Dmitriev | 474e3f7 | 2016-10-21 16:46:09 +0300 | [diff] [blame] | 165 | 'roles': [d_node.role], |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 166 | 'address_pool': self._get_network_pool( |
dis | c529838 | 2016-11-23 16:03:33 +0200 | [diff] [blame] | 167 | ext.NETWORK_TYPE.admin).address_pool.name, |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 168 | 'host': self.node_ip(d_node), |
| 169 | 'login': settings.SSH_NODE_CREDENTIALS['login'], |
| 170 | 'password': settings.SSH_NODE_CREDENTIALS['password'], |
Artem Panchenko | db0a97f | 2017-06-27 19:09:13 +0300 | [diff] [blame] | 171 | 'keys': [k['private'] for k in self.__config.underlay.ssh_keys] |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 172 | } |
| 173 | config_ssh.append(ssh_data) |
| 174 | return config_ssh |
| 175 | |
Dennis Dmitriev | 411dd10 | 2017-09-15 16:04:47 +0300 | [diff] [blame] | 176 | def create_snapshot(self, name, description=None, force=False): |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 177 | """Create named snapshot of current env. |
| 178 | |
| 179 | - Create a libvirt snapshots for all nodes in the environment |
| 180 | - Save 'config' object to a file 'config_<name>.ini' |
| 181 | |
| 182 | :name: string |
| 183 | """ |
Dennis Dmitriev | 411dd10 | 2017-09-15 16:04:47 +0300 | [diff] [blame] | 184 | if not settings.MAKE_SNAPSHOT_STAGES and not force: |
| 185 | msg = ("[ SKIP snapshot '{0}' because MAKE_SNAPSHOT_STAGES=false ]" |
| 186 | " {1}".format(name, description or '')) |
| 187 | LOG.info("\n\n{0}\n{1}".format(msg, '*' * len(msg))) |
| 188 | return |
Dennis Dmitriev | 99b26fe | 2017-04-26 12:34:44 +0300 | [diff] [blame] | 189 | msg = "[ Create snapshot '{0}' ] {1}".format(name, description or '') |
| 190 | LOG.info("\n\n{0}\n{1}".format(msg, '*' * len(msg))) |
| 191 | |
Dmitry Tyzhnenko | 1fb041c | 2017-04-28 16:07:48 +0300 | [diff] [blame] | 192 | self.__config.hardware.current_snapshot = name |
Dennis Dmitriev | 99b26fe | 2017-04-26 12:34:44 +0300 | [diff] [blame] | 193 | LOG.info("Set current snapshot in config to '{0}'".format( |
Dmitry Tyzhnenko | 1fb041c | 2017-04-28 16:07:48 +0300 | [diff] [blame] | 194 | self.__config.hardware.current_snapshot)) |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 195 | if self.__env is not None: |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 196 | LOG.info('trying to suspend ....') |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 197 | self.__env.suspend() |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 198 | LOG.info('trying to snapshot ....') |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 199 | self.__env.snapshot(name, description=description, force=True) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 200 | LOG.info('trying to resume ....') |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 201 | self.__env.resume() |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 202 | else: |
| 203 | raise exceptions.EnvironmentIsNotSet() |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 204 | settings_oslo.save_config(self.__config, name, self.__env.name) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 205 | |
Dennis Dmitriev | 99b26fe | 2017-04-26 12:34:44 +0300 | [diff] [blame] | 206 | if settings.VIRTUAL_ENV: |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 207 | venv_msg = "source {0}/bin/activate;\n".format( |
| 208 | settings.VIRTUAL_ENV) |
Dennis Dmitriev | 99b26fe | 2017-04-26 12:34:44 +0300 | [diff] [blame] | 209 | else: |
| 210 | venv_msg = "" |
| 211 | LOG.info("To revert the snapshot:\n\n" |
| 212 | "************************************\n" |
| 213 | "{venv_msg}" |
| 214 | "dos.py revert {env_name} {snapshot_name};\n" |
| 215 | "dos.py resume {env_name};\n" |
| 216 | "# dos.py time-sync {env_name}; # Optional\n" |
Artem Panchenko | db0a97f | 2017-06-27 19:09:13 +0300 | [diff] [blame] | 217 | "ssh -i {key_file} {login}@{salt_master_host} " |
| 218 | "# Optional password: {password}\n" |
Dennis Dmitriev | 99b26fe | 2017-04-26 12:34:44 +0300 | [diff] [blame] | 219 | "************************************\n" |
| 220 | .format(venv_msg=venv_msg, |
Dennis Dmitriev | 68671a6 | 2017-05-13 16:40:32 +0300 | [diff] [blame] | 221 | env_name=self._d_env_name, |
Dennis Dmitriev | 99b26fe | 2017-04-26 12:34:44 +0300 | [diff] [blame] | 222 | snapshot_name=name, |
| 223 | login=settings.SSH_NODE_CREDENTIALS['login'], |
| 224 | password=settings.SSH_NODE_CREDENTIALS['password'], |
Artem Panchenko | db0a97f | 2017-06-27 19:09:13 +0300 | [diff] [blame] | 225 | salt_master_host=self.__config.salt.salt_master_host, |
| 226 | key_file=self.__config.underlay.ssh_key_file)) |
Dennis Dmitriev | 99b26fe | 2017-04-26 12:34:44 +0300 | [diff] [blame] | 227 | |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 228 | def _get_snapshot_config_name(self, snapshot_name): |
| 229 | """Get config name for the environment""" |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 230 | env_name = self.__env.name |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 231 | if env_name is None: |
| 232 | env_name = 'config' |
| 233 | test_config_path = os.path.join( |
| 234 | settings.LOGS_DIR, '{0}_{1}.ini'.format(env_name, snapshot_name)) |
| 235 | return test_config_path |
| 236 | |
| 237 | def revert_snapshot(self, name): |
| 238 | """Revert snapshot by name |
| 239 | |
| 240 | - Revert a libvirt snapshots for all nodes in the environment |
| 241 | - Try to reload 'config' object from a file 'config_<name>.ini' |
| 242 | If the file not found, then pass with defaults. |
| 243 | - Set <name> as the current state of the environment after reload |
| 244 | |
| 245 | :param name: string |
| 246 | """ |
Dennis Dmitriev | 411dd10 | 2017-09-15 16:04:47 +0300 | [diff] [blame] | 247 | if not settings.MAKE_SNAPSHOT_STAGES: |
| 248 | LOG.info("SKIP reverting from snapshot '{0}' " |
| 249 | "because MAKE_SNAPSHOT_STAGES=false".format(name)) |
| 250 | return |
| 251 | |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 252 | if self.__env is not None: |
Dennis Dmitriev | a5978eb | 2018-02-21 10:12:33 +0200 | [diff] [blame] | 253 | LOG.info("Suspending environment to stop IO") |
| 254 | self.__env.suspend() |
| 255 | LOG.info("Reverting from snapshot named '{0}'".format(name)) |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 256 | self.__env.revert(name=name) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 257 | LOG.info("Resuming environment after revert") |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 258 | self.__env.resume() |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 259 | else: |
| 260 | raise exceptions.EnvironmentIsNotSet() |
| 261 | |
| 262 | try: |
| 263 | test_config_path = self._get_snapshot_config_name(name) |
Dmitry Tyzhnenko | 1fb041c | 2017-04-28 16:07:48 +0300 | [diff] [blame] | 264 | settings_oslo.reload_snapshot_config(self.__config, |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 265 | test_config_path) |
| 266 | except cfg.ConfigFilesNotFoundError as conf_err: |
| 267 | LOG.error("Config file(s) {0} not found!".format( |
| 268 | conf_err.config_files)) |
| 269 | |
Dmitry Tyzhnenko | 1fb041c | 2017-04-28 16:07:48 +0300 | [diff] [blame] | 270 | self.__config.hardware.current_snapshot = name |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 271 | |
| 272 | def _create_environment(self): |
| 273 | """Create environment and start VMs. |
| 274 | |
| 275 | If config was provided earlier, we simply create and start VMs, |
| 276 | otherwise we tries to generate config from self.config_file, |
| 277 | """ |
| 278 | if self._devops_config.config is None: |
| 279 | raise exceptions.DevopsConfigPathIsNotSet() |
| 280 | settings = self._devops_config |
| 281 | env_name = settings['env_name'] |
| 282 | LOG.debug( |
| 283 | 'Preparing to create environment named "{0}"'.format(env_name) |
| 284 | ) |
| 285 | if env_name is None: |
| 286 | LOG.error('Environment name is not set!') |
| 287 | raise exceptions.EnvironmentNameIsNotSet() |
| 288 | try: |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 289 | self.__env = models.Environment.create_environment( |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 290 | settings.config |
| 291 | ) |
| 292 | except db.IntegrityError: |
| 293 | LOG.error( |
dis | 2b2d863 | 2016-12-08 17:56:57 +0200 | [diff] [blame] | 294 | 'Seems like environment {0} already exists or contain errors' |
| 295 | ' in template.'.format(env_name) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 296 | ) |
dis | 2b2d863 | 2016-12-08 17:56:57 +0200 | [diff] [blame] | 297 | raise |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 298 | self.__env.define() |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 299 | LOG.info( |
Dennis Dmitriev | 53d3b77 | 2016-10-18 14:31:58 +0300 | [diff] [blame] | 300 | 'Environment "{0}" created'.format(env_name) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 301 | ) |
| 302 | |
Dennis Dmitriev | 7b9538f | 2017-05-15 17:01:34 +0300 | [diff] [blame] | 303 | def start(self, underlay_node_roles, timeout=480): |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 304 | """Method for start environment |
| 305 | |
| 306 | """ |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 307 | if self.__env is None: |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 308 | raise exceptions.EnvironmentIsNotSet() |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 309 | self.__env.start() |
| 310 | LOG.info('Environment "{0}" started'.format(self.__env.name)) |
Dennis Dmitriev | b01b90e | 2018-06-07 14:57:53 +0300 | [diff] [blame] | 311 | check_cloudinit_started = '[ -f /is_cloud_init_started ]' |
| 312 | check_cloudinit_finished = '[ -f /is_cloud_init_finished ]' |
Dennis Dmitriev | 86085b4 | 2018-07-02 14:14:25 +0300 | [diff] [blame] | 313 | check_cloudinit_failed = 'cat /is_cloud_init_failed' |
Dennis Dmitriev | b01b90e | 2018-06-07 14:57:53 +0300 | [diff] [blame] | 314 | passed = {} |
Dennis Dmitriev | 7b9538f | 2017-05-15 17:01:34 +0300 | [diff] [blame] | 315 | for node in self.__env.get_nodes(role__in=underlay_node_roles): |
Dennis Dmitriev | a63bac6 | 2017-05-15 18:36:26 +0300 | [diff] [blame] | 316 | LOG.info("Waiting for SSH on node '{0}' / {1} ...".format( |
| 317 | node.name, self.node_ip(node))) |
Dmitry Tyzhnenko | b610afd | 2018-02-19 15:43:45 +0200 | [diff] [blame] | 318 | |
Dennis Dmitriev | b01b90e | 2018-06-07 14:57:53 +0300 | [diff] [blame] | 319 | def _ssh_check(host, |
| 320 | port, |
| 321 | username=settings.SSH_NODE_CREDENTIALS['login'], |
| 322 | password=settings.SSH_NODE_CREDENTIALS['password'], |
| 323 | timeout=0): |
Dmitry Tyzhnenko | b610afd | 2018-02-19 15:43:45 +0200 | [diff] [blame] | 324 | try: |
| 325 | ssh = ssh_client.SSHClient( |
| 326 | host=host, port=port, |
| 327 | auth=ssh_client.SSHAuth( |
| 328 | username=username, |
| 329 | password=password)) |
Dennis Dmitriev | b01b90e | 2018-06-07 14:57:53 +0300 | [diff] [blame] | 330 | |
| 331 | # If '/is_cloud_init_started' exists, then wait for |
| 332 | # the flag /is_cloud_init_finished |
| 333 | if ssh.execute(check_cloudinit_started)['exit_code'] == 0: |
Dennis Dmitriev | 86085b4 | 2018-07-02 14:14:25 +0300 | [diff] [blame] | 334 | result = ssh.execute(check_cloudinit_failed) |
| 335 | if result['exit_code'] == 0: |
| 336 | raise exceptions.EnvironmentNodeIsNotStarted( |
| 337 | "{0}:{1}".format(host, port), |
| 338 | result.stdout_str) |
| 339 | |
Dennis Dmitriev | b01b90e | 2018-06-07 14:57:53 +0300 | [diff] [blame] | 340 | status = ssh.execute( |
| 341 | check_cloudinit_finished)['exit_code'] == 0 |
| 342 | # Else, just wait for SSH |
| 343 | else: |
| 344 | status = ssh.execute('echo ok')['exit_code'] == 0 |
| 345 | return status |
| 346 | |
| 347 | except (AuthenticationException, BadAuthenticationType): |
Dmitry Tyzhnenko | b610afd | 2018-02-19 15:43:45 +0200 | [diff] [blame] | 348 | return True |
| 349 | except Exception: |
| 350 | return False |
| 351 | |
Dennis Dmitriev | b01b90e | 2018-06-07 14:57:53 +0300 | [diff] [blame] | 352 | def _ssh_wait(host, |
| 353 | port, |
| 354 | username=settings.SSH_NODE_CREDENTIALS['login'], |
| 355 | password=settings.SSH_NODE_CREDENTIALS['password'], |
| 356 | timeout=0): |
| 357 | |
| 358 | if host in passed and passed[host] >= 2: |
| 359 | # host already passed the check |
| 360 | return True |
| 361 | |
| 362 | for node in self.__env.get_nodes(role__in=underlay_node_roles): |
| 363 | ip = self.node_ip(node) |
| 364 | if ip not in passed: |
| 365 | passed[ip] = 0 |
| 366 | if _ssh_check(ip, port): |
| 367 | passed[ip] += 1 |
| 368 | else: |
| 369 | passed[ip] = 0 |
Dmitry Tyzhnenko | b610afd | 2018-02-19 15:43:45 +0200 | [diff] [blame] | 370 | |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 371 | helpers.wait( |
Dmitry Tyzhnenko | b610afd | 2018-02-19 15:43:45 +0200 | [diff] [blame] | 372 | lambda: _ssh_wait(self.node_ip(node), 22), |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 373 | timeout=timeout, |
| 374 | timeout_msg="Node '{}' didn't open SSH in {} sec".format( |
| 375 | node.name, timeout |
| 376 | ) |
| 377 | ) |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 378 | LOG.info('Environment "{0}" ready'.format(self.__env.name)) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 379 | |
| 380 | def resume(self): |
| 381 | """Resume environment""" |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 382 | if self.__env is None: |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 383 | raise exceptions.EnvironmentIsNotSet() |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 384 | self.__env.resume() |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 385 | |
| 386 | def suspend(self): |
| 387 | """Suspend environment""" |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 388 | if self.__env is None: |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 389 | raise exceptions.EnvironmentIsNotSet() |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 390 | self.__env.suspend() |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 391 | |
| 392 | def stop(self): |
| 393 | """Stop environment""" |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 394 | if self.__env is None: |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 395 | raise exceptions.EnvironmentIsNotSet() |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 396 | self.__env.destroy() |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 397 | |
Tatyana Leontovich | e5ccdb3 | 2017-10-09 20:10:43 +0300 | [diff] [blame] | 398 | def destroy_node(self, node_name): |
| 399 | """Destroy node""" |
| 400 | node = self.__env.get_node(name=node_name) |
| 401 | node.destroy() |
| 402 | |
| 403 | def start_node(self, node_name): |
| 404 | """Start node""" |
| 405 | node = self.__env.get_node(name=node_name) |
| 406 | node.start() |
| 407 | |
| 408 | def reboot_node(self, node_name): |
| 409 | """Reboot node""" |
| 410 | node = self.__env.get_node(name=node_name) |
| 411 | node.reboot() |
| 412 | |
| 413 | def remove_node(self, node_name): |
| 414 | """Remove node""" |
| 415 | node = self.__env.get_node(name=node_name) |
| 416 | node.remove() |
| 417 | |
| 418 | def wait_for_node_state(self, node_name, state, timeout): |
| 419 | node = self.__env.get_node(name=node_name) |
| 420 | if 'active' in state: |
| 421 | helpers.wait(lambda: node.is_active(), |
| 422 | timeout=timeout, |
| 423 | timeout_msg=('Node {0} failed ' |
| 424 | 'to become active'.format(node))) |
| 425 | else: |
| 426 | helpers.wait(lambda: not node.is_active(), |
| 427 | timeout=timeout, |
| 428 | timeout_msg=('Node {0} failed ' |
| 429 | 'to become active'.format(node))) |
| 430 | |
Vladimir Jigulin | ee1faa5 | 2018-06-25 13:00:51 +0400 | [diff] [blame] | 431 | def warm_shutdown_nodes(self, underlay, nodes_prefix, timeout=600): |
| 432 | node_names = underlay.get_target_node_names(nodes_prefix) |
| 433 | for node in node_names: |
| 434 | LOG.debug('Shutdown node {0}'.format(node)) |
| 435 | underlay.check_call(cmd="shutdown +1", node_name=node) |
| 436 | for node in node_names: |
| 437 | self.wait_for_node_state(node, state='offline', timeout=timeout) |
| 438 | |
| 439 | def warm_restart_nodes(self, underlay, nodes_prefix, timeout=600): |
| 440 | self.warm_shutdown_nodes(underlay, nodes_prefix, timeout) |
| 441 | node_names = underlay.get_target_node_names(nodes_prefix) |
| 442 | for node in node_names: |
| 443 | LOG.debug('Starting node {0}'.format(node)) |
| 444 | self.start_node(node) |
| 445 | self.wait_for_node_state(node, state='active', timeout=timeout) |
| 446 | |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 447 | def has_snapshot(self, name): |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 448 | return self.__env.has_snapshot(name) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 449 | |
| 450 | def has_snapshot_config(self, name): |
| 451 | test_config_path = self._get_snapshot_config_name(name) |
| 452 | return os.path.isfile(test_config_path) |
| 453 | |
| 454 | def delete_environment(self): |
| 455 | """Delete environment |
| 456 | |
| 457 | """ |
| 458 | LOG.debug("Deleting environment") |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 459 | self.__env.erase() |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 460 | |
| 461 | def __get_nodes_by_role(self, node_role): |
| 462 | """Get node by given role name |
| 463 | |
| 464 | :param node_role: string |
| 465 | :rtype: devops.models.Node |
| 466 | """ |
| 467 | LOG.debug('Trying to get nodes by role {0}'.format(node_role)) |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 468 | return self.__env.get_nodes(role=node_role) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 469 | |
Tatyana Leontovich | e5ccdb3 | 2017-10-09 20:10:43 +0300 | [diff] [blame] | 470 | def __get_nodes_by_name(self, node_name): |
| 471 | """Get node by given role name |
| 472 | |
| 473 | :param node_name: string |
| 474 | :rtype: devops.models.Node |
| 475 | """ |
| 476 | LOG.debug('Trying to get nodes by role {0}'.format(node_name)) |
| 477 | return self.__env.get_nodes(name=node_name) |
| 478 | |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 479 | @property |
| 480 | def master_nodes(self): |
| 481 | """Get all master nodes |
| 482 | |
| 483 | :rtype: list |
| 484 | """ |
| 485 | nodes = self.__get_nodes_by_role( |
Dennis Dmitriev | 53d3b77 | 2016-10-18 14:31:58 +0300 | [diff] [blame] | 486 | node_role=ext.UNDERLAY_NODE_ROLES.salt_master) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 487 | return nodes |
| 488 | |
| 489 | @property |
| 490 | def slave_nodes(self): |
| 491 | """Get all slave nodes |
| 492 | |
| 493 | :rtype: list |
| 494 | """ |
| 495 | nodes = self.__get_nodes_by_role( |
Dennis Dmitriev | 53d3b77 | 2016-10-18 14:31:58 +0300 | [diff] [blame] | 496 | node_role=ext.UNDERLAY_NODE_ROLES.salt_minion) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 497 | return nodes |
| 498 | |
Dennis Dmitriev | 53d3b77 | 2016-10-18 14:31:58 +0300 | [diff] [blame] | 499 | @staticmethod |
| 500 | def node_ip(node): |
| 501 | """Determine node's IP |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 502 | |
Dennis Dmitriev | 53d3b77 | 2016-10-18 14:31:58 +0300 | [diff] [blame] | 503 | :param node: devops.models.Node |
| 504 | :return: string |
| 505 | """ |
| 506 | LOG.debug('Trying to determine {0} ip.'.format(node.name)) |
| 507 | return node.get_ip_address_by_network_name( |
dis | c529838 | 2016-11-23 16:03:33 +0200 | [diff] [blame] | 508 | ext.NETWORK_TYPE.admin |
Dennis Dmitriev | 53d3b77 | 2016-10-18 14:31:58 +0300 | [diff] [blame] | 509 | ) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 510 | |
| 511 | @property |
| 512 | def nameserver(self): |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 513 | return self.__env.router(ext.NETWORK_TYPE.admin) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 514 | |
| 515 | def set_dns_config(self): |
| 516 | # Set local nameserver to use by default |
Dmitry Tyzhnenko | 1fb041c | 2017-04-28 16:07:48 +0300 | [diff] [blame] | 517 | if not self.__config.underlay.nameservers: |
| 518 | self.__config.underlay.nameservers = [self.nameserver] |
| 519 | if not self.__config.underlay.upstream_dns_servers: |
| 520 | self.__config.underlay.upstream_dns_servers = [self.nameserver] |
Dennis Dmitriev | 99b26fe | 2017-04-26 12:34:44 +0300 | [diff] [blame] | 521 | |
| 522 | def set_address_pools_config(self): |
| 523 | """Store address pools CIDRs in config object""" |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 524 | for ap in self.__env.get_address_pools(): |
Dmitry Tyzhnenko | 1fb041c | 2017-04-28 16:07:48 +0300 | [diff] [blame] | 525 | self.__config.underlay.address_pools[ap.name] = ap.net |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 526 | |
| 527 | def set_dhcp_ranges_config(self): |
| 528 | """Store DHCP ranges in config object""" |
| 529 | for ap in self.__env.get_address_pools(): |
| 530 | if "gateway" in ap.ip_reserved and "dhcp" in ap.ip_ranges: |
| 531 | self.__config.underlay.dhcp_ranges[ap.name] = { |
| 532 | "cidr": ap.net, |
| 533 | "start": ap.ip_range_start("dhcp"), |
| 534 | "end": ap.ip_range_end("dhcp"), |
| 535 | "gateway": ap.gateway, |
| 536 | } |