fix the pep8 check
[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.identity_auth import get_session
19 from doctor_tests.installer.base import BaseInstaller
20 from doctor_tests.os_clients import nova_client
21
22
23 class DaisyInstaller(BaseInstaller):
24     node_user_name = 'root'
25
26     def __init__(self, conf, log):
27         super(DaisyInstaller, self).__init__(conf, log)
28         self.client = SSHClient(self.conf.installer.ip,
29                                 self.conf.installer.username,
30                                 password='r00tme')
31         self.key_file = None
32         self.controllers = list()
33         self.servers = list()
34         self.test_dir = get_doctor_test_root_dir()
35
36     def setup(self):
37         self.log.info('Setup Daisy installer start......')
38
39         self.get_ssh_key_from_installer()
40         self.get_controller_ips()
41         self.create_flavor()
42         self.setup_stunnel()
43
44     def cleanup(self):
45         for server in self.servers:
46             server.terminate()
47
48     def get_ssh_key_from_installer(self):
49         self.log.info('Get SSH keys from Daisy installer......')
50
51         if self.key_file is not None:
52             self.log.info('Already have SSH keys from Daisy installer......')
53             return self.key_file
54
55         ssh_key = '{0}/{1}'.format(self.test_dir, 'instack_key')
56         self.client.scp('/root/.ssh/id_dsa', ssh_key, method='get')
57         user = getpass.getuser()
58         uid = pwd.getpwnam(user).pw_uid
59         gid = grp.getgrnam(user).gr_gid
60         os.chown(ssh_key, uid, gid)
61         os.chmod(ssh_key, stat.S_IREAD)
62         self.key_file = ssh_key
63         return self.key_file
64
65     def get_controller_ips(self):
66         self.log.info('Get controller ips from Daisy installer......')
67
68         command = "source daisyrc_admin; " \
69                   "daisy host-list | grep 'CONTROLLER_LB' | cut -d '|' -f 3 "
70         ret, controllers = self.client.ssh(command)
71         if ret:
72             raise Exception('Exec command to get controller ips'
73                             'in Daisy installer failed'
74                             'ret=%s, output=%s' % (ret, controllers))
75         controller_ips = []
76         for controller in controllers:
77             controller_ips.append(self.get_host_ip_from_hostname(controller))
78         self.log.info('Get controller_ips:%s from Daisy installer'
79                       % controller_ips)
80         self.controllers = controller_ips
81
82     def get_host_ip_from_hostname(self, hostname):
83         self.log.info('Get host ip from host name......')
84
85         hostip = hostname.split('-')[1:]
86         host_ip = '.'.join(hostip)
87         self.log.info('Get host_ip:%s from host_name:%s'
88                       % (host_ip, hostname))
89         return host_ip
90
91     def create_flavor(self):
92         self.nova = \
93             nova_client(self.conf.nova_version,
94                         get_session())
95         flavors = {flavor.name: flavor for flavor in self.nova.flavors.list()}
96         if self.conf.flavor not in flavors:
97             self.nova.flavors.create(self.conf.flavor, 512, 1, 1)
98
99     def setup_stunnel(self):
100         self.log.info('Setup ssh stunnel in controller nodes'
101                       'in Daisy installer......')
102         for node_ip in self.controllers:
103             cmd = ("ssh -o UserKnownHostsFile=/dev/null"
104                    " -o StrictHostKeyChecking=no"
105                    " -i %s %s@%s -R %s:localhost:%s"
106                    " sleep 600 > ssh_tunnel.%s 2>&1 < /dev/null &"
107                    % (self.key_file, self.node_user_name,
108                       node_ip, self.conf.consumer.port,
109                       self.conf.consumer.port, node_ip))
110             server = subprocess.Popen(cmd, shell=True)
111             self.servers.append(server)
112             server.communicate()