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 ##############################################################################
11 from SSHUtils import SSH_Connection
12 import RelengLogger as rl
17 def __init__(self, installer_ip, user="root", password="r00tme"):
18 self.installer_ip = installer_ip
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()
25 def runcmd_fuel_nodes(self):
26 output, error = self.connection.run_remote_cmd('fuel nodes')
28 self.logger.error("error %s" % error)
32 def runcmd_fuel_env(self):
33 output, error = self.connection.run_remote_cmd('fuel env')
35 self.logger.error("error %s" % error)
39 def get_clusters(self):
41 output = self.runcmd_fuel_env()
42 lines = output.rsplit('\n')
44 self.logger.infp("No environments found in the deployment.")
47 fields = lines[0].rsplit(' | ')
54 for i in range(0, len(fields) - 1):
57 elif "status" in fields[i]:
59 elif "name" in fields[i]:
61 elif "release_id" in fields[i]:
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)
75 def get_nodes(self, options=None):
77 output = self.runcmd_fuel_nodes()
78 lines = output.rsplit('\n')
80 self.logger.info("No nodes found in the deployment.")
84 fields = lines[0].rsplit(' | ')
95 for i in range(0, len(fields) - 1):
98 elif "status" in fields[i]:
100 elif "name" in fields[i]:
102 elif "cluster" in fields[i]:
104 elif "ip" in fields[i]:
106 elif "mac" in fields[i]:
108 elif "roles " in fields[i]:
110 elif "online" in fields[i]:
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']:
132 def get_controller_ips(self, options):
133 nodes = self.get_nodes(options=options)
136 if "controller" in node["roles"]:
137 controllers.append(node['ip'])
140 def get_compute_ips(self, options=None):
141 nodes = self.get_nodes(options=options)
144 if "compute" in node["roles"]:
145 computes.append(node['ip'])
148 def get_deployment_info(self):
149 str = "Deployment details:\n"
150 str += "\tInstaller: Fuel\n"
151 str += "\tScenario: Unknown\n"
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)
164 if "opendaylight" in node['roles']:
166 elif "onos" in node['roles']:
168 num_controllers = len(
169 self.get_controller_ips(options=cluster_dic))
170 num_computes = len(self.get_compute_ips(options=cluster_dic))
172 if num_controllers > 1:
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()
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.")
189 self.logger.info("%s successfully copied from Fuel to %s" %
190 (remote_path, local_path))
192 def get_file_from_controller(self,
198 controllers = self.get_controller_ips(options=options)
199 if len(controllers) == 0:
200 self.logger.info("No controllers found in the deployment.")
203 target_ip = controllers[0]
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))