1 ##############################################################################
2 # Copyright (c) 2019 ZTE Corporation and others.
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
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
19 class McpInstaller(BaseInstaller):
20 node_user_name = 'ubuntu'
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'
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,
36 key_filename=self.key_file,
38 self.controllers = list()
39 self.controller_clients = list()
40 self.computes = list()
43 self.log.info('Setup MCP installer start......')
46 if is_fenix(self.conf):
47 self.set_apply_patches()
51 if is_fenix(self.conf):
52 self.restore_apply_patches()
53 for server in self.servers:
56 def get_ssh_key_from_installer(self):
57 self.log.info('Get SSH keys from MCP......')
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
64 def get_transport_url(self):
65 client = SSHClient(self.controllers[0], self.node_user_name,
66 key_filename=self.key_file)
68 cmd = 'sudo grep -m1 "^transport_url" /etc/nova/nova.conf'
69 ret, url = client.ssh(cmd)
72 raise Exception('Exec command to get transport from '
73 'controller(%s) in MCP installer failed, '
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]))
81 cmd = 'grep -i "^rabbit" /etc/nova/nova.conf'
82 ret, lines = client.ssh(cmd)
84 raise Exception('Exec command to get transport from '
85 'controller(%s) in MCP installer failed, '
87 % (self.controllers[0], ret, url))
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,
100 self.log.info('get_transport_url %s' % url)
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)
108 def get_node_ips(self):
109 self.log.info('Get node ips from Mcp installer......')
111 command = 'sudo salt "*" --out yaml pillar.get _param:single_address'
112 node_details = self._run_cmd_remote(self.client, command)
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")]
119 self.log.info('controller_ips:%s' % self.controllers)
120 self.log.info('compute_ips:%s' % self.computes)
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)
129 def set_apply_patches(self):
130 self.log.info('Set apply patches start......')
132 set_scripts = [self.cm_set_script]
135 restart_cmd = 'sudo systemctl restart' \
136 ' ceilometer-agent-notification.service'
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)
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,
154 client.scp(src_file, fenix_file)
155 thrs.append(self._run_apply_patches(client,
161 self.log.info('Set apply patches start......')
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 thrs.append(self._run_apply_patches(
171 [self.nc_set_compute_script],
174 # If Fenix container ir build, it needs to be ready before continue
178 def restore_apply_patches(self):
179 self.log.info('restore apply patches start......')
181 restore_scripts = [self.cm_restore_script]
183 restore_scripts.append(self.ac_restore_script)
184 restart_cmd = 'sudo systemctl restart' \
185 ' ceilometer-agent-notification.service'
187 if self.conf.test_case != 'fault_management':
188 restart_cmd += ' nova-scheduler.service'
189 restore_scripts.append(self.nc_restore_compute_script)
191 for node_ip in self.controllers:
192 client = SSHClient(node_ip, self.node_user_name,
193 key_filename=self.key_file)
194 self._run_apply_patches(client,
199 if self.conf.test_case != 'fault_management':
200 restart_cmd = 'sudo systemctl restart nova-compute.service'
201 for node_ip in self.computes:
202 client = SSHClient(node_ip, self.node_user_name,
203 key_filename=self.key_file)
204 self._run_apply_patches(
206 [self.nc_restore_compute_script],