Skip to content
This repository has been archived by the owner on Jan 6, 2023. It is now read-only.

Commit

Permalink
Avoid depending on molecule.config (#58)
Browse files Browse the repository at this point in the history
Since molecule commit "Avoid parallel execution of a specific schenario"
(65ccd6b), the molecule scenario directory is protected by a lock.
Which is fine. Except that the vagrant module is using molecule.config.Config
to get:
- the molecule ephemeral directory,
- path to the Vagrantfile.

This will result in the vagrant module trying again to get the lock help
by molecule and fails with messages like:
Retrying to acquire lock on /home/zuul/.cache/molecule/scenarios/config_options, waiting for 30 seconds
Retrying to acquire lock on /home/zuul/.cache/molecule/scenarios/config_options, waiting for 60 seconds
Retrying to acquire lock on /home/zuul/.cache/molecule/scenarios/config_options, waiting for 90 seconds
Retrying to acquire lock on /home/zuul/.cache/molecule/scenarios/config_options, waiting for 120 seconds
Timedout trying to acquire lock on /home/zuul/.cache/molecule/scenarios/config_options

As a solution, add a new parameter to the module, called 'workdir', used
to set the vagrant working directory, which is in reality what's the
path used by the module to store its files and this path is obviously
the molecule ephemeral directory in case of molecule.
So, if this parameter is not set, it will defaults to the molecule
ephemeral directory.

Signed-off-by: Arnaud Patard <apatard@hupstream.com>
  • Loading branch information
apatard committed Oct 18, 2020
1 parent 5b15459 commit 80b6c3e
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions molecule_vagrant/modules/vagrant.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import sys

import molecule
import molecule.config
import molecule.util

try:
Expand Down Expand Up @@ -136,6 +135,11 @@
required: True
choices: ['up', 'halt', 'destroy']
default: None
workdir:
description:
- vagrant working directory
required: False
default: content of MOLECULE_EPHEMERAL_DIRECTORY environment variable
requirements:
- python >= 2.6
- python-vagrant
Expand Down Expand Up @@ -385,7 +389,7 @@ def __init__(self, module):
self._module = module

self._config = self._get_config()
self._vagrantfile = self._config.driver.vagrantfile
self._vagrantfile = self._config["vagrantfile"]
self._vagrant = self._get_vagrant()
self._write_configs()
self._has_error = None
Expand Down Expand Up @@ -497,20 +501,23 @@ def _created(self):
return {}

def _get_config(self):
molecule_file = os.environ["MOLECULE_FILE"]

return molecule.config.Config(molecule_file)
conf = dict()
conf["workdir"] = os.getenv("MOLECULE_EPHEMERAL_DIRECTORY")
if self._module.params["workdir"] is not None:
conf["workdir"] = self._module.params["workdir"]
conf["vagrantfile"] = os.path.join(conf["workdir"], "Vagrantfile")
conf["vagrantfile_config"] = os.path.join(conf["workdir"], "vagrant.yml")
return conf

def _write_vagrantfile(self):
template = molecule.util.render_template(
VAGRANTFILE_TEMPLATE,
vagrantfile_config=self._config.driver.vagrantfile_config,
VAGRANTFILE_TEMPLATE, vagrantfile_config=self._config["vagrantfile_config"]
)
molecule.util.write_file(self._vagrantfile, template)

def _write_vagrantfile_config(self, data):
molecule.util.write_file(
self._config.driver.vagrantfile_config, molecule.util.safe_dump(data)
self._config["vagrantfile_config"], molecule.util.safe_dump(data)
)

def _write_configs(self):
Expand All @@ -521,7 +528,7 @@ def _get_vagrant(self):
v = vagrant.Vagrant(
out_cm=self.stdout_cm,
err_cm=self.stderr_cm,
root=os.environ["MOLECULE_EPHEMERAL_DIRECTORY"],
root=self._config["workdir"],
)

return v
Expand Down Expand Up @@ -586,8 +593,7 @@ def _get_vagrant_log(self, __type):
instance_name = self._module.params["instance_name"]

return os.path.join(
self._config.scenario.ephemeral_directory,
"vagrant-{}.{}".format(instance_name, __type),
self._config["workdir"], "vagrant-{}.{}".format(instance_name, __type)
)


Expand All @@ -610,6 +616,7 @@ def main():
provision=dict(type="bool", default=False),
force_stop=dict(type="bool", default=False),
state=dict(type="str", default="up", choices=["up", "destroy", "halt"]),
workdir=dict(type="str"),
),
supports_check_mode=False,
)
Expand Down

0 comments on commit 80b6c3e

Please sign in to comment.