summaryrefslogtreecommitdiffstats
path: root/ci/genDeploymentConfig.py
blob: 0d3a1c9574887e83212c33894579dbe597133910 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
This script generates a deployment config based on lab config file.

Parameters:
 -l, --lab      : lab config file
"""

from optparse import OptionParser
from jinja2 import Environment, FileSystemLoader
from distutils.version import LooseVersion, StrictVersion
import os
import yaml
import subprocess
import socket
import fcntl
import struct

#
# Parse parameters
#

parser = OptionParser()
parser.add_option("-l", "--lab", dest="lab", help="lab config file")
(options, args) = parser.parse_args()
labconfig_file = options.lab

#
# Set Path and configs path
#
TPL_DIR = os.path.dirname(os.path.abspath(__file__))+'/config_tpl/juju2'

HOME = os.environ['HOME']
USER = os.environ['USER']

#
# Prepare variables
#

# Prepare a storage for passwords
passwords_store = dict()

#
# Local Functions
#


def load_yaml(filepath):
    """Load YAML file"""
    with open(filepath, 'r') as stream:
        try:
            return yaml.load(stream)
        except yaml.YAMLError as exc:
            print(exc)


def get_ip_address(ifname):
    """Get local IP"""
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', bytes(ifname.encode('utf-8')[:15]))
    )[20:24])


#
# Config import
#

#
# Config import
#

# Load scenario Config
config = load_yaml(labconfig_file)

# Set a dict copy of opnfv/spaces
config['opnfv']['spaces_dict'] = dict()
for space in config['opnfv']['spaces']:
    config['opnfv']['spaces_dict'][space['type']] = space

# Set a dict copy of opnfv/storage
config['opnfv']['storage_dict'] = dict()
for storage in config['opnfv']['storage']:
    config['opnfv']['storage_dict'][storage['type']] = storage

# Add some OS environment variables
config['os'] = {'home': HOME,
                'user': USER,
                'brAdmIP': get_ip_address(config['opnfv']['spaces_dict']
                                                ['admin']['bridge'])}

# Prepare interface-enable, more easy to do it here
ifnamelist = set()
for node in config['lab']['racks'][0]['nodes']:
    for nic in node['nics']:
        if 'admin' not in nic['spaces']:
            ifnamelist.add(nic['ifname'])
config['lab']['racks'][0]['ifnamelist'] = ','.join(ifnamelist)

#
# Transform template to deployconfig.yaml according to config
#

# Create the jinja2 environment.
env = Environment(loader=FileSystemLoader(TPL_DIR),
                  trim_blocks=True)
template = env.get_template('deployconfig.yaml')

# Render the template
output = template.render(**config)

# Check output syntax
try:
    yaml.load(output)
except yaml.YAMLError as exc:
    print(exc)

# print output
print(output)