Support Apex with services in containers
[doctor.git] / doctor_tests / installer / base.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 abc
10 import getpass
11 import grp
12 import os
13 import pwd
14 import six
15 import stat
16 import subprocess
17
18 from doctor_tests.common.utils import get_doctor_test_root_dir
19 from doctor_tests.identity_auth import get_session
20 from doctor_tests.os_clients import nova_client
21
22
23 @six.add_metaclass(abc.ABCMeta)
24 class BaseInstaller(object):
25     def __init__(self, conf, log):
26         self.conf = conf
27         self.log = log
28         self.servers = list()
29         self.use_containers = False
30
31     @abc.abstractproperty
32     def node_user_name(self):
33         """user name for login to cloud node"""
34
35     @abc.abstractmethod
36     def get_ssh_key_from_installer(self):
37         pass
38
39     @abc.abstractmethod
40     def get_host_ip_from_hostname(self, hostname):
41         pass
42
43     @abc.abstractmethod
44     def setup(self):
45         pass
46
47     @abc.abstractmethod
48     def cleanup(self):
49         pass
50
51     def create_flavor(self):
52         self.nova = \
53             nova_client(self.conf.nova_version,
54                         get_session())
55         flavors = {flavor.name: flavor for flavor in self.nova.flavors.list()}
56         if self.conf.flavor not in flavors:
57             self.nova.flavors.create(self.conf.flavor, 512, 1, 1)
58
59     def setup_stunnel(self):
60         self.log.info('Setup ssh stunnel in %s installer......'
61                       % self.conf.installer.type)
62         tunnels = [self.conf.consumer.port]
63         if self.conf.test_case == 'maintenance':
64             tunnel_uptime = 1200
65             tunnels += [self.conf.app_manager.port, self.conf.inspector.port]
66         elif self.conf.test_case == 'all':
67             tunnel_uptime = 1800
68             tunnels += [self.conf.app_manager.port, self.conf.inspector.port]
69         else:
70             tunnel_uptime = 600
71
72         for node_ip in self.controllers:
73             for port in tunnels:
74                 self.log.info('tunnel for port %s' % port)
75                 cmd = ("ssh -o UserKnownHostsFile=/dev/null"
76                        " -o StrictHostKeyChecking=no"
77                        " -i %s %s@%s -R %s:localhost:%s"
78                        " sleep %s > ssh_tunnel.%s"
79                        " 2>&1 < /dev/null "
80                        % (self.key_file,
81                           self.node_user_name,
82                           node_ip,
83                           port,
84                           port,
85                           tunnel_uptime,
86                           node_ip))
87                 server = subprocess.Popen('exec ' + cmd, shell=True)
88                 self.servers.append(server)
89
90     def _get_ssh_key(self, client, key_path):
91         self.log.info('Get SSH keys from %s installer......'
92                       % self.conf.installer.type)
93
94         if self.key_file is not None:
95             self.log.info('Already have SSH keys from %s installer......'
96                           % self.conf.installer.type)
97             return self.key_file
98
99         ssh_key = '{0}/{1}'.format(get_doctor_test_root_dir(), 'instack_key')
100         client.scp(key_path, ssh_key, method='get')
101         user = getpass.getuser()
102         uid = pwd.getpwnam(user).pw_uid
103         gid = grp.getgrnam(user).gr_gid
104         os.chown(ssh_key, uid, gid)
105         os.chmod(ssh_key, stat.S_IREAD)
106         return ssh_key
107
108     def _run_cmd_remote(self, client, command):
109         self.log.info('Run command=%s in %s installer......'
110                       % (command, self.conf.installer.type))
111
112         ret, output = client.ssh(command)
113         if ret:
114             raise Exception('Exec command in %s installer failed,'
115                             'ret=%s, output=%s'
116                             % (self.conf.installer.type,
117                                ret, output))
118         self.log.info('Output=%s command=%s in %s installer'
119                       % (output, command, self.conf.installer.type))
120         return output
121
122     def _check_cmd_remote(self, client, command):
123         self.log.info('Check command=%s return in %s installer......'
124                       % (command, self.conf.installer.type))
125
126         ret, output = client.ssh(command, raise_enabled=False)
127         self.log.info('return %s' % ret)
128         if ret == 0:
129             ret = True
130         else:
131             ret = False
132         return ret
133
134     def _run_apply_patches(self, client, restart_cmd, script_names,
135                            python='python3'):
136         installer_dir = os.path.dirname(os.path.realpath(__file__))
137
138         if isinstance(script_names, list):
139             for script_name in script_names:
140                 script_abs_path = '{0}/{1}/{2}'.format(installer_dir,
141                                                        'common', script_name)
142                 client.scp(script_abs_path, script_name)
143                 cmd = 'sudo %s %s' % (python, script_name)
144                 ret, output = client.ssh(cmd)
145                 if ret:
146                     raise Exception('Do the command in remote'
147                                     ' node failed, ret=%s, cmd=%s, output=%s'
148                                     % (ret, cmd, output))
149             client.ssh(restart_cmd)