Merge "adjust parameters setting"
[releng.git] / modules / opnfv / installer_adapters / apex / ApexAdapter.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 import os
11 import re
12
13 import opnfv.utils.SSHUtils as ssh_utils
14 import opnfv.utils.OPNFVLogger as logger
15
16
17 class ApexAdapter:
18
19     def __init__(self, installer_ip, pkey_file, user="stack"):
20         self.installer_ip = installer_ip
21         self.installer_user = user
22         self.pkey_file = pkey_file
23         self.installer_connection = ssh_utils.get_ssh_client(
24             self.installer_ip,
25             self.installer_user,
26             pkey_file=self.pkey_file)
27         self.logger = logger.Logger("ApexHandler").getLogger()
28
29     def runcmd_apex_installer(self, cmd):
30         _, stdout, stderr = (self.installer_connection.exec_command(cmd))
31         error = stderr.readlines()
32         if len(error) > 0:
33             self.logger.error("error %s" % ''.join(error))
34             return error
35         output = ''.join(stdout.readlines())
36         return output
37
38     def get_nodes(self):
39         nodes = []
40         output = self.runcmd_apex_installer(
41             "source /home/stack/stackrc;nova list")
42         lines = output.rsplit('\n')
43         if len(lines) < 4:
44             self.logger.info("No nodes found in the deployment.")
45             return None
46
47         for line in lines:
48             if 'controller' in line:
49                 roles = "controller"
50             elif 'compute' in line:
51                 roles = "compute"
52             else:
53                 continue
54             if 'Daylight' in line:
55                 roles = + ", OpenDaylight"
56             fields = line.split('|')
57             dict = {"id": re.sub('[!| ]', '', fields[1]),
58                     "roles": roles,
59                     "name": re.sub('[!| ]', '', fields[2]),
60                     "status": re.sub('[!| ]', '', fields[3]),
61                     "ip": re.sub('[!| ctlplane=]', '', fields[6])}
62             nodes.append(dict)
63
64         return nodes
65
66     def get_deployment_info(self):
67         str = "Deployment details:\n"
68         str += "\tINSTALLER:   Apex\n"
69         str += ("\tSCENARIO:    %s\n" %
70                 os.getenv('DEPLOY_SCENARIO', 'Unknown'))
71         sdn = "None"
72
73         nodes = self.get_nodes()
74         if nodes is None:
75             self.logger.info("No nodes found in the deployment.")
76             return
77         num_nodes = len(nodes)
78         num_controllers = 0
79         num_computes = 0
80         for node in nodes:
81             if 'controller' in node['roles']:
82                 num_controllers += 1
83             if 'compute' in node['roles']:
84                 num_computes += 1
85             if 'Daylight' in node['name']:
86                 sdn = 'OpenDaylight'
87
88         ha = str(num_controllers >= 3)
89
90         str += "\tHA:          %s\n" % ha
91         str += "\tNUM.NODES:   %s\n" % num_nodes
92         str += "\tCONTROLLERS: %s\n" % num_controllers
93         str += "\tCOMPUTES:    %s\n" % num_computes
94         str += "\tSDN CONTR.:  %s\n\n" % sdn
95
96         str += "\tNODES:\n"
97         for node in nodes:
98             str += ("\t  ID:     %s\n" % node['id'])
99             str += ("\t  Name:   %s\n" % node['name'])
100             str += ("\t  Roles:  %s\n" % node['roles'])
101             str += ("\t  Status: %s\n" % node['status'])
102             str += ("\t  IP:     %s\n\n" % node['ip'])
103
104         return str
105
106     def get_controller_ips(self, options=None):
107         nodes = self.get_nodes()
108         controllers = []
109         for node in nodes:
110             if "controller" in node["roles"]:
111                 controllers.append(node['ip'])
112         return controllers
113
114     def get_compute_ips(self, options=None):
115         nodes = self.get_nodes()
116         computes = []
117         for node in nodes:
118             if "compute" in node["roles"]:
119                 computes.append(node['ip'])
120         return computes
121
122     def get_file_from_installer(self, remote_path, local_path, options=None):
123         self.logger.debug("Fetching %s from Undercloud %s" %
124                           (remote_path, self.installer_ip))
125         get_file_result = ssh_utils.get_file(self.installer_connection,
126                                              remote_path,
127                                              local_path)
128         if get_file_result is None:
129             self.logger.error("SFTP failed to retrieve the file.")
130             return 1
131         self.logger.info("%s successfully copied from Undercloud to %s" %
132                          (remote_path, local_path))
133
134     def get_file_from_controller(self,
135                                  remote_path,
136                                  local_path,
137                                  ip=None,
138                                  options=None):
139         if ip is None:
140             controllers = self.get_controller_ips()
141             ip = controllers[0]
142
143         connection = ssh_utils.get_ssh_client(ip,
144                                               'heat-admin',
145                                               pkey_file=self.pkey_file)
146
147         get_file_result = ssh_utils.get_file(connection,
148                                              remote_path,
149                                              local_path)
150         if get_file_result is None:
151             self.logger.error("SFTP failed to retrieve the file.")
152             return 1
153         self.logger.info("%s successfully copied from %s to %s" %
154                          (remote_path, ip, local_path))