1 ##############################################################################
2 # Copyright (c) 2018 Nokia Corporation and others.
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 ##############################################################################
13 def set_cpu_allocation_ratio():
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")
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"
29 if found_list and len(found_list):
32 for car in found_list:
33 if car.startswith('#'):
35 if car.startswith('cpu_allocation_ratio'):
37 if "1.0" not in car.split('=')[1]:
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")
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"
53 set_cpu_allocation_ratio()