Support Apex with services in containers
[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 make_initial_config(service, dest):
14     for mk in ["", "/etc", "/%s" % service]:
15         dest += mk
16         os.mkdir(dest)
17     src = "/etc/%s/%s.conf" % (service, service)
18     dest += "/%s.conf" % service
19     shutil.copyfile(src, dest)
20
21
22 def set_cpu_allocation_ratio():
23     docker_conf_base_dir = "/var/lib/config-data/puppet-generated"
24     if not os.path.isdir(docker_conf_base_dir):
25         nova_base = ""
26     else:
27         nova_base = "%s/nova" % docker_conf_base_dir
28         if not os.path.isdir(nova_base):
29             # nova.conf to be used might not exist
30             make_initial_config("nova", nova_base)
31     nova_file = nova_base + '/etc/nova/nova.conf'
32     nova_file_bak = nova_base + '/etc/nova/nova.bak'
33
34     if not os.path.isfile(nova_file):
35         raise Exception("File doesn't exist: %s." % nova_file)
36     # TODO (tojuvone): Unfortunately ConfigParser did not produce working conf
37     fcheck = open(nova_file)
38     found_list = ([ca for ca in fcheck.readlines() if "cpu_allocation_ratio"
39                   in ca])
40     fcheck.close()
41     if found_list and len(found_list):
42         change = False
43         found = False
44         for car in found_list:
45             if car.startswith('#'):
46                 continue
47             if car.startswith('cpu_allocation_ratio'):
48                 found = True
49                 if "1.0" not in car.split('=')[1]:
50                     change = True
51     if not found or change:
52         # need to add or change
53         shutil.copyfile(nova_file, nova_file_bak)
54         fin = open(nova_file_bak)
55         fout = open(nova_file, "wt")
56         for line in fin:
57             if change and line.startswith("cpu_allocation_ratio"):
58                 line = "cpu_allocation_ratio=1.0"
59             if not found and line.startswith("[DEFAULT]"):
60                 line += "cpu_allocation_ratio=1.0\n"
61             fout.write(line)
62         fin.close()
63         fout.close()
64
65 set_cpu_allocation_ratio()