Merge "support MCP installer"
[doctor.git] / doctor_tests / installer / mcp.py
1 ##############################################################################
2 # Copyright (c) 2018 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 os.path import isfile
10
11 from doctor_tests.common.utils import SSHClient
12 from doctor_tests.installer.base import BaseInstaller
13
14
15 class McpInstaller(BaseInstaller):
16     node_user_name = 'ubuntu'
17     cm_set_script = 'set_ceilometer.py'
18     cm_restore_script = 'restore_ceilometer.py'
19
20     def __init__(self, conf, log):
21         super(McpInstaller, self).__init__(conf, log)
22         self.key_file = self.get_ssh_key_from_installer()
23         self.client = SSHClient(self.conf.installer.ip,
24                                 self.node_user_name,
25                                 key_filename=self.key_file)
26         self.controllers = list()
27         self.controller_clients = list()
28
29     def setup(self):
30         self.log.info('Setup MCP installer start......')
31
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         self.log.info('Get SSH keys from MCP......')
44
45         # Assuming mcp.rsa is already mapped to functest container
46         # if not, only the test runs on jumphost can get the ssh_key
47         # default in path /var/lib/opnfv/mcp.rsa
48         ssh_key = '/root/.ssh/id_rsa'
49         mcp_key = '/var/lib/opnfv/mcp.rsa'
50         return ssh_key if isfile(ssh_key) else mcp_key
51
52     def get_controller_ips(self):
53         self.log.info('Get controller ips from Mcp installer......')
54
55         command = "sudo salt --out yaml 'ctl*' " \
56                   "pillar.get _param:openstack_control_address |" \
57                   "awk '{print $2}'"
58         controllers = self._run_cmd_remote(self.client, command)
59         self.log.info('Get controller_ips:%s from Mcp installer'
60                       % controllers)
61         return controllers
62
63     def get_host_ip_from_hostname(self, hostname):
64         command = "sudo salt --out yaml '%s*' " \
65                   "pillar.get _param:single_address |" \
66                   "awk '{print $2}'" % hostname
67         host_ips = self._run_cmd_remote(self.client, command)
68         return host_ips[0]
69
70     def set_apply_patches(self):
71         self.log.info('Set apply patches start......')
72
73         restart_cm_cmd = 'sudo service ceilometer-agent-notification restart'
74         for node_ip in self.controllers:
75             client = SSHClient(node_ip, self.node_user_name,
76                                key_filename=self.key_file)
77             self.controller_clients.append(client)
78             self._run_apply_patches(client,
79                                     restart_cm_cmd,
80                                     self.cm_set_script)
81
82     def restore_apply_patches(self):
83         self.log.info('restore apply patches start......')
84
85         restart_cm_cmd = 'sudo service ceilometer-agent-notification restart'
86         for client in self.controller_clients:
87             self._run_apply_patches(client,
88                                     restart_cm_cmd,
89                                     self.cm_restore_script)