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
25 parser = OptionParser()
26 parser.add_option("-l", "--lab", dest="lab", help="lab config file")
27 (options, args) = parser.parse_args()
28 labconfig_file = options.lab
31 # Set Path and configs path
34 # Capture our current directory
35 jujuver = subprocess.check_output(["juju", "--version"])
37 if LooseVersion(jujuver) >= LooseVersion('2'):
38 TPL_DIR = os.path.dirname(os.path.abspath(__file__))+'/config_tpl/juju2'
40 TPL_DIR = os.path.dirname(os.path.abspath(__file__))+'/config_tpl'
42 HOME = os.environ['HOME']
43 USER = os.environ['USER']
49 # Prepare a storage for passwords
50 passwords_store = dict()
57 def load_yaml(filepath):
59 with open(filepath, 'r') as stream:
61 return yaml.load(stream)
62 except yaml.YAMLError as exc:
66 def get_ip_address(ifname):
68 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
69 return socket.inet_ntoa(fcntl.ioctl(
72 struct.pack('256s', bytes(ifname.encode('utf-8')[:15]))
84 # Load scenario Config
85 config = load_yaml(labconfig_file)
87 # Set a dict copy of opnfv/spaces
88 config['opnfv']['spaces_dict'] = dict()
89 for space in config['opnfv']['spaces']:
90 config['opnfv']['spaces_dict'][space['type']] = space
92 # Set a dict copy of opnfv/storage
93 config['opnfv']['storage_dict'] = dict()
94 for storage in config['opnfv']['storage']:
95 config['opnfv']['storage_dict'][storage['type']] = storage
97 # Add some OS environment variables
98 config['os'] = {'home': HOME,
100 'brAdmIP': get_ip_address(config['opnfv']['spaces_dict']
101 ['admin']['bridge'])}
103 # Prepare interface-enable, more easy to do it here
105 for node in config['lab']['racks'][0]['nodes']:
106 for nic in node['nics']:
107 if 'admin' not in nic['spaces']:
108 ifnamelist.add(nic['ifname'])
109 config['lab']['racks'][0]['ifnamelist'] = ','.join(ifnamelist)
112 # Transform template to deployconfig.yaml according to config
115 # Create the jinja2 environment.
116 env = Environment(loader=FileSystemLoader(TPL_DIR),
118 template = env.get_template('deployconfig.yaml')
120 # Render the template
121 output = template.render(**config)
123 # Check output syntax
126 except yaml.YAMLError as exc: