Skip to content

Commit

Permalink
Allow to force vagrant provider in vagrant up
Browse files Browse the repository at this point in the history
Currently, vagrant is still choosing the provider to use on its own
liking. While it's fine for cases like our CI or people who are fine
with default VM configurations, it's not always the case.

See:
- ansible-community#174
- https://github.com/ansible-community/molecule-vagrant/issues/181

So, add a new configuration setting to call vagrant up with
--provider, in order to force the provider to use and lead to an
error if it's not usable. The default value is to keep current
behaviour to not break things.

Signed-off-by: Arnaud Patard <[email protected]>
  • Loading branch information
apatard committed Sep 5, 2022
1 parent efedef4 commit 8cd95b9
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 5 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ jobs:
platform: ubuntu-latest
skip_vagrant: true
- tox_env: py38,py38-devel
PREFIX: PYTEST_REQPASS=11
PREFIX: PYTEST_REQPASS=12
platform: macos-12
python_version: "3.8"
- tox_env: py39,py39-devel
PREFIX: PYTEST_REQPASS=11
PREFIX: PYTEST_REQPASS=12
platform: macos-12
python_version: "3.9"
- tox_env: py310,py310-devel
PREFIX: PYTEST_REQPASS=11
PREFIX: PYTEST_REQPASS=12
platform: macos-12
python_version: "3.10"
- tox_env: packaging
Expand Down
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ Here's a full example with the libvirt provider:
# Can be any supported provider (virtualbox, parallels, libvirt, etc)
# Defaults to virtualbox
name: libvirt
# Run vagrant up with --provider
force_provider: false
# Run vagrant up with --provision.
# Defaults to --no-provision)
provision: no
Expand Down
10 changes: 8 additions & 2 deletions molecule_vagrant/modules/vagrant.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@
class VagrantClient(object):
def __init__(self, module):
self._module = module
self.provider = self._module.params["provider_name"]
self.provision = self._module.params["provision"]
self.cachier = self._module.params["cachier"]

Expand Down Expand Up @@ -435,8 +436,12 @@ def up(self):
if self._running() != len(self.instances):
changed = True
provision = self.provision
provider = self.provider
try:
self._vagrant.up(provision=provision)
if self._module.params["provider_force"] is True:
self._vagrant.up(provider=provider, provision=provision)
else:
self._vagrant.up(provision=provision)
except Exception:
# NOTE(retr0h): Ignore the exception since python-vagrant
# passes the actual error as a no-argument ContextManager.
Expand Down Expand Up @@ -629,7 +634,7 @@ def _get_instance_vagrant_config_dict(self, instance):
"box_url": instance.get("box_url"),
"box_download_checksum": checksum,
"box_download_checksum_type": checksum_type,
"provider": self._module.params["provider_name"],
"provider": self.provider,
"provider_options": {},
"provider_raw_config_args": instance.get("provider_raw_config_args", None),
"provider_override_args": instance.get("provider_override_args", None),
Expand Down Expand Up @@ -689,6 +694,7 @@ def main():
provider_override_args=dict(type="list", default=None),
provider_raw_config_args=dict(type="list", default=None),
provider_name=dict(type="str", default="virtualbox"),
provider_force=dict(type="bool", default=False),
default_box=dict(type="str", default=None),
provision=dict(type="bool", default=False),
force_stop=dict(type="bool", default=False),
Expand Down
1 change: 1 addition & 0 deletions molecule_vagrant/playbooks/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
instances: "{{ molecule_yml.platforms }}"
default_box: "{{ molecule_yml.driver.default_box | default('generic/alpine310') }}"
provider_name: "{{ molecule_yml.driver.provider.name | default(omit, true) }}"
provider_force: "{{ molecule_yml.driver.force_provider | default(omit) }}"
provision: "{{ molecule_yml.driver.provision | default(omit) }}"
cachier: "{{ molecule_yml.driver.cachier | default(omit) }}"
parallel: "{{ molecule_yml.driver.parallel | default(omit) }}"
Expand Down
14 changes: 14 additions & 0 deletions molecule_vagrant/test/functional/test_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ def test_invalide_settings(temp_dir):
assert "Failed to validate generated Vagrantfile" in result.stdout


def test_provider_force(temp_dir):

scenario_directory = os.path.join(
os.path.dirname(util.abs_path(__file__)), os.path.pardir, "scenarios"
)

with change_dir_to(scenario_directory):
cmd = ["molecule", "create", "--scenario-name", "provider_force"]
result = run_command(cmd)
assert result.returncode == 2

assert "The provider 'vmware' could not be found" in result.stdout


@pytest.mark.parametrize(
"scenario",
[
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
- name: Converge
hosts: all
gather_facts: false
become: true
tasks:
- name: Sample task # noqa command-instead-of-shell
ansible.builtin.shell:
cmd: uname
warn: false
changed_when: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
dependency:
name: galaxy
driver:
name: vagrant
provider:
name: vmware
force_provider: true
platforms:
- name: instance
provisioner:
name: ansible

0 comments on commit 8cd95b9

Please sign in to comment.