07db1e166a1d155a7c51a952dfd6abc379a82ad4
[doctor.git] / doctor_tests / installer / common / set_compute_config.py
1 ##############################################################################
2 # Copyright (c) 2018 Nokia 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
13 def set_cpu_allocation_ratio():
14     nova_file = '/etc/nova/nova.conf'
15     nova_file_bak = '/etc/nova/nova.bak'
16
17     if not os.path.isfile(nova_file):
18         raise Exception("File doesn't exist: %s." % nova_file)
19     # TODO (tojuvone): Unfortunately ConfigParser did not produce working conf
20     fcheck = open(nova_file)
21     found_list = ([ca for ca in fcheck.readlines() if "cpu_allocation_ratio"
22                   in ca])
23     fcheck.close()
24     if found_list and len(found_list):
25         change = False
26         found = False
27         for car in found_list:
28             if car.startswith('#'):
29                 continue
30             if car.startswith('cpu_allocation_ratio'):
31                 found = True
32                 if "1.0" not in car.split('=')[1]:
33                     change = True
34     if not found or change:
35         # need to add or change
36         shutil.copyfile(nova_file, nova_file_bak)
37         fin = open(nova_file_bak)
38         fout = open(nova_file, "wt")
39         for line in fin:
40             if change and line.startswith("cpu_allocation_ratio"):
41                 line = "cpu_allocation_ratio=1.0"
42             if not found and line.startswith("[DEFAULT]"):
43                 line += "cpu_allocation_ratio=1.0\n"
44             fout.write(line)
45         fin.close()
46         fout.close()
47
48 set_cpu_allocation_ratio()