diff --git a/generate.py b/generate.py index d68d4694355163ee913850587f4e8a7fab7f4ab2..58c5be94d6555015164fed234ef3b4d1fa7d98af 100644 --- a/generate.py +++ b/generate.py @@ -1,22 +1,22 @@ #!/usr/bin/python3 +""" A script that generates a Vagrantfile from yaml. """ import sys import jinja2 from modules.device_creator import create_devices -if (len(sys.argv) != 2): - print ("Error: Expecting 1 argument (yml file).") - sys.exit(); +if len(sys.argv) != 2: + print("Error: Expecting 1 argument (yml file).") + sys.exit() # A structure with formatted commands for each device -devices = create_devices(str(sys.argv[1])) +DEVICES = create_devices(str(sys.argv[1])) # Generating output via Jinja2 -templateLoader = jinja2.FileSystemLoader(searchpath="templates") -templateEnv = jinja2.Environment(loader=templateLoader, trim_blocks=True) -baseTemplate = templateEnv.get_template("base") -output = baseTemplate.render(devices = devices) - -print (output) +TEMPLATE_LOADER = jinja2.FileSystemLoader(searchpath="templates") +TEMPLATE_ENV = jinja2.Environment(loader=TEMPLATE_LOADER, trim_blocks=True) +BASE_TEMPLATE = TEMPLATE_ENV.get_template("base") +OUTPUT = BASE_TEMPLATE.render(devices=DEVICES) +print(OUTPUT) diff --git a/modules/attribute_formatter.py b/modules/attribute_formatter.py index 6ad8d9e95c022d441f5cae95ee50fcfd5ad24374..484d8c2e61234531de57c6d4b8c1d1919f0fe2fc 100644 --- a/modules/attribute_formatter.py +++ b/modules/attribute_formatter.py @@ -1,24 +1,27 @@ +""" This module handles the creation of simple vagrant commands from yaml +attributes. """ + + def _format_and_add(key, value, mappings, device_definition): """ Formats and adds the definition of a simple attribute. """ - if (key in mappings['string']): + if key in mappings['string']: device_definition.append( - 'device.' + mappings['string'][key] - + ' = \"' + str(value) + '\"') - elif (key in mappings['integer']): + 'device.' + mappings['string'][key] + + ' = \"' + str(value) + '\"') + elif key in mappings['integer']: device_definition.append( - 'device.' + mappings['integer'][key] - + ' = ' + str(value)) - elif (key in mappings['boolean']): + 'device.' + mappings['integer'][key] + + ' = ' + str(value)) + elif key in mappings['boolean']: device_definition.append( - 'device.' + mappings['boolean'][key] - + ' = ' + str(value).lower()) + 'device.' + mappings['boolean'][key] + + ' = ' + 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(): - if (key != "name"): + if key != "name": _format_and_add(key, value, mappings, device_definition) - diff --git a/modules/device_creator.py b/modules/device_creator.py index f1f37d1600d077838baa6f015050e669e4a12370..ae5923889787033ee13f8be0ec52a61c2bcadfe8 100644 --- a/modules/device_creator.py +++ b/modules/device_creator.py @@ -1,8 +1,11 @@ +""" This package handles file import and creation of an input structure for +Jinja2 """ + import yaml -from .attribute_formatter import add_simple_commands -from .provider import add_prov_attributes, add_router_specification -from .network_parser import add_networks, add_router_ip +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 def _open_yml(yml_file_name): @@ -12,7 +15,7 @@ def _open_yml(yml_file_name): yml = yaml.safe_load(ymlfile) return yml except IOError: - print ("Error: file does not exist.") + print("Error: file does not exist.") def _open_mappings(): @@ -22,7 +25,7 @@ def _open_mappings(): mappings = yaml.safe_load(mapping_file) return mappings except IOError: - print ("Error: cannot find mappings.") + print("Error: cannot find mappings.") def _open_flavors(): @@ -32,7 +35,7 @@ def _open_flavors(): flavors = yaml.safe_load(flavors_file) return flavors except IOError: - print ("Error: cannot find the list of flavors.") + print("Error: cannot find the list of flavors.") def _create_hosts(yml, mappings, flavors): @@ -41,16 +44,16 @@ def _create_hosts(yml, mappings, flavors): 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_prov_attributes( + host, flavors, mappings['need_provider'], host_definitions) return host_definitions -def _create_routers(yml, mappings): +def _create_routers(yml): """ Creates a dictionary with formatted definition of each router. """ router_definitions = {} @@ -69,6 +72,6 @@ def create_devices(yml_file_name): mappings = _open_mappings() flavors = _open_flavors() - return dict(**_create_hosts(yml, mappings, flavors) - , **_create_routers(yml, mappings)) - + return { + **_create_hosts(yml, mappings, flavors), + **_create_routers(yml)} diff --git a/modules/network_parser.py b/modules/network_parser.py index e8be261bf0237ddf1242a6c01bd34134c048d150..9a85a4bcba36c0224ea7e667c82c4941ba01d576 100644 --- a/modules/network_parser.py +++ b/modules/network_parser.py @@ -1,27 +1,41 @@ +""" This module handles network creation. """ + +import sys + + def _find_ip(hostname, mappings, device_type): """ Finds the ip address of a device. """ for mapping in mappings: - if ((device_type == 'host' - and mapping['host'] and mapping['host'] == hostname) - or (device_type == 'router' - and mapping['router'] and mapping['router'] == hostname)): - return mapping['ip'] + if device_type == 'host': + if mapping['host'] and mapping['host'] == hostname: + return mapping['ip'] + elif device_type == 'router': + if mapping['router'] and mapping['router'] == hostname: + return mapping['ip'] + + return None def _add_ip(hostname, mappings, device_type, definitions): """ Adds a formatted ip address to a device definition. """ + ip_address = _find_ip(hostname, mappings, device_type) + + if not ip_address: + print("Cannot find network mapping.") + sys.exit() + definitions[hostname].append( - "device.vm.network :private_network, ip: \"" - + str(_find_ip(hostname, mappings, device_type)) + '\"') + "device.vm.network :private_network, ip: \"" + + ip_address + '\"') 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): + if network['name'] == my_network: address, mask = network['cidr'].split('/') definitions[hostname][-1] += (', netmask: \"' + mask + "\"") @@ -29,25 +43,23 @@ def _add_netmask(hostname, my_network, networks, definitions): def add_networks(hostname, yml, definitions): """ Adds ip address and natmask to a host. """ - if (not yml['net_mappings']): + if not yml['net_mappings']: return for mapping in yml['net_mappings']: - if (mapping['host'] == hostname): + if mapping['host'] == hostname: _add_ip(hostname, yml['net_mappings'], 'host', definitions) - if (yml['networks']): + if yml['networks']: _add_netmask( - hostname, mapping['network'] - , yml['networks'], definitions) + hostname, mapping['network'], yml['networks'], definitions) def add_router_ip(routername, yml, definitions): """ Adds ip address to a router. """ - if (not yml['router_mappings']): + if not yml['router_mappings']: return for mapping in yml['router_mappings']: - if (mapping['router'] == routername): + if mapping['router'] == routername: _add_ip(routername, yml['router_mappings'], 'router', definitions) - diff --git a/modules/provider.py b/modules/provider.py index f4fb506b56721c213327e76aeacab81b65087405..343187c91fb7d4f95251d9725575bffe69ffb0f1 100644 --- a/modules/provider.py +++ b/modules/provider.py @@ -1,28 +1,31 @@ +""" 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'])) + 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): + 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) @@ -30,7 +33,7 @@ def _need_provider(host, provider_attributes): """ Checks if provision attributes are present. """ for attribute in provider_attributes: - if (attribute in host): + if attribute in host: return True return False @@ -38,22 +41,22 @@ def _need_provider(host, provider_attributes): def add_prov_attributes(host, flavors, provider_attributes, definitions): """ Adds provider attributes. """ - if (_need_provider(host, 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) + "device.vm.provider \"virtualbox\" do |vb|") + _add_params(host, flavors, provider_attributes, definitions) definitions[host['name']].append("end") def add_router_specification(router, definitions): """ Adds the default specification for a router. """ - + ROUTER_BOX = "generic/debian10" ROUTER_MEMORY = 256 - definitions[router['name']].append("device.vm.box = \"" + ROUTER_BOX + "\"") definitions[router['name']].append( - "device.vm.provider \"virtualbox\" do |vb|") + "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("end") -