Adding a new verification job for testapi UI.
[releng.git] / modules / opnfv / deployment / fuel / adapter.py
1 ##############################################################################
2 # Copyright (c) 2017 Ericsson AB and others.
3 # Author: Jose Lausuch (jose.lausuch@ericsson.com)
4 #         George Paraskevopoulos (geopar@intracom-telecom.com)
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
11
12 from opnfv.deployment import manager
13 from opnfv.utils import opnfv_logger as logger
14 from opnfv.utils import ssh_utils
15
16 logger = logger.Logger(__name__).getLogger()
17
18
19 class FuelAdapter(manager.DeploymentHandler):
20
21     def __init__(self, installer_ip, installer_user, installer_pwd):
22         super(FuelAdapter, self).__init__(installer='fuel',
23                                           installer_ip=installer_ip,
24                                           installer_user=installer_user,
25                                           installer_pwd=installer_pwd,
26                                           pkey_file=None)
27
28     def _get_clusters(self):
29         environments = []
30         output = self.runcmd_fuel_env()
31         lines = output.rsplit('\n')
32         if len(lines) < 2:
33             logger.info("No environments found in the deployment.")
34             return None
35         else:
36             fields = lines[0].rsplit(' | ')
37
38             index_id = -1
39             index_status = -1
40             index_name = -1
41             index_release_id = -1
42
43             for i in range(len(fields)):
44                 if "id" in fields[i]:
45                     index_id = i
46                 elif "status" in fields[i]:
47                     index_status = i
48                 elif "name" in fields[i]:
49                     index_name = i
50                 elif "release_id" in fields[i]:
51                     index_release_id = i
52
53             # order env info
54             for i in range(2, len(lines)):
55                 fields = lines[i].rsplit(' | ')
56                 dict = {"id": fields[index_id].strip(),
57                         "status": fields[index_status].strip(),
58                         "name": fields[index_name].strip(),
59                         "release_id": fields[index_release_id].strip()}
60                 environments.append(dict)
61
62         return environments
63
64     def get_nodes(self, options=None):
65
66         if options and options['cluster'] and len(self.nodes) > 0:
67             n = []
68             for node in self.nodes:
69                 if str(node.info['cluster']) == str(options['cluster']):
70                     n.append(node)
71             return n
72
73         try:
74             # if we have retrieved previously all the nodes, don't do it again
75             # This fails the first time when the constructor calls this method
76             # therefore the try/except
77             if len(self.nodes) > 0:
78                 return self.nodes
79         except:
80             pass
81
82         nodes = []
83         cmd = 'fuel node'
84         output = self.installer_node.run_cmd(cmd)
85         lines = output.rsplit('\n')
86         if len(lines) < 2:
87             logger.info("No nodes found in the deployment.")
88             return nodes
89
90         # get fields indexes
91         fields = lines[0].rsplit(' | ')
92
93         index_id = -1
94         index_status = -1
95         index_name = -1
96         index_cluster = -1
97         index_ip = -1
98         index_mac = -1
99         index_roles = -1
100         index_online = -1
101
102         for i in range(len(fields)):
103             if "group_id" in fields[i]:
104                 break
105             elif "id" in fields[i]:
106                 index_id = i
107             elif "status" in fields[i]:
108                 index_status = i
109             elif "name" in fields[i]:
110                 index_name = i
111             elif "cluster" in fields[i]:
112                 index_cluster = i
113             elif "ip" in fields[i]:
114                 index_ip = i
115             elif "mac" in fields[i]:
116                 index_mac = i
117             elif "roles " in fields[i] and "pending_roles" not in fields[i]:
118                 index_roles = i
119             elif "online" in fields[i]:
120                 index_online = i
121
122         # order nodes info
123         for i in range(2, len(lines)):
124             fields = lines[i].rsplit(' | ')
125             id = fields[index_id].strip().encode()
126             ip = fields[index_ip].strip().encode()
127             status_node = fields[index_status].strip().encode().lower()
128             name = fields[index_name].strip().encode()
129             roles_all = fields[index_roles].strip().encode().lower()
130
131             roles = [x for x in [manager.Role.CONTROLLER,
132                                  manager.Role.COMPUTE,
133                                  manager.Role.ODL] if x in roles_all]
134
135             dict = {"cluster": fields[index_cluster].strip().encode(),
136                     "mac": fields[index_mac].strip().encode(),
137                     "status_node": status_node,
138                     "online": fields[index_online].strip().encode()}
139
140             ssh_client = None
141             if status_node == 'ready':
142                 status = manager.NodeStatus.STATUS_OK
143                 proxy = {'ip': self.installer_ip,
144                          'username': self.installer_user,
145                          'password': self.installer_pwd}
146                 ssh_client = ssh_utils.get_ssh_client(hostname=ip,
147                                                       username='root',
148                                                       proxy=proxy)
149             elif 'error' in status_node:
150                 status = manager.NodeStatus.STATUS_ERROR
151             elif 'off' in status_node:
152                 status = manager.NodeStatus.STATUS_OFFLINE
153             elif 'discover' in status_node:
154                 status = manager.NodeStatus.STATUS_UNUSED
155             else:
156                 status = manager.NodeStatus.STATUS_INACTIVE
157
158             node = manager.Node(
159                 id, ip, name, status, roles, ssh_client, dict)
160             if options and options['cluster']:
161                 if fields[index_cluster].strip() == options['cluster']:
162                     nodes.append(node)
163             else:
164                 nodes.append(node)
165
166         self.get_nodes_called = True
167         return nodes
168
169     def get_openstack_version(self):
170         cmd = 'source openrc;nova-manage version 2>/dev/null'
171         version = None
172         for node in self.nodes:
173             if node.is_controller() and node.is_active():
174                 version = node.run_cmd(cmd)
175                 break
176         return version
177
178     def get_sdn_version(self):
179         cmd = "apt-cache policy opendaylight|grep Installed"
180         version = None
181         for node in self.nodes:
182             if manager.Role.ODL in node.roles and node.is_active():
183                 odl_version = node.run_cmd(cmd)
184                 if odl_version:
185                     version = 'OpenDaylight ' + odl_version.split(' ')[-1]
186                     break
187         return version
188
189     def get_deployment_status(self):
190         cmd = "fuel env|tail -1|awk '{print $3}'"
191         result = self.installer_node.run_cmd(cmd)
192         if result is None or len(result) == 0:
193             return 'unknown'
194         elif 'operational' in result:
195             return 'active'
196         elif 'deploy' in result:
197             return 'deploying'
198         else:
199             return 'active'