fix the username to login undercloud in Apex
[doctor.git] / doctor_tests / installer / apex.py
1 ##############################################################################
2 # Copyright (c) 2017 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 import re
10 import time
11
12 from doctor_tests.common.constants import Inspector
13 from doctor_tests.common.utils import SSHClient
14 from doctor_tests.installer.base import BaseInstaller
15
16
17 class ApexInstaller(BaseInstaller):
18     node_user_name = 'heat-admin'
19     installer_username = 'stack'
20     cm_set_script = 'set_config.py'
21     nc_set_compute_script = 'set_compute_config.py'
22     cg_set_script = 'set_congress.py'
23     cm_restore_script = 'restore_config.py'
24     nc_restore_compute_script = 'restore_compute_config.py'
25     cg_restore_script = 'restore_congress.py'
26
27     def __init__(self, conf, log):
28         super(ApexInstaller, self).__init__(conf, log)
29         self.client = SSHClient(self.conf.installer.ip,
30                                 self.installer_username,
31                                 key_filename=self.conf.installer.key_file,
32                                 look_for_keys=True)
33         self.key_file = None
34         self.controllers = list()
35         self.computes = list()
36         self.controller_clients = list()
37         self.compute_clients = list()
38
39     def setup(self):
40         self.log.info('Setup Apex installer start......')
41         self.key_file = self.get_ssh_key_from_installer()
42         self._get_and_set_ips()
43         self.create_flavor()
44         self.set_apply_patches()
45         self.setup_stunnel()
46
47     def cleanup(self):
48         self.restore_apply_patches()
49         for server in self.servers:
50             server.terminate()
51
52     def get_ssh_key_from_installer(self):
53         key_path = '/home/stack/.ssh/id_rsa'
54         return self._get_ssh_key(self.client, key_path)
55
56     def _get_and_set_ips(self):
57         self.log.info('Get controller and compute ips from Apex installer'
58                       '......')
59
60         command = "source stackrc; nova list | grep ' overcloud-'"
61         raw_ips_list = self._run_cmd_remote(self.client, command)
62         for line in raw_ips_list:
63             ip = line.split('ctlplane=', 1)[1].split(" ", 1)[0]
64             if 'overcloud-controller-' in line:
65                 self.controllers.append(ip)
66             elif 'overcloud-novacompute-' in line:
67                 self.computes.append(ip)
68         self.log.info('controller_ips:%s' % self.controllers)
69         self.log.info('compute_ips:%s' % self.computes)
70
71     def get_host_ip_from_hostname(self, hostname):
72         self.log.info('Get host ip by hostname=%s from Apex installer......'
73                       % hostname)
74
75         hostname_in_undercloud = hostname.split('.')[0]
76         command = "source stackrc; nova show %s | awk '/ ctlplane network /{print $5}'" % (hostname_in_undercloud)   # noqa
77         host_ips = self._run_cmd_remote(self.client, command)
78         return host_ips[0]
79
80     def get_transport_url(self):
81         client = SSHClient(self.controllers[0], self.node_user_name,
82                            key_filename=self.key_file)
83
84         command = 'sudo grep "^transport_url" /etc/nova/nova.conf'
85         ret, url = client.ssh(command)
86         if ret:
87             raise Exception('Exec command to get host ip from controller(%s)'
88                             'in Apex installer failed, ret=%s, output=%s'
89                             % (self.controllers[0], ret, url))
90         # need to use ip instead of hostname
91         ret = (re.sub("@.*:", "@%s:" % self.controllers[0],
92                url[0].split("=", 1)[1]))
93         self.log.debug('get_transport_url %s' % ret)
94         return ret
95
96     def set_apply_patches(self):
97         self.log.info('Set apply patches start......')
98
99         restart_cmd = 'sudo systemctl restart' \
100                       ' openstack-ceilometer-notification.service'
101
102         set_scripts = [self.cm_set_script]
103
104         if self.conf.test_case != 'fault_management':
105             restart_cmd += ' openstack-nova-scheduler.service'
106
107         if self.conf.inspector.type == Inspector.CONGRESS:
108             restart_cmd += ' openstack-congress-server.service'
109             set_scripts.append(self.cg_set_script)
110
111         for node_ip in self.controllers:
112             client = SSHClient(node_ip, self.node_user_name,
113                                key_filename=self.key_file)
114             self.controller_clients.append(client)
115             self._run_apply_patches(client,
116                                     restart_cmd,
117                                     set_scripts)
118
119         if self.conf.test_case != 'fault_management':
120             restart_cmd = 'sudo systemctl restart' \
121                           ' openstack-nova-compute.service'
122             for node_ip in self.computes:
123                 client = SSHClient(node_ip, self.node_user_name,
124                                    key_filename=self.key_file)
125                 self.compute_clients.append(client)
126                 self._run_apply_patches(client,
127                                         restart_cmd,
128                                         [self.nc_set_compute_script])
129
130         if self.conf.test_case != 'fault_management':
131             time.sleep(10)
132
133     def restore_apply_patches(self):
134         self.log.info('restore apply patches start......')
135
136         restart_cmd = 'sudo systemctl restart' \
137                       ' openstack-ceilometer-notification.service'
138
139         restore_scripts = [self.cm_restore_script]
140
141         if self.conf.test_case != 'fault_management':
142             restart_cmd += ' openstack-nova-scheduler.service'
143
144         if self.conf.inspector.type == Inspector.CONGRESS:
145             restart_cmd += ' openstack-congress-server.service'
146             restore_scripts.append(self.cg_restore_script)
147
148         for client in self.controller_clients:
149             self._run_apply_patches(client,
150                                     restart_cmd,
151                                     restore_scripts)
152
153         if self.conf.test_case != 'fault_management':
154             restart_cmd = 'sudo systemctl restart' \
155                           ' openstack-nova-compute.service'
156             for client in self.compute_clients:
157                 self._run_apply_patches(client,
158                                         restart_cmd,
159                                         [self.nc_restore_compute_script])