Statistics
| Branch: | Revision:

iof-tools / gen-config.py @ 28dbee38

History | View | Annotate | Download (3.9 KB)

1
#!/usr/bin/env python
2
import xml.etree.ElementTree as ET
3
from argparse import ArgumentParser
4
import sys
5
from config_templates import SSHConfig, AnsibleConfig
6

    
7
from const import COMPONENT_ID, NODE, SSH_CONFIG_TEMPLATE, \
8
    HOST_CONFIG_TEMPLATE, IDENTITY_FILE, NODE_NAME, ANSIBLE_CONFIG_TEMPLATE, \
9
    INVENTORY_CONFIG_TEMPLATE, ANSIBLE_HOST_TEMPLATE, PROXY_COMMAND_TEMPLATE
10

    
11
parser = ArgumentParser()
12
parser.add_argument("-r", "--rspec", dest="rspec",
13
                    nargs='+', action="store", metavar="FILENAME",
14
                    help="Rspec file to be parsed")
15
parser.add_argument("-s", "--ssh-config", dest="ssh_config",
16
                    default="ssh-config", action="store", metavar="FILENAME",
17
                    help="Output file onto which the SSH configuration is "
18
                         "written (default=%(default)s)")
19
parser.add_argument("-a", "--ansible-config", dest="ansible_config",
20
                    default="ansible.cfg", action="store", metavar="FILENAME",
21
                    help="Output file onto which the ansible configuration is "
22
                         "written (default=%(default)s)")
23
parser.add_argument("-u", "--user", dest="user",
24
                    default="segata", action="store", metavar="USERNAME",
25
                    help="Username for ssh config file (default=%(default)s)")
26
parser.add_argument("-i", "--inventory", dest="ansible_inventory",
27
                    default="ansible-hosts", action="store", metavar="FILENAME",
28
                    help="Output file onto which the ansible inventory is "
29
                         "written (default=%(default)s)")
30
parser.add_argument("-k", "--key", dest="identity",
31
                    default=IDENTITY_FILE, action="store",
32
                    metavar="FILENAME",
33
                    help="Private key or certificate used for the "
34
                         "authentication (default=%(default)s)")
35

    
36
args = parser.parse_args()
37

    
38
if not args.rspec:
39
    print("You must specify an Rspec input file")
40
    sys.exit(1)
41

    
42
rspec_list = args.rspec
43
ssh_config_file = args.ssh_config
44
ssh_config_no_proxy_file = ssh_config_file + "-no-proxy"
45
ansible_config_file = args.ansible_config
46
ansible_inventory_file = args.ansible_inventory
47
identity_file = args.identity
48
user = args.user
49

    
50
config_file = open(ssh_config_file, "w")
51
config_no_proxy_file = open(ssh_config_no_proxy_file, "w")
52
ansible_file = open(ansible_config_file, "w")
53
inventory_file = open(ansible_inventory_file, "w")
54

    
55
ssh_config = SSHConfig(SSH_CONFIG_TEMPLATE, HOST_CONFIG_TEMPLATE,
56
                       identity_file, user, PROXY_COMMAND_TEMPLATE)
57
ssh_config_no_proxy = SSHConfig(SSH_CONFIG_TEMPLATE, HOST_CONFIG_TEMPLATE,
58
                                identity_file, user)
59
ansible_config = AnsibleConfig(ANSIBLE_CONFIG_TEMPLATE,
60
                               INVENTORY_CONFIG_TEMPLATE,
61
                               ANSIBLE_HOST_TEMPLATE, ansible_inventory_file,
62
                               ssh_config_file)
63

    
64
nodes = []
65
for r in rspec_list:
66
  xml_file = ET.iterparse(r)
67
  for _, el in xml_file:
68
    el.tag = el.tag.split('}', 1)[1]  # strip all namespaces
69
  root = xml_file.root
70
  n = root.findall(NODE)
71
  nodes = nodes + n
72

    
73
n = 0
74
#root = xml_file.root
75
#nodes = root.findall(NODE)
76
n_nodes = len(nodes)
77
name_template = NODE_NAME
78
for node in nodes:
79
    component_id = node.get(COMPONENT_ID)
80
    components = component_id.split("+")
81
    domain = components[1]
82
    name = components[3]
83
    hostname = "{}.{}".format(name, domain)
84
    friendly_name = name_template.format(n)
85
    ssh_config.add_host(friendly_name, hostname)
86
    ssh_config_no_proxy.add_host(friendly_name, hostname)
87
    ansible_config.add_host(friendly_name)
88
    n += 1
89

    
90
config_file.write(str(ssh_config))
91
config_no_proxy_file.write(str(ssh_config_no_proxy))
92
ansible_file.write(ansible_config.get_ansible_config())
93
inventory_file.write(ansible_config.get_ansible_inventory())
94

    
95
config_file.close()
96
config_no_proxy_file.close()
97
ansible_file.close()
98
inventory_file.close()