Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src/molecule_plugins/vagrant/modules/vagrant.py: Get rid of molecule dependency #142

Merged
merged 1 commit into from
May 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions src/molecule_plugins/vagrant/modules/vagrant.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@


import contextlib
import copy
import datetime
import os
import subprocess
import sys
from collections.abc import MutableMapping

import jinja2
from ansible.module_utils.basic import AnsibleModule

import molecule
import molecule.util

try:
import vagrant
except ImportError:
Expand Down Expand Up @@ -199,6 +199,8 @@
{%- endfor -%}
{%- endmacro -%}
# Ansible managed
Vagrant.configure('2') do |config|
if Vagrant.has_plugin?('vagrant-cachier')
{% if cachier is not none and cachier in [ "machine", "box" ] %}
Expand Down Expand Up @@ -335,6 +337,27 @@
"""


# Taken from molecule.util.
def merge_dicts(a: MutableMapping, b: MutableMapping) -> MutableMapping:
"""Merge the values of b into a and returns a new dict.
This function uses the same algorithm as Ansible's `combine(recursive=True)` filter.
:param a: the target dictionary
:param b: the dictionary to import
:return: dict
"""
result = copy.deepcopy(a)

for k, v in b.items():
if k in a and isinstance(a[k], dict) and isinstance(v, dict):
result[k] = merge_dicts(a[k], v)
else:
result[k] = v

return result


class VagrantClient:
def __init__(self, module) -> None:
self._module = module
Expand Down Expand Up @@ -548,13 +571,15 @@ def _get_config(self):

def _write_vagrantfile(self):
instances = self._get_vagrant_config_dict()
template = molecule.util.render_template(
VAGRANTFILE_TEMPLATE,
j_env = jinja2.Environment(autoescape=True)
t = j_env.from_string(VAGRANTFILE_TEMPLATE)
template = t.render(
instances=instances,
cachier=self.cachier,
no_kvm=not os.path.exists("/dev/kvm"),
)
molecule.util.write_file(self._vagrantfile, template)
with open(self._vagrantfile, "w") as f:
f.write(template)

def _write_configs(self):
self._write_vagrantfile()
Expand Down Expand Up @@ -628,7 +653,7 @@ def _get_instance_vagrant_config_dict(self, instance):
}

d["config_options"].update(
molecule.util.merge_dicts(
merge_dicts(
d["config_options"],
instance.get("config_options", {}),
),
Expand All @@ -640,7 +665,7 @@ def _get_instance_vagrant_config_dict(self, instance):
)

d["provider_options"].update(
molecule.util.merge_dicts(
merge_dicts(
d["provider_options"],
instance.get("provider_options", {}),
),
Expand Down