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 ##############################################################################
12 from installer.base import BaseInstaller
13 from utils import load_json_file
14 from utils import write_json_file
17 class LocalInstaller(BaseInstaller):
18 node_user_name = 'root'
20 nova_policy_file = '/etc/nova/policy.json'
21 nova_policy_file_backup = '%s%s' % (nova_policy_file, '.bak')
23 def __init__(self, conf, log):
24 super(LocalInstaller, self).__init__(conf, log)
25 self.policy_modified = False
26 self.add_policy_file = False
29 self.get_ssh_key_from_installer()
30 self.set_apply_patches()
33 self.restore_apply_patches()
35 def get_ssh_key_from_installer(self):
36 self.log.info('Assuming SSH keys already exchanged with computer for local installer type')
39 def set_apply_patches(self):
40 self._set_nova_policy()
42 def restore_apply_patches(self):
43 self._restore_nova_policy()
45 def _set_nova_policy(self):
46 host_status_policy = 'os_compute_api:servers:show:host_status'
47 host_status_rule = 'rule:admin_or_owner'
49 'context_is_admin': 'role:admin',
50 'owner': 'user_id:%(user_id)s',
51 'admin_or_owner': 'rule:context_is_admin or rule:owner',
52 host_status_policy: host_status_rule
55 if os.path.isfile(self.nova_policy_file):
56 data = load_json_file(self.nova_policy_file)
57 if host_status_policy in data:
58 rule_origion = data[host_status_policy]
59 if host_status_rule == rule_origion:
60 self.log.info('Do not need to modify nova policy.')
61 self.policy_modified = False
63 # update the host_status_policy
64 data[host_status_policy] = host_status_rule
65 self.policy_modified = True
67 # add the host_status_policy, if the admin_or_owner is not
68 # defined, add it also
69 for policy, rule in policy_data.items():
70 if policy not in data:
72 self.policy_modified = True
73 if self.policy_modified:
74 self.log.info('Nova policy is Modified.')
75 shutil.copyfile(self.nova_policy_file,
76 self.nova_policy_file_backup)
78 # file does not exit, create a new one and add the policy
79 self.log.info('Nova policy file not exist. Creating a new one')
81 self.add_policy_file = True
83 if self.policy_modified or self.add_policy_file:
84 write_json_file(self.nova_policy_file, data)
85 os.system('screen -S stack -p n-api -X stuff "^C^M^[[A^M"')
87 def _restore_nova_policy(self):
88 if self.policy_modified:
89 shutil.copyfile(self.nova_policy_file_backup, self.nova_policy_file)
90 os.remove(self.nova_policy_file_backup)
91 elif self.add_policy_file:
92 os.remove(self.nova_policy_file)
94 if self.add_policy_file or self.policy_modified:
95 os.system('screen -S stack -p n-api -X stuff "^C^M^[[A^M"')
96 self.add_policy_file = False
97 self.policy_modified = False