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