blob: 958e7f14174688c8d4e1f471cebd84b7457c0ab1 [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
19from devops import models
20from django import db
21from oslo_config import cfg
22
23from tcp_tests import settings
24from tcp_tests import settings_oslo
25from tcp_tests.helpers import env_config
26from tcp_tests.helpers import ext
27from tcp_tests.helpers import exceptions
28from tcp_tests import logger
29
30LOG = logger.logger
31
32
33class EnvironmentManager(object):
34 """Class-helper for creating VMs via devops environments"""
35
Dmitry Tyzhnenko1fb041c2017-04-28 16:07:48 +030036 __config = None
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030037
38 def __init__(self, config=None):
39 """Initializing class instance and create the environment
40
41 :param config: oslo.config object
42 :param config.hardware.conf_path: path to devops YAML template
43 :param config.hardware.current_snapshot: name of the snapshot that
44 descriebe environment status.
45 """
46 self.__devops_config = env_config.EnvironmentConfig()
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +030047 self.__env = None
Dmitry Tyzhnenko1fb041c2017-04-28 16:07:48 +030048 self.__config = config
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030049
50 if config.hardware.conf_path is not None:
Artem Panchenkodb0a97f2017-06-27 19:09:13 +030051 options = {
52 'config': self.__config,
53 }
54 self._devops_config.load_template(config.hardware.conf_path,
55 options=options)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030056 else:
57 raise Exception("Devops YAML template is not set in config object")
58
59 try:
60 self._get_env_by_name(self._d_env_name)
61 if not self.has_snapshot(config.hardware.current_snapshot):
62 raise exceptions.EnvironmentSnapshotMissing(
63 self._d_env_name, config.hardware.current_snapshot)
64 except error.DevopsObjNotFound:
65 LOG.info("Environment doesn't exist, creating a new one")
66 self._create_environment()
67 self.set_dns_config()
Dennis Dmitriev99b26fe2017-04-26 12:34:44 +030068 self.set_address_pools_config()
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030069
70 @property
71 def _devops_config(self):
72 return self.__devops_config
73
74 @_devops_config.setter
75 def _devops_config(self, conf):
76 """Setter for self.__devops_config
77
78 :param conf: tcp_tests.helpers.env_config.EnvironmentConfig
79 """
80 if not isinstance(conf, env_config.EnvironmentConfig):
81 msg = ("Unexpected type of devops config. Got '{0}' " +
82 "instead of '{1}'")
83 raise TypeError(
84 msg.format(
85 type(conf).__name__,
86 env_config.EnvironmentConfig.__name__
87 )
88 )
89 self.__devops_config = conf
90
91 def lvm_storages(self):
92 """Returns a dict object of lvm storages in current environment
93
94 returned data example:
95 {
96 "master": {
97 "id": "virtio-bff72959d1a54cb19d08"
98 },
99 "slave-0": {
100 "id": "virtio-5e33affc8fe44503839f"
101 },
102 "slave-1": {
103 "id": "virtio-10b6a262f1ec4341a1ba"
104 },
105 }
106
107 :rtype: dict
108 """
109 result = {}
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300110 for node in self.__env.get_nodes(role__in=ext.UNDERLAY_NODE_ROLES):
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300111 lvm = filter(lambda x: x.volume.name == 'lvm', node.disk_devices)
112 if len(lvm) == 0:
113 continue
114 lvm = lvm[0]
115 result[node.name] = {}
116 result_node = result[node.name]
117 result_node['id'] = "{bus}-{serial}".format(
118 bus=lvm.bus,
119 serial=lvm.volume.serial[:20])
120 LOG.info("Got disk-id '{}' for node '{}'".format(
121 result_node['id'], node.name))
122 return result
123
124 @property
125 def _d_env_name(self):
126 """Get environment name from fuel devops config
127
128 :rtype: string
129 """
130 return self._devops_config['env_name']
131
132 def _get_env_by_name(self, name):
133 """Set existing environment by name
134
135 :param name: string
136 """
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300137 self.__env = models.Environment.get(name=name)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300138
139 def _get_default_node_group(self):
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300140 return self.__env.get_group(name='default')
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300141
142 def _get_network_pool(self, net_pool_name):
143 default_node_group = self._get_default_node_group()
144 network_pool = default_node_group.get_network_pool(name=net_pool_name)
145 return network_pool
146
147 def get_ssh_data(self, roles=None):
148 """Generate ssh config for Underlay
149
150 :param roles: list of strings
151 """
152 if roles is None:
153 raise Exception("No roles specified for the environment!")
154
155 config_ssh = []
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300156 for d_node in self.__env.get_nodes(role__in=roles):
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300157 ssh_data = {
158 'node_name': d_node.name,
Dennis Dmitriev474e3f72016-10-21 16:46:09 +0300159 'roles': [d_node.role],
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300160 'address_pool': self._get_network_pool(
disc5298382016-11-23 16:03:33 +0200161 ext.NETWORK_TYPE.admin).address_pool.name,
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300162 'host': self.node_ip(d_node),
163 'login': settings.SSH_NODE_CREDENTIALS['login'],
164 'password': settings.SSH_NODE_CREDENTIALS['password'],
Artem Panchenkodb0a97f2017-06-27 19:09:13 +0300165 'keys': [k['private'] for k in self.__config.underlay.ssh_keys]
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300166 }
167 config_ssh.append(ssh_data)
168 return config_ssh
169
170 def create_snapshot(self, name, description=None):
171 """Create named snapshot of current env.
172
173 - Create a libvirt snapshots for all nodes in the environment
174 - Save 'config' object to a file 'config_<name>.ini'
175
176 :name: string
177 """
Dennis Dmitriev99b26fe2017-04-26 12:34:44 +0300178 msg = "[ Create snapshot '{0}' ] {1}".format(name, description or '')
179 LOG.info("\n\n{0}\n{1}".format(msg, '*' * len(msg)))
180
Dmitry Tyzhnenko1fb041c2017-04-28 16:07:48 +0300181 self.__config.hardware.current_snapshot = name
Dennis Dmitriev99b26fe2017-04-26 12:34:44 +0300182 LOG.info("Set current snapshot in config to '{0}'".format(
Dmitry Tyzhnenko1fb041c2017-04-28 16:07:48 +0300183 self.__config.hardware.current_snapshot))
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300184 if self.__env is not None:
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300185 LOG.info('trying to suspend ....')
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300186 self.__env.suspend()
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300187 LOG.info('trying to snapshot ....')
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300188 self.__env.snapshot(name, description=description, force=True)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300189 LOG.info('trying to resume ....')
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300190 self.__env.resume()
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300191 else:
192 raise exceptions.EnvironmentIsNotSet()
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300193 settings_oslo.save_config(self.__config, name, self.__env.name)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300194
Dennis Dmitriev99b26fe2017-04-26 12:34:44 +0300195 if settings.VIRTUAL_ENV:
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +0300196 venv_msg = "source {0}/bin/activate;\n".format(
197 settings.VIRTUAL_ENV)
Dennis Dmitriev99b26fe2017-04-26 12:34:44 +0300198 else:
199 venv_msg = ""
200 LOG.info("To revert the snapshot:\n\n"
201 "************************************\n"
202 "{venv_msg}"
203 "dos.py revert {env_name} {snapshot_name};\n"
204 "dos.py resume {env_name};\n"
205 "# dos.py time-sync {env_name}; # Optional\n"
Artem Panchenkodb0a97f2017-06-27 19:09:13 +0300206 "ssh -i {key_file} {login}@{salt_master_host} "
207 "# Optional password: {password}\n"
Dennis Dmitriev99b26fe2017-04-26 12:34:44 +0300208 "************************************\n"
209 .format(venv_msg=venv_msg,
Dennis Dmitriev68671a62017-05-13 16:40:32 +0300210 env_name=self._d_env_name,
Dennis Dmitriev99b26fe2017-04-26 12:34:44 +0300211 snapshot_name=name,
212 login=settings.SSH_NODE_CREDENTIALS['login'],
213 password=settings.SSH_NODE_CREDENTIALS['password'],
Artem Panchenkodb0a97f2017-06-27 19:09:13 +0300214 salt_master_host=self.__config.salt.salt_master_host,
215 key_file=self.__config.underlay.ssh_key_file))
Dennis Dmitriev99b26fe2017-04-26 12:34:44 +0300216
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300217 def _get_snapshot_config_name(self, snapshot_name):
218 """Get config name for the environment"""
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300219 env_name = self.__env.name
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300220 if env_name is None:
221 env_name = 'config'
222 test_config_path = os.path.join(
223 settings.LOGS_DIR, '{0}_{1}.ini'.format(env_name, snapshot_name))
224 return test_config_path
225
226 def revert_snapshot(self, name):
227 """Revert snapshot by name
228
229 - Revert a libvirt snapshots for all nodes in the environment
230 - Try to reload 'config' object from a file 'config_<name>.ini'
231 If the file not found, then pass with defaults.
232 - Set <name> as the current state of the environment after reload
233
234 :param name: string
235 """
236 LOG.info("Reverting from snapshot named '{0}'".format(name))
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300237 if self.__env is not None:
238 self.__env.revert(name=name)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300239 LOG.info("Resuming environment after revert")
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300240 self.__env.resume()
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300241 else:
242 raise exceptions.EnvironmentIsNotSet()
243
244 try:
245 test_config_path = self._get_snapshot_config_name(name)
Dmitry Tyzhnenko1fb041c2017-04-28 16:07:48 +0300246 settings_oslo.reload_snapshot_config(self.__config,
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300247 test_config_path)
248 except cfg.ConfigFilesNotFoundError as conf_err:
249 LOG.error("Config file(s) {0} not found!".format(
250 conf_err.config_files))
251
Dmitry Tyzhnenko1fb041c2017-04-28 16:07:48 +0300252 self.__config.hardware.current_snapshot = name
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300253
254 def _create_environment(self):
255 """Create environment and start VMs.
256
257 If config was provided earlier, we simply create and start VMs,
258 otherwise we tries to generate config from self.config_file,
259 """
260 if self._devops_config.config is None:
261 raise exceptions.DevopsConfigPathIsNotSet()
262 settings = self._devops_config
263 env_name = settings['env_name']
264 LOG.debug(
265 'Preparing to create environment named "{0}"'.format(env_name)
266 )
267 if env_name is None:
268 LOG.error('Environment name is not set!')
269 raise exceptions.EnvironmentNameIsNotSet()
270 try:
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300271 self.__env = models.Environment.create_environment(
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300272 settings.config
273 )
274 except db.IntegrityError:
275 LOG.error(
dis2b2d8632016-12-08 17:56:57 +0200276 'Seems like environment {0} already exists or contain errors'
277 ' in template.'.format(env_name)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300278 )
dis2b2d8632016-12-08 17:56:57 +0200279 raise
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300280 self.__env.define()
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300281 LOG.info(
Dennis Dmitriev53d3b772016-10-18 14:31:58 +0300282 'Environment "{0}" created'.format(env_name)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300283 )
284
Dennis Dmitriev7b9538f2017-05-15 17:01:34 +0300285 def start(self, underlay_node_roles, timeout=480):
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300286 """Method for start environment
287
288 """
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300289 if self.__env is None:
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300290 raise exceptions.EnvironmentIsNotSet()
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300291 self.__env.start()
292 LOG.info('Environment "{0}" started'.format(self.__env.name))
Dennis Dmitriev7b9538f2017-05-15 17:01:34 +0300293 for node in self.__env.get_nodes(role__in=underlay_node_roles):
Dennis Dmitrieva63bac62017-05-15 18:36:26 +0300294 LOG.info("Waiting for SSH on node '{0}' / {1} ...".format(
295 node.name, self.node_ip(node)))
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300296 helpers.wait(
297 lambda: helpers.tcp_ping(self.node_ip(node), 22),
298 timeout=timeout,
299 timeout_msg="Node '{}' didn't open SSH in {} sec".format(
300 node.name, timeout
301 )
302 )
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300303 LOG.info('Environment "{0}" ready'.format(self.__env.name))
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300304
305 def resume(self):
306 """Resume environment"""
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300307 if self.__env is None:
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300308 raise exceptions.EnvironmentIsNotSet()
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300309 self.__env.resume()
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300310
311 def suspend(self):
312 """Suspend environment"""
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300313 if self.__env is None:
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300314 raise exceptions.EnvironmentIsNotSet()
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300315 self.__env.suspend()
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300316
317 def stop(self):
318 """Stop environment"""
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300319 if self.__env is None:
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300320 raise exceptions.EnvironmentIsNotSet()
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300321 self.__env.destroy()
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300322
323 def has_snapshot(self, name):
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300324 return self.__env.has_snapshot(name)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300325
326 def has_snapshot_config(self, name):
327 test_config_path = self._get_snapshot_config_name(name)
328 return os.path.isfile(test_config_path)
329
330 def delete_environment(self):
331 """Delete environment
332
333 """
334 LOG.debug("Deleting environment")
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300335 self.__env.erase()
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300336
337 def __get_nodes_by_role(self, node_role):
338 """Get node by given role name
339
340 :param node_role: string
341 :rtype: devops.models.Node
342 """
343 LOG.debug('Trying to get nodes by role {0}'.format(node_role))
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300344 return self.__env.get_nodes(role=node_role)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300345
346 @property
347 def master_nodes(self):
348 """Get all master nodes
349
350 :rtype: list
351 """
352 nodes = self.__get_nodes_by_role(
Dennis Dmitriev53d3b772016-10-18 14:31:58 +0300353 node_role=ext.UNDERLAY_NODE_ROLES.salt_master)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300354 return nodes
355
356 @property
357 def slave_nodes(self):
358 """Get all slave nodes
359
360 :rtype: list
361 """
362 nodes = self.__get_nodes_by_role(
Dennis Dmitriev53d3b772016-10-18 14:31:58 +0300363 node_role=ext.UNDERLAY_NODE_ROLES.salt_minion)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300364 return nodes
365
Dennis Dmitriev53d3b772016-10-18 14:31:58 +0300366 @staticmethod
367 def node_ip(node):
368 """Determine node's IP
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300369
Dennis Dmitriev53d3b772016-10-18 14:31:58 +0300370 :param node: devops.models.Node
371 :return: string
372 """
373 LOG.debug('Trying to determine {0} ip.'.format(node.name))
374 return node.get_ip_address_by_network_name(
disc5298382016-11-23 16:03:33 +0200375 ext.NETWORK_TYPE.admin
Dennis Dmitriev53d3b772016-10-18 14:31:58 +0300376 )
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300377
378 @property
379 def nameserver(self):
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300380 return self.__env.router(ext.NETWORK_TYPE.admin)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300381
382 def set_dns_config(self):
383 # Set local nameserver to use by default
Dmitry Tyzhnenko1fb041c2017-04-28 16:07:48 +0300384 if not self.__config.underlay.nameservers:
385 self.__config.underlay.nameservers = [self.nameserver]
386 if not self.__config.underlay.upstream_dns_servers:
387 self.__config.underlay.upstream_dns_servers = [self.nameserver]
Dennis Dmitriev99b26fe2017-04-26 12:34:44 +0300388
389 def set_address_pools_config(self):
390 """Store address pools CIDRs in config object"""
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300391 for ap in self.__env.get_address_pools():
Dmitry Tyzhnenko1fb041c2017-04-28 16:07:48 +0300392 self.__config.underlay.address_pools[ap.name] = ap.net