Merge "Auto Generated INFO.yaml file"
[joid.git] / ci / genDeploymentConfig.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 """
5 This script generates a deployment config based on lab config file.
6
7 Parameters:
8  -l, --lab      : lab config file
9 """
10
11 from optparse import OptionParser
12 from jinja2 import Environment, FileSystemLoader
13 from distutils.version import LooseVersion, StrictVersion
14 import os
15 import yaml
16 import subprocess
17 import socket
18 import fcntl
19 import struct
20
21 #
22 # Parse parameters
23 #
24
25 parser = OptionParser()
26 parser.add_option("-l", "--lab", dest="lab", help="lab config file")
27 (options, args) = parser.parse_args()
28 labconfig_file = options.lab
29
30 #
31 # Set Path and configs path
32 #
33 TPL_DIR = os.path.dirname(os.path.abspath(__file__))+'/config_tpl/juju2'
34
35 HOME = os.environ['HOME']
36 USER = os.environ['USER']
37
38 #
39 # Prepare variables
40 #
41
42 # Prepare a storage for passwords
43 passwords_store = dict()
44
45 #
46 # Local Functions
47 #
48
49
50 def load_yaml(filepath):
51     """Load YAML file"""
52     with open(filepath, 'r') as stream:
53         try:
54             return yaml.load(stream)
55         except yaml.YAMLError as exc:
56             print(exc)
57
58
59 def get_ip_address(ifname):
60     """Get local IP"""
61     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
62     return socket.inet_ntoa(fcntl.ioctl(
63         s.fileno(),
64         0x8915,  # SIOCGIFADDR
65         struct.pack('256s', bytes(ifname.encode('utf-8')[:15]))
66     )[20:24])
67
68
69 #
70 # Config import
71 #
72
73 #
74 # Config import
75 #
76
77 # Load scenario Config
78 config = load_yaml(labconfig_file)
79
80 # Set a dict copy of opnfv/spaces
81 config['opnfv']['spaces_dict'] = dict()
82 for space in config['opnfv']['spaces']:
83     config['opnfv']['spaces_dict'][space['type']] = space
84
85 # Set a dict copy of opnfv/storage
86 config['opnfv']['storage_dict'] = dict()
87 for storage in config['opnfv']['storage']:
88     config['opnfv']['storage_dict'][storage['type']] = storage
89
90 # Add some OS environment variables
91 config['os'] = {'home': HOME,
92                 'user': USER,
93                 'brAdmIP': get_ip_address(config['opnfv']['spaces_dict']
94                                                 ['admin']['bridge'])}
95
96 # Prepare interface-enable, more easy to do it here
97 ifnamelist = set()
98 for node in config['lab']['racks'][0]['nodes']:
99     for nic in node['nics']:
100         if 'admin' not in nic['spaces']:
101             ifnamelist.add(nic['ifname'])
102 config['lab']['racks'][0]['ifnamelist'] = ','.join(ifnamelist)
103
104 #
105 # Transform template to deployconfig.yaml according to config
106 #
107
108 # Create the jinja2 environment.
109 env = Environment(loader=FileSystemLoader(TPL_DIR),
110                   trim_blocks=True)
111 template = env.get_template('deployconfig.yaml')
112
113 # Render the template
114 output = template.render(**config)
115
116 # Check output syntax
117 try:
118     yaml.load(output)
119 except yaml.YAMLError as exc:
120     print(exc)
121
122 # print output
123 print(output)