aboutsummaryrefslogtreecommitdiffstats
path: root/process_conf.py
blob: a28f8090f9ad106dffdcbbcebc6809f3dc4d44f6 (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
66
67
68
69
70
71
72
73
74
import os
import yaml
import sys
import subprocess
import traceback


def load_file(file):
    with open(file) as fd:
        try:
            return yaml.load(fd)
        except:
            traceback.print_exc()
            return None


def generate_vm_conf(vm_config, scripts_dir):
    """generate opera/work/scripts_dir/openo-vm.conf"""
    print vm_config["openo"]["cpu"]
    with open(scripts_dir + "/openo-vm.conf", "w") as fd:
        fd.write("OPENO_TAG=" + str(vm_config["openo"]["tag"]) + "\n")
        fd.write("OPENO_VIRT_CPUS=" + str(vm_config["openo"]["cpu"]) + "\n")
        fd.write("OPENO_VIRT_MEM=" + str(vm_config["openo"]["memory"]) + "\n")
        fd.write("OPENO_VIRT_DISK=" + str(vm_config["openo"]["disk"]) + "\n")
        fd.write("OPENO_VM_NET=" + vm_config["openo"]["vnet"] + "\n")


def generate_net_conf(net_config, scripts_dir):
    """generate opera/work/scripts_dir/network.conf"""
    with open(scripts_dir + "/network.conf", "w") as fd:
        for i in net_config["openo_net"].keys():
            fd.write(i.upper() + "=" + net_config["openo_net"][i])
            fd.write("\n")

        for i in net_config["openo_docker_net"].keys():
            fd.write(i.upper() + "=" + net_config["openo_docker_net"][i])
            fd.write("\n")

        for i in net_config["juju_net"].keys():
            fd.write(i.upper() + "=" + net_config["juju_net"][i])
            fd.write("\n")


if __name__ == "__main__":
    if len(sys.argv) != 3:
        print("parameter wrong%d %s" % (len(sys.argv), sys.argv))
        sys.exit(1)

    _, vm_file, net_file = sys.argv

    if not os.path.exists(vm_file):
        print("openo-vm.yml doesn't exit")
        sys.exit(1)
    if not os.path.exists(net_file):
        print("network.yml doesn't exit")
        sys.exit(1)

    vm_config = load_file(vm_file)
    net_config = load_file(net_file)
    if not vm_config:
        print('format error in %s' % vm_file)
        sys.exit(1)
    if not net_config:
        print('format error in %s' % net_file)
        sys.exit(1)

    opera_dir = os.getenv('OPERA_DIR')
    scripts_dir = os.path.join(opera_dir, 'work/scripts')
    if not os.path.exists(scripts_dir):
        print("dir opera/work/scripts doesn't exit")
        sys.exit(1)

    generate_vm_conf(vm_config, scripts_dir)
    generate_net_conf(net_config, scripts_dir)