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