create flavor for Apex installer
[doctor.git] / doctor_tests / installer / apex.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 ApexInstaller(BaseInstaller):
22     node_user_name = 'heat-admin'
23     cm_set_script = 'set_ceilometer.py'
24     cm_restore_script = 'restore_ceilometer.py'
25
26     def __init__(self, conf, log):
27         super(ApexInstaller, self).__init__(conf, log)
28         self.client = SSHClient(self.conf.installer.ip,
29                                 self.conf.installer.username,
30                                 look_for_keys=True)
31         self.key_file = None
32         self.controllers = list()
33         self.controller_clients = list()
34         self.servers = list()
35         self.test_dir = get_doctor_test_root_dir()
36
37     def setup(self):
38         self.log.info('Setup Apex installer start......')
39
40         self.get_ssh_key_from_installer()
41         self.get_controller_ips()
42         self.create_flavor()
43         self.set_apply_patches()
44         self.setup_stunnel()
45
46     def cleanup(self):
47         self.restore_apply_patches()
48         for server in self.servers:
49             server.terminate()
50
51     def get_ssh_key_from_installer(self):
52         self.log.info('Get SSH keys from Apex installer......')
53
54         if self.key_file is not None:
55             self.log.info('Already have SSH keys from Apex installer......')
56             return self.key_file
57
58         ssh_key = '{0}/{1}'.format(self.test_dir, 'instack_key')
59         self.client.scp('/home/stack/.ssh/id_rsa', ssh_key, method='get')
60         user = getpass.getuser()
61         uid = pwd.getpwnam(user).pw_uid
62         gid = grp.getgrnam(user).gr_gid
63         os.chown(ssh_key, uid, gid)
64         os.chmod(ssh_key, stat.S_IREAD)
65         self.key_file = ssh_key
66         return self.key_file
67
68     def get_controller_ips(self):
69         self.log.info('Get controller ips from Apex installer......')
70
71         command = "source stackrc; " \
72                   "nova list | grep ' overcloud-controller-[0-9] ' " \
73                   "| sed -e 's/^.*ctlplane=//' |awk '{print $1}'"
74         ret, controllers = self.client.ssh(command)
75         if ret:
76             raise Exception('Exec command to get controller ips'
77                             'in Apex installer failed, ret=%s, output=%s'
78                             % (ret, controllers))
79         self.log.info('Get controller_ips:%s from Apex installer'
80                       % controllers)
81         self.controllers = controllers
82
83     def get_host_ip_from_hostname(self, hostname):
84         self.log.info('Get host ip from host name in Apex installer......')
85
86         hostname_in_undercloud = hostname.split('.')[0]
87
88         command = "source stackrc; nova show %s | awk '/ ctlplane network /{print $5}'" % (hostname_in_undercloud)   # noqa
89         ret, host_ip = self.client.ssh(command)
90         if ret:
91             raise Exception('Exec command to get host ip from hostname(%s)'
92                             'in Apex installer failed, ret=%s, output=%s'
93                             % (hostname, ret, host_ip))
94         self.log.info('Get host_ip:%s from host_name:%s in Apex installer'
95                       % (host_ip, hostname))
96         return host_ip[0]
97
98     def setup_stunnel(self):
99         self.log.info('Setup ssh stunnel in controller nodes'
100                       'in Apex installer......')
101         for node_ip in self.controllers:
102             cmd = ("ssh -o UserKnownHostsFile=/dev/null"
103                    " -o StrictHostKeyChecking=no"
104                    " -i %s %s@%s -R %s:localhost:%s"
105                    " sleep 600 > ssh_tunnel.%s.log"
106                    " 2>&1 < /dev/null &"
107                    % (self.key_file,
108                       self.node_user_name,
109                       node_ip,
110                       self.conf.consumer.port,
111                       self.conf.consumer.port,
112                       node_ip))
113             server = subprocess.Popen(cmd, shell=True)
114             self.servers.append(server)
115             server.communicate()
116
117     def set_apply_patches(self):
118         self.log.info('Set apply patches start......')
119
120         for node_ip in self.controllers:
121             client = SSHClient(node_ip, self.node_user_name,
122                                key_filename=self.key_file)
123             self.controller_clients.append(client)
124             self._ceilometer_apply_patches(client, self.cm_set_script)
125
126     def restore_apply_patches(self):
127         self.log.info('restore apply patches start......')
128
129         for client in self.controller_clients:
130             self._ceilometer_apply_patches(client, self.cm_restore_script)
131
132     def _ceilometer_apply_patches(self, ssh_client, script_name):
133         installer_dir = os.path.dirname(os.path.realpath(__file__))
134         script_abs_path = '{0}/{1}/{2}'.format(installer_dir,
135                                                'common', script_name)
136
137         ssh_client.scp(script_abs_path, script_name)
138         cmd = 'sudo python %s' % script_name
139         ret, output = ssh_client.ssh(cmd)
140         if ret:
141             raise Exception('Do the ceilometer command in controller'
142                             ' node failed, ret=%s, cmd=%s, output=%s'
143                             % (ret, cmd, output))
144         ssh_client.ssh('sudo systemctl restart '
145                        'openstack-ceilometer-notification.service')