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
33 TPL_DIR = os.path.dirname(os.path.abspath(__file__))+'/config_tpl/juju2'
35 HOME = os.environ['HOME']
36 USER = os.environ['USER']
42 # Prepare a storage for passwords
43 passwords_store = dict()
50 def load_yaml(filepath):
52 with open(filepath, 'r') as stream:
54 return yaml.load(stream)
55 except yaml.YAMLError as exc:
59 def get_ip_address(ifname):
61 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
62 return socket.inet_ntoa(fcntl.ioctl(
65 struct.pack('256s', bytes(ifname.encode('utf-8')[:15]))
77 # Load scenario Config
78 config = load_yaml(labconfig_file)
80 # Set a dict copy of opnfv/spaces
81 config['opnfv']['spaces_dict'] = dict()
82 for space in config['opnfv']['spaces']:
83 config['opnfv']['spaces_dict'][space['type']] = space
85 # Set a dict copy of opnfv/storage
86 config['opnfv']['storage_dict'] = dict()
87 for storage in config['opnfv']['storage']:
88 config['opnfv']['storage_dict'][storage['type']] = storage
90 # Add some OS environment variables
91 config['os'] = {'home': HOME,
93 'brAdmIP': get_ip_address(config['opnfv']['spaces_dict']
96 # Prepare interface-enable, more easy to do it here
98 for node in config['lab']['racks'][0]['nodes']:
99 for nic in node['nics']:
100 if 'admin' not in nic['spaces']:
101 ifnamelist.add(nic['ifname'])
102 config['lab']['racks'][0]['ifnamelist'] = ','.join(ifnamelist)
105 # Transform template to deployconfig.yaml according to config
108 # Create the jinja2 environment.
109 env = Environment(loader=FileSystemLoader(TPL_DIR),
111 template = env.get_template('deployconfig.yaml')
113 # Render the template
114 output = template.render(**config)
116 # Check output syntax
119 except yaml.YAMLError as exc: