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 DaisyInstaller(BaseInstaller):
22 node_user_name = 'root'
24 def __init__(self, conf, log):
25 super(DaisyInstaller, self).__init__(conf, log)
26 self.client = SSHClient(self.conf.installer.ip,
27 self.conf.installer.username,
30 self.controllers = list()
32 self.test_dir = get_doctor_test_root_dir()
35 self.log.info('Setup Daisy installer start......')
37 self.get_ssh_key_from_installer()
38 self.get_controller_ips()
43 for server in self.servers:
46 def get_ssh_key_from_installer(self):
47 self.log.info('Get SSH keys from Daisy installer......')
49 if self.key_file is not None:
50 self.log.info('Already have SSH keys from Daisy installer......')
53 ssh_key = '{0}/{1}'.format(self.test_dir, 'instack_key')
54 self.client.scp('/root/.ssh/id_dsa', ssh_key, method='get')
55 user = getpass.getuser()
56 uid = pwd.getpwnam(user).pw_uid
57 gid = grp.getgrnam(user).gr_gid
58 os.chown(ssh_key, uid, gid)
59 os.chmod(ssh_key, stat.S_IREAD)
60 self.key_file = ssh_key
63 def get_controller_ips(self):
64 self.log.info('Get controller ips from Daisy installer......')
66 command = "source daisyrc_admin; " \
67 "daisy host-list | grep 'CONTROLLER_LB' | cut -d '|' -f 3 "
68 ret, controllers = self.client.ssh(command)
70 raise Exception('Exec command to get controller ips'
71 'in Daisy installer failed'
72 'ret=%s, output=%s' % (ret, controllers))
74 for controller in controllers:
75 controller_ips.append(self.get_host_ip_from_hostname(controller))
76 self.log.info('Get controller_ips:%s from Daisy installer'
78 self.controllers = controller_ips
80 def get_host_ip_from_hostname(self, hostname):
81 self.log.info('Get host ip from host name......')
83 hostip = hostname.split('-')[1:]
84 host_ip = '.'.join(hostip)
85 self.log.info('Get host_ip:%s from host_name:%s'
86 % (host_ip, hostname))
89 def setup_stunnel(self):
90 self.log.info('Setup ssh stunnel in controller nodes'
91 'in Daisy installer......')
92 for node_ip in self.controllers:
93 cmd = ("ssh -o UserKnownHostsFile=/dev/null"
94 " -o StrictHostKeyChecking=no"
95 " -i %s %s@%s -R %s:localhost:%s"
96 " sleep 600 > ssh_tunnel.%s 2>&1 < /dev/null &"
97 % (self.key_file, self.node_user_name,
98 node_ip, self.conf.consumer.port,
99 self.conf.consumer.port, node_ip))
100 server = subprocess.Popen(cmd, shell=True)
101 self.servers.append(server)