42465247d4d69342b0bce0059142a967bb91b957
[doctor.git] / doctor_tests / installer / common / set_config.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 yaml
12
13 ep_file = '/etc/ceilometer/event_pipeline.yaml'
14 ep_file_bak = '/etc/ceilometer/event_pipeline.yaml.bak'
15 event_notifier_topic = 'notifier://?topic=alarm.all'
16
17
18 def set_notifier_topic():
19     config_modified = False
20
21     if not os.path.isfile(ep_file):
22         raise Exception("File doesn't exist: %s." % ep_file)
23
24     with open(ep_file, 'r') as file:
25         config = yaml.safe_load(file)
26
27     sinks = config['sinks']
28     for sink in sinks:
29         if sink['name'] == 'event_sink':
30             publishers = sink['publishers']
31             if event_notifier_topic not in publishers:
32                 print('Add event notifier in ceilometer')
33                 publishers.append(event_notifier_topic)
34                 config_modified = True
35             else:
36                 print('NOTE: event notifier is configured'
37                       'in ceilometer as we needed')
38
39     if config_modified:
40         shutil.copyfile(ep_file, ep_file_bak)
41         with open(ep_file, 'w+') as file:
42             file.write(yaml.safe_dump(config))
43
44
45 def set_maintenance_event_definitions():
46     ed_file = '/etc/ceilometer/event_definitions.yaml'
47     ed_file_bak = '/etc/ceilometer/event_definitions.bak'
48
49     if not os.path.isfile(ed_file):
50         raise Exception("File doesn't exist: %s." % ed_file)
51
52     with open(ed_file, 'r') as file:
53         config = yaml.safe_load(file)
54
55     et_list = [et['event_type'] for et in config]
56
57     if 'maintenance.scheduled' in et_list:
58         add_mscheduled = False
59         print('NOTE: maintenance.scheduled allready configured')
60     else:
61         print('NOTE: add maintenance.scheduled to event_definitions.yaml')
62         add_mscheduled = True
63         mscheduled = {
64             'event_type': 'maintenance.scheduled',
65             'traits': {
66                 'allowed_actions': {'fields': 'payload.allowed_actions'},
67                 'instance_ids': {'fields': 'payload.instance_ids'},
68                 'reply_url': {'fields': 'payload.reply_url'},
69                 'actions_at': {'fields': 'payload.actions_at',
70                                'type': 'datetime'},
71                 'state': {'fields': 'payload.state'},
72                 'session_id': {'fields': 'payload.session_id'},
73                 'project_id': {'fields': 'payload.project_id'},
74                 'metadata': {'fields': 'payload.metadata'}
75             }
76         }
77         config.append(mscheduled)
78
79     if 'maintenance.host' in et_list:
80         add_mhost = False
81         print('NOTE: maintenance.host allready configured')
82     else:
83         print('NOTE: add maintenance.host to event_definitions.yaml')
84         add_mhost = True
85         mhost = {
86             'event_type': 'maintenance.host',
87             'traits': {
88                 'host': {'fields': 'payload.host'},
89                 'project_id': {'fields': 'payload.project_id'},
90                 'state': {'fields': 'payload.state'},
91                 'session_id': {'fields': 'payload.session_id'}
92             }
93         }
94         config.append(mhost)
95
96     if add_mscheduled or add_mhost:
97         shutil.copyfile(ed_file, ed_file_bak)
98         with open(ed_file, 'w+') as file:
99             file.write(yaml.safe_dump(config))
100
101
102 def set_cpu_allocation_ratio():
103     nova_file = '/etc/nova/nova.conf'
104     nova_file_bak = '/etc/nova/nova.bak'
105
106     if not os.path.isfile(nova_file):
107         raise Exception("File doesn't exist: %s." % nova_file)
108     # TODO (tojuvone): Unfortunately ConfigParser did not produce working conf
109     fcheck = open(nova_file)
110     found_list = ([ca for ca in fcheck.readlines() if "cpu_allocation_ratio"
111                   in ca])
112     fcheck.close()
113     if found_list and len(found_list):
114         change = False
115         found = False
116         for car in found_list:
117             if car.startswith('#'):
118                 continue
119             if car.startswith('cpu_allocation_ratio'):
120                 found = True
121                 if "1.0" not in car.split('=')[1]:
122                     change = True
123     if not found or change:
124         # need to add or change
125         shutil.copyfile(nova_file, nova_file_bak)
126         fin = open(nova_file_bak)
127         fout = open(nova_file, "wt")
128         for line in fin:
129             if change and line.startswith("cpu_allocation_ratio"):
130                 line = "cpu_allocation_ratio=1.0"
131             if not found and line.startswith("[DEFAULT]"):
132                 line += "cpu_allocation_ratio=1.0\n"
133             fout.write(line)
134         fin.close()
135         fout.close()
136
137 set_notifier_topic()
138 set_maintenance_event_definitions()
139 set_cpu_allocation_ratio()