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__))+'/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']
82 def to_select(qty=False):
83 """Return a random list of machines numbers to deploy"""
86 qty = config['os']['ha']['cluster_size'] if \
87 config['os']['ha']['mode'] == 'ha' else 1
88 if config['os']['ha']['mode'] == 'ha':
89 return random.sample(range(0, config['opnfv']['units']), qty)
91 return random.sample(range(0, 2), qty)
94 def get_password(key, length=16, special=False):
95 """Return a new random password or a already created one"""
96 global passwords_store
97 if key not in passwords_store.keys():
98 alphabet = "abcdefghijklmnopqrstuvwxyz"
99 upperalphabet = alphabet.upper()
100 char_list = alphabet + upperalphabet + '0123456789'
103 char_list += "+-,;./:?!*"
104 for i in range(length):
105 pwlist.append(char_list[random.randrange(len(char_list))])
106 random.shuffle(pwlist)
107 passwords_store[key] = "".join(pwlist)
108 return passwords_store[key]
114 # Load scenario Config
115 config = load_yaml(scenarioconfig_file)
117 config.update(load_yaml(labconfig_file))
119 # We transform array to hash for an easier work
120 config['opnfv']['spaces_dict'] = dict()
121 for space in config['opnfv']['spaces']:
122 config['opnfv']['spaces_dict'][space['type']] = space
123 config['opnfv']['storage_dict'] = dict()
124 for storage in config['opnfv']['storage']:
125 config['opnfv']['storage_dict'][storage['type']] = storage
128 # Parse scenario name
131 # Set default scenario name
133 scenario = "os-nosdn-nofeature-nonha"
135 # Parse scenario name
137 sc = scenario.split('-')
138 (sdn, features, hamode) = sc[1:4]
139 features = features.split('_')
141 extra = sc[4].split('_')
144 except ValueError as err:
145 print('Error: Bad scenario name syntax, use '
146 '"os-<controller>-<nfvfeature>-<mode>[-<extrastuff>]" format')
150 # Update config with scenario name
154 config['os']['ha']['mode'] = hamode
157 config['os']['network']['controller'] = sdn
160 if 'lxd' in features:
161 config['os']['lxd'] = True
162 if 'dvr' in features:
163 config['os']['network']['dvr'] = True
164 if 'ipv6' in features:
165 config['os']['network']['ipv6'] = True
166 if 'ovs' in features:
167 config['os']['network']['enhanced_ovs'] = True
168 if 'sfc' in features:
169 config['os']['network']['sfc'] = True
170 if 'dpdk' in features:
171 config['os']['network']['dpdk'] = True
172 if 'bgpvpn' in features:
173 config['os']['network']['bgpvpn'] = True
174 if 'odll3' in features:
175 config['os']['network']['odll3'] = True
177 # Set beta option from extra
178 if 'publicapi' in extra:
179 config['os']['beta']['public_api'] = True
180 if 'radosgwcluster' in extra:
181 config['os']['beta']['hacluster_ceph_radosgw'] = True
182 if 'hugepages' in extra:
183 config['os']['beta']['huge_pages'] = True
184 if 'trusty' in extra:
185 config['ubuntu']['release'] = 'trusty'
186 if 'liberty' in extra:
187 config['os']['release'] = 'liberty'
192 # Transform template to bundle.yaml according to config
195 # Create the jinja2 environment.
196 env = Environment(loader=FileSystemLoader(TPL_DIR),
198 template = env.get_template('bundle.yaml')
201 env.globals.update(get_password=get_password)
202 env.globals.update(unit_qty=unit_qty)
203 env.globals.update(unit_ceph_qty=unit_ceph_qty)
204 env.globals.update(to_select=to_select)
206 # Render the template
207 print(template.render(**config))