2 # -*- coding: utf-8 -*-
5 This script generates a deployment config based on lab config file.
8 -l, --lab : lab config file
11 from optparse import OptionParser
12 from jinja2 import Environment, FileSystemLoader
13 from distutils.version import LooseVersion, StrictVersion
22 parser = OptionParser()
23 parser.add_option("-l", "--lab", dest="lab", help="lab config file")
24 (options, args) = parser.parse_args()
25 labconfig_file = options.lab
28 # Set Path and configs path
31 # Capture our current directory
32 jujuver = subprocess.check_output(["juju", "--version"])
34 if LooseVersion(jujuver) >= LooseVersion('2'):
35 TPL_DIR = os.path.dirname(os.path.abspath(__file__))+'/config_tpl/juju2'
37 TPL_DIR = os.path.dirname(os.path.abspath(__file__))+'/config_tpl'
39 HOME = os.environ['HOME']
40 USER = os.environ['USER']
46 # Prepare a storage for passwords
47 passwords_store = dict()
54 def load_yaml(filepath):
56 with open(filepath, 'r') as stream:
58 return yaml.load(stream)
59 except yaml.YAMLError as exc:
66 # Load scenario Config
67 config = load_yaml(labconfig_file)
69 # Set a dict copy of opnfv/spaces
70 config['opnfv']['spaces_dict'] = dict()
71 for space in config['opnfv']['spaces']:
72 config['opnfv']['spaces_dict'][space['type']] = space
74 # Set a dict copy of opnfv/storage
75 config['opnfv']['storage_dict'] = dict()
76 for storage in config['opnfv']['storage']:
77 config['opnfv']['storage_dict'][storage['type']] = storage
79 # Add some OS environment variables
80 config['os'] = {'home': HOME,
84 # Prepare interface-enable, more easy to do it here
86 for node in config['lab']['racks'][0]['nodes']:
87 for nic in node['nics']:
88 if 'admin' not in nic['spaces']:
89 ifnamelist.add(nic['ifname'])
90 config['lab']['racks'][0]['ifnamelist'] = ','.join(ifnamelist)
93 # Transform template to deployconfig.yaml according to config
96 # Create the jinja2 environment.
97 env = Environment(loader=FileSystemLoader(TPL_DIR),
99 template = env.get_template('deployconfig.yaml')
101 # Render the template
102 output = template.render(**config)
104 # Check output syntax
107 except yaml.YAMLError as exc: