create flavor for Apex installer
[doctor.git] / doctor_tests / installer / daisy.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 get_doctor_test_root_dir
17 from doctor_tests.common.utils import SSHClient
18 from doctor_tests.installer.base import BaseInstaller
19
20
21 class DaisyInstaller(BaseInstaller):
22     node_user_name = 'root'
23
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,
28                                 password='r00tme')
29         self.key_file = None
30         self.controllers = list()
31         self.servers = list()
32         self.test_dir = get_doctor_test_root_dir()
33
34     def setup(self):
35         self.log.info('Setup Daisy installer start......')
36
37         self.get_ssh_key_from_installer()
38         self.get_controller_ips()
39         self.create_flavor()
40         self.setup_stunnel()
41
42     def cleanup(self):
43         for server in self.servers:
44             server.terminate()
45
46     def get_ssh_key_from_installer(self):
47         self.log.info('Get SSH keys from Daisy installer......')
48
49         if self.key_file is not None:
50             self.log.info('Already have SSH keys from Daisy installer......')
51             return self.key_file
52
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
61         return self.key_file
62
63     def get_controller_ips(self):
64         self.log.info('Get controller ips from Daisy installer......')
65
66         command = "source daisyrc_admin; " \
67                   "daisy host-list | grep 'CONTROLLER_LB' | cut -d '|' -f 3 "
68         ret, controllers = self.client.ssh(command)
69         if ret:
70             raise Exception('Exec command to get controller ips'
71                             'in Daisy installer failed'
72                             'ret=%s, output=%s' % (ret, controllers))
73         controller_ips = []
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'
77                       % controller_ips)
78         self.controllers = controller_ips
79
80     def get_host_ip_from_hostname(self, hostname):
81         self.log.info('Get host ip from host name......')
82
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))
87         return host_ip
88
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)
102             server.communicate()