fix to get logfile
[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 in Apex installer failed'
76                             'ret=%s, output=%s' % (ret, controllers))
77         self.log.info('Get controller_ips:%s from Apex installer' % controllers)
78         self.controllers = controllers
79
80     def get_host_ip_from_hostname(self, hostname):
81         self.log.info('Get host ip from host name in Apex installer......')
82
83         hostname_in_undercloud = hostname.split('.')[0]
84
85         command = "source stackrc; nova show %s  | awk '/ ctlplane network /{print $5}'" % (hostname_in_undercloud)
86         ret, host_ip = self.client.ssh(command)
87         if ret:
88             raise Exception('Exec command to get host ip from hostname(%s) in Apex installer failed'
89                             'ret=%s, output=%s' % (hostname, ret, host_ip))
90         self.log.info('Get host_ip:%s from host_name:%s in Apex installer' % (host_ip, hostname))
91         return host_ip[0]
92
93     def setup_stunnel(self):
94         self.log.info('Setup ssh stunnel in controller nodes in Apex installer......')
95         for node_ip in self.controllers:
96             cmd = "sudo ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i %s %s@%s -R %s:localhost:%s sleep 600 > ssh_tunnel.%s.log 2>&1 < /dev/null &" \
97                   % (self.key_file, self.node_user_name, node_ip,
98                      self.conf.consumer.port, self.conf.consumer.port, node_ip)
99             server = subprocess.Popen(cmd, shell=True)
100             self.servers.append(server)
101             server.communicate()
102
103     def set_apply_patches(self):
104         self.log.info('Set apply patches start......')
105
106         for node_ip in self.controllers:
107             client = SSHClient(node_ip, self.node_user_name, key_filename=self.key_file)
108             self.controller_clients.append(client)
109             self._ceilometer_apply_patches(client, self.cm_set_script)
110
111     def restore_apply_patches(self):
112         self.log.info('restore apply patches start......')
113
114         for client in self.controller_clients:
115             self._ceilometer_apply_patches(client, self.cm_restore_script)
116
117     def _ceilometer_apply_patches(self, ssh_client, script_name):
118         installer_dir = os.path.dirname(os.path.realpath(__file__))
119         script_abs_path = '{0}/{1}/{2}'.format(installer_dir, 'common', script_name)
120
121         ssh_client.scp(script_abs_path, script_name)
122         cmd = 'sudo python %s' % script_name
123         ret, output = ssh_client.ssh(cmd)
124         if ret:
125             raise Exception('Do the ceilometer command in controller node failed....'
126                             'ret=%s, cmd=%s, output=%s' % (ret, cmd, output))
127         ssh_client.ssh('sudo systemctl restart openstack-ceilometer-notification.service')
128