Add parallel execution and shortcut notification to inspector design guideline
[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 import subprocess
12
13 from installer.base import BaseInstaller
14 from common.utils import load_json_file
15 from common.utils import write_json_file
16
17
18 class LocalInstaller(BaseInstaller):
19     node_user_name = 'root'
20
21     nova_policy_file = '/etc/nova/policy.json'
22     nova_policy_file_backup = '%s%s' % (nova_policy_file, '.bak')
23
24     def __init__(self, conf, log):
25         super(LocalInstaller, self).__init__(conf, log)
26         self.policy_modified = False
27         self.add_policy_file = False
28
29     def setup(self):
30         self.get_ssh_key_from_installer()
31         self.set_apply_patches()
32
33     def cleanup(self):
34         self.restore_apply_patches()
35
36     def get_ssh_key_from_installer(self):
37         self.log.info('Assuming SSH keys already exchanged with computer for local installer type')
38         return None
39
40     def get_host_ip_from_hostname(self, hostname):
41         self.log.info('Get host ip from host name in local installer......')
42
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()
47
48         self.log.info('Get host_ip:%s from host_name:%s in local installer' % (host_ip, hostname))
49         return host_ip
50
51     def set_apply_patches(self):
52         self._set_nova_policy()
53
54     def restore_apply_patches(self):
55         self._restore_nova_policy()
56
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'
60         policy_data = {
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
65         }
66
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
74                 else:
75                     # update the host_status_policy
76                     data[host_status_policy] = host_status_rule
77                     self.policy_modified = True
78             else:
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:
83                         data[policy] = rule
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)
89         else:
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')
92             data = policy_data
93             self.add_policy_file = True
94
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"')
98
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)
105
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