2 # -*- coding: utf-8 -*-
5 This script generates a juju deployer bundle based on
6 scenario name, and lab config file.
9 -s, --scenario : scenario name
10 -l, --lab : lab config file
13 from optparse import OptionParser
14 from jinja2 import Environment, FileSystemLoader
24 parser = OptionParser()
25 parser.add_option("-s", "--scenario", dest="scenario", help="scenario name")
26 parser.add_option("-l", "--lab", dest="lab", help="lab config file")
27 (options, args) = parser.parse_args()
28 scenario = options.scenario
29 labconfig_file = options.lab
32 # Set Path and configs path
35 scenarioconfig_file = 'default_deployment_config.yaml'
36 # Capture our current directory
37 TPL_DIR = os.path.dirname(os.path.abspath(__file__))+'/config_tpl/bundle_tpl'
43 # Prepare a storage for passwords
44 passwords_store = dict()
51 def load_yaml(filepath):
53 with open(filepath, 'r') as stream:
55 return yaml.load(stream)
56 except yaml.YAMLError as exc:
65 """Return quantity of units to deploy"""
67 if config['os']['ha']['mode'] == 'ha':
68 return config['os']['ha']['cluster_size']
74 """Return size of the ceph cluster"""
76 if config['os']['ha']['mode'] == 'ha':
77 return config['os']['ha']['cluster_size']
79 if config['opnfv']['units'] >= 3:
80 return config['os']['ha']['cluster_size']
85 def to_select(qty=False):
86 """Return a random list of machines numbers to deploy"""
89 qty = config['os']['ha']['cluster_size'] if \
90 config['os']['ha']['mode'] == 'ha' else 1
91 if config['os']['hyperconverged']:
92 return random.sample(range(0, config['opnfv']['units']), qty)
94 return random.sample(range(0, qty), qty)
97 def get_password(key, length=16, special=False):
98 """Return a new random password or a already created one"""
99 global passwords_store
100 if key not in passwords_store.keys():
101 alphabet = "abcdefghijklmnopqrstuvwxyz"
102 upperalphabet = alphabet.upper()
103 char_list = alphabet + upperalphabet + '0123456789'
106 char_list += "+-,;./:?!*"
107 for i in range(length):
108 pwlist.append(char_list[random.randrange(len(char_list))])
109 random.shuffle(pwlist)
110 passwords_store[key] = "".join(pwlist)
111 return passwords_store[key]
117 # Load scenario Config
118 config = load_yaml(scenarioconfig_file)
120 config.update(load_yaml(labconfig_file))
122 # We transform array to hash for an easier work
123 config['opnfv']['spaces_dict'] = dict()
124 for space in config['opnfv']['spaces']:
125 config['opnfv']['spaces_dict'][space['type']] = space
126 config['opnfv']['storage_dict'] = dict()
127 for storage in config['opnfv']['storage']:
128 config['opnfv']['storage_dict'][storage['type']] = storage
131 # Parse scenario name
134 # Set default scenario name
136 scenario = "os-nosdn-nofeature-nonha"
138 # Parse scenario name
140 sc = scenario.split('-')
141 (sdn, features, hamode) = sc[1:4]
142 features = features.split('_')
144 extra = sc[4].split('_')
147 except ValueError as err:
148 print('Error: Bad scenario name syntax, use '
149 '"os-<controller>-<nfvfeature>-<mode>[-<extrastuff>]" format')
153 # Update config with scenario name
157 config['os']['ha']['mode'] = hamode
160 config['os']['network']['controller'] = sdn
163 if 'lxd' in features:
164 config['os']['lxd'] = True
165 if 'dvr' in features:
166 config['os']['network']['dvr'] = True
167 if 'ipv6' in features:
168 config['os']['network']['ipv6'] = True
169 if 'ovs' in features:
170 config['os']['network']['enhanced_ovs'] = True
171 if 'sfc' in features:
172 config['os']['network']['sfc'] = True
173 if 'dpdk' in features:
174 config['os']['network']['dpdk'] = True
175 if 'bgpvpn' in features:
176 config['os']['network']['bgpvpn'] = True
177 if 'odll3' in features:
178 config['os']['network']['odll3'] = True
179 if 'dishypcon' in features:
180 config['os']['hyperconverged'] = False
182 # Set beta option from extra
183 if 'publicapi' in extra:
184 config['os']['beta']['public_api'] = True
185 if 'radosgwcluster' in extra:
186 config['os']['beta']['hacluster_ceph_radosgw'] = True
187 if 'hugepages' in extra:
188 config['os']['beta']['huge_pages'] = True
189 if 'trusty' in extra:
190 config['ubuntu']['release'] = 'trusty'
191 if 'liberty' in extra:
192 config['os']['release'] = 'liberty'
193 if 'dishypcon' in extra:
194 config['os']['hyperconverged'] = False
197 # Transform template to bundle.yaml according to config
200 # Create the jinja2 environment.
201 env = Environment(loader=FileSystemLoader(TPL_DIR),
203 template = env.get_template('bundle.yaml')
206 env.globals.update(get_password=get_password)
207 env.globals.update(unit_qty=unit_qty)
208 env.globals.update(unit_ceph_qty=unit_ceph_qty)
209 env.globals.update(to_select=to_select)
211 # Render the template
212 output = template.render(**config)
214 # Check output syntax
217 except yaml.YAMLError as exc: