summaryrefslogtreecommitdiffstats
path: root/deploy/opera_adapter.py
blob: bccbfce1f7ecb296f5290c1dcbac9b6296a760ce (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
import yaml
import sys
import subprocess
import traceback
import ipaddress


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


def dump_file(data, file):
    with open(file, 'w') as fd:
        try:
            return yaml.dump(data, fd, default_flow_style=False)
        except:
            traceback.print_exc()
            return None


def sync_openo_network_yml(network, net_config):
    """sync opera/conf/network.yml according to Network file"""
    for i in net_config["openo_net"].keys():
        net_config["openo_net"][i] = network["openo_net"][i]

    sorted_ips = sorted(net_config["openo_docker_net"].items(),
                        key=lambda item: item[1])
    docker_ips = [i[0] for i in sorted_ips]
    docker_start_ip = unicode(network["openo_docker_net"]["docker_ip_start"],
                              "utf-8")
    docker_start_ip = ipaddress.IPv4Address(docker_start_ip)
    for i in docker_ips:
        net_config["openo_docker_net"][i] = str(docker_start_ip)
        docker_start_ip += 1

    for i in net_config["juju_net"].keys():
        net_config["juju_net"][i] = network["juju_net"][i]


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

    _, dha_file, network_file = sys.argv

    if not os.path.exists(dha_file):
        print("DHA file doesn't exit")
        sys.exit(1)
    if not os.path.exists(network_file):
        print("NETWORK file doesn't exit")
        sys.exit(1)

    dha = load_file(dha_file)
    network = load_file(network_file)

    if not dha:
        print('format error in DHA: %s' % dha_file)
        sys.exit(1)
    if not network:
        print('format error in NETWORK: %s' % network_file)
        sys.exit(1)

    if dha["deploy_options"][0]["orchestrator"] != "open-o":
        sys.exit(0)

    compass_dir = os.getenv('COMPASS_DIR')
    work_dir = os.path.join(compass_dir, 'work')
    opera_dir = os.path.join(work_dir, 'opera')
    conf_dir = os.path.join(opera_dir, 'conf')
    net_config_file = os.path.join(conf_dir, 'network.yml')

    p1 = subprocess.Popen(
        "git clone https://gerrit.opnfv.org/gerrit/opera",
        cwd=work_dir, shell=True)
    p1.communicate()

    if not os.path.exists(net_config_file):
        print('file opera/conf/network.yml not found')
        sys.exit(1)

    net_config = load_file(net_config_file)
    sync_openo_network_yml(network, net_config)
    dump_file(net_config, net_config_file)

    p2 = subprocess.Popen("./opera_launch.sh", cwd=opera_dir, shell=True)
    p2.communicate()