1 From 6ee55c7b7a9e556fc18bbc14144b8ca1ee7be845 Mon Sep 17 00:00:00 2001
2 From: Arnaud Patard <apatard@hupstream.com>
3 Date: Thu, 27 Apr 2023 18:13:56 +0200
4 Subject: [PATCH] src/molecule_plugins/vagrant/modules/vagrant.py: Get rid of
7 To avoid changes in molecule.util breaking the vagrant module, let's
8 get rid of the dependency by:
9 - embedded the recursive dict merge function
10 - replace template handling by our own code.
11 (and keep the autoescaping enabled)
13 Moreover, it will make it easier to use community.vagrant since molecule
14 is not needed anymore.
16 Signed-off-by: Arnaud Patard <apatard@hupstream.com>
18 .../vagrant/modules/vagrant.py | 41 +++++++++++++++----
19 1 file changed, 33 insertions(+), 8 deletions(-)
21 diff --git a/src/molecule_plugins/vagrant/modules/vagrant.py b/src/molecule_plugins/vagrant/modules/vagrant.py
22 index 8232695..b8cb11f 100644
23 --- a/src/molecule_plugins/vagrant/modules/vagrant.py
24 +++ b/src/molecule_plugins/vagrant/modules/vagrant.py
34 +from collections.abc import MutableMapping
37 from ansible.module_utils.basic import AnsibleModule
51 Vagrant.configure('2') do |config|
52 if Vagrant.has_plugin?('vagrant-cachier')
53 {% if cachier is not none and cachier in [ "machine", "box" ] %}
58 +# Taken from molecule.util.
59 +def merge_dicts(a: MutableMapping, b: MutableMapping) -> MutableMapping:
60 + """Merge the values of b into a and returns a new dict.
62 + This function uses the same algorithm as Ansible's `combine(recursive=True)` filter.
64 + :param a: the target dictionary
65 + :param b: the dictionary to import
68 + result = copy.deepcopy(a)
70 + for k, v in b.items():
71 + if k in a and isinstance(a[k], dict) and isinstance(v, dict):
72 + result[k] = merge_dicts(a[k], v)
80 def __init__(self, module) -> None:
82 @@ -548,13 +571,15 @@ def _get_config(self):
84 def _write_vagrantfile(self):
85 instances = self._get_vagrant_config_dict()
86 - template = molecule.util.render_template(
87 - VAGRANTFILE_TEMPLATE,
88 + j_env = jinja2.Environment(autoescape=True)
89 + t = j_env.from_string(VAGRANTFILE_TEMPLATE)
90 + template = t.render(
93 no_kvm=not os.path.exists("/dev/kvm"),
95 - molecule.util.write_file(self._vagrantfile, template)
96 + with open(self._vagrantfile, "w") as f:
99 def _write_configs(self):
100 self._write_vagrantfile()
101 @@ -628,7 +653,7 @@ def _get_instance_vagrant_config_dict(self, instance):
104 d["config_options"].update(
105 - molecule.util.merge_dicts(
108 instance.get("config_options", {}),
110 @@ -640,7 +665,7 @@ def _get_instance_vagrant_config_dict(self, instance):
113 d["provider_options"].update(
114 - molecule.util.merge_dicts(
116 d["provider_options"],
117 instance.get("provider_options", {}),