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