hashicorp / packer-plugin-vagrant

Packer plugin for Vagrant
https://packer.io
Mozilla Public License 2.0
13 stars 24 forks source link

Invalid Vagrantfile with local vagrant cloud repository #117

Open wysiayg opened 3 months ago

wysiayg commented 3 months ago

Overview of the Issue

Maybe I'm missing something obvious here, but if a different Vagrant repository is used with the builder, the generated Vagrantfile does not work. In my case, JFrog's Artifactory is used to host a local box repository. To resolve a box, a URL with the following format: https://{Artifactory URL}/api/vagrant/{vagrantRepoKey}/{boxName} is used and resolves to the latest version. The box is added correctly by packer. However, the created Vagrantfile looks as follows:

Vagrant.configure("2") do |config|
  config.vm.define "source", autostart: false do |source|
    source.vm.box = "https://<local_adress>/artifactory/api/vagrant/vagrant-repo/debian-12.5"
    config.ssh.insert_key = false
  end
  config.vm.define "output" do |output|
    output.vm.box = "debian-12.5"
    output.vm.box_url = "file://package.box"
    config.ssh.insert_key = false
  end
  config.vm.synced_folder ".", "/vagrant", disabled: true
end

This results in the following error messages:

The box you're adding has a name different from the name you
2024/03/20 10:50:52 packer-plugin-vagrant_v1.1.1_x5.0_linux_amd64 plugin: 2024/03/20 10:50:52 [vagrant driver] stderr: requested. For boxes with metadata, you cannot override the name.
2024/03/20 10:50:52 packer-plugin-vagrant_v1.1.1_x5.0_linux_amd64 plugin: 2024/03/20 10:50:52 [vagrant driver] stderr: If you're adding a box using `vagrant box add`, don't specify
2024/03/20 10:50:52 packer-plugin-vagrant_v1.1.1_x5.0_linux_amd64 plugin: 2024/03/20 10:50:52 [vagrant driver] stderr: the `--name` parameter. If the box is being added via a Vagrantfile,
2024/03/20 10:50:52 packer-plugin-vagrant_v1.1.1_x5.0_linux_amd64 plugin: 2024/03/20 10:50:52 [vagrant driver] stderr: change the `config.vm.box` value to match the name below.
2024/03/20 10:50:52 packer-plugin-vagrant_v1.1.1_x5.0_linux_amd64 plugin: 2024/03/20 10:50:52 [vagrant driver] stderr:
2024/03/20 10:50:52 packer-plugin-vagrant_v1.1.1_x5.0_linux_amd64 plugin: 2024/03/20 10:50:52 [vagrant driver] stderr: Requested name: https://<local_adress>/artifactory/api/vagrant/vagrant-repo/debian-12.5
2024/03/20 10:50:52 packer-plugin-vagrant_v1.1.1_x5.0_linux_amd64 plugin: 2024/03/20 10:50:52 [vagrant driver] stderr: Actual name: debian-12.5

Considering how the Vagrantfile is created, source.vm.box = "{{.BoxName}}" would resolve this issue and allows a correct run. As a temporary workaround I provided an adjusted template:

Vagrant.configure("2") do |config|
  config.vm.define "source", autostart: false do |source|
    source.vm.box = "{{.BoxName}}"
    config.ssh.insert_key = {{.InsertKey}}
  end
  config.vm.define "output" do |output|
    output.vm.box = "{{.BoxName}}"
    output.vm.box_url = "file://package.box"
    config.ssh.insert_key = {{.InsertKey}}
  end
  {{ if ne .SyncedFolder "" -}}
        config.vm.synced_folder "{{.SyncedFolder}}", "/vagrant"
  {{- else -}}
        config.vm.synced_folder ".", "/vagrant", disabled: true
  {{- end}}
end

Reproduction Steps

  1. Setup a local vagrant repository
  2. Add a box
  3. Use the Buildfile provided below
  4. Run packer with PACKER_LOG=10 packer build --debug . to see the appropriate error messages.

Plugin and Packer version

Packer v1.9.4 Plugin: v1.1.1_x5.0_linux_amd64

Simplified Packer Buildfile

packer {
  required_version = ">= 1.7.0"
  required_plugins {
    vagrant = {
      version = ">= 1.0.2"
      source  = "github.com/hashicorp/vagrant"
    }
  }
}

source "vagrant" "vm" {
  communicator    = "ssh"
  source_path     = "https://{Artifactory URL}/api/vagrant/{vagrantRepoKey}/{boxName}"
  box_name        = "{boxName}"
  teardown_method = "destroy"
}

build {
  sources = ["source.vagrant.vm"]
}