New scripts for configs generation, based on jinja2
[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 import os
14 import yaml
15
16 #
17 # Parse parameters
18 #
19
20 parser = OptionParser()
21 parser.add_option("-l", "--lab", dest="lab", help="lab config file")
22 (options, args) = parser.parse_args()
23 labconfig_file = options.lab
24
25 #
26 # Set Path and configs path
27 #
28
29 # Capture our current directory
30 TPL_DIR = os.path.dirname(os.path.abspath(__file__))+'/config_tpl'
31 HOME = os.environ['HOME']
32 USER = os.environ['USER']
33
34 #
35 # Prepare variables
36 #
37
38 # Prepare a storage for passwords
39 passwords_store = dict()
40
41 #
42 # Local Functions
43 #
44
45
46 def load_yaml(filepath):
47     """Load YAML file"""
48     with open(filepath, 'r') as stream:
49         try:
50             return yaml.load(stream)
51         except yaml.YAMLError as exc:
52             print(exc)
53
54 #
55 # Config import
56 #
57
58 # Load scenario Config
59 config = load_yaml(labconfig_file)
60
61 # Set a dict copy of opnfv/spaces
62 config['opnfv']['spaces_dict'] = dict()
63 for space in config['opnfv']['spaces']:
64     config['opnfv']['spaces_dict'][space['type']] = space
65
66 # Set a dict copy of opnfv/storage
67 config['opnfv']['storage_dict'] = dict()
68 for storage in config['opnfv']['storage']:
69     config['opnfv']['storage_dict'][storage['type']] = storage
70
71 # Add some OS environment variables
72 config['os'] = {'home': HOME,
73                 'user': USER,
74                 }
75
76 # Prepare interface-enable, more easy to do it here
77 ifnamelist = set()
78 for node in config['lab']['racks'][0]['nodes']:
79     for nic in node['nics']:
80         if 'admin' not in nic['spaces']:
81             ifnamelist.add(nic['ifname'])
82 config['lab']['racks'][0]['ifnamelist'] = ', '.join(ifnamelist)
83
84 #
85 # Transform template to deployconfig.yaml according to config
86 #
87
88 # Create the jinja2 environment.
89 env = Environment(loader=FileSystemLoader(TPL_DIR),
90                   trim_blocks=True)
91 template = env.get_template('deployconfig.yaml')
92
93 # Render the template
94 output = template.render(**config)
95
96 # Check output syntax
97 try:
98     yaml.load(output)
99 except yaml.YAMLError as exc:
100     print(exc)
101
102 # print output
103 print(output)