diff --git a/modules/ansible_generator.py b/modules/ansible_generator.py index 9d20533834ded389ecd1ee0bf549e13da130d064..13c7090f9c848206031830fffa052bf87ef5d637 100644 --- a/modules/ansible_generator.py +++ b/modules/ansible_generator.py @@ -1,6 +1,12 @@ +def _create_inventory(input_definitions): + """ Creates an inventory file with host groups. """ + + def generate_playbooks(input_definitions, flags): """ Generates ansible playbooks. :param definitions: device definitions structure :param flags: command line input flags - """ \ No newline at end of file + """ + + _create_inventory(input_definitions) \ No newline at end of file diff --git a/modules/vagrant_generator.py b/modules/vagrant_generator.py index 1539b2e84162fc0cae6225057bc6a75066c05de5..82ff2482c585533d54bb546fcb596bdc90be5f1d 100644 --- a/modules/vagrant_generator.py +++ b/modules/vagrant_generator.py @@ -41,7 +41,7 @@ def _create_commands(device_attributes, device_type, input_definitions, flags): vb["name"] = "virtualbox" vb["commands"] = vb_commands commands.append(vb) - + return commands @@ -49,7 +49,7 @@ def _create_ansible_commands(playbook_location, flags): """ Creates commands for running a playbook from the Vagrantfile. """ commands = [] - + playbook = dict() playbook["type"] = "string" playbook["command"] = "playbook" @@ -94,14 +94,14 @@ def _add_all_networks(vagrant_definitions, input_definitions, flags): for definition in vagrant_definitions: if definition["type"] == "host": - _add_networks_to_device(definition, input_definitions["net_mappings"], input_definitions) + _add_networks_to_device(definition, input_definitions["net_mappings"], input_definitions) elif definition["type"] == "router": _add_networks_to_device(definition, input_definitions["router_mappings"], input_definitions) def _call_provisioner(flags): """ Creates entry to vagrant definitions for calling the provisioner. """ - + provisioner_calls = [] config_playbook = dict() @@ -125,7 +125,7 @@ def _call_provisioner(flags): user_playbook["note"] = "user configuration of devices" user_playbook["commands"] = _create_ansible_commands( "base_provisioning/playbook.yml", flags) - + provisioner_calls.append(user_playbook) return provisioner_calls @@ -153,9 +153,9 @@ def _build_vagrant_definitions(input_definitions, flags): vagrant_definitions.append(device) _add_all_networks(vagrant_definitions, input_definitions, flags) - + vagrant_definitions.extend(_call_provisioner(flags)) - + return vagrant_definitions @@ -165,11 +165,10 @@ def _build_vagrantfile(vagrant_definitions): definitions. """ -# TODO change newvagrantfile to vagrantfile - template = load_template("newvagrantfile") + template = load_template("vagrantfile") output = template.render(defs=vagrant_definitions) generate_file("Vagrantfile", output) - + def generate_vagrantfile(input_definitions, flags): """ diff --git a/templates/newvagrantfile b/templates/newvagrantfile deleted file mode 100644 index c87f2c6fbda24ae524a801c79121e97bf376010e..0000000000000000000000000000000000000000 --- a/templates/newvagrantfile +++ /dev/null @@ -1,111 +0,0 @@ -# Vagrantfile generated by Sandbox Creator. -# -# -*- mode: ruby -*- -# vi: set ft=ruby : - -{# Macro for router items #} -{% macro router(item, namespace) %} - # device (router): {{ item.name }} - {{ namespace }}.vm.define "{{ item.name }}" do |device| -{{ layer2(item.commands, "device") }} -{% endmacro -%} - -{# Macro for host items #} -{% macro host(item, namespace) %} - # device (host): {{ item.name }} - {{ namespace }}.vm.define "{{ item.name }}" do |device| -{{ layer2(item.commands, "device") }} -{% endmacro -%} - -{# Macro for provider items #} -{% macro provider(item, namespace) %} - {{ namespace }}.vm.provider "{{ item.name }}" do |provider| -{{ layer3(item.commands, "provider") }} -{% endmacro -%} - -{# Macro for provision items #} -{% macro provision(item, namespace) %} - # {{ item.note }} - {{ namespace }}.vm.provision :{{ item.provisioner }} do |provisioner| -{{ layer2(item.commands, "provisioner") }} -{% endmacro -%} - -{# Macro for string items #} -{% macro string(item, namespace) %} - {{ namespace }}.{{ item.command }} = "{{ item.value }}" -{% endmacro -%} - -{# Macro for boolean items #} -{% macro boolean(item, namespace) %} -{% if item.value %} - {{ namespace }}.{{ item.command }} = true -{% else %} - {{ namespace }}.{{ item.command }} = false -{% endif %} -{% endmacro -%} - -{# Macro for integer items #} -{% macro integer(item, namespace) %} - {{ namespace }}.{{ item.command }} = {{ item.value }} -{% endmacro -%} - -{# Macro for network items #} -{% macro network(item, namespace) %} - {{ namespace }}.vm.network :{{ item.network_type }}, ip: "{{ item.ip }}" -{%- if item.netmask %} -, netmask: "{{ item.netmask }}" -{%- endif %} -{%- if item.network_type == "private_network" %} -, virtualbox__intnet: "{{ item.name }}" -{% endif %} -{% endmacro -%} - -{# A macro that generates the first level of indentation. #} -{% macro layer1(structure, namespace) %} -{% for item in structure %} -{% if item.type == "router" %} -{{ router(item, namespace) }} -{% elif item.type == "host" %} -{{ host(item, namespace) }} -{% elif item.type == "provision" %} -{{ provision(item, namespace) }} -{% endif %} -{% endfor %} -end -{%- endmacro -%} - -{# A macro that generates the second level of indentation. #} -{% macro layer2(structure, namespace) %} -{% for item in structure %} -{% if item.type == "string" %} - {{ string(item, namespace) -}} -{% elif item.type == "boolean" %} - {{ boolean(item, namespace) -}} -{% elif item.type == "integer" %} - {{ integer(item, namespace) -}} -{% elif item.type == "provider" %} - {{ provider(item, namespace) -}} -{% elif item.type == "network" %} - {{ network(item, namespace) -}} -{% endif %} -{% endfor %} - end -{%- endmacro -%} - -{# A macro that generates the third level of indentation. #} -{% macro layer3(structure, namespace) %} -{% for item in structure %} -{% if item.type == "string" %} - {{ string(item, namespace) -}} -{% elif item.type == "boolean" %} - {{ boolean(item, namespace) -}} -{% elif item.type == "integer" %} - {{ integer(item, namespace) -}} -{% endif %} -{% endfor %} - end -{%- endmacro -%} - -Vagrant.configure("2") do |config| - -{{ layer1(defs, "config") -}} diff --git a/templates/vagrantfile b/templates/vagrantfile index 946446c71fc08127a3026740579c095125186d20..c87f2c6fbda24ae524a801c79121e97bf376010e 100644 --- a/templates/vagrantfile +++ b/templates/vagrantfile @@ -1,51 +1,111 @@ -# Generated vagrant file +# Vagrantfile generated by Sandbox Creator. # # -*- mode: ruby -*- # vi: set ft=ruby : -{# Macro that prints out attributes of a device #} -{% macro printAttributes(device_name) %} -{% for command in devices[device_name] %} - {{ command }} -{% endfor %} -{% endmacro %} +{# Macro for router items #} +{% macro router(item, namespace) %} + # device (router): {{ item.name }} + {{ namespace }}.vm.define "{{ item.name }}" do |device| +{{ layer2(item.commands, "device") }} +{% endmacro -%} -{# Device definitions #} -Vagrant.configure("2") do |config| -{% for name, attributes in devices.items() %} +{# Macro for host items #} +{% macro host(item, namespace) %} + # device (host): {{ item.name }} + {{ namespace }}.vm.define "{{ item.name }}" do |device| +{{ layer2(item.commands, "device") }} +{% endmacro -%} - # device: {{ name }} - config.vm.define "{{ name }}" do |device| -{{ printAttributes(name) }} end -{% endfor %} +{# Macro for provider items #} +{% macro provider(item, namespace) %} + {{ namespace }}.vm.provider "{{ item.name }}" do |provider| +{{ layer3(item.commands, "provider") }} +{% endmacro -%} - # basic ansible configuration of devices and networks - config.vm.provision :ansible{% if ansible_local %}_local{% endif %} do |ansible| - ansible.playbook = "base_provisioning/device_configuration.yml" - ansible.verbose = true - ansible.extra_vars = { - ansible_python_interpreter: "/usr/bin/python3", - } - end +{# Macro for provision items #} +{% macro provision(item, namespace) %} + # {{ item.note }} + {{ namespace }}.vm.provision :{{ item.provisioner }} do |provisioner| +{{ layer2(item.commands, "provisioner") }} +{% endmacro -%} +{# Macro for string items #} +{% macro string(item, namespace) %} + {{ namespace }}.{{ item.command }} = "{{ item.value }}" +{% endmacro -%} - # user configuration of devices with ansible - config.vm.provision :ansible{% if ansible_local %}_local{% endif %} do |ansible| - ansible.playbook = "provisioning/playbook.yml" - ansible.verbose = true - ansible.extra_vars = { - ansible_python_interpreter: "/usr/bin/python3", - } - end +{# Macro for boolean items #} +{% macro boolean(item, namespace) %} +{% if item.value %} + {{ namespace }}.{{ item.command }} = true +{% else %} + {{ namespace }}.{{ item.command }} = false +{% endif %} +{% endmacro -%} - {% for name in user_files %} - config.vm.provision :ansible{% if ansible_local %}_local{% endif %} do |ansible| - ansible.playbook = "provisioning/{{ name }}.yml" - ansible.verbose = true - ansible.extra_vars = { - ansible_python_interpreter: "/usr/bin/python3", - } - end - {% endfor %} +{# Macro for integer items #} +{% macro integer(item, namespace) %} + {{ namespace }}.{{ item.command }} = {{ item.value }} +{% endmacro -%} +{# Macro for network items #} +{% macro network(item, namespace) %} + {{ namespace }}.vm.network :{{ item.network_type }}, ip: "{{ item.ip }}" +{%- if item.netmask %} +, netmask: "{{ item.netmask }}" +{%- endif %} +{%- if item.network_type == "private_network" %} +, virtualbox__intnet: "{{ item.name }}" +{% endif %} +{% endmacro -%} + +{# A macro that generates the first level of indentation. #} +{% macro layer1(structure, namespace) %} +{% for item in structure %} +{% if item.type == "router" %} +{{ router(item, namespace) }} +{% elif item.type == "host" %} +{{ host(item, namespace) }} +{% elif item.type == "provision" %} +{{ provision(item, namespace) }} +{% endif %} +{% endfor %} end +{%- endmacro -%} + +{# A macro that generates the second level of indentation. #} +{% macro layer2(structure, namespace) %} +{% for item in structure %} +{% if item.type == "string" %} + {{ string(item, namespace) -}} +{% elif item.type == "boolean" %} + {{ boolean(item, namespace) -}} +{% elif item.type == "integer" %} + {{ integer(item, namespace) -}} +{% elif item.type == "provider" %} + {{ provider(item, namespace) -}} +{% elif item.type == "network" %} + {{ network(item, namespace) -}} +{% endif %} +{% endfor %} + end +{%- endmacro -%} + +{# A macro that generates the third level of indentation. #} +{% macro layer3(structure, namespace) %} +{% for item in structure %} +{% if item.type == "string" %} + {{ string(item, namespace) -}} +{% elif item.type == "boolean" %} + {{ boolean(item, namespace) -}} +{% elif item.type == "integer" %} + {{ integer(item, namespace) -}} +{% endif %} +{% endfor %} + end +{%- endmacro -%} + +Vagrant.configure("2") do |config| + +{{ layer1(defs, "config") -}}