Statistics
| Branch: | Revision:

iof-tools / gen-config.py @ b9954290

History | View | Annotate | Download (3.18 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
10

    
11
parser = ArgumentParser()
12
parser.add_argument("-r", "--rspec", dest="rspec",
13
                    default="", 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("-i", "--inventory", dest="ansible_inventory",
24
                    default="ansible-hosts", action="store", metavar="FILENAME",
25
                    help="Output file onto which the ansible inventory is "
26
                         "written (default=%(default)s)")
27
parser.add_argument("-k", "--key", dest="identity",
28
                    default=IDENTITY_FILE, action="store",
29
                    metavar="FILENAME",
30
                    help="Private key or certificate used for the "
31
                         "authentication (default=%(default)s)")
32

    
33
args = parser.parse_args()
34

    
35
if not args.rspec:
36
    print("You must specify an Rspec input file")
37
    sys.exit(1)
38

    
39
rspec_file = args.rspec
40
ssh_config_file = args.ssh_config
41
ansible_config_file = args.ansible_config
42
ansible_inventory_file = args.ansible_inventory
43
identity_file = args.identity
44

    
45
config_file = open(ssh_config_file, "w")
46
ansible_file = open(ansible_config_file, "w")
47
inventory_file = open(ansible_inventory_file, "w")
48

    
49
ssh_config = SSHConfig(SSH_CONFIG_TEMPLATE, HOST_CONFIG_TEMPLATE, identity_file)
50
ansible_config = AnsibleConfig(ANSIBLE_CONFIG_TEMPLATE,
51
                               INVENTORY_CONFIG_TEMPLATE,
52
                               ANSIBLE_HOST_TEMPLATE, identity_file,
53
                               ansible_inventory_file, ssh_config_file)
54

    
55
xml_file = ET.iterparse(rspec_file)
56
for _, el in xml_file:
57
    el.tag = el.tag.split('}', 1)[1]  # strip all namespaces
58

    
59
n = 0
60
root = xml_file.root
61
nodes = root.findall(NODE)
62
n_nodes = len(nodes)
63
name_template = NODE_NAME.format(len(str(n_nodes)))
64
for node in nodes:
65
    component_id = node.get(COMPONENT_ID)
66
    components = component_id.split("+")
67
    domain = components[1]
68
    name = components[3]
69
    hostname = "{}.{}".format(name, domain)
70
    friendly_name = name_template.format(n)
71
    ssh_config.add_host(friendly_name, hostname)
72
    ansible_config.add_host(friendly_name)
73
    n += 1
74

    
75
config_file.write(str(ssh_config))
76
ansible_file.write(ansible_config.get_ansible_config())
77
inventory_file.write(ansible_config.get_ansible_inventory())
78

    
79
config_file.close()
80
ansible_file.close()
81
inventory_file.close()