DevStack support
[doctor.git] / doctor_tests / installer / devstack.py
1 ##############################################################################
2 # Copyright (c) 2019 Nokia 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 os
10 import socket
11 import time
12
13 from doctor_tests.common.utils import SSHClient
14 from doctor_tests.common.utils import LocalSSH
15 from doctor_tests.identity_auth import get_session
16 from doctor_tests.installer.base import BaseInstaller
17 from doctor_tests.os_clients import nova_client
18
19
20 class DevstackInstaller(BaseInstaller):
21     node_user_name = None
22     cm_set_script = 'set_config.py'
23     nc_set_compute_script = 'set_compute_config.py'
24     cm_restore_script = 'restore_config.py'
25     nc_restore_compute_script = 'restore_compute_config.py'
26     ac_restart_script = 'restart_aodh.py'
27     ac_restore_script = 'restore_aodh.py'
28     python = 'python'
29
30     def __init__(self, conf, log):
31         super(DevstackInstaller, self).__init__(conf, log)
32         # Run Doctor under users home. sudo hides other env param to be used
33         home, self.node_user_name = (iter(os.environ.get('VIRTUAL_ENV')
34                                      .split('/', 3)[1:3]))
35         # Migration needs to work so ssh should have proper key defined
36         self.key_file = '/%s/%s/.ssh/id_rsa' % (home, self.node_user_name)
37         self.log.info('ssh uses: %s and %s' % (self.node_user_name,
38                                                self.key_file))
39         self.controllers = ([ip for ip in
40                             socket.gethostbyname_ex(socket.gethostname())[2]
41                             if not ip.startswith('127.')] or
42                             [[(s.connect(('8.8.8.8', 53)),
43                              s.getsockname()[0], s.close())
44                              for s in [socket.socket(socket.AF_INET,
45                                        socket.SOCK_DGRAM)]][0][1]])
46         conf.admin_tool.ip = self.controllers[0]
47         self.computes = list()
48         self.nova = nova_client(conf.nova_version, get_session())
49
50     def setup(self):
51         self.log.info('Setup Devstack installer start......')
52         self._get_devstack_conf()
53         self.create_flavor()
54         self.set_apply_patches()
55
56     def cleanup(self):
57         self.restore_apply_patches()
58
59     def get_ssh_key_from_installer(self):
60         return self.key_file
61
62     def get_transport_url(self):
63         client = LocalSSH(self.log)
64         cmd = 'sudo grep -m1 "^transport_url" /etc/nova/nova.conf'
65         ret, url = client.ssh(cmd)
66         url = url.split("= ", 1)[1][:-1]
67         self.log.info('get_transport_url %s' % url)
68         return url
69
70     def get_host_ip_from_hostname(self, hostname):
71         return [hvisor.__getattr__('host_ip') for hvisor in self.hvisors
72                 if hvisor.__getattr__('hypervisor_hostname') == hostname][0]
73
74     def _get_devstack_conf(self):
75         self.log.info('Get devstack config details for Devstack installer'
76                       '......')
77         self.hvisors = self.nova.hypervisors.list(detailed=True)
78         self.log.info('checking hypervisors.......')
79         self.computes = [hvisor.__getattr__('host_ip') for hvisor in
80                          self.hvisors]
81         self.use_containers = False
82         self.log.info('controller_ips:%s' % self.controllers)
83         self.log.info('compute_ips:%s' % self.computes)
84         self.log.info('use_containers:%s' % self.use_containers)
85
86     def _set_docker_restart_cmd(self, service):
87         # There can be multiple instances running so need to restart all
88         cmd = "for container in `sudo docker ps | grep "
89         cmd += service
90         cmd += " | awk '{print $1}'`; do sudo docker restart $container; \
91                done;"
92         return cmd
93
94     def set_apply_patches(self):
95         self.log.info('Set apply patches start......')
96
97         set_scripts = [self.cm_set_script]
98
99         restart_cmd = 'sudo systemctl restart' \
100                       ' devstack@ceilometer-anotification.service'
101
102         client = LocalSSH(self.log)
103         self._run_apply_patches(client,
104                                 restart_cmd,
105                                 set_scripts,
106                                 python=self.python)
107         time.sleep(7)
108
109         self.log.info('Set apply patches start......')
110
111         if self.conf.test_case != 'fault_management':
112             restart_cmd = 'sudo systemctl restart' \
113                           ' devstack@n-cpu.service'
114             for node_ip in self.computes:
115                 client = SSHClient(node_ip, self.node_user_name,
116                                    key_filename=self.key_file)
117                 self._run_apply_patches(client,
118                                         restart_cmd,
119                                         [self.nc_set_compute_script],
120                                         python=self.python)
121             time.sleep(7)
122
123     def restore_apply_patches(self):
124         self.log.info('restore apply patches start......')
125
126         restore_scripts = [self.cm_restore_script]
127
128         restart_cmd = 'sudo systemctl restart' \
129                       ' devstack@ceilometer-anotification.service'
130
131         if self.conf.test_case != 'fault_management':
132             restart_cmd += ' devstack@n-sch.service'
133             restore_scripts.append(self.nc_restore_compute_script)
134
135         client = LocalSSH(self.log)
136         self._run_apply_patches(client,
137                                 restart_cmd,
138                                 restore_scripts,
139                                 python=self.python)
140
141         if self.conf.test_case != 'fault_management':
142
143             restart_cmd = 'sudo systemctl restart' \
144                           ' devstack@n-cpu.service'
145             for node_ip in self.computes:
146                 client = SSHClient(node_ip, self.node_user_name,
147                                    key_filename=self.key_file)
148                 self._run_apply_patches(
149                     client, restart_cmd,
150                     [self.nc_restore_compute_script],
151                     python=self.python)