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