refactor apex installer
[doctor.git] / 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 sys
15
16 from installer.common.congress import set_doctor_driver_conf
17 from installer.common.congress import restore_doctor_driver_conf
18 from installer.base import BaseInstaller
19 from utils import SSHClient
20
21
22 class ApexInstaller(BaseInstaller):
23     node_user_name = 'heat-admin'
24     cm_set_script = 'set_ceilometer.py'
25     cm_restore_script = 'restore_ceilometer.py'
26
27     def __init__(self, conf, log):
28         super(ApexInstaller, self).__init__(conf, log)
29         self.client = SSHClient(self.conf.installer.ip,
30                                 self.conf.installer.username,
31                                 look_for_keys=True)
32         self.key_file = None
33         self.controllers = list()
34         self.controller_clients = list()
35
36     def setup(self):
37         self.log.info('Setup Apex installer start......')
38
39         self.key_file = self.get_ssh_key_from_installer()
40         self.get_controller_ips()
41         self.set_apply_patches()
42
43     def cleanup(self):
44         self.restore_apply_patches()
45
46     def get_ssh_key_from_installer(self):
47         self.log.info('Get SSH keys from Apex installer......')
48
49         self.client.scp('/home/stack/.ssh/id_rsa', './instack_key', method='get')
50         user = getpass.getuser()
51         uid = pwd.getpwnam(user).pw_uid
52         gid = grp.getgrnam(user).gr_gid
53         os.chown('./instack_key', uid, gid)
54         os.chmod('./instack_key', stat.S_IREAD)
55         current_dir = sys.path[0]
56         return '{0}/{1}'.format(current_dir, 'instack_key')
57
58     def get_controller_ips(self):
59         self.log.info('Get controller ips from Apex installer......')
60
61         command = "source stackrc; " \
62                   "nova list | grep ' overcloud-controller-[0-9] ' " \
63                   "| sed -e 's/^.*ctlplane=//' |awk '{print $1}'"
64         ret, controllers = self.client.ssh(command)
65         if ret:
66             raise Exception('Exec command to get controller ips in Apex installer failed'
67                             'ret=%s, output=%s' % (ret, controllers))
68         self.controllers = controllers
69
70     def set_apply_patches(self):
71         self.log.info('Set apply patches start......')
72
73         for node_ip in self.controllers:
74             client = SSHClient(node_ip, self.node_user_name, key_filename=self.key_file)
75             self.controller_clients.append(client)
76             self._ceilometer_apply_patches(client, self.cm_set_script)
77             cmd = 'sudo systemctl restart openstack-congress-server.service'
78             set_doctor_driver_conf(client, cmd)
79
80     def restore_apply_patches(self):
81         self.log.info('restore apply patches start......')
82
83         for client in self.controller_clients:
84             self._ceilometer_apply_patches(client, self.cm_restore_script)
85             cmd = 'sudo systemctl restart openstack-congress-server.service'
86             restore_doctor_driver_conf(client, cmd)
87
88     def _ceilometer_apply_patches(self, ssh_client, script_name):
89         installer_dir = os.path.dirname(os.path.realpath(__file__))
90         script_abs_path = '{0}/{1}/{2}'.format(installer_dir, 'common', script_name)
91
92         ssh_client.scp(script_abs_path, script_name)
93         cmd = 'sudo python %s' % script_name
94         ret, output = ssh_client.ssh(cmd)
95         if ret:
96             raise Exception('Do the ceilometer command in controller node failed....'
97                             'ret=%s, cmd=%s, output=%s' % (ret, cmd, output))
98         ssh_client.ssh('sudo systemctl restart openstack-ceilometer-notification.service')
99