e156ac1a71108fde13e219e212ffade49d91e2c2
[doctor.git] / 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
12 from installer.base import BaseInstaller
13 from utils import load_json_file
14 from utils import write_json_file
15
16
17 class LocalInstaller(BaseInstaller):
18     computer_user_name = 'root'
19
20     nova_policy_file = '/etc/nova/policy.json'
21     nova_policy_file_backup = '%s%s' % (nova_policy_file, '.bak')
22
23     def __init__(self, conf, log):
24         super(LocalInstaller, self).__init__(conf, log)
25         self.policy_modified = False
26         self.add_policy_file = False
27
28     def setup(self):
29         self.get_ssh_key_from_installer()
30         self.set_apply_patches()
31
32     def cleanup(self):
33         self.restore_apply_patches()
34
35     def get_ssh_key_from_installer(self):
36         self.log.info('Assuming SSH keys already exchanged with computer for local installer type')
37         return
38
39     def set_apply_patches(self):
40         self._set_nova_policy()
41
42     def restore_apply_patches(self):
43         self._restore_nova_policy()
44
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'
48         policy_data = {
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
53         }
54
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
62                 else:
63                     # update the host_status_policy
64                     data[host_status_policy] = host_status_rule
65                     self.policy_modified = True
66             else:
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:
71                         data[policy] = rule
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)
77         else:
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')
80             data = policy_data
81             self.add_policy_file = True
82
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"')
86
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)
93
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