Skip to content
Snippets Groups Projects
Commit b3d8c70b authored by Attila Farkas's avatar Attila Farkas
Browse files

remove old modules

parent 448c9034
No related branches found
No related tags found
1 merge request!7Resolve Refactoring
from modules.device_creator import open_file
INTERFACE_FILE = "name_mapping/interface.yml"
def create_network_map(definitions):
""" Creates a structure with network topology for Jinja2 template. """
routings = []
for router_mapping in definitions["router_mappings"]:
routing = dict()
routing["router_name"] = router_mapping["router"]
routing["router_network"] = router_mapping["network"]
routing["router_network_ip"] = _find_network_ip(router_mapping["network"], definitions)
routing["router_ip"] = router_mapping["ip"]
routings.append(routing)
return routings
def _find_router_ip(network_name, router_mappings):
for router_mapping in router_mappings:
if router_mapping["network"] == network_name:
return router_mapping["ip"]
def _find_network_ip(network_name, definitions):
for network in definitions["networks"]:
if network["name"] == network_name:
return network["cidr"]
def _find_interface(host_name, hosts):
for host in hosts:
if host["name"] == host_name:
interfaces = open_file(INTERFACE_FILE)
if host["base_box"] in interfaces:
return interfaces[host["base_box"]]
return "eth1"
def create_host_map(net_mappings, router_mappings, host_list):
""" Creates a structure with hosts and their primary routers ip """
hosts = []
for net_mapping in net_mappings:
host = dict()
host["host_name"] = net_mapping["host"]
host["host_ip"] = net_mapping["ip"]
host["router_ip"] = _find_router_ip(
net_mapping["network"], router_mappings)
host["interface"] = _find_interface(net_mapping["host"], host_list)
hosts.append(host)
return hosts
def create_network_ips(networks):
network_ips = []
for network in networks:
if network["cidr"] not in network_ips:
network_ips.append(network["cidr"])
return network_ips
""" This module handles the creation of simple vagrant commands from yaml
attributes. """
def _format_variable_name(key, variable_type, mappings):
""" Formats the variable to the required vagrantfile definition. """
return "device." + mappings[variable_type][key] + " = "
def _format_and_add(key, value, mappings, device_definition):
""" Formats and adds the definition of a simple attribute. """
if key in mappings["string"]:
device_definition.append(
_format_variable_name(key, "string", mappings)
+ '\"' + str(value) + '\"')
elif key in mappings["integer"]:
device_definition.append(
_format_variable_name(key, "integer", mappings)
+ str(value))
elif key in mappings["boolean"]:
device_definition.append(
_format_variable_name(key, "boolean", mappings)
+ str(value).lower())
def add_simple_commands(device, mappings, device_definition):
""" Adds definitions of string, integer and boolean vagrant attributes. """
for key, value in device.items():
_format_and_add(key, value, mappings, device_definition)
""" This package handles file import and creation of an input structure for
Jinja2 """
import yaml
import sys
from modules.attribute_formatter import add_simple_commands
from modules.provider import add_prov_attributes, add_router_specification
from modules.network_parser import add_networks, add_router_ip
MAPPING_FILE = "name_mapping/mapping.yml"
FLAVORS_FILE = "name_mapping/flavors.yml"
def open_file(file_name):
""" Opens and returns a file from the argument. """
try:
input_file = open(str(file_name))
return yaml.safe_load(input_file)
except IOError:
print("Error: Cannot find a required file: " + str(file_name))
sys.exit(1)
def _add_provisioning(hostname, host_definitions):
""" Adds provisioning to the device if the file exists. """
try:
provision_file = open("provision/" + str(hostname) + ".yml")
host_definitions[hostname].append("device.vm.provision \"ansible\" do |ansible|")
host_definitions[hostname].append(" ansible.playbook = \"provision/" + hostname + ".yml\"")
host_definitions[hostname].append("end")
except IOError:
pass
def _add_rsync(box, host_name, definitions):
""" add rsync to debian machines """
if box == "generic/debian10":
definitions[host_name].append("# standard shared folder doesn't work on debian")
definitions[host_name].append("device.vm.synced_folder \".\", \"/vagrant\", type: \"rsync\", rsync__exclude: \".git/\"")
if box == "kalilinux/rolling-light":
definitions[host_name].append("device.ssh.password = \"vagrant\"")
def _create_hosts(yml, mappings, flavors, ansible_local):
""" Creates a dictionary with formatted definition of each host. """
host_definitions = {}
for host in yml['hosts']:
host_definitions[host['name']] = []
add_simple_commands(host, mappings, host_definitions[host['name']])
add_networks(host["name"], yml, host_definitions)
add_prov_attributes(
host, flavors, mappings['need_provider'], host_definitions)
_add_provisioning(host["name"], host_definitions)
if ansible_local:
_add_rsync(host["base_box"], host["name"], host_definitions)
return host_definitions
def _create_routers(yml, ansible_local):
""" Creates a dictionary with formatted definition of each router. """
router_definitions = {}
for router in yml['routers']:
router_definitions[router['name']] = []
add_router_ip(router["name"], yml, router_definitions)
add_router_specification(router, router_definitions, ansible_local)
if ansible_local:
_add_rsync("generic/debian10", router["name"], router_definitions)
return router_definitions
def create_devices(definitions, ansible_local):
""" Returns a merged dictionary of host and router definitions. """
mappings = open_file(MAPPING_FILE)
flavors = open_file(FLAVORS_FILE)
return {
**_create_hosts(definitions, mappings, flavors, ansible_local),
**_create_routers(definitions, ansible_local)}
import jinja2
import os
from modules.ansible_data_generator import create_network_map, create_host_map, create_network_ips
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.")
def _create_role_directory(role_name, provisioning_dir):
""" Creates directory structure for a role. """
try:
os.mkdir(provisioning_dir)
except FileExistsError:
pass
try:
os.mkdir(provisioning_dir + "/roles")
except FileExistsError:
pass
try:
os.mkdir(provisioning_dir + "/roles/" + role_name)
except FileExistsError:
pass
try:
os.mkdir(provisioning_dir + "/roles/" + role_name +"/tasks")
except FileExistsError:
pass
def _generate_playbook(definitions):
""" Generates the main playbook. """
host_map = create_host_map(definitions["net_mappings"], definitions["router_mappings"], definitions["hosts"])
network = create_network_map(definitions)
template = _load_template("playbook")
output = template.render(hosts=host_map, routers=network)
try:
os.mkdir("provisioning")
except FileExistsError:
pass
_generate_file("./provisioning/playbook.yml", output)
def _generate_device_configuration(definitions):
""" Generates a playbook with basic device configutarion. """
host_map = create_host_map(definitions["net_mappings"], definitions["router_mappings"], definitions["hosts"])
network = create_network_map(definitions)
network_ips = create_network_ips(definitions["networks"])
template = _load_template("device_configuration")
output = template.render(hosts=host_map, routers=network, network_ips=network_ips, border_router_name = BORDER_ROUTER_NAME)
try:
os.mkdir("base_provisioning")
except FileExistsError:
pass
_generate_file("./base_provisioning/device_configuration.yml", output)
def _generate_hosts_role(definitions):
""" Generates hosts role. """
host_map = create_host_map(definitions["net_mappings"], definitions["router_mappings"], definitions["hosts"])
network = create_network_map(definitions)
template = _load_template("hosts")
output = template.render(hosts=host_map, routers=network)
_create_role_directory("hosts", "base_provisioning")
_generate_file("./base_provisioning/roles/hosts/tasks/main.yml", output)
user_template = _load_template("user_hosts")
user_output = template.render()
_create_role_directory("hosts", "provisioning")
_generate_file("./provisioning/roles/hosts/tasks/main.yml", output)
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"]:
host_name = host_attributes["host_name"]
router_ip = host_attributes["router_ip"]
interface = host_attributes["interface"]
template = _load_template("separate_hosts")
output = template.render(host_name=host_name, router_ip=router_ip, interface=interface)
_create_role_directory(host["name"], "base_provisioning")
_generate_file("./base_provisioning/roles/" + host["name"] + "/tasks/main.yml", output)
template = _load_template("user_separate_hosts")
output = template.render(host_name=host_name)
_create_role_directory(host["name"], "provisioning")
_generate_file("./provisioning/roles/" + host["name"] + "/tasks/main.yml", output)
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
host_map = create_host_map(definitions["net_mappings"], definitions["router_mappings"], definitions["hosts"])
network = create_network_map(definitions)
template = _load_template("routers")
output = template.render(hosts=host_map, routers=network, border_router_ip=BORDER_ROUTER_IP)
_create_role_directory("routers", "base_provisioning")
_generate_file("./base_provisioning/roles/routers/tasks/main.yml", output)
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, border_router_public_ip=BORDER_ROUTER_PUBLIC_IP)
_create_role_directory("br", "base_provisioning")
_generate_file("./base_provisioning/roles/br/tasks/main.yml", output)
def generate_ansible_files(definitions, flags):
"""
Generates ansible playbooks.
:param definitions: device definitions structure
:param flags: command line input flags
"""
_generate_playbook(definitions)
_generate_device_configuration(definitions)
_generate_hosts_role(definitions)
_generate_separate_hosts_role(definitions)
_generate_routers_role(definitions)
_generate_br_role(definitions)
print("Info: Ansible files successfully created.")
""" This module handles network creation. """
import sys
from conf.border_router import BORDER_ROUTER_NAME, BORDER_ROUTER_PUBLIC_IP
def _find_networks(hostname, mappings, device_type):
""" Matches the device to networks. Returns a list of maiching networks. """
network_list = []
for mapping in mappings:
if device_type in ('host', 'router'):
if mapping[device_type] and mapping[device_type] == hostname:
network_list.append(mapping)
return network_list
def _add_ip(hostname, mappings, device_type, definitions):
""" Adds a formatted ip address and network name to device definition. """
networks = _find_networks(hostname, mappings, device_type)
for network in networks:
if not network["ip"]:
print("Cannot find network mapping.")
sys.exit()
definitions[hostname].append(
"device.vm.network :private_network, ip: \""
+ network["ip"] + '\", virtualbox__intnet: ' + network["network"])
def _add_netmask(hostname, my_network, networks, definitions):
""" Adds netmask to the end of a formatted ip definition. """
for network in networks:
if network['name'] == my_network:
address, mask = network['cidr'].split('/')
definitions[hostname][-1] += (', netmask: \"' + mask + "\"")
def _add_interfaces(hostname, mapping, device_type, networks, definitions):
""" Adds all network interfaces to a device. """
if not mapping["ip"]:
print("Cannot find network mapping.")
sys.exit()
definitions[hostname].append(
"device.vm.network :private_network, ip: \"" + mapping["ip"]
+ "\", virtualbox__intnet: \"" + mapping["network"] + "\"")
if hostname == BORDER_ROUTER_NAME:
definitions[BORDER_ROUTER_NAME].append("device.vm.network :public_network, ip: \" " + BORDER_ROUTER_PUBLIC_IP + "\"")
_add_netmask(hostname, mapping["network"], networks, definitions)
def add_networks(hostname, yml, definitions):
""" Adds ip address and natmask to a host. """
if not yml['net_mappings']:
return
for mapping in yml['net_mappings']:
if mapping['host'] == hostname:
_add_interfaces(
hostname, mapping,
'host', yml['networks'], definitions)
def add_router_ip(routername, yml, definitions):
""" Adds ip address to a router. """
if not yml['router_mappings']:
return
for mapping in yml['router_mappings']:
if mapping['router'] == routername:
_add_interfaces(
routername, mapping,
'router', yml['networks'], definitions)
""" This module handles VirtualBox attributes. """
def _print_flavor(host, flavors, provider_attributes, definitions):
""" Formats and add a flavor for a device. """
if 'memory' not in host:
definitions[host['name']].append(
' vb.' + provider_attributes['memory'] + ' = '
+ str(flavors[host['flavor']]['memory']))
if 'cpus' not in host:
definitions[host['name']].append(
' vb.' + provider_attributes['cpus'] + ' = '
+ str(flavors[host['flavor']]['cores']))
def _add_params(host, flavors, provider_attributes, definitions):
""" Formats and adds simple provision attributes. """
if 'memory' in host:
definitions[host['name']].append(
' vb.' + provider_attributes['memory'] + ' = '
+ str(host['memory']))
if 'cpus' in host:
definitions[host['name']].append(
' vb.' + provider_attributes['cpus'] + ' = '
+ str(host['cpus']))
if 'flavor' in host and host['flavor'] in flavors:
_print_flavor(host, flavors, provider_attributes, definitions)
def _need_provider(host, provider_attributes):
""" Checks if provision attributes are present. """
for attribute in provider_attributes:
if attribute in host:
return True
return False
def add_prov_attributes(host, flavors, provider_attributes, definitions):
""" Adds provider attributes. """
if _need_provider(host, provider_attributes):
definitions[host['name']].append(
"device.vm.provider \"virtualbox\" do |vb|")
_add_params(host, flavors, provider_attributes, definitions)
definitions[host['name']].append("end")
def add_router_specification(router, definitions, ansible_local):
""" Adds the default specification for a router. """
router_box = "generic/debian10"
router_memory = 256
router_cpus = 1
definitions[router['name']].append(
"device.vm.hostname = \"" + router['name'] + "\"")
definitions[router['name']].append(
"device.vm.box = \"" + router_box + "\"")
definitions[router['name']].append(
"device.vm.provider \"virtualbox\" do |vb|")
definitions[router['name']].append(" vb.memory = " + str(router_memory))
definitions[router['name']].append(
" vb.cpus = " + str(router_cpus))
definitions[router['name']].append("end")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment