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 installer.base import BaseInstaller
14 from common.utils import load_json_file
15 from common.utils import write_json_file
18 class LocalInstaller(BaseInstaller):
19 node_user_name = 'root'
21 nova_policy_file = '/etc/nova/policy.json'
22 nova_policy_file_backup = '%s%s' % (nova_policy_file, '.bak')
24 def __init__(self, conf, log):
25 super(LocalInstaller, self).__init__(conf, log)
26 self.policy_modified = False
27 self.add_policy_file = False
30 self.get_ssh_key_from_installer()
31 self.set_apply_patches()
34 self.restore_apply_patches()
36 def get_ssh_key_from_installer(self):
37 self.log.info('Assuming SSH keys already exchanged with computer for local installer type')
40 def get_host_ip_from_hostname(self, hostname):
41 self.log.info('Get host ip from host name in local installer......')
43 cmd = "getent hosts %s | awk '{ print $1 }'" % (hostname)
44 server = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
45 stdout, stderr = server.communicate()
46 host_ip = stdout.strip()
48 self.log.info('Get host_ip:%s from host_name:%s in local installer' % (host_ip, hostname))
51 def set_apply_patches(self):
52 self._set_nova_policy()
54 def restore_apply_patches(self):
55 self._restore_nova_policy()
57 def _set_nova_policy(self):
58 host_status_policy = 'os_compute_api:servers:show:host_status'
59 host_status_rule = 'rule:admin_or_owner'
61 'context_is_admin': 'role:admin',
62 'owner': 'user_id:%(user_id)s',
63 'admin_or_owner': 'rule:context_is_admin or rule:owner',
64 host_status_policy: host_status_rule
67 if os.path.isfile(self.nova_policy_file):
68 data = load_json_file(self.nova_policy_file)
69 if host_status_policy in data:
70 rule_origion = data[host_status_policy]
71 if host_status_rule == rule_origion:
72 self.log.info('Do not need to modify nova policy.')
73 self.policy_modified = False
75 # update the host_status_policy
76 data[host_status_policy] = host_status_rule
77 self.policy_modified = True
79 # add the host_status_policy, if the admin_or_owner is not
80 # defined, add it also
81 for policy, rule in policy_data.items():
82 if policy not in data:
84 self.policy_modified = True
85 if self.policy_modified:
86 self.log.info('Nova policy is Modified.')
87 shutil.copyfile(self.nova_policy_file,
88 self.nova_policy_file_backup)
90 # file does not exit, create a new one and add the policy
91 self.log.info('Nova policy file not exist. Creating a new one')
93 self.add_policy_file = True
95 if self.policy_modified or self.add_policy_file:
96 write_json_file(self.nova_policy_file, data)
97 os.system('screen -S stack -p n-api -X stuff "^C^M^[[A^M"')
99 def _restore_nova_policy(self):
100 if self.policy_modified:
101 shutil.copyfile(self.nova_policy_file_backup, self.nova_policy_file)
102 os.remove(self.nova_policy_file_backup)
103 elif self.add_policy_file:
104 os.remove(self.nova_policy_file)
106 if self.add_policy_file or self.policy_modified:
107 os.system('screen -S stack -p n-api -X stuff "^C^M^[[A^M"')
108 self.add_policy_file = False
109 self.policy_modified = False