modfied to use the trusty as extra feature.
[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 unit_ceph_qty():
61     global config
62     if config['os']['ha']['mode'] == 'ha':
63         return config['os']['ha']['cluster_size']
64     else:
65         return 2
66
67 def to_select( qty = False ):
68     global config
69     if not qty:
70         qty = config['os']['ha']['cluster_size'] if config['os']['ha']['mode'] == 'ha' else 1
71     if config['os']['ha']['mode'] == 'ha':
72         return random.sample(range(0,config['opnfv']['units']), qty )
73     else:
74         return random.sample(range(0,2),qty )
75
76 def get_password(key, length=16, special=False):
77     global passwords_store
78     if key not in passwords_store.keys():
79         alphabet = "abcdefghijklmnopqrstuvwxyz"
80         upperalphabet = alphabet.upper()
81         char_list = alphabet + upperalphabet
82         pwlist = []
83         if special:
84             char_list += "+-,;./:?!*"
85         for i in range(length):
86             pwlist.append(char_list[random.randrange(len(char_list))])
87         random.shuffle(pwlist)
88         passwords_store[key] = "".join(pwlist)
89     return passwords_store[key]
90
91 ##
92 ## Config import
93 ##
94
95 #Load scenario Config
96 config = load_yaml(scenarioconfig_file)
97 #Load lab Config
98 config.update(load_yaml(labconfig_file))
99
100 #We transform array to hash for an easier work
101 config['opnfv']['spaces_dict'] = dict()
102 for space in config['opnfv']['spaces']:
103     config['opnfv']['spaces_dict'][space['type']] = space
104 config['opnfv']['storage_dict'] = dict()
105 for storage in config['opnfv']['storage']:
106     config['opnfv']['storage_dict'][storage['type']] = storage
107
108 ##
109 ## Parse scenario name
110 ##
111
112 # Set default scenario name
113 if not scenario:
114     scenario = "os-nosdn-nofeature-nonha"
115
116 # Parse scenario name
117 try:
118     sc = scenario.split('-')
119     (sdn, features, hamode) = sc[1:4]
120     features = features.split('_')
121     if len(sc) > 4:
122         extra = sc[4].split('_')
123     else:
124         extra = []
125 except ValueError as err:
126     print('Error: Bad scenario name syntax, use '
127           '"os-<controller>-<nfvfeature>-<mode>[-<extrastuff>]" format')
128     sys.exit(1)
129
130 ##
131 ## Update config with scenario name
132 ##
133
134 # change ha mode
135 config['os']['ha']['mode'] = hamode
136
137 # change ha mode
138 config['os']['network']['controller'] = sdn
139
140 # Change features
141 if 'lxd' in features:
142     config['os']['lxd'] = True
143 if 'dvr' in features:
144     config['os']['network']['dvr'] = True
145 if 'ipv6' in features:
146     config['os']['network']['ipv6'] = True
147 if 'ovs' in features:
148     config['os']['network']['enhanced_ovs'] = True
149 if 'sfc' in features:
150     config['os']['network']['sfc'] = True
151 if 'dpdk' in features:
152     config['os']['network']['dpdk'] = True
153 if 'bgpvpn' in features:
154     config['os']['network']['bgpvpn'] = True
155 if 'odll3' in features:
156     config['os']['network']['odll3'] = True
157
158 # Set beta option from extra
159 if 'publicapi' in extra:
160     config['os']['beta']['public_api'] = True
161 if 'radosgwcluster' in extra:
162     config['os']['beta']['hacluster_ceph_radosgw'] = True
163 if 'hugepages' in extra:
164     config['os']['beta']['huge_pages'] = True
165 if 'trusty' in extra:
166     config['ubuntu']['release'] = 'trusty'
167     if 'liberty' in extra:
168         config['os']['release'] = 'liberty'
169
170 # pp(config)
171
172 ##
173 ## Transform template to bundle.yaml according to config
174 ##
175
176 # Create the jinja2 environment.
177 env = Environment(loader=FileSystemLoader(TPL_DIR),
178                   trim_blocks=True)
179 template = env.get_template('bundle.yaml')
180
181 # Add functions
182 env.globals.update(get_password=get_password)
183 env.globals.update(unit_qty=unit_qty)
184 env.globals.update(unit_ceph_qty=unit_ceph_qty)
185 env.globals.update(to_select=to_select)
186
187 # Render the template
188 print(template.render(**config))