1 ##############################################################################
2 # Copyright (c) 2017 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 ##############################################################################
10 from abc import abstractmethod
14 from opnfv.utils import opnfv_logger as logger
15 from opnfv.utils import ssh_utils
17 logger = logger.Logger(__name__).getLogger()
20 class Deployment(object):
32 self.deployment_info = {
33 'installer': installer,
34 'installer_ip': installer_ip,
38 'openstack_version': openstack_version,
39 'sdn_controller': sdn_controller,
43 def _get_openstack_release(self):
45 Translates an openstack version into the release name
56 version = self.deployment_info['openstack_version'].split('.')[0]
57 name = os_versions[version]
59 except Exception as e:
60 return 'Unknown release'
64 Returns a dictionary will all the attributes
66 return self.deployment_info
70 Override of the str method
73 INSTALLER: {installer}
75 INSTALLER IP: {installer_ip}
78 OPENSTACK: {openstack_version} ({openstack_release})
81 '''.format(installer=self.deployment_info['installer'],
82 scenario=self.deployment_info['scenario'],
83 installer_ip=self.deployment_info['installer_ip'],
84 pod=self.deployment_info['pod'],
85 status=self.deployment_info['status'],
86 openstack_version=self.deployment_info[
88 openstack_release=self._get_openstack_release(),
89 sdn_controller=self.deployment_info['sdn_controller'])
91 for node in self.deployment_info['nodes']:
92 s += '\t\t{node_object}\n'.format(node_object=node)
100 STATUS_INACTIVE = 'inactive'
101 STATUS_OFFLINE = 'offline'
102 STATUS_FAILED = 'failed'
116 self.ssh_client = ssh_client
120 def get_file(self, src, dest):
124 if self.status is not Node.STATUS_OK:
125 logger.info("The node %s is not active" % self.ip)
127 logger.info("Fetching %s from %s" % (src, self.ip))
128 get_file_result = ssh_utils.get_file(self.ssh_client, src, dest)
129 if get_file_result is None:
130 logger.error("SFTP failed to retrieve the file.")
132 logger.info("Successfully copied %s:%s to %s" %
133 (self.ip, src, dest))
134 return get_file_result
136 def put_file(self, src, dest):
140 if self.status is not Node.STATUS_OK:
141 logger.info("The node %s is not active" % self.ip)
143 logger.info("Copying %s to %s" % (src, self.ip))
144 put_file_result = ssh_utils.put_file(self.ssh_client, src, dest)
145 if put_file_result is None:
146 logger.error("SFTP failed to retrieve the file.")
148 logger.info("Successfully copied %s to %s:%s" %
149 (src, dest, self.ip))
150 return put_file_result
152 def run_cmd(self, cmd):
154 Run command remotely on a node
156 if self.status is not Node.STATUS_OK:
157 logger.info("The node %s is not active" % self.ip)
159 _, stdout, stderr = (self.ssh_client.exec_command(cmd))
160 error = stderr.readlines()
162 logger.error("error %s" % ''.join(error))
164 output = ''.join(stdout.readlines()).rstrip()
169 Returns a dictionary with all the attributes
175 'status': self.status,
180 def get_attribute(self, attribute):
182 Returns an attribute given the name
184 return self.get_dict()[attribute]
186 def is_controller(self):
188 Returns if the node is a controller
190 if 'controller' in self.get_attribute('roles'):
194 def is_compute(self):
196 Returns if the node is a compute
198 if 'compute' in self.get_attribute('roles'):
203 return str(self.get_dict())
206 class DeploymentHandler(object):
209 EX_ERROR = os.EX_SOFTWARE
210 FUNCTION_NOT_IMPLEMENTED = "Function not implemented by adapter!"
219 self.installer = installer.lower()
220 self.installer_ip = installer_ip
221 self.installer_user = installer_user
222 self.installer_pwd = installer_pwd
223 self.pkey_file = pkey_file
225 if pkey_file is not None and not os.path.isfile(pkey_file):
227 'The private key file %s does not exist!' % pkey_file)
229 self.installer_connection = ssh_utils.get_ssh_client(
230 hostname=self.installer_ip,
231 username=self.installer_user,
232 password=self.installer_pwd,
233 pkey_file=self.pkey_file)
235 if self.installer_connection:
236 self.installer_node = Node(id='',
240 ssh_client=self.installer_connection,
241 roles='installer node')
244 'Cannot establish connection to the installer node!')
246 self.nodes = self.nodes()
249 def get_openstack_version(self):
251 Returns a string of the openstack version (nova-compute)
253 raise Exception(DeploymentHandler.FUNCTION_NOT_IMPLEMENTED)
256 def get_sdn_version(self):
258 Returns a string of the sdn controller and its version, if exists
260 raise Exception(DeploymentHandler.FUNCTION_NOT_IMPLEMENTED)
263 def get_deployment_status(self):
265 Returns a string of the status of the deployment
267 raise Exception(DeploymentHandler.FUNCTION_NOT_IMPLEMENTED)
270 def nodes(self, options=None):
272 Generates a list of all the nodes in the deployment
274 raise Exception(DeploymentHandler.FUNCTION_NOT_IMPLEMENTED)
276 def get_nodes(self, options=None):
278 Returns the list of Node objects
282 def get_installer_node(self):
284 Returns the installer node object
286 return self.installer_node
288 def get_deployment_info(self):
290 Returns an object of type Deployment
292 return Deployment(installer=self.installer,
293 installer_ip=self.installer_ip,
294 scenario=os.getenv('DEPLOY_SCENARIO', 'Unknown'),
295 status=self.get_deployment_status(),
296 pod=os.getenv('NODE_NAME', 'Unknown'),
297 openstack_version=self.get_openstack_version(),
298 sdn_controller=self.get_sdn_version(),