1 ##############################################################################
2 # Copyright (c) 2017 ZTE Corporation and others.
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 ##############################################################################
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
21 class ApexInstaller(BaseInstaller):
22 node_user_name = 'heat-admin'
23 cm_set_script = 'set_ceilometer.py'
24 cm_restore_script = 'restore_ceilometer.py'
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,
32 self.controllers = list()
33 self.controller_clients = list()
35 self.test_dir = get_doctor_test_root_dir()
38 self.log.info('Setup Apex installer start......')
40 self.get_ssh_key_from_installer()
41 self.get_controller_ips()
42 self.set_apply_patches()
46 self.restore_apply_patches()
47 for server in self.servers:
50 def get_ssh_key_from_installer(self):
51 self.log.info('Get SSH keys from Apex installer......')
53 if self.key_file is not None:
54 self.log.info('Already have SSH keys from Apex installer......')
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
67 def get_controller_ips(self):
68 self.log.info('Get controller ips from Apex installer......')
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)
75 raise Exception('Exec command to get controller ips'
76 'in Apex installer failed, ret=%s, output=%s'
78 self.log.info('Get controller_ips:%s from Apex installer'
80 self.controllers = controllers
82 def get_host_ip_from_hostname(self, hostname):
83 self.log.info('Get host ip from host name in Apex installer......')
85 hostname_in_undercloud = hostname.split('.')[0]
87 command = "source stackrc; nova show %s | awk '/ ctlplane network /{print $5}'" % (hostname_in_undercloud) # noqa
88 ret, host_ip = self.client.ssh(command)
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))
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"
109 self.conf.consumer.port,
110 self.conf.consumer.port,
112 server = subprocess.Popen(cmd, shell=True)
113 self.servers.append(server)
116 def set_apply_patches(self):
117 self.log.info('Set apply patches start......')
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)
125 def restore_apply_patches(self):
126 self.log.info('restore apply patches start......')
128 for client in self.controller_clients:
129 self._ceilometer_apply_patches(client, self.cm_restore_script)
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)
136 ssh_client.scp(script_abs_path, script_name)
137 cmd = 'sudo python %s' % script_name
138 ret, output = ssh_client.ssh(cmd)
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')