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

Avoid depending on molecule.config #58

Merged
merged 1 commit into from
Oct 18, 2020
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
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