Merge "Update table headers in the reporting page"
[releng.git] / modules / opnfv / deployment / osa / adapter.py
1 ##############################################################################
2 # Copyright (c) 2017 SUSE Linux GmbH
3 # Author: Manuel Buil (mbuil@suse.com)
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10
11 from opnfv.deployment import manager
12 from opnfv.utils import opnfv_logger as logger
13 from opnfv.utils import ssh_utils
14 import yaml
15
16 logger = logger.Logger(__name__).getLogger()
17
18
19 class OSAAdapter(manager.DeploymentHandler):
20
21     def __init__(self, installer_ip, installer_user, pkey_file):
22         self.SOURCE_PATH_UC = "/etc/openstack_deploy/openstack_user_config.yml"
23         self.DEST_PATH_UC = "/tmp/openstack_user_config.yml"
24         super(OSAAdapter, self).__init__(installer='osa',
25                                          installer_ip=installer_ip,
26                                          installer_user=installer_user,
27                                          installer_pwd=None,
28                                          pkey_file=pkey_file)
29
30     def _find_nodes(self, file):
31         nodes = file['compute_hosts']
32         controllers = file['haproxy_hosts']
33         for controller in controllers:
34             nodes[controller] = controllers[controller]
35         return nodes
36
37     def _process_nodes(self, raw_nodes):
38         nodes = []
39
40         for node in raw_nodes:
41             name = node
42             ip = raw_nodes[node]['ip']
43             # TODO when xci provides status and id of nodes add logic
44             status = 'active'
45             id = None
46             if 'controller' in node:
47                 roles = 'controller'
48             elif 'compute' in node:
49                 roles = 'compute'
50             ssh_client = ssh_utils.get_ssh_client(hostname=ip,
51                                                   username=self.installer_user,
52                                                   pkey_file=self.pkey_file)
53             node = manager.Node(id, ip, name, status, roles, ssh_client)
54             nodes.append(node)
55
56         return nodes
57
58     def get_nodes(self, options=None):
59         try:
60             # if we have retrieved previously all the nodes, don't do it again
61             # This fails the first time when the constructor calls this method
62             # therefore the try/except
63             if len(self.nodes) > 0:
64                 return self.nodes
65         except:
66             pass
67
68         self.installer_node.get_file(self.SOURCE_PATH_UC, self.DEST_PATH_UC)
69         with open(self.DEST_PATH_UC, 'r') as stream:
70             try:
71                 file = yaml.load(stream)
72                 raw_nodes = self._find_nodes(file)
73             except yaml.YAMLError as exc:
74                 logger.error(exc)
75         nodes = self._process_nodes(raw_nodes)
76         return nodes