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
150 # Set beta option from extra
151 if 'publicapi' in extra:
152 config['os']['beta']['public_api'] = True
153 if 'radosgwcluster' in extra:
154 config['os']['beta']['hacluster_ceph_radosgw'] = True
155 if 'hugepages' in extra:
156 config['os']['beta']['huge_pages'] = True
161 ## Transform template to bundle.yaml according to config
164 # Create the jinja2 environment.
165 env = Environment(loader=FileSystemLoader(TPL_DIR),
167 template = env.get_template('bundle.yaml')
170 env.globals.update(get_password=get_password)
171 env.globals.update(unit_qty=unit_qty)
172 env.globals.update(unit_ceph_qty=unit_ceph_qty)
173 env.globals.update(to_select=to_select)
175 # Render the template
176 print(template.render(**config))