79c59e9ab774269134a4708110ddcdbf12e4aa18
[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 time
10
11 from doctor_tests.common.constants import Inspector
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     installer_username = 'stack'
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     ac_restart_script = 'restart_aodh.py'
26     ac_restore_script = 'restore_aodh.py'
27     python = 'python'
28
29     def __init__(self, conf, log):
30         super(ApexInstaller, self).__init__(conf, log)
31         self.client = SSHClient(self.conf.installer.ip,
32                                 self.installer_username,
33                                 key_filename=self.conf.installer.key_file,
34                                 look_for_keys=True)
35         self.key_file = None
36         self.controllers = list()
37         self.computes = 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_overcloud_conf()
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_overcloud_conf(self):
57         self.log.info('Get overcloud config details 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         command = "grep docker /home/stack/deploy_command"
69         self.use_containers = self._check_cmd_remote(self.client, command)
70         self.log.info('controller_ips:%s' % self.controllers)
71         self.log.info('compute_ips:%s' % self.computes)
72         self.log.info('use_containers:%s' % self.use_containers)
73
74     def get_host_ip_from_hostname(self, hostname):
75         self.log.info('Get host ip by hostname=%s from Apex installer......'
76                       % hostname)
77
78         hostname_in_undercloud = hostname.split('.')[0]
79         command = "source stackrc; nova show %s | awk '/ ctlplane network /{print $5}'" % (hostname_in_undercloud)   # noqa
80         host_ips = self._run_cmd_remote(self.client, command)
81         return host_ips[0]
82
83     def _set_docker_restart_cmd(self, service):
84         # There can be multiple instances running so need to restart all
85         cmd = "for container in `sudo docker ps | grep "
86         cmd += service
87         cmd += " | awk '{print $1}'`; do sudo docker restart $container; \
88                done;"
89         return cmd
90
91     def set_apply_patches(self):
92         self.log.info('Set apply patches start......')
93
94         set_scripts = [self.cm_set_script]
95
96         if self.use_containers:
97             restart_cmd = (self._set_docker_restart_cmd(
98                            "ceilometer-notification"))
99             set_scripts.append(self.ac_restart_script)
100         else:
101             restart_cmd = 'sudo systemctl restart' \
102                           ' openstack-ceilometer-notification.service'
103
104         if self.conf.test_case != 'fault_management':
105             if self.use_containers:
106                 restart_cmd += self._set_docker_restart_cmd("nova-scheduler")
107             else:
108                 restart_cmd += ' openstack-nova-scheduler.service'
109             set_scripts.append(self.nc_set_compute_script)
110
111         if self.conf.inspector.type == Inspector.CONGRESS:
112             if self.use_containers:
113                 restart_cmd += self._set_docker_restart_cmd("congress-server")
114             else:
115                 restart_cmd += ' openstack-congress-server.service'
116             set_scripts.append(self.cg_set_script)
117
118         for node_ip in self.controllers:
119             client = SSHClient(node_ip, self.node_user_name,
120                                key_filename=self.key_file)
121             self._run_apply_patches(client,
122                                     restart_cmd,
123                                     set_scripts,
124                                     python=self.python)
125         time.sleep(5)
126
127         self.log.info('Set apply patches start......')
128
129         if self.conf.test_case != 'fault_management':
130             if self.use_containers:
131                 restart_cmd = self._set_docker_restart_cmd("nova")
132             else:
133                 restart_cmd = 'sudo systemctl restart' \
134                               ' openstack-nova-compute.service'
135             for node_ip in self.computes:
136                 client = SSHClient(node_ip, self.node_user_name,
137                                    key_filename=self.key_file)
138                 self._run_apply_patches(client,
139                                         restart_cmd,
140                                         [self.nc_set_compute_script],
141                                         python=self.python)
142             time.sleep(5)
143
144     def restore_apply_patches(self):
145         self.log.info('restore apply patches start......')
146
147         restore_scripts = [self.cm_restore_script]
148
149         if self.use_containers:
150             restart_cmd = (self._set_docker_restart_cmd(
151                            "ceilometer-notification"))
152             restore_scripts.append(self.ac_restore_script)
153         else:
154             restart_cmd = 'sudo systemctl restart' \
155                           ' openstack-ceilometer-notification.service'
156
157         if self.conf.test_case != 'fault_management':
158             if self.use_containers:
159                 restart_cmd += self._set_docker_restart_cmd("nova-scheduler")
160             else:
161                 restart_cmd += ' openstack-nova-scheduler.service'
162             restore_scripts.append(self.nc_restore_compute_script)
163
164         if self.conf.inspector.type == Inspector.CONGRESS:
165             if self.use_containers:
166                 restart_cmd += self._set_docker_restart_cmd("congress-server")
167             else:
168                 restart_cmd += ' openstack-congress-server.service'
169             restore_scripts.append(self.cg_restore_script)
170
171         for node_ip in self.controllers:
172             client = SSHClient(node_ip, self.node_user_name,
173                                key_filename=self.key_file)
174             self._run_apply_patches(client,
175                                     restart_cmd,
176                                     restore_scripts,
177                                     python=self.python)
178
179         if self.conf.test_case != 'fault_management':
180             if self.use_containers:
181                 restart_cmd = self._set_docker_restart_cmd("nova-compute")
182             else:
183                 restart_cmd = 'sudo systemctl restart' \
184                               ' openstack-nova-compute.service'
185             for node_ip in self.computes:
186                 self._run_apply_patches(
187                     client, restart_cmd,
188                     [self.nc_restore_compute_script],
189                     python=self.python)