fix the pep8 check
[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 getpass
10 import grp
11 import os
12 import pwd
13 import stat
14 import subprocess
15
16 from doctor_tests.common.utils import get_doctor_test_root_dir
17 from doctor_tests.common.utils import SSHClient
18 from doctor_tests.installer.base import BaseInstaller
19
20
21 class ApexInstaller(BaseInstaller):
22     node_user_name = 'heat-admin'
23     cm_set_script = 'set_ceilometer.py'
24     cm_restore_script = 'restore_ceilometer.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                                 look_for_keys=True)
31         self.key_file = None
32         self.controllers = list()
33         self.controller_clients = list()
34         self.servers = list()
35         self.test_dir = get_doctor_test_root_dir()
36
37     def setup(self):
38         self.log.info('Setup Apex installer start......')
39
40         self.get_ssh_key_from_installer()
41         self.get_controller_ips()
42         self.set_apply_patches()
43         self.setup_stunnel()
44
45     def cleanup(self):
46         self.restore_apply_patches()
47         for server in self.servers:
48             server.terminate()
49
50     def get_ssh_key_from_installer(self):
51         self.log.info('Get SSH keys from Apex installer......')
52
53         if self.key_file is not None:
54             self.log.info('Already have SSH keys from Apex installer......')
55             return self.key_file
56
57         ssh_key = '{0}/{1}'.format(self.test_dir, 'instack_key')
58         self.client.scp('/home/stack/.ssh/id_rsa', ssh_key, method='get')
59         user = getpass.getuser()
60         uid = pwd.getpwnam(user).pw_uid
61         gid = grp.getgrnam(user).gr_gid
62         os.chown(ssh_key, uid, gid)
63         os.chmod(ssh_key, stat.S_IREAD)
64         self.key_file = ssh_key
65         return self.key_file
66
67     def get_controller_ips(self):
68         self.log.info('Get controller ips from Apex installer......')
69
70         command = "source stackrc; " \
71                   "nova list | grep ' overcloud-controller-[0-9] ' " \
72                   "| sed -e 's/^.*ctlplane=//' |awk '{print $1}'"
73         ret, controllers = self.client.ssh(command)
74         if ret:
75             raise Exception('Exec command to get controller ips'
76                             'in Apex installer failed, ret=%s, output=%s'
77                             % (ret, controllers))
78         self.log.info('Get controller_ips:%s from Apex installer'
79                       % controllers)
80         self.controllers = controllers
81
82     def get_host_ip_from_hostname(self, hostname):
83         self.log.info('Get host ip from host name in Apex installer......')
84
85         hostname_in_undercloud = hostname.split('.')[0]
86
87         command = "source stackrc; nova show %s | awk '/ ctlplane network /{print $5}'" % (hostname_in_undercloud)   # noqa
88         ret, host_ip = self.client.ssh(command)
89         if ret:
90             raise Exception('Exec command to get host ip from hostname(%s)'
91                             'in Apex installer failed, ret=%s, output=%s'
92                             % (hostname, ret, host_ip))
93         self.log.info('Get host_ip:%s from host_name:%s in Apex installer'
94                       % (host_ip, hostname))
95         return host_ip[0]
96
97     def setup_stunnel(self):
98         self.log.info('Setup ssh stunnel in controller nodes'
99                       'in Apex installer......')
100         for node_ip in self.controllers:
101             cmd = ("ssh -o UserKnownHostsFile=/dev/null"
102                    " -o StrictHostKeyChecking=no"
103                    " -i %s %s@%s -R %s:localhost:%s"
104                    " sleep 600 > ssh_tunnel.%s.log"
105                    " 2>&1 < /dev/null &"
106                    % (self.key_file,
107                       self.node_user_name,
108                       node_ip,
109                       self.conf.consumer.port,
110                       self.conf.consumer.port,
111                       node_ip))
112             server = subprocess.Popen(cmd, shell=True)
113             self.servers.append(server)
114             server.communicate()
115
116     def set_apply_patches(self):
117         self.log.info('Set apply patches start......')
118
119         for node_ip in self.controllers:
120             client = SSHClient(node_ip, self.node_user_name,
121                                key_filename=self.key_file)
122             self.controller_clients.append(client)
123             self._ceilometer_apply_patches(client, self.cm_set_script)
124
125     def restore_apply_patches(self):
126         self.log.info('restore apply patches start......')
127
128         for client in self.controller_clients:
129             self._ceilometer_apply_patches(client, self.cm_restore_script)
130
131     def _ceilometer_apply_patches(self, ssh_client, script_name):
132         installer_dir = os.path.dirname(os.path.realpath(__file__))
133         script_abs_path = '{0}/{1}/{2}'.format(installer_dir,
134                                                'common', script_name)
135
136         ssh_client.scp(script_abs_path, script_name)
137         cmd = 'sudo python %s' % script_name
138         ret, output = ssh_client.ssh(cmd)
139         if ret:
140             raise Exception('Do the ceilometer command in controller'
141                             ' node failed, ret=%s, cmd=%s, output=%s'
142                             % (ret, cmd, output))
143         ssh_client.ssh('sudo systemctl restart '
144                        'openstack-ceilometer-notification.service')