3 # Copyright (c) 2017 HUAWEI TECHNOLOGIES CO.,LTD and others.
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
13 from opnfv.deployment import manager
14 from opnfv.utils import opnfv_logger as logger
15 from opnfv.utils import ssh_utils
17 logger = logger.Logger(__name__).getLogger()
20 class CompassAdapter(manager.DeploymentHandler):
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,
29 def get_nodes(self, options=None):
31 self.deployment_status = None
32 self.nodes_dict = self._get_deployment_nodes()
33 self.deployment_status = self.get_deployment_status()
35 for k, v in self.nodes_dict.iteritems():
36 node = manager.Node(v['id'], v['ip'],
38 v['roles'], v['ssh_client'], v['mac'])
41 self.get_nodes_called = True
44 def get_openstack_version(self):
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())
51 def get_sdn_version(self):
52 for node in self.nodes:
54 sdn_info = self._get_sdn_info(node, manager.Role.ODL)
57 sdn_info = self._get_sdn_info(node, manager.Role.ONOS)
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:
69 raise KeyError('SDN %s is not supported', sdn_type)
71 cmd = "find /opt -name '{0}*'".format(sdn_key)
72 sdn_info = node.run_cmd(cmd)
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)
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
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;'
91 +----+---------------------+-------+---------+
93 | ID | Hypervisor hostname | State | Status |
95 +----+---------------------+-------+---------+
97 | 3 | host4 | up | enabled |
99 | 6 | host5 | up | enabled |
101 +----+---------------------+-------+---------+
103 _, stdout, stderr = (v['ssh_client'].exec_command(cmd))
104 error = stderr.readlines()
106 logger.error("error %s" % ''.join(error))
107 status = manager.NodeStatus.STATUS_ERROR
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()
117 status = manager.NodeStatus.STATUS_OK
119 status = manager.NodeStatus.STATUS_ERROR
120 self.nodes_dict[hostname]['status'] = status
121 v['status'] = manager.NodeStatus.STATUS_OK
123 failed_nodes = [k for k, v in self.nodes_dict.iteritems()
124 if v['status'] != manager.NodeStatus.STATUS_OK]
126 if failed_nodes and len(failed_nodes) > 0:
127 return 'Hosts {0} failed'.format(','.join(failed_nodes))
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)
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
151 lines = output.encode().rsplit('\n')
153 if (not lines or len(lines) < 2):
154 logger.error('No nodes are found in the deployment.')
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,
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()
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,
183 nodes_dict[name]['ssh_client'] = ssh_client
184 nodes_dict[name]['status'] = manager.NodeStatus.STATUS_UNKNOWN