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 #289

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dinadins
Copy link
Contributor

@dinadins dinadins commented Dec 21, 2024

Reopen of #277, reproducing here for convenience.

Per the discussion there, the merge conflicts have been resolved, and the linting error seems to be gone.


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

Copy link

Label error. Requires exactly 1 of: bug, enhancement, major, minor, patch, skip-changelog. Found:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant