Add a new adapter for containerized Compass installer
[releng.git] / modules / opnfv / deployment / compass / adapter_container.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2018 HUAWEI TECHNOLOGIES CO.,LTD and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 from opnfv.deployment import manager
11 from opnfv.utils import opnfv_logger as logger
12 from opnfv.utils import ssh_utils
13
14 import yaml
15 import os
16
17 logger = logger.Logger(__name__).getLogger()
18
19
20 class ContainerizedCompassAdapter():
21
22     def __init__(self, installer_ip, installer_user, pkey_file):
23
24         self.installer = 'compass'
25         self.installer_ip = installer_ip
26         self.installer_user = installer_user
27         self.pkey_file = pkey_file
28         self.DST_PATH_UC = "/tmp/openstack_user_config.yml"
29         self.nodes = []
30         self.ROLES = {}
31
32         if pkey_file is not None and not os.path.isfile(pkey_file):
33             raise Exception(
34                 'The private key file %s does not exist!' % pkey_file)
35
36     def _find_nodes(self, file):
37         nodes = file['compute_hosts']
38         for compute in nodes:
39             self.ROLES[compute] = 'compute'
40         controllers = file['haproxy_hosts']
41         for controller in controllers:
42             nodes[controller] = controllers[controller]
43             self.ROLES[controller] = 'controller'
44         return nodes
45
46     def _process_nodes(self, raw_nodes):
47         nodes = []
48
49         for node in raw_nodes:
50             name = node
51             ip = raw_nodes[node]['ip']
52             status = 'active'
53             id = None
54             if self.ROLES[node] == 'controller':
55                 roles = 'controller'
56             elif self.ROLES[node] == 'compute':
57                 roles = 'compute'
58             ssh_client = ssh_utils.get_ssh_client(hostname=ip,
59                                                   username=self.installer_user,
60                                                   pkey_file=self.pkey_file)
61             node = manager.Node(id, ip, name, status, roles, ssh_client)
62             nodes.append(node)
63
64         return nodes
65
66     def get_nodes(self, options=None):
67         try:
68             # if we have retrieved previously all the nodes, don't do it again
69             # This fails the first time when the constructor calls this method
70             # therefore the try/except
71             if len(self.nodes) > 0:
72                 return self.nodes
73         except:
74             pass
75
76         with open(self.DST_PATH_UC, 'r') as stream:
77             try:
78                 file = yaml.load(stream)
79                 raw_nodes = self._find_nodes(file)
80             except yaml.YAMLError as exc:
81                 logger.error(exc)
82         self.nodes = self._process_nodes(raw_nodes)
83         return self.nodes