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