Skip to content

Support Packer 1.7.1 #8

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Change log

## [Unreleased]

### Fixed

* Updated internal package interface for Packer 1.4.0. To use this plugin with older versions of Packer, download the 0.3.0 release.
* Fixed typo in `keep_input_artifact` configuration key.

## [0.3.0 (2015-09-26)][0.3.0]

### Added

* Added support for VirtualBox OVA archives.

[Unreleased]: https://github.com/benwebber/packer-post-processor-vhd/compare/v0.3.0..HEAD
[0.3.0]: https://github.com/benwebber/packer-post-processor-vhd/releases/tag/v0.3.0
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ help:
@echo "release push tags and binaries upstream"

.depend:
go get -d github.com/hashicorp/packer
go mod download

clean:
go clean -x
Expand Down
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/benwebber/packer-post-processor-vhd

go 1.15

require (
github.com/hashicorp/hcl/v2 v2.9.1
github.com/hashicorp/packer v1.7.1
github.com/hashicorp/packer-plugin-sdk v0.1.1
github.com/zclconf/go-cty v1.8.1
)
961 changes: 961 additions & 0 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"github.com/benwebber/packer-post-processor-vhd/vhd"
"github.com/hashicorp/packer/packer/plugin"
"github.com/hashicorp/packer-plugin-sdk/plugin"
)

func main() {
Expand Down
29 changes: 18 additions & 11 deletions vhd/post-processor.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
//go:generate mapstructure-to-hcl2 -type Config
// Package vhd implements the packer.PostProcessor interface and adds a
// post-processor that produces a standalone VHD file.
package vhd

import (
"context"
"fmt"
"os"

"github.com/hashicorp/packer/builder/qemu"
vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
"github.com/hashicorp/packer-plugin-sdk/common"
"github.com/hashicorp/packer-plugin-sdk/template/config"
"github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
"github.com/hashicorp/hcl/v2/hcldec"
)

// Map Builders to Providers: these are the types of artifacts we know how to
Expand All @@ -29,7 +32,7 @@ type Config struct {
OutputPath string `mapstructure:"output"`

// Whether to keep the Provider artifact (e.g., VirtualBox VMDK).
KeepInputArtifact bool `mapstructure:"keep_input_artifict"`
KeepInputArtifact bool `mapstructure:"keep_input_artifact"`

// Whether to overwrite the VHD if it exists.
Force bool `mapstructure:"force"`
Expand All @@ -50,6 +53,10 @@ type outputPathTemplate struct {
Provider string
}

func (p *PostProcessor) ConfigSpec() hcldec.ObjectSpec {
return p.config.FlatMapstructure().HCL2Spec()
}

// Configure the PostProcessor, rendering templated values if necessary.
func (p *PostProcessor) Configure(raws ...interface{}) error {
err := config.Decode(&p.config, &config.DecodeOpts{
Expand All @@ -71,10 +78,10 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {

// PostProcess is the main entry point. It calls a Provider's Convert() method
// to delegate conversion to that Provider's command-line tool.
func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, bool, error) {
provider, err := providerForBuilderId(artifact.BuilderId())
if err != nil {
return nil, false, err
return nil, false, false, err
}

ui.Say(fmt.Sprintf("Converting %s image to VHD file...", provider))
Expand All @@ -87,7 +94,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
}
outputPath, err := interpolate.Render(p.config.OutputPath, &p.config.ctx)
if err != nil {
return nil, false, err
return nil, false, false, err
}

// Check if VHD file exists. Remove if the user specified `force` in the
Expand All @@ -100,20 +107,20 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
ui.Message(fmt.Sprintf("Removing existing VHD file at %s", outputPath))
os.Remove(outputPath)
} else {
return nil, false, fmt.Errorf("VHD file exists: %s\nUse the force flag to delete it.", outputPath)
return nil, false, false, fmt.Errorf("VHD file exists: %s\nUse the force flag to delete it.", outputPath)
}
}

err = provider.Convert(ui, artifact, outputPath)
if err != nil {
return nil, false, err
return nil, false, false, err
}

ui.Say(fmt.Sprintf("Converted VHD: %s", outputPath))
artifact = NewArtifact(provider.String(), outputPath)
keep := p.config.KeepInputArtifact

return artifact, keep, nil
return artifact, keep, false, nil
}

// Pick a provider to use from known builder sources.
Expand Down
51 changes: 51 additions & 0 deletions vhd/post-processor.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vhd/post-processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package vhd
import (
"testing"

"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer-plugin-sdk/packer"
)

func TestPostProcessor_ImplementsPostProcessor(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion vhd/provider.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package vhd

import "github.com/hashicorp/packer/packer"
import "github.com/hashicorp/packer-plugin-sdk/packer"

// A Provider wraps logic necessary to convert specific builder artifacts to
// VHD.
Expand Down
2 changes: 1 addition & 1 deletion vhd/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"

"github.com/hashicorp/packer/builder/qemu"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer-plugin-sdk/packer"
)

// QEMUProvider satisfies the Provider interface.
Expand Down
2 changes: 1 addition & 1 deletion vhd/virtualbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"

vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer/post-processor/vagrant"
)

Expand Down