Revision 314da287

View differences:

gen-config.py
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")
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")
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")
27

  
28
args = parser.parse_args()
29

  
30
if not args.rspec:
31
    print("You must specify an Rspec input file")
32
    sys.exit(1)
33

  
34
rspec_file = args.rspec
35
ssh_config_file = args.ssh_config
36
ansible_config_file = args.ansible_config
37
ansible_inventory_file = args.ansible_inventory
38

  
39
config_file = open(ssh_config_file, "w")
40
ansible_file = open(ansible_config_file, "w")
41
inventory_file = open(ansible_inventory_file, "w")
42

  
43
ssh_config = SSHConfig(SSH_CONFIG_TEMPLATE, HOST_CONFIG_TEMPLATE, IDENTITY_FILE)
44
ansible_config = AnsibleConfig(ANSIBLE_CONFIG_TEMPLATE,
45
                               INVENTORY_CONFIG_TEMPLATE,
46
                               ANSIBLE_HOST_TEMPLATE, IDENTITY_FILE,
47
                               ansible_inventory_file, ssh_config_file)
48

  
49
xml_file = ET.iterparse(rspec_file)
50
for _, el in xml_file:
51
    el.tag = el.tag.split('}', 1)[1]  # strip all namespaces
52

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

  
69
config_file.write(str(ssh_config))
70
ansible_file.write(ansible_config.get_ansible_config())
71
inventory_file.write(ansible_config.get_ansible_inventory())
72

  
73
config_file.close()
74
ansible_file.close()
75
inventory_file.close()
gen-ssh-config.py
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")
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")
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")
27

  
28
args = parser.parse_args()
29

  
30
if not args.rspec:
31
    print("You must specify an Rspec input file")
32
    sys.exit(1)
33

  
34
rspec_file = args.rspec
35
ssh_config_file = args.ssh_config
36
ansible_config_file = args.ansible_config
37
ansible_inventory_file = args.ansible_inventory
38

  
39
config_file = open(ssh_config_file, "w")
40
ansible_file = open(ansible_config_file, "w")
41
inventory_file = open(ansible_inventory_file, "w")
42

  
43
ssh_config = SSHConfig(SSH_CONFIG_TEMPLATE, HOST_CONFIG_TEMPLATE, IDENTITY_FILE)
44
ansible_config = AnsibleConfig(ANSIBLE_CONFIG_TEMPLATE,
45
                               INVENTORY_CONFIG_TEMPLATE,
46
                               ANSIBLE_HOST_TEMPLATE, IDENTITY_FILE,
47
                               ansible_inventory_file, ssh_config_file)
48

  
49
xml_file = ET.iterparse(rspec_file)
50
for _, el in xml_file:
51
    el.tag = el.tag.split('}', 1)[1]  # strip all namespaces
52

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

  
69
config_file.write(str(ssh_config))
70
ansible_file.write(ansible_config.get_ansible_config())
71
inventory_file.write(ansible_config.get_ansible_inventory())
72

  
73
config_file.close()
74
ansible_file.close()
75
inventory_file.close()

Also available in: Unified diff