2017-01-06 20:19:42 -06:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os.path
|
|
|
|
import argparse
|
|
|
|
import yaml
|
|
|
|
import jinja2
|
|
|
|
import netaddr
|
2017-01-19 22:56:26 -06:00
|
|
|
import json
|
2017-01-06 20:19:42 -06:00
|
|
|
from minigraph import parse_xml
|
2017-02-28 12:52:56 -06:00
|
|
|
from minigraph import minigraph_encoder
|
2017-01-06 20:19:42 -06:00
|
|
|
|
|
|
|
|
|
|
|
def is_ipv4(value):
|
|
|
|
if not value:
|
|
|
|
return False
|
|
|
|
if isinstance(value, netaddr.IPAddress):
|
|
|
|
addr = value
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
addr = netaddr.IPAddress(str(value))
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
return addr.version == 4
|
|
|
|
|
|
|
|
def is_ipv6(value):
|
|
|
|
if not value:
|
|
|
|
return False
|
|
|
|
if isinstance(value, netaddr.IPAddress):
|
|
|
|
addr = value
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
addr = netaddr.IPAddress(str(value))
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
return addr.version == 6
|
|
|
|
|
2017-02-17 15:03:42 -06:00
|
|
|
def get_machine_info():
|
|
|
|
if not os.path.isfile('/host/machine.conf'):
|
|
|
|
return None
|
|
|
|
machine_vars = {}
|
|
|
|
with open('/host/machine.conf') as machine_file:
|
|
|
|
for line in machine_file:
|
|
|
|
tokens = line.split('=')
|
|
|
|
if len(tokens) < 2:
|
|
|
|
continue
|
|
|
|
machine_vars[tokens[0]] = tokens[1].strip()
|
|
|
|
return machine_vars
|
|
|
|
|
2017-01-06 20:19:42 -06:00
|
|
|
|
|
|
|
def main():
|
|
|
|
parser=argparse.ArgumentParser(description="Render configuration file from minigraph data and jinja2 template.")
|
2017-01-19 22:56:26 -06:00
|
|
|
parser.add_argument("-m", "--minigraph", help="minigraph xml file")
|
|
|
|
parser.add_argument("-y", "--yaml", help="yaml file that contains addtional variables")
|
|
|
|
parser.add_argument("-a", "--additional-data", help="addition data, in json string")
|
|
|
|
group = parser.add_mutually_exclusive_group()
|
|
|
|
group.add_argument("-t", "--template", help="render the data with the template file")
|
2017-02-22 16:28:37 -06:00
|
|
|
group.add_argument("-s", "--alias-mapping", help="print alias mapping json if available", action='store_true')
|
2017-02-28 12:52:56 -06:00
|
|
|
group.add_argument("-v", "--var", help="print the value of a variable, support jinja2 expression")
|
2017-01-19 22:56:26 -06:00
|
|
|
group.add_argument("--var-json", help="print the value of a variable, in json format")
|
2017-02-28 12:52:56 -06:00
|
|
|
group.add_argument("--var-keys", help="print all keys of a map variable - to be deprecated, use -v and keys()")
|
2017-01-19 22:56:26 -06:00
|
|
|
group.add_argument("--print-data", help="print all data", action='store_true')
|
2017-01-06 20:19:42 -06:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2017-01-19 22:56:26 -06:00
|
|
|
data = {}
|
2017-02-17 15:03:42 -06:00
|
|
|
machine_info = get_machine_info()
|
2017-02-17 21:47:50 -06:00
|
|
|
if machine_info != None:
|
|
|
|
data.update(machine_info)
|
|
|
|
if machine_info.has_key('onie_platform'):
|
|
|
|
data['platform'] = machine_info['onie_platform']
|
|
|
|
elif machine_info.has_key('aboot_platform'):
|
|
|
|
data['platform'] = machine_info['aboot_platform']
|
2017-01-19 22:56:26 -06:00
|
|
|
|
|
|
|
if args.minigraph != None:
|
|
|
|
minigraph = args.minigraph
|
2017-02-17 15:03:42 -06:00
|
|
|
if data.has_key('platform'):
|
|
|
|
data.update(parse_xml(minigraph, data['platform']))
|
|
|
|
else:
|
|
|
|
data.update(parse_xml(minigraph))
|
2017-01-06 20:19:42 -06:00
|
|
|
|
2017-01-19 22:56:26 -06:00
|
|
|
if args.yaml != None:
|
|
|
|
with open(args.yaml, 'r') as stream:
|
|
|
|
additional_data = yaml.load(stream)
|
2017-01-06 20:19:42 -06:00
|
|
|
data.update(additional_data)
|
|
|
|
|
2017-01-19 22:56:26 -06:00
|
|
|
if args.additional_data != None:
|
|
|
|
data.update(json.loads(args.additional_data))
|
|
|
|
|
|
|
|
if args.template != None:
|
|
|
|
template_file = os.path.abspath(args.template)
|
|
|
|
env = jinja2.Environment(loader=jinja2.FileSystemLoader('/'), trim_blocks=True)
|
|
|
|
env.filters['ipv4'] = is_ipv4
|
|
|
|
env.filters['ipv6'] = is_ipv6
|
|
|
|
template = env.get_template(template_file)
|
|
|
|
print template.render(data)
|
|
|
|
|
|
|
|
if args.var != None:
|
2017-02-28 12:52:56 -06:00
|
|
|
template = jinja2.Template('{{' + args.var + '}}')
|
|
|
|
print template.render(data)
|
2017-01-19 22:56:26 -06:00
|
|
|
|
|
|
|
if args.var_json != None:
|
2017-02-28 12:52:56 -06:00
|
|
|
print json.dumps(data[args.var_json], indent=4, cls=minigraph_encoder)
|
2017-01-19 22:56:26 -06:00
|
|
|
|
|
|
|
if args.var_keys != None:
|
|
|
|
for key in data[args.var_keys].keys():
|
|
|
|
print key
|
2017-02-22 16:28:37 -06:00
|
|
|
|
2017-02-22 21:25:30 -06:00
|
|
|
if args.alias_mapping:
|
2017-02-22 16:28:37 -06:00
|
|
|
mapping = {}
|
|
|
|
if data.has_key('alias_map'):
|
|
|
|
for item in data['alias_map']:
|
|
|
|
mapping[item['sonic']] = item['origin']
|
|
|
|
print json.dumps(mapping)
|
2017-01-06 20:19:42 -06:00
|
|
|
|
2017-01-19 22:56:26 -06:00
|
|
|
if args.print_data:
|
2017-02-28 12:52:56 -06:00
|
|
|
print json.dumps(data, indent=4, cls=minigraph_encoder)
|
2017-01-06 20:19:42 -06:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|
|
|
|
|