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 SSHClient
17 from doctor_tests.installer.base import BaseInstaller
20 class ApexInstaller(BaseInstaller):
21 node_user_name = 'heat-admin'
22 cm_set_script = 'set_ceilometer.py'
23 cm_restore_script = 'restore_ceilometer.py'
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,
31 self.controllers = list()
32 self.controller_clients = list()
36 self.log.info('Setup Apex installer start......')
38 self.get_ssh_key_from_installer()
39 self.get_controller_ips()
40 self.set_apply_patches()
44 self.restore_apply_patches()
45 for server in self.servers:
48 def get_ssh_key_from_installer(self):
49 self.log.info('Get SSH keys from Apex installer......')
51 if self.key_file is not None:
52 self.log.info('Already have SSH keys from Apex installer......')
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')
65 def get_controller_ips(self):
66 self.log.info('Get controller ips from Apex installer......')
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)
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
78 def get_host_ip_from_hostname(self, hostname):
79 self.log.info('Get host ip from host name in Apex installer......')
81 hostname_in_undercloud = hostname.split('.')[0]
83 command = "source stackrc; nova show %s | awk '/ ctlplane network /{print $5}'" % (hostname_in_undercloud)
84 ret, host_ip = self.client.ssh(command)
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))
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)
101 def set_apply_patches(self):
102 self.log.info('Set apply patches start......')
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)
109 def restore_apply_patches(self):
110 self.log.info('restore apply patches start......')
112 for client in self.controller_clients:
113 self._ceilometer_apply_patches(client, self.cm_restore_script)
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)
119 ssh_client.scp(script_abs_path, script_name)
120 cmd = 'sudo python %s' % script_name
121 ret, output = ssh_client.ssh(cmd)
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')