diff --git a/build-test.sh b/build-test.sh new file mode 100755 index 0000000..6b8c6e9 --- /dev/null +++ b/build-test.sh @@ -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 + diff --git a/builder/parallels/common/artifact.go b/builder/parallels/common/artifact.go index 4290a81..1bddab0 100644 --- a/builder/parallels/common/artifact.go +++ b/builder/parallels/common/artifact.go @@ -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. @@ -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 @@ -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 { @@ -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 } @@ -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 +} diff --git a/builder/parallels/common/artifact_test.go b/builder/parallels/common/artifact_test.go index 780173f..fa25bda 100644 --- a/builder/parallels/common/artifact_test.go +++ b/builder/parallels/common/artifact_test.go @@ -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) } diff --git a/builder/parallels/ipsw/builder.go b/builder/parallels/ipsw/builder.go index 1338aca..c318806 100644 --- a/builder/parallels/ipsw/builder.go +++ b/builder/parallels/ipsw/builder.go @@ -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"` @@ -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) } diff --git a/builder/parallels/iso/builder.go b/builder/parallels/iso/builder.go index 874790b..e3e0731 100644 --- a/builder/parallels/iso/builder.go +++ b/builder/parallels/iso/builder.go @@ -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" @@ -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"` @@ -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) } diff --git a/builder/parallels/macvm/builder.go b/builder/parallels/macvm/builder.go index b910951..364e699 100644 --- a/builder/parallels/macvm/builder.go +++ b/builder/parallels/macvm/builder.go @@ -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. diff --git a/builder/parallels/macvm/config.go b/builder/parallels/macvm/config.go index 7160b70..206d7ba 100644 --- a/builder/parallels/macvm/config.go +++ b/builder/parallels/macvm/config.go @@ -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"` diff --git a/builder/parallels/pvm/builder.go b/builder/parallels/pvm/builder.go index b47bc39..807a683 100644 --- a/builder/parallels/pvm/builder.go +++ b/builder/parallels/pvm/builder.go @@ -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. diff --git a/builder/parallels/pvm/config.go b/builder/parallels/pvm/config.go index 3275920..7e24602 100644 --- a/builder/parallels/pvm/config.go +++ b/builder/parallels/pvm/config.go @@ -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"`