file_generator.py 7.06 KB
Newer Older
1
2
3
4
import jinja2
import os

from modules.device_creator import create_devices
Attila Farkas's avatar
Attila Farkas committed
5
from modules.ansible_data_generator import create_network_map, create_host_map, create_network_ips
Attila Farkas's avatar
Attila Farkas committed
6
from modules.routing import BORDER_ROUTER_IP, BORDER_ROUTER_NAME, BORDER_ROUTER_NETWORK_NAME
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

def _load_template(template_name):
    """ Returns a loaded jinja2 template. """

    template_loader = jinja2.FileSystemLoader(searchpath="templates")
    template_env = jinja2.Environment(loader=template_loader, trim_blocks=True)
    return template_env.get_template(template_name)


def _generate_file(filename, output_string):
    """ Generates a file from output string. """

    try:
        new_file = open(filename, "w")
        new_file.write(output_string)
    except IOError:
        print("Error: cannot write to this location.")


Attila Farkas's avatar
Attila Farkas committed
26
27
28
29
def _create_role_directory(role_name):
    """ Creates directory structure for a role. """

    try:
30
        os.mkdir("provisioning")
Attila Farkas's avatar
Attila Farkas committed
31
32
33
    except FileExistsError:
        pass
    try:
34
        os.mkdir("provisioning/roles")
Attila Farkas's avatar
Attila Farkas committed
35
36
37
    except FileExistsError:
        pass
    try:
38
        os.mkdir("provisioning/roles/" + role_name)
Attila Farkas's avatar
Attila Farkas committed
39
40
41
    except FileExistsError:
        pass
    try:
42
        os.mkdir("provisioning/roles/" + role_name +"/tasks")
Attila Farkas's avatar
Attila Farkas committed
43
44
45
46
    except FileExistsError:
        pass


47
48
49
50
51
52
53
54
55
56
def _find_user_ansible_files(definitions):
    """ Finds the user ansible files and returns a list of host names. """
    host_names = []

    for host in definitions["hosts"]:
        if os.path.isfile("provisioning/" + host["name"] + ".yml" ):
            host_names.append(host["name"])

    return host_names

57
def generate_vagrantfile(definitions, ansible_local):
58
    """ Writes the prepared output to a Vagrantfile. """
59
60
   
    device_definitions = create_devices(definitions, ansible_local)
61
    user_ansible_files = _find_user_ansible_files(definitions)
Attila Farkas's avatar
Attila Farkas committed
62
    template = _load_template("vagrantfile")
63
    output = template.render(devices=device_definitions, user_files=user_ansible_files, ansible_local=ansible_local)
64
65
66
67
68
    _generate_file("Vagrantfile", output)
    
    print("Info: Vagrantfile successfully created.")


Attila Farkas's avatar
Attila Farkas committed
69
def _generate_playbook(definitions):
Attila Farkas's avatar
Attila Farkas committed
70
    """ Generates the main playbook. """
71

Attila Farkas's avatar
Attila Farkas committed
72
    host_map = create_host_map(definitions["net_mappings"], definitions["router_mappings"], definitions["hosts"])
Attila Farkas's avatar
Attila Farkas committed
73
    network = create_network_map(definitions)
Attila Farkas's avatar
Attila Farkas committed
74
    network_ips = create_network_ips(definitions["networks"])
Attila Farkas's avatar
Attila Farkas committed
75
    
Attila Farkas's avatar
Attila Farkas committed
76
    template = _load_template("playbook")
Attila Farkas's avatar
Attila Farkas committed
77
    output = template.render(hosts=host_map, routers=network, network_ips=network_ips, border_router_name = BORDER_ROUTER_NAME)
78
79

    try:
80
        os.mkdir("provisioning")
81
82
83
    except FileExistsError:
        pass

84
    _generate_file("./provisioning/playbook.yml", output)
85
86


Attila Farkas's avatar
Attila Farkas committed
87
88
def _generate_device_configuration(definitions):
    """ Generates a playbook with basic device configutarion. """
89

Attila Farkas's avatar
Attila Farkas committed
90
    host_map = create_host_map(definitions["net_mappings"], definitions["router_mappings"], definitions["hosts"])
Attila Farkas's avatar
Attila Farkas committed
91
    network = create_network_map(definitions) 
Attila Farkas's avatar
Attila Farkas committed
92
    network_ips = create_network_ips(definitions["networks"])
93

Attila Farkas's avatar
Attila Farkas committed
94
    template = _load_template("device_configuration")
Attila Farkas's avatar
Attila Farkas committed
95
    output = template.render(hosts=host_map, routers=network, network_ips=network_ips)
96
97

    try:
98
        os.mkdir("provisioning")
99
100
101
    except FileExistsError:
        pass

102
    _generate_file("./provisioning/device_configuration.yml", output)
Attila Farkas's avatar
Attila Farkas committed
103
104
105
106
107


def _generate_hosts_role(definitions):
    """ Generates hosts role. """

