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