DevStack support
[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_bak = None
15     for nova_file in ["/var/lib/config-data/puppet-generated/nova_libvirt/etc/nova/nova.conf",  # noqa
16                       "/var/lib/config-data/puppet-generated/nova/etc/nova/nova.conf",  # noqa
17                       "/etc/nova/nova.conf"]:
18         if os.path.isfile(nova_file):
19             nova_file_bak = nova_file.replace(".conf", ".bak")
20             break
21
22     if nova_file_bak is None:
23         raise Exception("Could not find nova.conf")
24     # TODO (tojuvone): Unfortunately ConfigParser did not produce working conf
25     fcheck = open(nova_file)
26     found_list = ([ca for ca in fcheck.readlines() if "cpu_allocation_ratio"
27                   in ca])
28     fcheck.close()
29     change = False
30     found = False
31     if found_list and len(found_list):
32         for car in found_list:
33             if car.startswith('#'):
34                 continue
35             if car.startswith('cpu_allocation_ratio'):
36                 found = True
37                 if "1.0" not in car.split('=')[1]:
38                     change = True
39     if not found or change:
40         # need to add or change
41         shutil.copyfile(nova_file, nova_file_bak)
42         fin = open(nova_file_bak)
43         fout = open(nova_file, "wt")
44         for line in fin:
45             if change and line.startswith("cpu_allocation_ratio"):
46                 line = "cpu_allocation_ratio=1.0"
47             if not found and line.startswith("[DEFAULT]"):
48                 line += "cpu_allocation_ratio=1.0\n"
49             fout.write(line)
50         fin.close()
51         fout.close()
52
53 set_cpu_allocation_ratio()