Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dinadins committed Oct 3, 2024
1 parent 36be097 commit 9bea6fb
Showing 1 changed file with 60 additions and 58 deletions.
118 changes: 60 additions & 58 deletions src/molecule_plugins/vagrant/modules/vagrant.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,27 +177,27 @@
"""

VAGRANTFILE_TEMPLATE = """
{%- macro ruby_format(value) -%}
{%- if value is boolean -%}
{{ value | string | lower }}
{%- elif value is string -%}
{% macro ruby_format(value) %}
{% if value is boolean %}
{{- value | string | lower -}}
{% elif value is string %}
{# "Bug" compat. To be removed later #}
{%- if value[0] == value[-1] and value.startswith(("'", '"')) -%}
{{ value }}
{%- else -%}
"{{ value }}"
{%- endif -%}
{%- else -%}
{{ value }}
{%- endif -%}
{%- endmacro -%}
{%- macro dict2args(dictionary) -%}
{% if value[0] == value[-1] and value.startswith(("'", '"')) %}
{{ value }}
{% else %}
"{{ value }}"
{% endif %}
{% else %}
{{ value }}
{% endif %}
{% endmacro %}
{% macro dict2args(dictionary) %}
{% set sep = joiner(", ") %}
{%- for key, value in dictionary.items() -%}
{{ sep() }}{{ key }}: {{ ruby_format(value) }}
{%- endfor -%}
{%- endmacro -%}
{% for key, value in dictionary.items() -%}
{{ sep() }}{{ key }}: {{ ruby_format(value) | trim }}
{%- endfor %}
{% endmacro -%}
# Ansible managed
Expand All @@ -212,51 +212,53 @@
{% for instance in instances %}
config.vm.define "{{ instance.name }}" do |c|
##
# Box definition
##
c.vm.box = "{{ instance.box }}"
{{ 'c.vm.box_version = "{}"'.format(instance.box_version) | safe if instance.box_version }}
{{ 'c.vm.box_url = "{}"'.format(instance.box_url) | safe if instance.box_url }}
{{ 'c.vm.box_download_checksum = "{}"'.format(instance.box_download_checksum) | safe if instance.box_download_checksum }}
{{ 'c.vm.box_download_checksum_type = "{}"'.format(instance.box_download_checksum_type) | safe if instance.box_download_checksum_type }}
{% if instance.box_version %}
{{ 'c.vm.box_version = "{}"'.format(instance.box_version) | safe }}
{% endif %}
{% if instance.box_url %}
{{ 'c.vm.box_url = "{}"'.format(instance.box_url) | safe }}
{% endif %}
{% if instance.box_download_checksum %}
{{ 'c.vm.box_download_checksum = "{}"'.format(instance.box_download_checksum) | safe }}
{% endif %}
{% if instance.box_download_checksum_type %}
{{ 'c.vm.box_download_checksum_type = "{}"'.format(instance.box_download_checksum_type) | safe }}
{% endif %}
##
# Config options
##
{% if instance.config_options['synced_folder'] is sameas false %}
c.vm.synced_folder ".", "/vagrant", disabled: true
{% endif %}
{% for k,v in instance.config_options.items() %}
{% if k not in ['synced_folder', 'cachier'] %}c.{{ k }} = {{ ruby_format(v) }}{% endif %}
{% if k not in ['synced_folder', 'cachier'] %}
c.{{ k }} = {{ ruby_format(v) }}
{% endif %}
{% endfor %}
{% if instance.hostname is boolean and instance.hostname is sameas false %}
# c.vm.hostname not set
{% elif instance.hostname is string %}
c.vm.hostname = "{{ instance.hostname }}"
{% else %}
c.vm.hostname = "{{ instance.name }}"
{% endif %}
{% if instance.networks | length > 0 %}
##
# Network
##
{% for n in instance.networks %}
c.vm.network "{{ n.name }}", {{ dict2args(n.options) }}
c.vm.network "{{ n.name }}", {{ dict2args(n.options) | trim }}
{% endfor %}
{% endif %}
{% if instance.instance_raw_config_args is not none %}
##
# instance_raw_config_args
##
{% if instance.instance_raw_config_args is not none %}{% for arg in instance.instance_raw_config_args -%}
{% for arg in instance.instance_raw_config_args %}
c.{{ arg | safe }}
{% endfor %}{% endif %}
{% endfor %}
{% endif %}
##
# Provider
##
# Provider options
c.vm.provider "{{ instance.provider }}" do |{{ instance.provider | lower }}, override|
{% if instance.provider == "vsphere" %}
{{ instance.provider | lower }}.memory_mb = {{ instance.memory }}
Expand All @@ -271,27 +273,13 @@
{{ instance.provider | lower }}.memory = {{ instance.memory }}
{{ instance.provider | lower }}.cpus = {{ instance.cpus }}
{% endif %}
{% for option, value in instance.provider_options.items() %}
{{ instance.provider | lower }}.{{ option }} = {{ ruby_format(value) }}
{{ instance.provider | lower }}.{{ option }} = {{ ruby_format(value) | trim }}
{% endfor %}
{% if instance.provider_raw_config_args is not none %}
{% for arg in instance.provider_raw_config_args %}
{{ instance.provider | lower }}.{{ arg | safe }}
{% endfor %}
{% endif %}
{% if instance.provider_override_args is not none %}
{% for arg in instance.provider_override_args -%}
override.{{ arg | safe }}
{% endfor %}
{% endif %}
{% if instance.provider == 'virtualbox' %}
{% if 'linked_clone' not in instance.provider_options %}
{% if 'linked_clone' not in instance.provider_options %}
virtualbox.linked_clone = true
{% endif %}
{% endif %}
{% endif %}
{% if instance.provider == 'libvirt' %}
{% if no_kvm is sameas true and 'driver' not in instance.provider_options %}
Expand All @@ -314,6 +302,20 @@
{% endif %}
{% endif %}
{% endif %}
{% if instance.provider_raw_config_args is not none %}
# provider_raw_config_args
{% for arg in instance.provider_raw_config_args %}
{{ instance.provider | lower }}.{{ arg | safe }}
{% endfor %}
{% endif %}
{% if instance.provider_override_args is not none %}
# provider_override_args
{% for arg in instance.provider_override_args %}
override.{{ arg | safe }}
{% endfor %}
{% endif %}
end
end
{% endfor %}
Expand Down Expand Up @@ -572,7 +574,7 @@ def _get_config(self):

def _write_vagrantfile(self):
instances = self._get_vagrant_config_dict()
j_env = jinja2.Environment(autoescape=True)
j_env = jinja2.Environment(autoescape=True, trim_blocks=True, lstrip_blocks=True)
t = j_env.from_string(VAGRANTFILE_TEMPLATE)
template = t.render(
instances=instances,
Expand Down

0 comments on commit 9bea6fb

Please sign in to comment.