f8f33c838ef9bbe4c759952e0dff218508fede44
[doctor.git] / doctor_tests / installer / mcp.py
1 ##############################################################################
2 # Copyright (c) 2019 ZTE Corporation and others.
3 #
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 from os.path import isfile
10 import re
11 import time
12
13 from doctor_tests.common.constants import is_fenix
14 from doctor_tests.common.utils import get_doctor_test_root_dir
15 from doctor_tests.common.utils import SSHClient
16 from doctor_tests.installer.base import BaseInstaller
17
18
19 class McpInstaller(BaseInstaller):
20     node_user_name = 'ubuntu'
21
22     cm_set_script = 'set_config.py'
23     nc_set_compute_script = 'set_compute_config.py'
24     fe_set_script = 'set_fenix.sh'
25     cm_restore_script = 'restore_config.py'
26     nc_restore_compute_script = 'restore_compute_config.py'
27     ac_restart_script = 'restart_aodh.py'
28     ac_restore_script = 'restore_aodh.py'
29     python = 'python3'
30
31     def __init__(self, conf, log):
32         super(McpInstaller, self).__init__(conf, log)
33         self.key_file = self.get_ssh_key_from_installer()
34         self.client = SSHClient(self.conf.installer.ip,
35                                 self.node_user_name,
36                                 key_filename=self.key_file,
37                                 look_for_keys=True)
38         self.controllers = list()
39         self.controller_clients = list()
40         self.computes = list()
41
42     def setup(self):
43         self.log.info('Setup MCP installer start......')
44         self.get_node_ips()
45         self.create_flavor()
46         if is_fenix(self.conf):
47             self.set_apply_patches()
48         self.setup_stunnel()
49
50     def cleanup(self):
51         if is_fenix(self.conf):
52             self.restore_apply_patches()
53         for server in self.servers:
54             server.terminate()
55
56     def get_ssh_key_from_installer(self):
57         self.log.info('Get SSH keys from MCP......')
58
59         # Default in path /var/lib/opnfv/mcp.rsa
60         ssh_key = '/root/.ssh/id_rsa'
61         mcp_key = '/var/lib/opnfv/mcp.rsa'
62         return mcp_key if isfile(mcp_key) else ssh_key
63
64     def get_transport_url(self):
65         client = SSHClient(self.controllers[0], self.node_user_name,
66                            key_filename=self.key_file)
67         try:
68             cmd = 'sudo grep -m1 "^transport_url" /etc/nova/nova.conf'
69             ret, url = client.ssh(cmd)
70
71             if ret:
72                 raise Exception('Exec command to get transport from '
73                                 'controller(%s) in MCP installer failed, '
74                                 'ret=%s, output=%s'
75                                 % (self.controllers[0], ret, url))
76             elif self.controllers[0] not in url:
77                 # need to use ip instead of hostname
78                 url = (re.sub("@.*:", "@%s:" % self.controllers[0],
79                        url[0].split("=", 1)[1]))
80         except Exception:
81             cmd = 'grep -i "^rabbit" /etc/nova/nova.conf'
82             ret, lines = client.ssh(cmd)
83             if ret:
84                 raise Exception('Exec command to get transport from '
85                                 'controller(%s) in MCP installer failed, '
86                                 'ret=%s, output=%s'
87                                 % (self.controllers[0], ret, url))
88             else:
89                 for line in lines.split('\n'):
90                     if line.startswith("rabbit_userid"):
91                         rabbit_userid = line.split("=")
92                     if line.startswith("rabbit_port"):
93                         rabbit_port = line.split("=")
94                     if line.startswith("rabbit_password"):
95                         rabbit_password = line.split("=")
96                 url = "rabbit://%s:%s@%s:%s/?ssl=0" % (rabbit_userid,
97                                                        rabbit_password,
98                                                        self.controllers[0],
99                                                        rabbit_port)
100         self.log.info('get_transport_url %s' % url)
101         return url
102
103     def _copy_overcloudrc_to_controllers(self):
104         for ip in self.controllers:
105             cmd = "scp overcloudrc %s@%s:" % (self.node_user_name, ip)
106             self._run_cmd_remote(self.client, cmd)
107
108     def get_node_ips(self):
109         self.log.info('Get node ips from Mcp installer......')
110
111         command = 'sudo salt "*" --out yaml pillar.get _param:single_address'
112         node_details = self._run_cmd_remote(self.client, command)
113
114         self.controllers = [line.split()[1] for line in node_details
115                             if line.startswith("ctl")]
116         self.computes = [line.split()[1] for line in node_details
117                          if line.startswith("cmp")]
118
119         self.log.info('controller_ips:%s' % self.controllers)
120         self.log.info('compute_ips:%s' % self.computes)
121
122     def get_host_ip_from_hostname(self, hostname):
123         command = "sudo salt --out yaml '%s*' " \
124                   "pillar.get _param:single_address |" \
125                   "awk '{print $2}'" % hostname
126         host_ips = self._run_cmd_remote(self.client, command)
127         return host_ips[0]
128
129     def set_apply_patches(self):
130         self.log.info('Set apply patches start......')
131         fenix_files = None
132
133         set_scripts = [self.cm_set_script]
134
135         restart_cmd = 'sudo systemctl restart' \
136                       ' ceilometer-agent-notification.service'
137
138         if self.conf.test_case != 'fault_management':
139             if is_fenix(self.conf):
140                 set_scripts.append(self.fe_set_script)
141                 testdir = get_doctor_test_root_dir()
142                 fenix_files = ["Dockerfile", "run"]
143             restart_cmd += ' nova-scheduler.service'
144             set_scripts.append(self.nc_set_compute_script)
145
146         for node_ip in self.controllers:
147             client = SSHClient(node_ip, self.node_user_name,
148                                key_filename=self.key_file)
149             if fenix_files is not None:
150                 for fenix_file in fenix_files:
151                     src_file = '{0}/{1}/{2}'.format(testdir,
152                                                     'admin_tool/fenix',
153                                                     fenix_file)
154                     client.scp(src_file, fenix_file)
155             self._run_apply_patches(client,
156                                     restart_cmd,
157                                     set_scripts,
158                                     python=self.python)
159         time.sleep(5)
160
161         self.log.info('Set apply patches start......')
162
163         if self.conf.test_case != 'fault_management':
164             restart_cmd = 'sudo systemctl restart nova-compute.service'
165             for node_ip in self.computes:
166                 client = SSHClient(node_ip, self.node_user_name,
167                                    key_filename=self.key_file)
168                 self._run_apply_patches(client,
169                                         restart_cmd,
170                                         [self.nc_set_compute_script],
171                                         python=self.python)
172             time.sleep(5)
173
174     def restore_apply_patches(self):
175         self.log.info('restore apply patches start......')
176
177         restore_scripts = [self.cm_restore_script]
178
179         restore_scripts.append(self.ac_restore_script)
180         restart_cmd = 'sudo systemctl restart' \
181                       ' ceilometer-agent-notification.service'
182
183         if self.conf.test_case != 'fault_management':
184             restart_cmd += ' nova-scheduler.service'
185             restore_scripts.append(self.nc_restore_compute_script)
186
187         for node_ip in self.controllers:
188             client = SSHClient(node_ip, self.node_user_name,
189                                key_filename=self.key_file)
190             self._run_apply_patches(client,
191                                     restart_cmd,
192                                     restore_scripts,
193                                     python=self.python)
194
195         if self.conf.test_case != 'fault_management':
196             restart_cmd = 'sudo systemctl restart nova-compute.service'
197             for node_ip in self.computes:
198                 client = SSHClient(node_ip, self.node_user_name,
199                                    key_filename=self.key_file)
200                 self._run_apply_patches(
201                     client, restart_cmd,
202                     [self.nc_restore_compute_script],
203                     python=self.python)