2 # -*- coding: utf-8 -*-
4 from optparse import OptionParser
5 from jinja2 import Environment, FileSystemLoader
6 from pprint import pprint as pp
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
24 ## Set Path and configs path
27 scenarioconfig_file = 'default_deployment_config.yaml'
28 # Capture our current directory
29 TPL_DIR = os.path.dirname(os.path.abspath(__file__))+'/bundle_tpl'
35 # Prepare a storage for passwords
36 passwords_store = dict()
42 def load_yaml(filepath):
43 with open(filepath, 'r') as stream:
45 return yaml.load(stream)
46 except yaml.YAMLError as exc:
50 ## Templates functions
55 if config['os']['ha']['mode'] == 'ha':
56 return config['os']['ha']['cluster_size']
62 if config['os']['ha']['mode'] == 'ha':
63 return config['os']['ha']['cluster_size']
67 def to_select( qty = False ):
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 )
74 return random.sample(range(0,2),qty )
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
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]
96 config = load_yaml(scenarioconfig_file)
98 config.update(load_yaml(labconfig_file))
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
109 ## Parse scenario name
112 # Set default scenario name
114 scenario = "os-nosdn-nofeature-nonha"
116 # Parse scenario name
118 sc = scenario.split('-')
119 (sdn, features, hamode) = sc[1:4]
120 features = features.split('_')
122 extra = sc[4].split('_')
125 except ValueError as err:
126 print('Error: Bad scenario name syntax, use '
127 '"os-<controller>-<nfvfeature>-<mode>[-<extrastuff>]" format')
131 ## Update config with scenario name
135 config['os']['ha']['mode'] = hamode
138 config['os']['network']['controller'] = sdn
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
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'
173 ## Transform template to bundle.yaml according to config
176 # Create the jinja2 environment.
177 env = Environment(loader=FileSystemLoader(TPL_DIR),
179 template = env.get_template('bundle.yaml')
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)
187 # Render the template
188 print(template.render(**config))