summaryrefslogtreecommitdiffstats
path: root/doctor_tests/installer/common/set_compute_config.py
blob: 86266085c26e6ebed6e249ce229cd07c5e249ee5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
##############################################################################
# Copyright (c) 2018 Nokia Corporation and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
import os
import shutil


def make_initial_config(service, dest):
    for mk in ["", "/etc", "/%s" % service]:
        dest += mk
        os.mkdir(dest)
    src = "/etc/%s/%s.conf" % (service, service)
    dest += "/%s.conf" % service
    shutil.copyfile(src, dest)


def set_cpu_allocation_ratio():
    docker_conf_base_dir = "/var/lib/config-data/puppet-generated"
    if not os.path.isdir(docker_conf_base_dir):
        nova_base = ""
    else:
        nova_base = "%s/nova" % docker_conf_base_dir
        if not os.path.isdir(nova_base):
            # nova.conf to be used might not exist
            make_initial_config("nova", nova_base)
    nova_file = nova_base + '/etc/nova/nova.conf'
    nova_file_bak = nova_base + '/etc/nova/nova.bak'

    if not os.path.isfile(nova_file):
        raise Exception("File doesn't exist: %s." % nova_file)
    # TODO (tojuvone): Unfortunately ConfigParser did not produce working conf
    fcheck = open(nova_file)
    found_list = ([ca for ca in fcheck.readlines() if "cpu_allocation_ratio"
                  in ca])
    fcheck.close()
    if found_list and len(found_list):
        change = False
        found = False
        for car in found_list:
            if car.startswith('#'):
                continue
            if car.startswith('cpu_allocation_ratio'):
                found = True
                if "1.0" not in car.split('=')[1]:
                    change = True
    if not found or change:
        # need to add or change
        shutil.copyfile(nova_file, nova_file_bak)
        fin = open(nova_file_bak)
        fout = open(nova_file, "wt")
        for line in fin:
            if change and line.startswith("cpu_allocation_ratio"):
                line = "cpu_allocation_ratio=1.0"
            if not found and line.startswith("[DEFAULT]"):
                line += "cpu_allocation_ratio=1.0\n"
            fout.write(line)
        fin.close()
        fout.close()

set_cpu_allocation_ratio()