Merge "project-ize kibana_dashboard"
[releng.git] / utils / installer-adapter / FuelAdapter.py
1 ##############################################################################
2 # Copyright (c) 2016 Ericsson AB and others.
3 # Author: Jose Lausuch (jose.lausuch@ericsson.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 SSHUtils import SSH_Connection
12 import RelengLogger as rl
13
14
15 class FuelAdapter:
16
17     def __init__(self, installer_ip, user="root", password="r00tme"):
18         self.installer_ip = installer_ip
19         self.user = user
20         self.password = password
21         self.connection = SSH_Connection(
22             installer_ip, self.user, self.password, use_system_keys=False)
23         self.logger = rl.Logger("Handler").getLogger()
24
25     def runcmd_fuel_nodes(self):
26         output, error = self.connection.run_remote_cmd('fuel nodes')
27         if len(error) > 0:
28             self.logger.error("error %s" % error)
29             return error
30         return output
31
32     def runcmd_fuel_env(self):
33         output, error = self.connection.run_remote_cmd('fuel env')
34         if len(error) > 0:
35             self.logger.error("error %s" % error)
36             return error
37         return output
38
39     def get_clusters(self):
40         environments = []
41         output = self.runcmd_fuel_env()
42         lines = output.rsplit('\n')
43         if len(lines) < 2:
44             self.logger.infp("No environments found in the deployment.")
45             return None
46         else:
47             fields = lines[0].rsplit(' | ')
48
49             index_id = -1
50             index_status = -1
51             index_name = -1
52             index_release_id = -1
53
54             for i in range(0, len(fields) - 1):
55                 if "id" in fields[i]:
56                     index_id = i
57                 elif "status" in fields[i]:
58                     index_status = i
59                 elif "name" in fields[i]:
60                     index_name = i
61                 elif "release_id" in fields[i]:
62                     index_release_id = i
63
64             # order env info
65             for i in range(2, len(lines) - 1):
66                 fields = lines[i].rsplit(' | ')
67                 dict = {"id": fields[index_id].strip(),
68                         "status": fields[index_status].strip(),
69                         "name": fields[index_name].strip(),
70                         "release_id": fields[index_release_id].strip()}
71                 environments.append(dict)
72
73         return environments
74
75     def get_nodes(self, options=None):
76         nodes = []
77         output = self.runcmd_fuel_nodes()
78         lines = output.rsplit('\n')
79         if len(lines) < 2:
80             self.logger.info("No nodes found in the deployment.")
81             return None
82         else:
83             # get fields indexes
84             fields = lines[0].rsplit(' | ')
85
86             index_id = -1
87             index_status = -1
88             index_name = -1
89             index_cluster = -1
90             index_ip = -1
91             index_mac = -1
92             index_roles = -1
93             index_online = -1
94
95             for i in range(0, len(fields) - 1):
96                 if "id" in fields[i]:
97                     index_id = i
98                 elif "status" in fields[i]:
99                     index_status = i
100                 elif "name" in fields[i]:
101                     index_name = i
102                 elif "cluster" in fields[i]:
103                     index_cluster = i
104                 elif "ip" in fields[i]:
105                     index_ip = i
106                 elif "mac" in fields[i]:
107                     index_mac = i
108                 elif "roles " in fields[i]:
109                     index_roles = i
110                 elif "online" in fields[i]:
111                     index_online = i
112
113             # order nodes info
114             for i in range(2, len(lines) - 1):
115                 fields = lines[i].rsplit(' | ')
116                 dict = {"id": fields[index_id].strip(),
117                         "status": fields[index_status].strip(),
118                         "name": fields[index_name].strip(),
119                         "cluster": fields[index_cluster].strip(),
120                         "ip": fields[index_ip].strip(),
121                         "mac": fields[index_mac].strip(),
122                         "roles": fields[index_roles].strip(),
123                         "online": fields[index_online].strip()}
124                 if options and options['cluster']:
125                     if fields[index_cluster].strip() == options['cluster']:
126                         nodes.append(dict)
127                 else:
128                     nodes.append(dict)
129
130         return nodes
131
132     def get_controller_ips(self, options):
133         nodes = self.get_nodes(options=options)
134         controllers = []
135         for node in nodes:
136             if "controller" in node["roles"]:
137                 controllers.append(node['ip'])
138         return controllers
139
140     def get_compute_ips(self, options=None):
141         nodes = self.get_nodes(options=options)
142         computes = []
143         for node in nodes:
144             if "compute" in node["roles"]:
145                 computes.append(node['ip'])
146         return computes
147
148     def get_deployment_info(self):
149         str = "Deployment details:\n"
150         str += "\tInstaller:  Fuel\n"
151         str += "\tScenario:   Unknown\n"
152         sdn = "None"
153         clusters = self.get_clusters()
154         str += "\tN.Clusters: %s\n" % len(clusters)
155         for cluster in clusters:
156             cluster_dic = {'cluster': cluster['id']}
157             str += "\tCluster info:\n"
158             str += "\t   ID:          %s\n" % cluster['id']
159             str += "\t   NAME:        %s\n" % cluster['name']
160             str += "\t   STATUS:      %s\n" % cluster['status']
161             nodes = self.get_nodes(options=cluster_dic)
162             num_nodes = len(nodes)
163             for node in nodes:
164                 if "opendaylight" in node['roles']:
165                     sdn = "OpenDaylight"
166                 elif "onos" in node['roles']:
167                     sdn = "ONOS"
168             num_controllers = len(
169                 self.get_controller_ips(options=cluster_dic))
170             num_computes = len(self.get_compute_ips(options=cluster_dic))
171             ha = False
172             if num_controllers > 1:
173                 ha = True
174
175             str += "\t   HA:          %s\n" % ha
176             str += "\t   NUM.NODES:   %s\n" % num_nodes
177             str += "\t   CONTROLLERS: %s\n" % num_controllers
178             str += "\t   COMPUTES:    %s\n" % num_computes
179             str += "\t   SDN CONTR.:  %s\n\n" % sdn
180         str += self.runcmd_fuel_nodes()
181         return str
182
183     def get_file_from_installer(self, remote_path, local_path, options=None):
184         self.logger.debug("Fetching %s from %s" %
185                           (remote_path, self.installer_ip))
186         if self.connection.scp_get(local_path, remote_path) != 0:
187             self.logger.error("SCP failed to retrieve the file.")
188             return 1
189         self.logger.info("%s successfully copied from Fuel to %s" %
190                          (remote_path, local_path))
191
192     def get_file_from_controller(self,
193                                  remote_path,
194                                  local_path,
195                                  ip=None,
196                                  options=None):
197         if ip is None:
198             controllers = self.get_controller_ips(options=options)
199             if len(controllers) == 0:
200                 self.logger.info("No controllers found in the deployment.")
201                 return 1
202             else:
203                 target_ip = controllers[0]
204         else:
205             target_ip = ip
206
207         fuel_dir = '/root/scp/'
208         cmd = 'mkdir -p %s;rsync -Rav %s:%s %s' % (
209             fuel_dir, target_ip, remote_path, fuel_dir)
210         self.logger.info("Copying %s from %s to Fuel..." %
211                          (remote_path, target_ip))
212         output, error = self.connection.run_remote_cmd(cmd)
213         self.logger.debug("Copying files from Fuel to %s..." % local_path)
214         self.get_file_from_installer(
215             fuel_dir + remote_path, local_path, options)
216         cmd = 'rm -r %s' % fuel_dir
217         output, error = self.connection.run_remote_cmd(cmd)
218         self.logger.info("%s successfully copied from %s to %s" %
219                          (remote_path, target_ip, local_path))