Skip to content
Draft
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
14 changes: 14 additions & 0 deletions build-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
echo "Starting to build debug version"

if [ ! -d ~/.packer.d ]; then
mkdir ~/.packer.d
fi
if [ ! -d ~/.packer.d/plugins ]; then
mkdir ~/.packer.d/plugins
fi

echo "Building packer-builder-parallels"
go build -o ~/.packer.d/plugins/packer-builder-parallels
echo "Setting the log environment variable"
export PACKER_LOG=1

42 changes: 36 additions & 6 deletions builder/parallels/common/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ package common

import (
"fmt"
"log"
"os"
"path/filepath"
"regexp"

packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/packer/registry/image"
)

// BuilderId is the common builder ID to all of these artifacts.
Expand All @@ -22,8 +24,10 @@ var unnecessaryFiles = []string{"\\.log$", "\\.backup$", "\\.Backup$", "\\.app"}
// Artifact is the result of running the parallels builder, namely a set
// of files associated with the resulting machine.
type artifact struct {
dir string
f []string
APIEndpoint string
SourceImageList string
dir string
f []string

// StateData should store data such as GeneratedData
// to be shared with post-processors
Expand All @@ -32,7 +36,7 @@ type artifact struct {

// NewArtifact returns a Parallels artifact containing the files
// in the given directory.
func NewArtifact(dir string, generatedData map[string]interface{}) (packersdk.Artifact, error) {
func NewArtifact(dir string, generatedData map[string]interface{}, apiEndpoint string, sourceImageList string) (packersdk.Artifact, error) {
files := make([]string, 0, 5)
visit := func(path string, info os.FileInfo, err error) error {
if err != nil {
Expand Down Expand Up @@ -64,9 +68,11 @@ func NewArtifact(dir string, generatedData map[string]interface{}) (packersdk.Ar
}

return &artifact{
dir: dir,
f: files,
StateData: generatedData,
APIEndpoint: apiEndpoint,
SourceImageList: sourceImageList,
dir: dir,
f: files,
StateData: generatedData,
}, nil
}

Expand All @@ -87,9 +93,33 @@ func (a *artifact) String() string {
}

func (a *artifact) State(name string) interface{} {
log.Printf("[TRACE] Artifact State is %s", name)

if name == image.ArtifactStateURI {
return a.buildHCPackerRegistryMetadata()
}
return a.StateData[name]
}

func (a *artifact) Destroy() error {
return os.RemoveAll(a.dir)
}

func (a *artifact) buildHCPackerRegistryMetadata() interface{} {
region := a.APIEndpoint
if region == "" {
region = "local"
}

log.Printf("[TRACE] Region is %s", a.APIEndpoint)
log.Printf("[TRACE] SourceId is %s", a.SourceImageList)

img, err := image.FromArtifact(a, image.WithRegion(region), image.WithSourceID(a.SourceImageList))

if err != nil {
log.Printf("[TRACE] error encountered when creating HCP Packer registry image for artifact: %s", err)
return nil
}

return img
}
2 changes: 1 addition & 1 deletion builder/parallels/common/artifact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestNewArtifact(t *testing.T) {
}

generatedData := map[string]interface{}{"generated_data": "data"}
a, err := NewArtifact(td, generatedData)
a, err := NewArtifact(td, generatedData, "", "")
if err != nil {
t.Fatalf("err: %s", err)
}
Expand Down
4 changes: 3 additions & 1 deletion builder/parallels/ipsw/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ type Builder struct {
}

type Config struct {
APIEndpoint string `mapstructure:"api_endpoint"`
SourceImageList string `mapstructure:"source_image_list"`
common.PackerConfig `mapstructure:",squash"`
commonsteps.HTTPConfig `mapstructure:",squash"`
bootcommand.BootConfig `mapstructure:",squash"`
Expand Down Expand Up @@ -270,5 +272,5 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
}

generatedData := map[string]interface{}{"generated_data": state.Get("generated_data")}
return parallelscommon.NewArtifact(b.config.OutputDir, generatedData)
return parallelscommon.NewArtifact(b.config.OutputDir, generatedData, b.config.APIEndpoint, b.config.SourceImageList)
}
9 changes: 8 additions & 1 deletion builder/parallels/iso/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"context"
"errors"
"fmt"
"log"

parallelscommon "github.com/Parallels/packer-plugin-parallels/builder/parallels/common"
"github.com/hashicorp/hcl/v2/hcldec"
Expand All @@ -32,6 +33,8 @@ type Builder struct {
}

type Config struct {
APIEndpoint string `mapstructure:"api_endpoint"`
SourceImageList string `mapstructure:"source_image_list"`
common.PackerConfig `mapstructure:",squash"`
commonsteps.HTTPConfig `mapstructure:",squash"`
commonsteps.ISOConfig `mapstructure:",squash"`
Expand Down Expand Up @@ -294,5 +297,9 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
}

generatedData := map[string]interface{}{"generated_data": state.Get("generated_data")}
return parallelscommon.NewArtifact(b.config.OutputDir, generatedData)
log.Printf("generatedData: %v", generatedData)
log.Printf("b.config.OutputDir: %v", b.config.OutputDir)
log.Printf("b.config.APIEndpoint: %v", b.config.APIEndpoint)
log.Printf("b.config.SourceImageList: %v", b.config.SourceImageList)
return parallelscommon.NewArtifact(b.config.OutputDir, generatedData, b.config.APIEndpoint, b.config.SourceImageList)
}
2 changes: 1 addition & 1 deletion builder/parallels/macvm/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
}

generatedData := map[string]interface{}{"generated_data": state.Get("generated_data")}
return parallelscommon.NewArtifact(b.config.OutputDir, generatedData)
return parallelscommon.NewArtifact(b.config.OutputDir, generatedData, b.config.APIEndpoint, b.config.SourceImageList)
}

// Cancel.
2 changes: 2 additions & 0 deletions builder/parallels/macvm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (

// Config is the configuration structure for the builder.
type Config struct {
APIEndpoint string `mapstructure:"api_endpoint"`
SourceImageList string `mapstructure:"source_image_list"`
common.PackerConfig `mapstructure:",squash"`
parallelscommon.OutputConfig `mapstructure:",squash"`
parallelscommon.PrlctlConfig `mapstructure:",squash"`
Expand Down
2 changes: 1 addition & 1 deletion builder/parallels/pvm/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
}

generatedData := map[string]interface{}{"generated_data": state.Get("generated_data")}
return parallelscommon.NewArtifact(b.config.OutputDir, generatedData)
return parallelscommon.NewArtifact(b.config.OutputDir, generatedData, b.config.APIEndpoint, b.config.SourceImageList)
}

// Cancel.
2 changes: 2 additions & 0 deletions builder/parallels/pvm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (

// Config is the configuration structure for the builder.
type Config struct {
APIEndpoint string `mapstructure:"api_endpoint"`
SourceImageList string `mapstructure:"source_image_list"`
common.PackerConfig `mapstructure:",squash"`
commonsteps.FloppyConfig `mapstructure:",squash"`
parallelscommon.OutputConfig `mapstructure:",squash"`
Expand Down