80e559ed105a8e1b2ee02b9713bd76c3a196b9d1
[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
18     def __init__(self, conf, log):
19         super(McpInstaller, self).__init__(conf, log)
20         self.key_file = self.get_ssh_key_from_installer()
21         self.client = SSHClient(self.conf.installer.ip,
22                                 self.node_user_name,
23                                 key_filename=self.key_file,
24                                 look_for_keys=True)
25         self.controllers = list()
26         self.controller_clients = list()
27
28     def setup(self):
29         self.log.info('Setup MCP installer start......')
30
31         self.controllers = self.get_controller_ips()
32         self.create_flavor()
33         self.set_apply_patches()
34         self.setup_stunnel()
35
36     def cleanup(self):
37         self.restore_apply_patches()
38         for server in self.servers:
39             server.terminate()
40
41     def get_ssh_key_from_installer(self):
42         self.log.info('Get SSH keys from MCP......')
43
44         # Assuming mcp.rsa is already mapped to functest container
45         # if not, only the test runs on jumphost can get the ssh_key
46         # default in path /var/lib/opnfv/mcp.rsa
47         ssh_key = '/root/.ssh/id_rsa'
48         mcp_key = '/var/lib/opnfv/mcp.rsa'
49         return ssh_key if isfile(ssh_key) else mcp_key
50
51     def get_controller_ips(self):
52         self.log.info('Get controller ips from Mcp installer......')
53
54         command = "sudo salt --out yaml 'ctl*' " \
55                   "pillar.get _param:openstack_control_address |" \
56                   "awk '{print $2}'"
57         controllers = self._run_cmd_remote(self.client, command)
58         self.log.info('Get controller_ips:%s from Mcp installer'
59                       % controllers)
60         return controllers
61
62     def get_host_ip_from_hostname(self, hostname):
63         command = "sudo salt --out yaml '%s*' " \
64                   "pillar.get _param:single_address |" \
65                   "awk '{print $2}'" % hostname
66         host_ips = self._run_cmd_remote(self.client, command)
67         return host_ips[0]
68
69     def set_apply_patches(self):
70         self.log.info('Set apply patches start......')
71
72     def restore_apply_patches(self):
73         self.log.info('restore apply patches start......')