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