856c7fc3841a10f352f494bd4a54e9948861bdfd
[releng.git] / modules / opnfv / deployment / compass / adapter.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2017 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 import netaddr
11 import re
12
13 from opnfv.deployment import manager
14 from opnfv.utils import opnfv_logger as logger
15 from opnfv.utils import ssh_utils
16
17 logger = logger.Logger(__name__).getLogger()
18
19
20 class CompassAdapter(manager.DeploymentHandler):
21
22     def __init__(self, installer_ip, installer_user, installer_pwd):
23         super(CompassAdapter, self).__init__(installer='compass',
24                                              installer_ip=installer_ip,
25                                              installer_user=installer_user,
26                                              installer_pwd=installer_pwd,
27                                              pkey_file=None)
28
29     def get_nodes(self, options=None):
30         nodes = []
31         self.deployment_status = None
32         self.nodes_dict = self._get_deployment_nodes()
33         self.deployment_status = self.get_deployment_status()
34
35         for k, v in self.nodes_dict.iteritems():
36             node = manager.Node(v['id'], v['ip'],
37                                 k, v['status'],
38                                 v['roles'], v['ssh_client'], v['mac'])
39             nodes.append(node)
40
41         self.get_nodes_called = True
42         return nodes
43
44     def get_openstack_version(self):
45         version = None
46         cmd = 'source /opt/admin-openrc.sh;nova-manage version 2>/dev/null'
47         version = next(node.run_cmd(cmd) for node in self.nodes
48                        if node.is_controller())
49         return version
50
51     def get_sdn_version(self):
52         for node in self.nodes:
53             if node.is_odl():
54                 sdn_info = self._get_sdn_info(node, manager.Role.ODL)
55                 break
56             elif node.is_onos():
57                 sdn_info = self._get_sdn_info(node, manager.Role.ONOS)
58                 break
59             else:
60                 sdn_info = None
61         return sdn_info
62
63     def _get_sdn_info(self, node, sdn_type):
64         if sdn_type == manager.Role.ODL:
65             sdn_key = 'distribution-karaf'
66         elif sdn_type == manager.Role.ONOS:
67             sdn_key = 'onos-'
68         else:
69             raise KeyError('SDN %s is not supported', sdn_type)
70
71         cmd = "find /opt -name '{0}*'".format(sdn_key)
72         sdn_info = node.run_cmd(cmd)
73         sdn_version = 'None'
74         if sdn_info:
75             # /opt/distribution-karaf-0.5.2-Boron-SR2.tar.gz
76             match_sdn = re.findall(r".*(0\.\d\.\d).*", sdn_info)
77             if (match_sdn and len(match_sdn) >= 1):
78                 sdn_version = match_sdn[0]
79                 sdn_version = '{0} {1}'.format(sdn_type, sdn_version)
80         return sdn_version
81
82     def get_deployment_status(self):
83         if self.deployment_status is not None:
84             logger.debug('Skip - Node status has been retrieved once')
85             return self.deployment_status
86
87         for k, v in self.nodes_dict.iteritems():
88             if manager.Role.CONTROLLER in v['roles']:
89                 cmd = 'source /opt/admin-openrc.sh; nova hypervisor-list;'
90                 '''
91                 +----+---------------------+-------+---------+
92
93                 | ID | Hypervisor hostname | State | Status  |
94
95                 +----+---------------------+-------+---------+
96
97                 | 3  | host4               | up    | enabled |
98
99                 | 6  | host5               | up    | enabled |
100
101                 +----+---------------------+-------+---------+
102                 '''
103                 _, stdout, stderr = (v['ssh_client'].exec_command(cmd))
104                 error = stderr.readlines()
105                 if len(error) > 0:
106                     logger.error("error %s" % ''.join(error))
107                     status = manager.NodeStatus.STATUS_ERROR
108                     v['status'] = status
109                     continue
110
111                 lines = stdout.readlines()
112                 for i in range(3, len(lines) - 1):
113                     fields = lines[i].strip().encode().rsplit(' | ')
114                     hostname = fields[1].strip().encode().lower()
115                     state = fields[2].strip().encode().lower()
116                     if 'up' == state:
117                         status = manager.NodeStatus.STATUS_OK
118                     else:
119                         status = manager.NodeStatus.STATUS_ERROR
120                     self.nodes_dict[hostname]['status'] = status
121                     v['status'] = manager.NodeStatus.STATUS_OK
122
123         failed_nodes = [k for k, v in self.nodes_dict.iteritems()
124                         if v['status'] != manager.NodeStatus.STATUS_OK]
125
126         if failed_nodes and len(failed_nodes) > 0:
127             return 'Hosts {0} failed'.format(','.join(failed_nodes))
128
129         return 'active'
130
131     def _get_deployment_nodes(self):
132         sql_query = ('select host.host_id, host.roles, '
133                      'network.ip_int, machine.mac from clusterhost as host, '
134                      'host_network as network, machine as machine '
135                      'where host.host_id=network.host_id '
136                      'and host.id=machine.id;')
137         cmd = 'mysql -uroot -Dcompass -e "{0}"'.format(sql_query)
138         logger.debug('mysql command: %s', cmd)
139         output = self.installer_node.run_cmd(cmd)
140         '''
141         host_id roles   ip_int  mac
142         1 ["controller", "ha", "odl", "ceph-adm", "ceph-mon"]
143         167837746 00:00:e3:ee:a8:63
144         2 ["controller", "ha", "odl", "ceph-mon"]
145         167837747 00:00:31:1d:16:7a
146         3 ["controller", "ha", "odl", "ceph-mon"]
147         167837748 00:00:0c:bf:eb:01
148         4 ["compute", "ceph-osd"] 167837749 00:00:d8:22:6f:59
149         5 ["compute", "ceph-osd"] 167837750 00:00:75:d5:6b:9e
150         '''
151         lines = output.encode().rsplit('\n')
152         nodes_dict = {}
153         if (not lines or len(lines) < 2):
154             logger.error('No nodes are found in the deployment.')
155             return nodes_dict
156
157         proxy = {'ip': self.installer_ip,
158                  'username': self.installer_user,
159                  'password': self.installer_pwd}
160         for i in range(1, len(lines)):
161             fields = lines[i].strip().encode().rsplit('\t')
162             host_id = fields[0].strip().encode()
163             name = 'host{0}'.format(host_id)
164             node_roles = fields[1].strip().encode().lower()
165             node_roles = [manager.Role.ODL if x == 'odl'
166                           else x for x in node_roles]
167             roles = [x for x in [manager.Role.CONTROLLER,
168                                  manager.Role.COMPUTE,
169                                  manager.Role.ODL,
170                                  manager.Role.ONOS] if x in node_roles]
171             ip = fields[2].strip().encode()
172             ip = str(netaddr.IPAddress(ip))
173             mac = fields[3].strip().encode()
174
175             nodes_dict[name] = {}
176             nodes_dict[name]['id'] = host_id
177             nodes_dict[name]['roles'] = roles
178             nodes_dict[name]['ip'] = ip
179             nodes_dict[name]['mac'] = mac
180             ssh_client = ssh_utils.get_ssh_client(hostname=ip,
181                                                   username='root',
182                                                   proxy=proxy)
183             nodes_dict[name]['ssh_client'] = ssh_client
184             nodes_dict[name]['status'] = manager.NodeStatus.STATUS_UNKNOWN
185         return nodes_dict