modified to make use of the bundle generated.
[joid.git] / ci / genBundle.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 from optparse import OptionParser
5 from jinja2 import Environment, FileSystemLoader
6 from pprint import pprint as pp
7 import os
8 import random
9 import yaml
10 import sys, traceback
11
12 ##
13 ## Parse parameters
14 ##
15
16 parser = OptionParser()
17 parser.add_option("-s", "--scenario", dest="scenario", help ="scenario name")
18 parser.add_option("-l", "--lab", dest="lab", help ="lab config file")
19 (options, args) = parser.parse_args()
20 scenario = options.scenario
21 labconfig_file = options.lab
22
23 ##
24 ## Set Path and configs path
25 ##
26
27 scenarioconfig_file = 'default_deployment_config.yaml'
28 # Capture our current directory
29 TPL_DIR = os.path.dirname(os.path.abspath(__file__))+'/bundle_tpl'
30
31 ##
32 ## Prepare variables
33 ##
34
35 # Prepare a storage for passwords
36 passwords_store = dict()
37
38 ##
39 ## Local Functions
40 ##
41
42 def load_yaml(filepath):
43     with open(filepath, 'r') as stream:
44         try:
45             return yaml.load(stream)
46         except yaml.YAMLError as exc:
47             print(exc)
48
49 ##
50 ## Templates functions
51 ##
52
53 def unit_qty():
54     global config
55     if config['os']['ha']['mode'] == 'ha':
56         return config['os']['ha']['cluster_size']
57     else:
58         return 1
59
60 def to_select( qty = False ):
61     global config
62     if not qty:
63         qty = config['os']['ha']['cluster_size'] if config['os']['ha']['mode'] == 'ha' else 1
64     return random.sample(range(0,config['opnfv']['units']), qty )
65
66 def get_password(key, length=16, special=False):
67     global passwords_store
68     if key not in passwords_store.keys():
69         alphabet = "abcdefghijklmnopqrstuvwxyz"
70         upperalphabet = alphabet.upper()
71         char_list = alphabet + upperalphabet
72         pwlist = []
73         if special:
74             char_list += "+-,;./:?!*"
75         for i in range(length):
76             pwlist.append(char_list[random.randrange(len(char_list))])
77         random.shuffle(pwlist)
78         passwords_store[key] = "".join(pwlist)
79     return passwords_store[key]
80
81 ##
82 ## Config import
83 ##
84
85 #Load scenario Config
86 config = load_yaml(scenarioconfig_file)
87 #Load lab Config
88 config.update(load_yaml(labconfig_file))
89
90 #We transform array to hash for an easier work
91 config['opnfv']['spaces_dict'] = dict()
92 for space in config['opnfv']['spaces']:
93     config['opnfv']['spaces_dict'][space['type']] = space
94 config['opnfv']['storage_dict'] = dict()
95 for storage in config['opnfv']['storage']:
96     config['opnfv']['storage_dict'][storage['type']] = storage
97
98 ##
99 ## Parse scenario name
100 ##
101
102 # Set default scenario name
103 if not scenario:
104     scenario = "os-nosdn-nofeature-nonha"
105
106 # Parse scenario name
107 try:
108     sc = scenario.split('-')
109     (sdn, features, hamode) = sc[1:4]
110     features = features.split('_')
111     if len(sc) > 4:
112         extra = sc[4].split('_')
113     else:
114         extra = []
115 except ValueError as err:
116     print('Error: Bad scenario name syntax, use '
117           '"os-<controller>-<nfvfeature>-<mode>[-<extrastuff>]" format')
118     sys.exit(1)
119
120 ##
121 ## Update config with scenario name
122 ##
123
124 # change ha mode
125 config['os']['ha']['mode'] = hamode
126
127 # change ha mode
128 config['os']['network']['controller'] = sdn
129
130 # Change features
131 if 'lxd' in features:
132     config['os']['lxd'] = True
133 if 'dvr' in features:
134     config['os']['network']['dvr'] = True
135 if 'ipv6' in features:
136     config['os']['network']['ipv6'] = True
137 if 'ovs' in features:
138     config['os']['network']['enhanced_ovs'] = True
139
140 # Set beta option from extra
141 if 'publicapi' in extra:
142     config['os']['beta']['public_api'] = True
143 if 'radosgwcluster' in extra:
144     config['os']['beta']['hacluster_ceph_radosgw'] = True
145 if 'hugepages' in extra:
146     config['os']['beta']['huge_pages'] = True
147
148 # pp(config)
149
150 ##
151 ## Transform template to bundle.yaml according to config
152 ##
153
154 # Create the jinja2 environment.
155 env = Environment(loader=FileSystemLoader(TPL_DIR),
156                   trim_blocks=True)
157 template = env.get_template('bundle.yaml')
158
159 # Add functions
160 env.globals.update(get_password=get_password)
161 env.globals.update(unit_qty=unit_qty)
162 env.globals.update(to_select=to_select)
163
164 # Render the template
165 print(template.render(**config))