Attila Farkas's avatar
Attila Farkas committed
108
    host_map = create_host_map(definitions["net_mappings"], definitions["router_mappings"], definitions["hosts"])
Attila Farkas's avatar
Attila Farkas committed
109

Attila Farkas's avatar
Attila Farkas committed
110
    network = create_network_map(definitions)
Attila Farkas's avatar
Attila Farkas committed
111

Attila Farkas's avatar
Attila Farkas committed
112
    template = _load_template("hosts")
Attila Farkas's avatar
Attila Farkas committed
113
    output = template.render(hosts=host_map, routers=network)
Attila Farkas's avatar
Attila Farkas committed
114
115

    _create_role_directory("hosts")
116
    _generate_file("./provisioning/roles/hosts/tasks/main.yml", output)
Attila Farkas's avatar
Attila Farkas committed
117

118

Attila Farkas's avatar
Attila Farkas committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
def _generate_separate_hosts_role(definitions):
    """ Generate roles for separate host devices. """
    
    host_map = create_host_map(definitions["net_mappings"], definitions["router_mappings"], definitions["hosts"])

    for host in definitions["hosts"]:

        for host_attributes in host_map:
            if host_attributes["host_name"] == host["name"]:
                router_ip = host_attributes["router_ip"]
                interface = host_attributes["interface"]

        template = _load_template("separate_hosts")
        output = template.render(router_ip=router_ip, interface=interface)

        _create_role_directory(host["name"])
        _generate_file("./provisioning/roles/" + host["name"]  + "/tasks/main.yml", output)

Attila Farkas's avatar
Attila Farkas committed
137
138
139
140
141
142
143
def _generate_routers_role(definitions):
    """ Generates routers role. """

    if not definitions['routers'] or not definitions['router_mappings']:
        print("Info: No router definition was found. Skipping router creation.")
        return

Attila Farkas's avatar
Attila Farkas committed
144
    host_map = create_host_map(definitions["net_mappings"], definitions["router_mappings"], definitions["hosts"])
Attila Farkas's avatar
Attila Farkas committed
145

Attila Farkas's avatar
Attila Farkas committed
146
    network = create_network_map(definitions) 
Attila Farkas's avatar
Attila Farkas committed
147
148

    template = _load_template("routers")
Attila Farkas's avatar
Attila Farkas committed
149
    output = template.render(hosts=host_map, routers=network, border_router_ip=BORDER_ROUTER_IP)
Attila Farkas's avatar
Attila Farkas committed
150
151

    _create_role_directory("routers")
152
    _generate_file("./provisioning/roles/routers/tasks/main.yml", output)
153
154


Attila Farkas's avatar
Attila Farkas committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
def _find_cidr(network_name, definitions):
    """ Finds cidr of a network from name. """

    for network in definitions["networks"]:
        if network["name"] == network_name:
            return network["cidr"]

def _get_br_routers(definitions):
    """ Returns a list of router ips that are in the border router network. """

    br_mappings = dict()
    for router_mapping in definitions["router_mappings"]:
        if router_mapping["network"] == BORDER_ROUTER_NETWORK_NAME:
            for router_mapping2 in definitions["router_mappings"]:
                if router_mapping["router"] == router_mapping2["router"] and router_mapping["network"] != router_mapping2["network"]:
                    br_mappings[_find_cidr(router_mapping2["network"], definitions)] = router_mapping["ip"]

    return br_mappings

def _generate_br_role(definitions):
    """ Generates br role. """

    if not definitions['routers'] or not definitions['router_mappings']:
        print("Info: No router definition was found. Skipping border router creation.")
        return

    network = create_network_map(definitions) 
    
    host_map = create_host_map(definitions["net_mappings"], definitions["router_mappings"], definitions["hosts"])

    routers_in_br_network = _get_br_routers(definitions)

    template = _load_template("br")
    output = template.render(hosts = host_map, routers=network, br_routes=routers_in_br_network, border_router_name=BORDER_ROUTER_NAME)

    _create_role_directory("br")
    _generate_file("./provisioning/roles/br/tasks/main.yml", output)


194
195
def generate_ansible_files(device_definitions):
    """ Generates files for ansible. """
Attila Farkas's avatar
Attila Farkas committed
196
    
Attila Farkas's avatar
Attila Farkas committed
197
    _generate_playbook(device_definitions)
Attila Farkas's avatar
Attila Farkas committed
198
199
    _generate_device_configuration(device_definitions)
    _generate_hosts_role(device_definitions)
Attila Farkas's avatar
Attila Farkas committed
200
    _generate_separate_hosts_role(device_definitions)
Attila Farkas's avatar
Attila Farkas committed
201
    _generate_routers_role(device_definitions)
Attila Farkas's avatar
Attila Farkas committed
202
    _generate_br_role(device_definitions)
Attila Farkas's avatar
Attila Farkas committed
203
204

    print("Info: Ansible files successfully created.")