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
15
from distutils.version import LooseVersion, StrictVersion
26
parser = OptionParser()
27
parser.add_option("-s", "--scenario", dest="scenario", help="scenario name")
28
parser.add_option("-l", "--lab", dest="lab", help="lab config file")
29
(options, args) = parser.parse_args()
30
scenario = options.scenario
31
labconfig_file = options.lab
34
# Set Path and configs path
37
scenarioconfig_file = 'default_deployment_config.yaml'
38
TPL_DIR = os.path.dirname(os.path.abspath(__file__))+'/config_tpl/juju2/bundlek8_tpl'
44
# Prepare a storage for passwords
45
passwords_store = dict()
52
def load_yaml(filepath):
54
with open(filepath, 'r') as stream:
56
return yaml.load(stream)
57
except yaml.YAMLError as exc:
66
"""Return quantity of units to deploy"""
68
if config['os']['ha']['mode'] == 'ha':
69
return config['os']['ha']['cluster_size']
75
"""Return size of the ceph cluster"""
77
if config['os']['ha']['mode'] == 'ha':
78
return config['os']['ha']['cluster_size']
80
if config['opnfv']['units'] >= 3:
81
return config['os']['ha']['cluster_size']
85
def unit_scaleio_qty():
86
"""Return size of the scaleio cluster"""
89
def to_select(qty=False):
90
"""Return a random list of machines numbers to deploy"""
93
qty = config['os']['ha']['cluster_size'] if \
94
config['os']['ha']['mode'] == 'ha' else 1
95
if config['os']['hyperconverged']:
96
return random.sample(range(0, config['opnfv']['units']), qty)
98
return random.sample(range(0, qty), qty)
101
def get_password(key, length=16, special=False):
102
"""Return a new random password or a already created one"""
103
global passwords_store
104
if key not in passwords_store.keys():
105
alphabet = "abcdefghijklmnopqrstuvwxyz"
106
upperalphabet = alphabet.upper()
107
char_list = alphabet + upperalphabet + '0123456789'
110
char_list += "+-,;./:?!*"
111
for i in range(length):
112
pwlist.append(char_list[random.randrange(len(char_list))])
113
random.shuffle(pwlist)
114
passwords_store[key] = "".join(pwlist)
115
return passwords_store[key]
121
# Load scenario Config
122
config = load_yaml(scenarioconfig_file)
124
config.update(load_yaml(labconfig_file))
126
# We transform array to hash for an easier work
127
config['opnfv']['spaces_dict'] = dict()
128
for space in config['opnfv']['spaces']:
129
config['opnfv']['spaces_dict'][space['type']] = space
130
config['opnfv']['storage_dict'] = dict()
131
for storage in config['opnfv']['storage']:
132
config['opnfv']['storage_dict'][storage['type']] = storage
135
# Parse scenario name
138
# Set default scenario name
140
scenario = "k8-nosdn-baremetal-core"
142
# Parse scenario name
144
sc = scenario.split('-')
145
(sdn, features, hamode) = sc[1:4]
146
features = features.split('_')
148
extra = sc[4].split('_')
151
except ValueError as err:
152
print('Error: Bad scenario name syntax, use '
153
'"k8-nosdn-baremetal-core" format')
157
# Update config with scenario name
160
if 'dpdk' in features:
161
config['os']['network']['dpdk'] = True
163
config['k8']['feature']['loadbalancer'] = True
164
if 'ceph' in features:
165
config['k8']['feature']['storage'] = 'ceph'
168
config['k8']['network']['controller'] = sdn
170
# Set beta option from extra
171
if 'hugepages' in extra:
172
config['os']['beta']['huge_pages'] = True
174
config['k8']['feature']['loadbalancer'] = True
175
if 'mitaka' in extra:
176
config['os']['release'] = 'mitaka'
177
if 'xenial' in extra:
178
config['ubuntu']['release'] = 'xenial'
181
# Transform template to bundle.yaml according to config
184
# Create the jinja2 environment.
185
env = Environment(loader=FileSystemLoader(TPL_DIR),
187
template = env.get_template('bundle.yaml')
190
env.globals.update(get_password=get_password)
191
env.globals.update(unit_qty=unit_qty)
192
env.globals.update(unit_ceph_qty=unit_ceph_qty)
193
env.globals.update(unit_scaleio_qty=unit_scaleio_qty)
194
env.globals.update(to_select=to_select)
196
# Render the template
197
output = template.render(**config)
199
# Check output syntax
202
except yaml.YAMLError as exc: