Maintenance support for latest Fenix, python3 and Fuel
[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 time
11
12 from doctor_tests.common.constants import is_fenix
13 from doctor_tests.common.utils import get_doctor_test_root_dir
14 from doctor_tests.common.utils import SSHClient
15 from doctor_tests.installer.base import BaseInstaller
16
17
18 class McpInstaller(BaseInstaller):
19     node_user_name = 'ubuntu'
20
21     cm_set_script = 'set_config.py'
22     nc_set_compute_script = 'set_compute_config.py'
23     fe_set_script = 'set_fenix.sh'
24     cm_restore_script = 'restore_config.py'
25     nc_restore_compute_script = 'restore_compute_config.py'
26     ac_restart_script = 'restart_aodh.py'
27     ac_restore_script = 'restore_aodh.py'
28     python = 'python3'
29
30     def __init__(self, conf, log):
31         super(McpInstaller, self).__init__(conf, log)
32         self.key_file = self.get_ssh_key_from_installer()
33         self.client = SSHClient(self.conf.installer.ip,
34                                 self.node_user_name,
35                                 key_filename=self.key_file,
36                                 look_for_keys=True)
37         self.controllers = list()
38         self.controller_clients = list()
39         self.computes = list()
40
41     def setup(self):
42         self.log.info('Setup MCP installer start......')
43         self.get_node_ips()
44         self.create_flavor()
45         if is_fenix(self.conf):
46             self.set_apply_patches()
47         self.setup_stunnel()
48
49     def cleanup(self):
50         if is_fenix(self.conf):
51             self.restore_apply_patches()
52         for server in self.servers:
53             server.terminate()
54
55     def get_ssh_key_from_installer(self):
56         self.log.info('Get SSH keys from MCP......')
57
58         # Default in path /var/lib/opnfv/mcp.rsa
59         ssh_key = '/root/.ssh/id_rsa'
60         mcp_key = '/var/lib/opnfv/mcp.rsa'
61         return mcp_key if isfile(mcp_key) else ssh_key
62
63     def _copy_overcloudrc_to_controllers(self):
64         for ip in self.controllers:
65             cmd = "scp overcloudrc %s@%s:" % (self.node_user_name, ip)
66             self._run_cmd_remote(self.client, cmd)
67
68     def get_node_ips(self):
69         self.log.info('Get node ips from Mcp installer......')
70
71         command = 'sudo salt "*" --out yaml pillar.get _param:single_address'
72         node_details = self._run_cmd_remote(self.client, command)
73
74         self.controllers = [line.split()[1] for line in node_details
75                             if line.startswith("ctl")]
76         self.computes = [line.split()[1] for line in node_details
77                          if line.startswith("cmp")]
78
79         self.log.info('controller_ips:%s' % self.controllers)
80         self.log.info('compute_ips:%s' % self.computes)
81
82     def get_host_ip_from_hostname(self, hostname):
83         command = "sudo salt --out yaml '%s*' " \
84                   "pillar.get _param:single_address |" \
85                   "awk '{print $2}'" % hostname
86         host_ips = self._run_cmd_remote(self.client, command)
87         return host_ips[0]
88
89     def set_apply_patches(self):
90         self.log.info('Set apply patches start......')
91         fenix_files = None
92
93         set_scripts = [self.cm_set_script]
94
95         restart_cmd = 'sudo systemctl restart' \
96                       ' ceilometer-agent-notification.service'
97
98         if self.conf.test_case != 'fault_management':
99             if is_fenix(self.conf):
100                 set_scripts.append(self.fe_set_script)
101                 testdir = get_doctor_test_root_dir()
102                 fenix_files = ["Dockerfile", "run"]
103             restart_cmd += ' nova-scheduler.service'
104             set_scripts.append(self.nc_set_compute_script)
105
106         for node_ip in self.controllers:
107             client = SSHClient(node_ip, self.node_user_name,
108                                key_filename=self.key_file)
109             if fenix_files is not None:
110                 for fenix_file in fenix_files:
111                     src_file = '{0}/{1}/{2}'.format(testdir,
112                                                     'admin_tool/fenix',
113                                                     fenix_file)
114                     client.scp(src_file, fenix_file)
115             self._run_apply_patches(client,
116                                     restart_cmd,
117                                     set_scripts,
118                                     python=self.python)
119         time.sleep(5)
120
121         self.log.info('Set apply patches start......')
122
123         if self.conf.test_case != 'fault_management':
124             restart_cmd = 'sudo systemctl restart nova-compute.service'
125             for node_ip in self.computes:
126                 client = SSHClient(node_ip, self.node_user_name,
127                                    key_filename=self.key_file)
128                 self._run_apply_patches(client,
129                                         restart_cmd,
130                                         [self.nc_set_compute_script],
131                                         python=self.python)
132             time.sleep(5)
133
134     def restore_apply_patches(self):
135         self.log.info('restore apply patches start......')
136
137         restore_scripts = [self.cm_restore_script]
138
139         restore_scripts.append(self.ac_restore_script)
140         restart_cmd = 'sudo systemctl restart' \
141                       ' ceilometer-agent-notification.service'
142
143         if self.conf.test_case != 'fault_management':
144             restart_cmd += ' nova-scheduler.service'
145             restore_scripts.append(self.nc_restore_compute_script)
146
147         for node_ip in self.controllers:
148             client = SSHClient(node_ip, self.node_user_name,
149                                key_filename=self.key_file)
150             self._run_apply_patches(client,
151                                     restart_cmd,
152                                     restore_scripts,
153                                     python=self.python)
154
155         if self.conf.test_case != 'fault_management':
156             restart_cmd = 'sudo systemctl restart nova-compute.service'
157             for node_ip in self.computes:
158                 client = SSHClient(node_ip, self.node_user_name,
159                                    key_filename=self.key_file)
160                 self._run_apply_patches(
161                     client, restart_cmd,
162                     [self.nc_restore_compute_script],
163                     python=self.python)