add ssh_keyfile for connect to installer in Apex
[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 from doctor_tests.common.utils import SSHClient
10 from doctor_tests.installer.base import BaseInstaller
11
12
13 class ApexInstaller(BaseInstaller):
14     node_user_name = 'heat-admin'
15     cm_set_script = 'set_ceilometer.py'
16     cm_restore_script = 'restore_ceilometer.py'
17
18     def __init__(self, conf, log):
19         super(ApexInstaller, self).__init__(conf, log)
20         self.client = SSHClient(self.conf.installer.ip,
21                                 self.conf.installer.username,
22                                 key_filename=self.conf.installer.key_file,
23                                 look_for_keys=True)
24         self.key_file = None
25         self.controllers = list()
26         self.controller_clients = list()
27
28     def setup(self):
29         self.log.info('Setup Apex installer start......')
30
31         self.key_file = self.get_ssh_key_from_installer()
32         self.controllers = self.get_controller_ips()
33         self.create_flavor()
34         self.set_apply_patches()
35         self.setup_stunnel()
36
37     def cleanup(self):
38         self.restore_apply_patches()
39         for server in self.servers:
40             server.terminate()
41
42     def get_ssh_key_from_installer(self):
43         key_path = '/home/stack/.ssh/id_rsa'
44         return self._get_ssh_key(self.client, key_path)
45
46     def get_controller_ips(self):
47         self.log.info('Get controller ips from Apex installer......')
48
49         command = "source stackrc; " \
50                   "nova list | grep ' overcloud-controller-[0-9] ' " \
51                   "| sed -e 's/^.*ctlplane=//' |awk '{print $1}'"
52         controllers = self._run_cmd_remote(self.client, command)
53         self.log.info('Get controller_ips:%s from Apex installer'
54                       % controllers)
55         return controllers
56
57     def get_host_ip_from_hostname(self, hostname):
58         self.log.info('Get host ip by hostname=%s from Apex installer......'
59                       % hostname)
60
61         hostname_in_undercloud = hostname.split('.')[0]
62         command = "source stackrc; nova show %s | awk '/ ctlplane network /{print $5}'" % (hostname_in_undercloud)   # noqa
63         host_ips = self._run_cmd_remote(self.client, command)
64         return host_ips[0]
65
66     def set_apply_patches(self):
67         self.log.info('Set apply patches start......')
68
69         restart_cm_cmd = 'sudo systemctl restart ' \
70                          'openstack-ceilometer-notification.service'
71
72         for node_ip in self.controllers:
73             client = SSHClient(node_ip, self.node_user_name,
74                                key_filename=self.key_file)
75             self.controller_clients.append(client)
76             self._run_apply_patches(client,
77                                     restart_cm_cmd,
78                                     self.cm_set_script)
79
80     def restore_apply_patches(self):
81         self.log.info('restore apply patches start......')
82
83         restart_cm_cmd = 'sudo systemctl restart ' \
84                          'openstack-ceilometer-notification.service'
85
86         for client in self.controller_clients:
87             self._run_apply_patches(client,
88                                     restart_cm_cmd,
89                                     self.cm_restore_script)