# Generated by Cyber Sandbox Creator # https://gitlab.ics.muni.cz/muni-kypo-csc/cyber-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| {{ layer3(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 general items (not str int or bool) #} {% macro other(item, namespace) %} {% if item.separator %} {{ namespace }}.{{ item.command }} {{ item.separator }} {{ item.value }} {% else %} {{ namespace }}.{{ item.command }} {{ item.value }} {% endif %} {% endmacro -%} {# Macro for dictionaries #} {% macro dictionary(item, namespace) %} {{ namespace }}.{{ item.command }} = { {% for key, value in item.dictionary.items() %} {% if loop.last %} {{ key }}: {{ value }} {% else %} {{ key }}: {{ value }}, {% endif %} {% endfor %} } {% endmacro -%} {# Macro for group dictionaries #} {% macro group_dict(item, namespace) %}{ {% for key, value in item.items() %} {% if loop.last %} "{{ key }}" => {{ value }} {% else %} "{{ key }}" => {{ value }}, {% endif %} {% endfor %} }{% endmacro -%} {# Macro for dictionaries #} {% macro groups(item, namespace) %} {{ namespace }}.groups = { {% for key, value in item.groups.items() %} {% if value is mapping %} "{{ key }}" => {{ group_dict(value, namespace) }} {% else %} {% if loop.last %} "{{ key }}" => {{ value }} {% else %} "{{ key }}" => {{ value }}, {% endif %} {% endif %} {% endfor %} } {% 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) }} {% 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 == "other" %} {{ other(item, namespace) -}} {% elif item.type == "provider" %} {{ provider(item, namespace) -}} {% elif item.type == "network" %} {{ network(item, namespace) -}} {% elif item.type == "dictionary" %} {{ dictionary(item, namespace) -}} {% elif item.type == "groups" %} {{ groups(item, namespace) -}} {% elif item.type == "provision" %} {{ provision(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) -}} {% elif item.type == "dictionary" %} {{ dictionary(item, namespace) -}} {% elif item.type == "groups" %} {{ groups(item, namespace) -}} {% elif item.type == "other" %} {{ other(item, namespace) -}} {% endif %} {% endfor %} end {%- endmacro -%} Vagrant.configure("2") do |config| {{ layer1(defs, "config") -}}