Merge "Doctor Iruya release notes"
[doctor.git] / doctor_tests / installer / local.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 os
10 import shutil
11 import subprocess
12
13 from doctor_tests.installer.base import BaseInstaller
14 from doctor_tests.installer.common.vitrage import \
15     set_vitrage_host_down_template
16 from doctor_tests.common.constants import Inspector
17 from doctor_tests.common.utils import load_json_file
18 from doctor_tests.common.utils import write_json_file
19
20
21 class LocalInstaller(BaseInstaller):
22     node_user_name = 'root'
23
24     nova_policy_file = '/etc/nova/policy.json'
25     nova_policy_file_backup = '%s%s' % (nova_policy_file, '.bak')
26
27     def __init__(self, conf, log):
28         super(LocalInstaller, self).__init__(conf, log)
29         self.policy_modified = False
30         self.add_policy_file = False
31
32     def setup(self):
33         self.get_ssh_key_from_installer()
34         self.set_apply_patches()
35
36     def cleanup(self):
37         self.restore_apply_patches()
38
39     def get_ssh_key_from_installer(self):
40         self.log.info('Assuming SSH keys already exchanged with computer'
41                       'for local installer type')
42         return None
43
44     def get_host_ip_from_hostname(self, hostname):
45         self.log.info('Get host ip from host name in local installer......')
46
47         cmd = "getent hosts %s | awk '{ print $1 }'" % (hostname)
48         server = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
49         stdout, stderr = server.communicate()
50         host_ip = stdout.strip().decode("utf-8")
51
52         self.log.info('Get host_ip:%s from host_name:%s in local installer'
53                       % (host_ip, hostname))
54         return host_ip
55
56     def set_apply_patches(self):
57         self._set_nova_policy()
58         if self.conf.inspector.type == Inspector.VITRAGE:
59             set_vitrage_host_down_template()
60             os.system('sudo systemctl restart devstack@vitrage-graph.service')
61
62     def restore_apply_patches(self):
63         self._restore_nova_policy()
64
65     def _set_nova_policy(self):
66         host_status_policy = 'os_compute_api:servers:show:host_status'
67         host_status_rule = 'rule:admin_or_owner'
68         policy_data = {
69             'context_is_admin': 'role:admin',
70             'owner': 'user_id:%(user_id)s',
71             'admin_or_owner': 'rule:context_is_admin or rule:owner',
72             host_status_policy: host_status_rule
73         }
74
75         if os.path.isfile(self.nova_policy_file):
76             data = load_json_file(self.nova_policy_file)
77             if host_status_policy in data:
78                 rule_origion = data[host_status_policy]
79                 if host_status_rule == rule_origion:
80                     self.log.info('Do not need to modify nova policy.')
81                     self.policy_modified = False
82                 else:
83                     # update the host_status_policy
84                     data[host_status_policy] = host_status_rule
85                     self.policy_modified = True
86             else:
87                 # add the host_status_policy, if the admin_or_owner is not
88                 # defined, add it also
89                 for policy, rule in policy_data.items():
90                     if policy not in data:
91                         data[policy] = rule
92                 self.policy_modified = True
93             if self.policy_modified:
94                 self.log.info('Nova policy is Modified.')
95                 shutil.copyfile(self.nova_policy_file,
96                                 self.nova_policy_file_backup)
97         else:
98             # file does not exit, create a new one and add the policy
99             self.log.info('Nova policy file not exist. Creating a new one')
100             data = policy_data
101             self.add_policy_file = True
102
103         if self.policy_modified or self.add_policy_file:
104             write_json_file(self.nova_policy_file, data)
105             os.system('sudo systemctl restart devstack@n-api.service')
106
107     def _restore_nova_policy(self):
108         if self.policy_modified:
109             shutil.copyfile(self.nova_policy_file_backup,
110                             self.nova_policy_file)
111             os.remove(self.nova_policy_file_backup)
112         elif self.add_policy_file:
113             os.remove(self.nova_policy_file)
114
115         if self.add_policy_file or self.policy_modified:
116             os.system('sudo systemctl restart devstack@n-api.service')
117             self.add_policy_file = False
118             self.policy_modified = False