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

Vagrant: Reducing whitespace in Vagrantfile #277

Closed
wants to merge 0 commits into from

Conversation

dinadins
Copy link
Contributor

@dinadins dinadins commented Oct 3, 2024

The current implementation in src/molecule_plugins/vagrant/modules/vagrant.py generates a Vagrantfile with excessive white space (a total of 42 empty lines with the molecule.yml sample in driver.py), and some indentation issues for the overwrite entries, making troubleshooting less pleasant. The proposed changes greatly reduce blank lines and keep the Vagrantfile in a more easily readable form.

I have first attempted to clear white space by {%- / -%}, however this proved challenging with indentation and other desired whitespace (separating blocks of code in the Vagrantfile). Eventually I went with adding trim_blocks=True, lstrip_blocks=True to the jinja2.Environment invocation and reworking the template. I have tried various forms of input in molecule.yml and get consistent spacing in Vagrantfile (see some examples below).

All Vagrant related tests pass (box_url modified as per #274 / #275) and I have verified every Vagrantfile produced by the tests has no spurious empty lines or uneven indentation.

Examples:

The example from #276:

molecule.yml:

driver:
  name: vagrant
platforms:
  - name: instance-1
    instance_raw_config_args:
      - "vm.network 'forwarded_port', guest: 80, host: 8080"
    interfaces:
      - auto_config: true
        network_name: private_network
        type: dhcp
    config_options:
      synced_folder: True
    box: debian/jessie64
    memory: 1024
    cpus: 1
    provider_options:
      gui: True
    provider_raw_config_args:
      - "customize ['modifyvm', :id, '--cpuexecutioncap', '50']"
    provider_override_args:
      - "vm.synced_folder './', '/vagrant', disabled: true, type: 'nfs'"
    provision: True

Vagrantfile:

# Ansible managed

Vagrant.configure('2') do |config|
  if Vagrant.has_plugin?('vagrant-cachier')
    config.cache.scope = 'machine'
  end

  config.vm.define "instance-1" do |c|
    # Box definition
    c.vm.box = "debian/jessie64"

    # Config options
    c.ssh.insert_key = true
    c.vm.hostname = "instance-1"

    # Network
    c.vm.network "private_network", auto_config: true, type: "dhcp"

    # instance_raw_config_args
    c.vm.network 'forwarded_port', guest: 80, host: 8080

    # Provider options
    c.vm.provider "virtualbox" do |virtualbox, override|
      virtualbox.memory = 1024
      virtualbox.cpus = 1
      virtualbox.gui = true
      virtualbox.linked_clone = true

      # provider_raw_config_args
      virtualbox.customize ['modifyvm', :id, '--cpuexecutioncap', '50']

      # provider_override_args
      override.vm.synced_folder './', '/vagrant', disabled: true, type: 'nfs'
    end
  end
end

A very basic config:

molecule.yml:

---
driver:
  name: vagrant
platforms:
  - name: instance-1
    box: debian/jessie64

Vagrantfile:

# Ansible managed

Vagrant.configure('2') do |config|
  if Vagrant.has_plugin?('vagrant-cachier')
    config.cache.scope = 'machine'
  end

  config.vm.define "instance-1" do |c|
    # Box definition
    c.vm.box = "debian/jessie64"

    # Config options
    c.vm.synced_folder ".", "/vagrant", disabled: true
    c.ssh.insert_key = true
    c.vm.hostname = "instance-1"

    # Provider options
    c.vm.provider "virtualbox" do |virtualbox, override|
      virtualbox.memory = 512
      virtualbox.cpus = 2
      virtualbox.linked_clone = true
    end
  end
end

molecule.yml:

---
driver:
  name: vagrant
platforms:
  - name: instance-1
    box: debian/jessie64
    box_version: 10.1
    box_url: http://repo.example.com/images/postmerge/debian.json
    box_download_checksum: 6d016aa287990152548f0a414048c2edeea7f84b48293cab21506f86649f79b8
    box_download_checksum_type: sha256
    instance_raw_config_args:
      - "vm.network 'forwarded_port', guest: 80, host: 8080"
      - "vm.network 'forwarded_port', guest: 90, host: 9090"
    interfaces:
      - auto_config: true
        network_name: private_network
        type: dhcp
      - network_name: private_network
        ip: "192.168.46.80"
    config_options:
      synced_folder: True
    memory: 1024
    cpus: 1
    provider_options:
      gui: True
    provider_raw_config_args:
      - "customize ['modifyvm', :id, '--cpuexecutioncap', '50']"
      - "customize ['modifyvm', :id, '--cpuexecutioncap', 'BLAH']"
    provider_override_args:
      - "vm.synced_folder './', '/vagrant', disabled: true, type: 'nfs'"
      - "vm.synced_folder '~/Downloads', '/kit', disabled: false, type: 'virtualbox'"

Vagrantfile:

# Ansible managed

Vagrant.configure('2') do |config|
  if Vagrant.has_plugin?('vagrant-cachier')
    config.cache.scope = 'machine'
  end

  config.vm.define "instance-1" do |c|
    # Box definition
    c.vm.box = "debian/jessie64"
    c.vm.box_version = "10.1"
    c.vm.box_url = "http://repo.example.com/images/postmerge/debian.json"
    c.vm.box_download_checksum = "6d016aa287990152548f0a414048c2edeea7f84b48293cab21506f86649f79b8"
    c.vm.box_download_checksum_type = "sha256"

    # Config options
    c.ssh.insert_key = true
    c.vm.hostname = "instance-1"

    # Network
    c.vm.network "private_network", auto_config: true, type: "dhcp"
    c.vm.network "private_network", ip: "192.168.46.80"

    # instance_raw_config_args
    c.vm.network 'forwarded_port', guest: 80, host: 8080
    c.vm.network 'forwarded_port', guest: 90, host: 9090

    # Provider options
    c.vm.provider "virtualbox" do |virtualbox, override|
      virtualbox.memory = 1024
      virtualbox.cpus = 1
      virtualbox.gui = true
      virtualbox.linked_clone = true

      # provider_raw_config_args
      virtualbox.customize ['modifyvm', :id, '--cpuexecutioncap', '50']
      virtualbox.customize ['modifyvm', :id, '--cpuexecutioncap', 'BLAH']

      # provider_override_args
      override.vm.synced_folder './', '/vagrant', disabled: true, type: 'nfs'
      override.vm.synced_folder '~/Downloads', '/kit', disabled: false, type: 'virtualbox'
    end
  end
end

libvirt:

molecule.yml:

---
dependency:
  name: galaxy
driver:
  name: vagrant
  provider:
    name: libvirt
platforms:
  - name: instance
    config_options:
      synced_folder: true
    box: ${TESTBOX:-centos/7}
    instance_raw_config_args:
      - 'vm.synced_folder ".", "/vagrant", type: "rsync"'
provisioner:
  name: ansible
verifier:
  name: ansible

Vagrantfile:

# Ansible managed

Vagrant.configure('2') do |config|
  if Vagrant.has_plugin?('vagrant-cachier')
    config.cache.scope = 'machine'
  end

  config.vm.define "instance" do |c|
    # Box definition
    c.vm.box = "centos/7"

    # Config options
    c.ssh.insert_key = true
    c.vm.hostname = "instance"

    # instance_raw_config_args
    c.vm.synced_folder ".", "/vagrant", type: "rsync"

    # Provider options
    c.vm.provider "libvirt" do |libvirt, override|
      libvirt.memory = 512
      libvirt.cpus = 2
    end
  end
end

@dinadins
Copy link
Contributor Author

dinadins commented Oct 3, 2024

pytest tests are not run for Vagrant in CICD, here's my results (Ubuntu 22.04):

$ pytest test/vagrant-plugin/functional/test_func.py
============================================= test session starts ==============================================
platform linux -- Python 3.10.12, pytest-8.3.3, pluggy-1.5.0 -- /usr/bin/python3
cachedir: .pytest_cache
metadata: {'Python': '3.10.12', 'Platform': 'Linux-6.8.0-45-generic-x86_64-with-glibc2.35', 'Packages': {'pytest': '8.3.3', 'pluggy': '1.5.0'}, 'Plugins': {'helpers-namespace': '2021.12.29', 'metadata': '3.1.1', 'html': '4.1.1'}}
rootdir: /home/xxxxxxxx/github/molecule-plugins
configfile: pytest.ini
plugins: helpers-namespace-2021.12.29, metadata-3.1.1, html-4.1.1
collected 11 items                                                                                             

test/vagrant-plugin/functional/test_func.py::test_vagrant_command_init_scenario PASSED                   [  9%]
test/vagrant-plugin/functional/test_func.py::test_invalid_settings PASSED                                [ 18%]
test/vagrant-plugin/functional/test_func.py::test_vagrant_root[vagrant_root] PASSED                      [ 27%]
test/vagrant-plugin/functional/test_func.py::test_vagrant_root[config_options] PASSED                    [ 36%]
test/vagrant-plugin/functional/test_func.py::test_vagrant_root[provider_config_options] PASSED           [ 45%]
test/vagrant-plugin/functional/test_func.py::test_vagrant_root[default] PASSED                           [ 54%]
test/vagrant-plugin/functional/test_func.py::test_vagrant_root[default-compat] PASSED                    [ 63%]
test/vagrant-plugin/functional/test_func.py::test_vagrant_root[box_url] PASSED                           [ 72%]
test/vagrant-plugin/functional/test_func.py::test_vagrant_root[network] PASSED                           [ 81%]
test/vagrant-plugin/functional/test_func.py::test_vagrant_root[hostname] PASSED                          [ 90%]
test/vagrant-plugin/functional/test_func.py::test_multi_node PASSED                                      [100%]

============================================= slowest 10 durations =============================================
489.41s call     test/vagrant-plugin/functional/test_func.py::test_multi_node
406.95s call     test/vagrant-plugin/functional/test_func.py::test_vagrant_command_init_scenario
84.91s call     test/vagrant-plugin/functional/test_func.py::test_vagrant_root[hostname]
68.00s call     test/vagrant-plugin/functional/test_func.py::test_vagrant_root[default]
62.89s call     test/vagrant-plugin/functional/test_func.py::test_vagrant_root[default-compat]
56.14s call     test/vagrant-plugin/functional/test_func.py::test_vagrant_root[config_options]
49.74s call     test/vagrant-plugin/functional/test_func.py::test_vagrant_root[vagrant_root]
49.58s call     test/vagrant-plugin/functional/test_func.py::test_vagrant_root[provider_config_options]
49.10s call     test/vagrant-plugin/functional/test_func.py::test_vagrant_root[network]
47.46s call     test/vagrant-plugin/functional/test_func.py::test_vagrant_root[box_url]
======================================= 11 passed in 1368.30s (0:22:48) ========================================

@apatard
Copy link
Member

apatard commented Dec 20, 2024

Can you please rebase and look at the lint issue (if still there after rebase) ?

@dinadins
Copy link
Contributor Author

Not sure how I got this closed, I'll just open a new PR later when I get my machine in order.

@dinadins
Copy link
Contributor Author

Reopened as #289

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request vagrant
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants