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']
60 def to_select( qty = False ):
63 qty = config['os']['ha']['cluster_size'] if config['os']['ha']['mode'] == 'ha' else 1
64 return random.sample(range(0,config['opnfv']['units']), qty )
66 def get_password(key, length=16, special=False):
67 global passwords_store
68 if key not in passwords_store.keys():
69 alphabet = "abcdefghijklmnopqrstuvwxyz"
70 upperalphabet = alphabet.upper()
71 char_list = alphabet + upperalphabet
74 char_list += "+-,;./:?!*"
75 for i in range(length):
76 pwlist.append(char_list[random.randrange(len(char_list))])
77 random.shuffle(pwlist)
78 passwords_store[key] = "".join(pwlist)
79 return passwords_store[key]
86 config = load_yaml(scenarioconfig_file)
88 config.update(load_yaml(labconfig_file))
90 #We transform array to hash for an easier work
91 config['opnfv']['spaces_dict'] = dict()
92 for space in config['opnfv']['spaces']:
93 config['opnfv']['spaces_dict'][space['type']] = space
94 config['opnfv']['storage_dict'] = dict()
95 for storage in config['opnfv']['storage']:
96 config['opnfv']['storage_dict'][storage['type']] = storage
99 ## Parse scenario name
102 # Set default scenario name
104 scenario = "os-nosdn-nofeature-nonha"
106 # Parse scenario name
108 sc = scenario.split('-')
109 (sdn, features, hamode) = sc[1:4]
110 features = features.split('_')
112 extra = sc[4].split('_')
115 except ValueError as err:
116 print('Error: Bad scenario name syntax, use '
117 '"os-<controller>-<nfvfeature>-<mode>[-<extrastuff>]" format')
121 ## Update config with scenario name
125 config['os']['ha']['mode'] = hamode
128 config['os']['network']['controller'] = sdn
131 if 'lxd' in features:
132 config['os']['lxd'] = True
133 if 'dvr' in features:
134 config['os']['network']['dvr'] = True
135 if 'ipv6' in features:
136 config['os']['network']['ipv6'] = True
137 if 'ovs' in features:
138 config['os']['network']['enhanced_ovs'] = True
140 # Set beta option from extra
141 if 'publicapi' in extra:
142 config['os']['beta']['public_api'] = True
143 if 'radosgwcluster' in extra:
144 config['os']['beta']['hacluster_ceph_radosgw'] = True
145 if 'hugepages' in extra:
146 config['os']['beta']['huge_pages'] = True
151 ## Transform template to bundle.yaml according to config
154 # Create the jinja2 environment.
155 env = Environment(loader=FileSystemLoader(TPL_DIR),
157 template = env.get_template('bundle.yaml')
160 env.globals.update(get_password=get_password)
161 env.globals.update(unit_qty=unit_qty)
162 env.globals.update(to_select=to_select)
164 # Render the template
165 print(template.render(**config))