blob: 8aa99138a23a299a31a13cf30ef910ac64ce2872 [file] [log] [blame]
koder aka kdanilov39e449e2016-12-17 15:15:26 +02001import os.path
2import socket
3import logging
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +02004from typing import Dict, Any, List, Tuple, cast, Optional
koder aka kdanilov39e449e2016-12-17 15:15:26 +02005
6from .node_interfaces import NodeInfo
7from .config import ConfigBlock, Config
8from .ssh_utils import ConnCreds
9from .openstack_api import (os_connect, find_vms,
10 OSCreds, get_openstack_credentials, prepare_os, launch_vms, clear_nodes)
11from .test_run_class import TestRun
12from .stage import Stage, StepOrder
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020013from .utils import LogError, StopTestError, get_creds_openrc, to_ip
koder aka kdanilov39e449e2016-12-17 15:15:26 +020014
15
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020016logger = logging.getLogger("wally")
koder aka kdanilov39e449e2016-12-17 15:15:26 +020017
18
19def get_floating_ip(vm: Any) -> str:
20 """Get VM floating IP address"""
21
22 for net_name, ifaces in vm.addresses.items():
23 for iface in ifaces:
24 if iface.get('OS-EXT-IPS:type') == "floating":
25 return iface['addr']
26
27 raise ValueError("VM {} has no floating ip".format(vm))
28
29
30def ensure_connected_to_openstack(ctx: TestRun) -> None:
31 if not ctx.os_connection is None:
32 if ctx.os_creds is None:
33 ctx.os_creds = get_OS_credentials(ctx)
34 ctx.os_connection = os_connect(ctx.os_creds)
35
36
37def get_OS_credentials(ctx: TestRun) -> OSCreds:
koder aka kdanilovffaf48d2016-12-27 02:25:29 +020038 stored = ctx.storage.get("openstack_openrc", None)
39 if stored is not None:
40 return OSCreds(*cast(List, stored))
koder aka kdanilov39e449e2016-12-17 15:15:26 +020041
koder aka kdanilov7f59d562016-12-26 01:34:23 +020042 creds = None # type: OSCreds
43 os_creds = None # type: OSCreds
koder aka kdanilov39e449e2016-12-17 15:15:26 +020044 force_insecure = False
45 cfg = ctx.config
46
47 if 'openstack' in cfg.clouds:
48 os_cfg = cfg.clouds['openstack']
49 if 'OPENRC' in os_cfg:
50 logger.info("Using OS credentials from " + os_cfg['OPENRC'])
51 creds_tuple = get_creds_openrc(os_cfg['OPENRC'])
52 os_creds = OSCreds(*creds_tuple)
53 elif 'ENV' in os_cfg:
54 logger.info("Using OS credentials from shell environment")
55 os_creds = get_openstack_credentials()
56 elif 'OS_TENANT_NAME' in os_cfg:
57 logger.info("Using predefined credentials")
58 os_creds = OSCreds(os_cfg['OS_USERNAME'].strip(),
59 os_cfg['OS_PASSWORD'].strip(),
60 os_cfg['OS_TENANT_NAME'].strip(),
61 os_cfg['OS_AUTH_URL'].strip(),
62 os_cfg.get('OS_INSECURE', False))
63
64 elif 'OS_INSECURE' in os_cfg:
65 force_insecure = os_cfg.get('OS_INSECURE', False)
66
67 if os_creds is None and 'fuel' in cfg.clouds and 'openstack_env' in cfg.clouds['fuel'] and \
68 ctx.fuel_openstack_creds is not None:
69 logger.info("Using fuel creds")
70 creds = ctx.fuel_openstack_creds
71 elif os_creds is None:
72 logger.error("Can't found OS credentials")
73 raise StopTestError("Can't found OS credentials", None)
74
75 if creds is None:
76 creds = os_creds
77
78 if force_insecure and not creds.insecure:
79 creds = OSCreds(creds.name, creds.passwd, creds.tenant, creds.auth_url, True)
80
81 logger.debug(("OS_CREDS: user={0.name} tenant={0.tenant} " +
82 "auth_url={0.auth_url} insecure={0.insecure}").format(creds))
83
koder aka kdanilov7f59d562016-12-26 01:34:23 +020084 ctx.storage.put(list(creds), "openstack_openrc")
koder aka kdanilov39e449e2016-12-17 15:15:26 +020085 return creds
86
87
88def get_vm_keypair_path(cfg: Config) -> Tuple[str, str]:
89 key_name = cfg.vm_configs['keypair_name']
90 private_path = os.path.join(cfg.settings_dir, key_name + "_private.pem")
91 public_path = os.path.join(cfg.settings_dir, key_name + "_public.pub")
92 return (private_path, public_path)
93
94
95class DiscoverOSStage(Stage):
96 """Discover openstack nodes and VMS"""
97
98 config_block = 'openstack'
99
100 # discover FUEL cluster first
101 priority = StepOrder.DISCOVER + 1
102
103 @classmethod
104 def validate(cls, conf: ConfigBlock) -> None:
105 pass
106
107 def run(self, ctx: TestRun) -> None:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200108 if 'all_nodes' in ctx.storage:
109 logger.debug("Skip openstack discovery, use previously discovered nodes")
110 return
111
112 ensure_connected_to_openstack(ctx)
113
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200114 cfg = ctx.config.openstack
115 os_nodes_auth = cfg.auth # type: str
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200116 if os_nodes_auth.count(":") == 2:
117 user, password, key_file = os_nodes_auth.split(":") # type: str, Optional[str], Optional[str]
118 if not password:
119 password = None
120 else:
121 user, password = os_nodes_auth.split(":")
122 key_file = None
123
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200124 if ctx.config.discovery not in ('disabled', 'metadata'):
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200125 services = ctx.os_connection.nova.services.list() # type: List[Any]
126 host_services_mapping = {} # type: Dict[str, List[str]]
127
128 for service in services:
129 ip = cast(str, socket.gethostbyname(service.host))
130 host_services_mapping.get(ip, []).append(service.binary)
131
132 logger.debug("Found %s openstack service nodes" % len(host_services_mapping))
133
134 for host, services in host_services_mapping.items():
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +0200135 creds = ConnCreds(host=to_ip(host), user=user, passwd=password, key_file=key_file)
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200136 ctx.merge_node(creds, set(services))
137 # TODO: log OS nodes discovery results
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200138 else:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200139 logger.info("Scip OS cluster discovery due to 'discovery' setting value")
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200140
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200141 private_key_path = get_vm_keypair_path(ctx.config)[0]
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200142
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200143 vm_creds = None # type: str
144 for vm_creds in cfg.get("vms", []):
145 user_name, vm_name_pattern = vm_creds.split("@", 1)
146 msg = "Vm like {} lookup failed".format(vm_name_pattern)
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200147
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200148 with LogError(msg):
149 msg = "Looking for vm with name like {0}".format(vm_name_pattern)
150 logger.debug(msg)
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200151
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200152 ensure_connected_to_openstack(ctx)
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200153
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200154 for ip, vm_id in find_vms(ctx.os_connection, vm_name_pattern):
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +0200155 creds = ConnCreds(host=to_ip(ip), user=user_name, key_file=private_key_path)
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200156 info = NodeInfo(creds, {'testnode'})
157 info.os_vm_id = vm_id
158 nid = info.node_id()
159 if nid in ctx.nodes_info:
160 logger.error("Test VM node has the same id(%s), as existing node %s", nid, ctx.nodes_info[nid])
161 raise StopTestError()
162 ctx.nodes_info[nid] = info
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200163
164
165class CreateOSVMSStage(Stage):
166 "Spawn new VM's in Openstack cluster"
167
168 priority = StepOrder.SPAWN # type: int
169 config_block = 'spawn_os_vms' # type: str
170
171 def run(self, ctx: TestRun) -> None:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200172 if 'all_nodes' in ctx.storage:
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200173 ctx.os_spawned_nodes_ids = ctx.storage.get('os_spawned_nodes_ids')
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200174 logger.info("Skipping OS VMS discovery/spawn as all data found in storage")
175 return
176
177 if 'os_spawned_nodes_ids' in ctx.storage:
178 logger.error("spawned_os_nodes_ids is found in storage, but no nodes_info is stored." +
179 "Fix this before continue")
180 raise StopTestError()
181
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200182 vm_spawn_config = ctx.config.spawn_os_vms
183 vm_image_config = ctx.config.vm_configs[vm_spawn_config.cfg_name]
184
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200185 ensure_connected_to_openstack(ctx)
186 params = vm_image_config.copy()
187 params.update(vm_spawn_config)
188 params.update(get_vm_keypair_path(ctx.config))
189 params['group_name'] = ctx.config.run_uuid
190 params['keypair_name'] = ctx.config.vm_configs['keypair_name']
191
192 if not ctx.config.openstack.get("skip_preparation", False):
193 logger.info("Preparing openstack")
194 prepare_os(ctx.os_connection, params)
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200195 else:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200196 logger.info("Scip openstack preparation as 'skip_preparation' is set")
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200197
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200198 ctx.os_spawned_nodes_ids = []
199 with ctx.get_pool() as pool:
200 for info in launch_vms(ctx.os_connection, params, pool):
201 info.roles.add('testnode')
202 nid = info.node_id()
203 if nid in ctx.nodes_info:
204 logger.error("Test VM node has the same id(%s), as existing node %s", nid, ctx.nodes_info[nid])
205 raise StopTestError()
206 ctx.nodes_info[nid] = info
207 ctx.os_spawned_nodes_ids.append(info.os_vm_id)
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200208
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200209 ctx.storage.put(ctx.os_spawned_nodes_ids, 'os_spawned_nodes_ids')
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200210
211 def cleanup(self, ctx: TestRun) -> None:
212 # keep nodes in case of error for future test restart
213 if not ctx.config.keep_vm and ctx.os_spawned_nodes_ids:
214 logger.info("Removing nodes")
215
216 clear_nodes(ctx.os_connection, ctx.os_spawned_nodes_ids)
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200217 ctx.storage.rm('spawned_os_nodes')
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200218
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200219 logger.info("OS spawned nodes has been successfully removed")
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200220
221
222
223# @contextlib.contextmanager
224# def suspend_vm_nodes_ctx(ctx: TestRun, unused_nodes: List[IRPCNode]) -> Iterator[List[int]]:
225#
226# pausable_nodes_ids = [cast(int, node.info.os_vm_id)
227# for node in unused_nodes
228# if node.info.os_vm_id is not None]
229#
230# non_pausable = len(unused_nodes) - len(pausable_nodes_ids)
231#
232# if non_pausable:
233# logger.warning("Can't pause {} nodes".format(non_pausable))
234#
235# if pausable_nodes_ids:
236# logger.debug("Try to pause {} unused nodes".format(len(pausable_nodes_ids)))
237# with ctx.get_pool() as pool:
238# openstack_api.pause(ctx.os_connection, pausable_nodes_ids, pool)
239#
240# try:
241# yield pausable_nodes_ids
242# finally:
243# if pausable_nodes_ids:
244# logger.debug("Unpausing {} nodes".format(len(pausable_nodes_ids)))
245# with ctx.get_pool() as pool:
246# openstack_api.unpause(ctx.os_connection, pausable_nodes_ids, pool)
247# def clouds_connect_stage(ctx: TestRun) -> None:
248 # TODO(koder): need to use this to connect to openstack in upper code
249 # conn = ctx.config['clouds/openstack']
250 # user, passwd, tenant = parse_creds(conn['creds'])
251 # auth_data = dict(auth_url=conn['auth_url'],
252 # username=user,
253 # api_key=passwd,
254 # project_id=tenant) # type: Dict[str, str]
255 # logger.debug("Discovering openstack nodes with connection details: %r", conn)
256 # connect to openstack, fuel
257
258 # # parse FUEL REST credentials
259 # username, tenant_name, password = parse_creds(fuel_data['creds'])
260 # creds = {"username": username,
261 # "tenant_name": tenant_name,
262 # "password": password}
263 #
264 # # connect to FUEL
265 # conn = fuel_rest_api.KeystoneAuth(fuel_data['url'], creds, headers=None)
266 # pass