1 ##############################################################################
2 # Copyright (c) 2017 ZTE Corporation and others.
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 ##############################################################################
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
21 class LocalInstaller(BaseInstaller):
22 node_user_name = 'root'
24 nova_policy_file = '/etc/nova/policy.json'
25 nova_policy_file_backup = '%s%s' % (nova_policy_file, '.bak')
27 def __init__(self, conf, log):
28 super(LocalInstaller, self).__init__(conf, log)
29 self.policy_modified = False
30 self.add_policy_file = False
33 self.get_ssh_key_from_installer()
34 self.set_apply_patches()
37 self.restore_apply_patches()
39 def get_ssh_key_from_installer(self):
40 self.log.info('Assuming SSH keys already exchanged with computer'
41 'for local installer type')
44 def get_host_ip_from_hostname(self, hostname):
45 self.log.info('Get host ip from host name in local installer......')
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")
52 self.log.info('Get host_ip:%s from host_name:%s in local installer'
53 % (host_ip, hostname))
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')
62 def restore_apply_patches(self):
63 self._restore_nova_policy()
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'
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
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
83 # update the host_status_policy
84 data[host_status_policy] = host_status_rule
85 self.policy_modified = True
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:
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)
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')
101 self.add_policy_file = True
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')
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)
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