Skip to content

fix post-processor for windows#76

Merged
lbajolet-hashicorp merged 2 commits intohashicorp:mainfrom
zijpn:main
Jan 9, 2023
Merged

fix post-processor for windows#76
lbajolet-hashicorp merged 2 commits intohashicorp:mainfrom
zijpn:main

Conversation

@zijpn
Copy link
Contributor

@zijpn zijpn commented Jan 6, 2023

I'm building a ubuntu 22.04 vagrant box for libvirt provider. The packer config is the one from https://github.com/lavabit/robox/blob/master/generic-libvirt.json.

This works fine when building on a linux host (which is probably the most common case). However, when building on a windows host this plugin fails to include the qcow2 image (renamed to box.img) in the box. I'm using Stefan Weil's QEMU for Windows distribution, which is rather slow, but it does produce a valid qcow2 image.

I've traced this down to be a problem with "/" that actually is a "\" when doing the build on windows.
My fix is rather optimistic and simply removes the slash for the suffix test.

I'm using packer v1.8.5

@zijpn zijpn requested a review from a team as a code owner January 6, 2023 11:03
@hashicorp-cla
Copy link

hashicorp-cla commented Jan 6, 2023

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

@lbajolet-hashicorp lbajolet-hashicorp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix looks good to me, I think we can use filepath.Base to ensure the full base name is really the name of the disk. This would avoid a potential problem if we have a path that results in something with a suffix that matches, but with an extra prefix.

Other than that, good catch!

// Copy the disk image into the temporary directory (as box.img)
for _, path := range artifact.Files() {
if strings.HasSuffix(path, "/"+diskName) {
if strings.HasSuffix(path, diskName) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could use filename.Base here instead of HasSuffix? Since we're expecting the name of the disk to be the last part of the path, we can use this and do an equality check on that.

Suggested change
if strings.HasSuffix(path, diskName) {
if filepath.Base(path) == diskName {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion. I tried it out and made another commit.

Copy link
Contributor

@lbajolet-hashicorp lbajolet-hashicorp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the fix!

@lbajolet-hashicorp lbajolet-hashicorp merged commit 84305ce into hashicorp:main Jan 9, 2023
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.

3 participants