file_generator.py 3.83 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
6
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def _create_role_directory(role_name):
    """ Creates directory structure for a role. """

    try:
        os.mkdir("provision")
    except FileExistsError:
        pass
    try:
        os.mkdir("provision/roles")
    except FileExistsError:
        pass
    try:
        os.mkdir("provision/roles/" + role_name)
    except FileExistsError:
        pass
    try:
        os.mkdir("provision/roles/" + role_name +"/tasks")
    except FileExistsError:
        pass


47
48
49
50
def generate_vagrantfile(definitions):
    """ Writes the prepared output to a Vagrantfile. """
    
    device_definitions = create_devices(definitions)
Attila Farkas's avatar
Attila Farkas committed
51
    template = _load_template("vagrantfile")
52
53
54
55
56
57
    output = template.render(devices=device_definitions)
    _generate_file("Vagrantfile", output)
    
    print("Info: Vagrantfile successfully created.")


Attila Farkas's avatar
Attila Farkas committed
58
59
def _generate_playbook():
    """ Generates the main playbook. """
60

Attila Farkas's avatar
Attila Farkas committed
61
62
    template = _load_template("playbook")
    output = template.render()
63
64
65
66
67
68

    try:
        os.mkdir("provision")
    except FileExistsError:
        pass

Attila Farkas's avatar
Attila Farkas committed
69
    _generate_file("./provision/playbook.yml", output)
70
71


Attila Farkas's avatar
Attila Farkas committed
72
73
def _generate_device_configuration(definitions):
    """ Generates a playbook with basic device configutarion. """
74

Attila Farkas's avatar
Attila Farkas committed
75
    host_map = create_host_map(definitions["net_mappings"], definitions["router_mappings"], definitions["hosts"])
Attila Farkas's avatar
Attila Farkas committed
76
77
    network = create_network_map(
            definitions["net_mappings"], definitions["router_mappings"]) 
78

Attila Farkas's avatar
Attila Farkas committed
79
80
    template = _load_template("device_configuration")
    output = template.render(hosts=host_map, routers=network)
81
82
83
84
85
86

    try:
        os.mkdir("provision")
    except FileExistsError:
        pass

Attila Farkas's avatar
Attila Farkas committed
87
88
89
90
91
92
    _generate_file("./provision/device_configuration.yml", output)


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

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

Attila Farkas's avatar
Attila Farkas committed
95
96
97
    network = create_network_map(
            definitions["net_mappings"], definitions["router_mappings"])

Attila Farkas's avatar
Attila Farkas committed
98
    template = _load_template("hosts")
Attila Farkas's avatar
Attila Farkas committed
99
    output = template.render(hosts=host_map, routers=network)
Attila Farkas's avatar
Attila Farkas committed
100
101
102
103

    _create_role_directory("hosts")
    _generate_file("./provision/roles/hosts/tasks/main.yml", output)

104

Attila Farkas's avatar
Attila Farkas committed
105
106
107
108
109
110
111
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
112
    host_map = create_host_map(definitions["net_mappings"], definitions["router_mappings"], definitions["hosts"])
Attila Farkas's avatar
Attila Farkas committed
113

Attila Farkas's avatar
Attila Farkas committed
114
115
116
117
    network = create_network_map(
            definitions["net_mappings"], definitions["router_mappings"]) 

    template = _load_template("routers")
Attila Farkas's avatar
Attila Farkas committed
118
    output = template.render(hosts=host_map, routers=network)
Attila Farkas's avatar
Attila Farkas committed
119
120
121

    _create_role_directory("routers")
    _generate_file("./provision/roles/routers/tasks/main.yml", output)
122
123
124
125


def generate_ansible_files(device_definitions):
    """ Generates files for ansible. """
Attila Farkas's avatar
Attila Farkas committed
126
127
128
129
130
131
132
    
    _generate_playbook()
    _generate_device_configuration(device_definitions)
    _generate_hosts_role(device_definitions)
    _generate_routers_role(device_definitions)

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