#!/usr/bin/env python3
#
# Create expanded topology file, Ansible inventory, host vars, or Vagrantfile from
# topology file
#

import sys
import os
import yaml
import re
from jinja2 import Environment, FileSystemLoader, Undefined, StrictUndefined, make_logging_undefined

sys.path[0] = sys.path[0] + '/lib/create-topology'

import common
import cli_parser
import read_topology
import augment
import inventory
import vagrant

LOGGING=False
VERBOSE=False


def dump_topology_data(topology,state):
  print("%s topology data" % state)
  print("===============================")
  print(yaml.dump(topology))

def create_topology_file(topology,fname):
  with open(fname,"w") as output:
    output.write("# Expanded topology created from %s " % topology.get('input','<unknown>'))
    output.write(yaml.dump(topology))
    output.close()
    print("Created expanded topology file: %s" % args.xpand)

def main():
  args = cli_parser.parse()

  path = os.path.dirname(os.path.realpath(__file__))
  settings = path+"/topology-defaults.yml"
  topology = read_topology.load(args.topology,args.defaults,settings)
  if args.verbose:
    dump_topology_data(topology,'Collected')
  common.exit_on_error()

  augment.augment(topology)
  common.exit_on_error()
  if args.vagrant:
    if args.verbose:
      vagrant.dump(topology,path)
    else:
      vagrant.create(topology,args.vagrant,path)

  if args.xpand:
    if args.verbose:
      dump_topology_data(topology,'Augmented')
    else:
      create_topology_file(topology)

  if args.inventory:
    if args.verbose:
      inventory.dump(topology)
    else:
      inventory.write(topology,args.inventory,args.hostvars)
      if args.config:
        inventory.config(args.config,args.inventory,path)
#    with open(args.inventory,"w") as output:
#      output.write(yaml.dump(create_ansible_inventory(topology['nodes'],defaults,args.hostvars)))

main()