hashicorp / hashicorp/packer-plugin-vagrant

Invalid Vagrantfile with local vagrant cloud repository

Open
#117 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Go
Stars
16
Forks
28
PR merge metrics
No merged PRs in 30d

Description

#### 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](https://jfrog.com/help/r/jfrog-artifactory-documentation/vagrant-repositories) 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:///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:///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](https://github.com/hashicorp/packer-plugin-vagrant/blob/main/builder/vagrant/step_create_vagrantfile.go), `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"]
}
```

Contributor guide

Open the contributing guide

Research direction

Start with builder/vagrant/step_create_vagrantfile.go and inspect how BoxName and the source URL are rendered into the generated Vagrantfile. Reproduce the build with the provided local repository configuration, then verify that the generated source definition uses the box name and that the local repository build completes without the name-mismatch error.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.