2 # -*- coding: utf-8 -*-
5 This script generates a maas deployer 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
17 from pprint import pprint as pp
26 parser = OptionParser()
27 parser.add_option("-l", "--lab", dest="lab", help="lab config file")
28 (options, args) = parser.parse_args()
29 labconfig_file = options.lab
32 # Set Path and configs path
35 # Capture our current directory
36 jujuver = subprocess.check_output(["juju", "--version"])
38 if LooseVersion(jujuver) >= LooseVersion('2'):
39 TPL_DIR = os.path.dirname(os.path.abspath(__file__))+'/config_tpl/maas2/maas_tpl'
41 TPL_DIR = os.path.dirname(os.path.abspath(__file__))+'/config_tpl/maas_tpl'
43 HOME = os.environ['HOME']
44 USER = os.environ['USER']
50 # Prepare a storage for passwords
51 passwords_store = dict()
58 def load_yaml(filepath):
60 with open(filepath, 'r') as stream:
62 return yaml.load(stream)
63 except yaml.YAMLError as exc:
67 def get_ip_address(ifname):
69 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
70 return socket.inet_ntoa(fcntl.ioctl(
73 struct.pack('256s', bytes(ifname.encode('utf-8')[:15]))
81 # Load scenario Config
82 config = load_yaml(labconfig_file)
83 config['opnfv']['spaces_dict'] = dict()
84 for space in config['opnfv']['spaces']:
85 config['opnfv']['spaces_dict'][space['type']] = space
86 config['os'] = {'home': HOME,
88 'brAdmIP': get_ip_address(config['opnfv']['spaces_dict']
94 # Transform template to bundle.yaml according to config
97 # Create the jinja2 environment.
98 env = Environment(loader=FileSystemLoader(TPL_DIR),
100 template = env.get_template('deployment.yaml')
102 # Render the template
103 output = template.render(**config)
105 # Check output syntax
108 except yaml.YAMLError as exc: