From 906728b229049658dd7e273de8729889077d1af0 Mon Sep 17 00:00:00 2001 From: Dom Del Nano Date: Wed, 13 Jan 2021 22:15:33 -0800 Subject: [PATCH] Upgrade packer and ensure that provisioners work --- build.sh | 47 ------------------- builder/xenserver/common/config.go | 41 ++++++++++++++++ .../config.hcl2spec.go} | 26 +++++----- builder/xenserver/common/ssh.go | 4 +- .../common/step_forward_port_over_ssh.go | 2 +- builder/xenserver/common/step_http_server.go | 8 +++- .../common/step_type_boot_command.go | 4 +- builder/xenserver/common/step_wait_for_ip.go | 2 +- builder/xenserver/iso/builder.go | 40 +++------------- builder/xenserver/iso/step_create_instance.go | 4 +- builder/xenserver/xva/builder.go | 27 ++++------- builder/xenserver/xva/builder_test.go | 25 +++++----- builder/xenserver/xva/step_import_instance.go | 2 +- examples/centos8.json | 1 + examples/http/centos8/ks-centos8.cfg | 10 ---- 15 files changed, 99 insertions(+), 144 deletions(-) delete mode 100755 build.sh create mode 100644 builder/xenserver/common/config.go rename builder/xenserver/{iso/builder.hcl2spec.go => common/config.hcl2spec.go} (96%) diff --git a/build.sh b/build.sh deleted file mode 100755 index 706fbf6..0000000 --- a/build.sh +++ /dev/null @@ -1,47 +0,0 @@ -#!/bin/bash -# -# This script builds the application from source for multiple platforms. -# Adapted from from packer/scripts/build.sh - -# Determine the arch/os combos we're building for -XC_OS=${XC_OS:-$(go env GOOS)} -XC_ARCH=${XC_ARCH:-$(go env GOARCH)} -GOPATH=${GOPATH:-$(go env GOPATH)} - -# Delete the old dir -echo "==> Removing old directory..." -rm -f bin/* -rm -rf pkg/* -# rm -rf $GOPATH/pkg/* -mkdir -p bin/ - -~/go/bin/gox \ - -os="${XC_OS}" \ - -arch="${XC_ARCH}" \ - -output "pkg/{{.OS}}_{{.Arch}}/packer-{{.Dir}}" \ - ./... \ - || exit 1 - -# Move all the compiled things to the $GOPATH/bin -GOPATH=${GOPATH:-$(go env GOPATH)} -case $(uname) in - CYGWIN*) - GOPATH="$(cygpath $GOPATH)" - ;; -esac -OLDIFS=$IFS -IFS=: MAIN_GOPATH=($GOPATH) -IFS=$OLDIFS - -# Copy our OS/Arch to the bin/ directory -echo "==> Copying binaries for this platform..." -DEV_PLATFORM="./pkg/$(go env GOOS)_$(go env GOARCH)" -for F in $(find ${DEV_PLATFORM} -mindepth 1 -maxdepth 1 -type f); do - cp ${F} bin/ - cp ${F} ${MAIN_GOPATH}/bin/ -done - -# Done! -echo -echo "==> Results:" -ls -hl bin/ diff --git a/builder/xenserver/common/config.go b/builder/xenserver/common/config.go new file mode 100644 index 0000000..2736710 --- /dev/null +++ b/builder/xenserver/common/config.go @@ -0,0 +1,41 @@ +//go:generate mapstructure-to-hcl2 -type Config +package common + +import ( + "time" + + "github.com/hashicorp/packer/common" + "github.com/hashicorp/packer/helper/communicator" + "github.com/hashicorp/packer/template/interpolate" +) + +type Config struct { + common.PackerConfig `mapstructure:",squash"` + CommonConfig `mapstructure:",squash"` + Comm communicator.Config `mapstructure:",squash"` + + VCPUsMax uint `mapstructure:"vcpus_max"` + VCPUsAtStartup uint `mapstructure:"vcpus_atstartup"` + VMMemory uint `mapstructure:"vm_memory"` + DiskSize uint `mapstructure:"disk_size"` + CloneTemplate string `mapstructure:"clone_template"` + VMOtherConfig map[string]string `mapstructure:"vm_other_config"` + + ISOChecksum string `mapstructure:"iso_checksum"` + ISOChecksumType string `mapstructure:"iso_checksum_type"` + ISOUrls []string `mapstructure:"iso_urls"` + ISOUrl string `mapstructure:"iso_url"` + ISOName string `mapstructure:"iso_name"` + + PlatformArgs map[string]string `mapstructure:"platform_args"` + + RawInstallTimeout string `mapstructure:"install_timeout"` + InstallTimeout time.Duration `` + SourcePath string `mapstructure:"source_path"` + + ctx interpolate.Context +} + +func (c Config) GetInterpContext() *interpolate.Context { + return &c.ctx +} diff --git a/builder/xenserver/iso/builder.hcl2spec.go b/builder/xenserver/common/config.hcl2spec.go similarity index 96% rename from builder/xenserver/iso/builder.hcl2spec.go rename to builder/xenserver/common/config.hcl2spec.go index 56ada21..768c5ee 100644 --- a/builder/xenserver/iso/builder.hcl2spec.go +++ b/builder/xenserver/common/config.hcl2spec.go @@ -1,15 +1,15 @@ -// Code generated by "mapstructure-to-hcl2 -type config"; DO NOT EDIT. +// Code generated by "mapstructure-to-hcl2 -type Config"; DO NOT EDIT. -package iso +package common import ( "github.com/hashicorp/hcl/v2/hcldec" "github.com/zclconf/go-cty/cty" ) -// Flatconfig is an auto-generated flat version of config. +// FlatConfig is an auto-generated flat version of Config. // Where the contents of a field with a `mapstructure:,squash` tag are bubbled up. -type Flatconfig struct { +type FlatConfig struct { PackerBuildName *string `mapstructure:"packer_build_name" cty:"packer_build_name" hcl:"packer_build_name"` PackerBuilderType *string `mapstructure:"packer_builder_type" cty:"packer_builder_type" hcl:"packer_builder_type"` PackerDebug *bool `mapstructure:"packer_debug" cty:"packer_debug" hcl:"packer_debug"` @@ -106,19 +106,20 @@ type Flatconfig struct { ISOName *string `mapstructure:"iso_name" cty:"iso_name" hcl:"iso_name"` PlatformArgs map[string]string `mapstructure:"platform_args" cty:"platform_args" hcl:"platform_args"` RawInstallTimeout *string `mapstructure:"install_timeout" cty:"install_timeout" hcl:"install_timeout"` + SourcePath *string `mapstructure:"source_path" cty:"source_path" hcl:"source_path"` } -// FlatMapstructure returns a new Flatconfig. -// Flatconfig is an auto-generated flat version of config. +// FlatMapstructure returns a new FlatConfig. +// FlatConfig is an auto-generated flat version of Config. // Where the contents a fields with a `mapstructure:,squash` tag are bubbled up. -func (*config) FlatMapstructure() interface{ HCL2Spec() map[string]hcldec.Spec } { - return new(Flatconfig) +func (*Config) FlatMapstructure() interface{ HCL2Spec() map[string]hcldec.Spec } { + return new(FlatConfig) } -// HCL2Spec returns the hcl spec of a config. -// This spec is used by HCL to read the fields of config. -// The decoded values from this spec will then be applied to a Flatconfig. -func (*Flatconfig) HCL2Spec() map[string]hcldec.Spec { +// HCL2Spec returns the hcl spec of a Config. +// This spec is used by HCL to read the fields of Config. +// The decoded values from this spec will then be applied to a FlatConfig. +func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { s := map[string]hcldec.Spec{ "packer_build_name": &hcldec.AttrSpec{Name: "packer_build_name", Type: cty.String, Required: false}, "packer_builder_type": &hcldec.AttrSpec{Name: "packer_builder_type", Type: cty.String, Required: false}, @@ -216,6 +217,7 @@ func (*Flatconfig) HCL2Spec() map[string]hcldec.Spec { "iso_name": &hcldec.AttrSpec{Name: "iso_name", Type: cty.String, Required: false}, "platform_args": &hcldec.AttrSpec{Name: "platform_args", Type: cty.Map(cty.String), Required: false}, "install_timeout": &hcldec.AttrSpec{Name: "install_timeout", Type: cty.String, Required: false}, + "source_path": &hcldec.AttrSpec{Name: "source_path", Type: cty.String, Required: false}, } return s } diff --git a/builder/xenserver/common/ssh.go b/builder/xenserver/common/ssh.go index b8e5f15..304b763 100644 --- a/builder/xenserver/common/ssh.go +++ b/builder/xenserver/common/ssh.go @@ -160,7 +160,7 @@ func forward(local_conn net.Conn, config *gossh.ClientConfig, server, remote_des return nil } -func ssh_port_forward(local_listener net.Listener, remote_port uint, remote_dest, host, username, password string) error { +func ssh_port_forward(local_listener net.Listener, remote_port int, remote_dest, host, username, password string) error { config := &gossh.ClientConfig{ User: username, @@ -179,7 +179,7 @@ func ssh_port_forward(local_listener net.Listener, remote_port uint, remote_dest } // Forward to a remote port - go forward(local_connection, config, host, remote_dest, remote_port) + go forward(local_connection, config, host, remote_dest, uint(remote_port)) } return nil diff --git a/builder/xenserver/common/step_forward_port_over_ssh.go b/builder/xenserver/common/step_forward_port_over_ssh.go index 01f5d91..0245e4f 100644 --- a/builder/xenserver/common/step_forward_port_over_ssh.go +++ b/builder/xenserver/common/step_forward_port_over_ssh.go @@ -9,7 +9,7 @@ import ( ) type StepForwardPortOverSSH struct { - RemotePort func(state multistep.StateBag) (uint, error) + RemotePort func(state multistep.StateBag) (int, error) RemoteDest func(state multistep.StateBag) (string, error) HostPortMin uint diff --git a/builder/xenserver/common/step_http_server.go b/builder/xenserver/common/step_http_server.go index 7b4557b..fbb972f 100644 --- a/builder/xenserver/common/step_http_server.go +++ b/builder/xenserver/common/step_http_server.go @@ -54,7 +54,9 @@ func (s *StepHTTPServer) Run(ctx context.Context, state multistep.StateBag) mult var httpPort uint = 0 if config.HTTPDir == "" { - state.Put("http_port", httpPort) + // the packer provision steps assert this type is an int + // so this cannot be a uint like the rest of the code + state.Put("http_port", int(httpPort)) return multistep.ActionContinue } @@ -79,7 +81,9 @@ func (s *StepHTTPServer) Run(ctx context.Context, state multistep.StateBag) mult go server.Serve(s.l) // Save the address into the state so it can be accessed in the future - state.Put("http_port", httpPort) + // the packer provision steps assert this type is an int + // so this cannot be a uint like the rest of the code + state.Put("http_port", int(httpPort)) return multistep.ActionContinue } diff --git a/builder/xenserver/common/step_type_boot_command.go b/builder/xenserver/common/step_type_boot_command.go index 81fab49..3fa067e 100644 --- a/builder/xenserver/common/step_type_boot_command.go +++ b/builder/xenserver/common/step_type_boot_command.go @@ -36,7 +36,7 @@ func (self *StepTypeBootCommand) Run(ctx context.Context, state multistep.StateB config := state.Get("commonconfig").(CommonConfig) ui := state.Get("ui").(packer.Ui) c := state.Get("client").(*Connection) - httpPort := state.Get("http_port").(uint) + httpPort := state.Get("http_port").(int) // skip this step if we have nothing to type if len(config.BootCommand) == 0 { @@ -145,7 +145,7 @@ func (self *StepTypeBootCommand) Run(ctx context.Context, state multistep.StateB self.Ctx.Data = &bootCommandTemplateData{ config.VMName, localIp, - httpPort, + uint(httpPort), } ui.Say("Typing boot commands over VNC...") diff --git a/builder/xenserver/common/step_wait_for_ip.go b/builder/xenserver/common/step_wait_for_ip.go index b7dd313..f445e1d 100644 --- a/builder/xenserver/common/step_wait_for_ip.go +++ b/builder/xenserver/common/step_wait_for_ip.go @@ -91,6 +91,6 @@ func InstanceSSHIP(state multistep.StateBag) (string, error) { return ip, nil } -func InstanceSSHPort(state multistep.StateBag) (uint, error) { +func InstanceSSHPort(state multistep.StateBag) (int, error) { return 22, nil } diff --git a/builder/xenserver/iso/builder.go b/builder/xenserver/iso/builder.go index 189beb5..d5ea99f 100644 --- a/builder/xenserver/iso/builder.go +++ b/builder/xenserver/iso/builder.go @@ -1,4 +1,3 @@ -//go:generate mapstructure-to-hcl2 -type config package iso import ( @@ -20,33 +19,8 @@ import ( xscommon "github.com/xenserver/packer-builder-xenserver/builder/xenserver/common" ) -type config struct { - common.PackerConfig `mapstructure:",squash"` - xscommon.CommonConfig `mapstructure:",squash"` - - VCPUsMax uint `mapstructure:"vcpus_max"` - VCPUsAtStartup uint `mapstructure:"vcpus_atstartup"` - VMMemory uint `mapstructure:"vm_memory"` - DiskSize uint `mapstructure:"disk_size"` - CloneTemplate string `mapstructure:"clone_template"` - VMOtherConfig map[string]string `mapstructure:"vm_other_config"` - - ISOChecksum string `mapstructure:"iso_checksum"` - ISOChecksumType string `mapstructure:"iso_checksum_type"` - ISOUrls []string `mapstructure:"iso_urls"` - ISOUrl string `mapstructure:"iso_url"` - ISOName string `mapstructure:"iso_name"` - - PlatformArgs map[string]string `mapstructure:"platform_args"` - - RawInstallTimeout string `mapstructure:"install_timeout"` - InstallTimeout time.Duration `` - - ctx interpolate.Context -} - type Builder struct { - config config + config xscommon.Config runner multistep.Runner } @@ -70,8 +44,8 @@ func (self *Builder) Prepare(raws ...interface{}) (params []string, warns []stri } errs = packer.MultiErrorAppend( - errs, self.config.CommonConfig.Prepare(&self.config.ctx, &self.config.PackerConfig)...) - errs = packer.MultiErrorAppend(errs, self.config.SSHConfig.Prepare(&self.config.ctx)...) + errs, self.config.CommonConfig.Prepare(self.config.GetInterpContext(), &self.config.PackerConfig)...) + errs = packer.MultiErrorAppend(errs, self.config.SSHConfig.Prepare(self.config.GetInterpContext())...) // Set default values @@ -284,7 +258,7 @@ func (self *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (p // }, new(xscommon.StepBootWait), &xscommon.StepTypeBootCommand{ - Ctx: self.config.ctx, + Ctx: *self.config.GetInterpContext(), }, &xscommon.StepWaitForIP{ Chan: httpReqChan, @@ -299,9 +273,9 @@ func (self *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (p }, &communicator.StepConnect{ Config: &self.config.SSHConfig.Comm, - Host: xscommon.CommHost, - SSHConfig: xscommon.SSHConfigFunc(self.config.CommonConfig.SSHConfig), - SSHPort: xscommon.SSHPort, + Host: xscommon.InstanceSSHIP, + SSHConfig: self.config.Comm.SSHConfigFunc(), + SSHPort: xscommon.InstanceSSHPort, }, new(common.StepProvision), new(xscommon.StepShutdown), diff --git a/builder/xenserver/iso/step_create_instance.go b/builder/xenserver/iso/step_create_instance.go index 359e04e..ef7b583 100644 --- a/builder/xenserver/iso/step_create_instance.go +++ b/builder/xenserver/iso/step_create_instance.go @@ -20,7 +20,7 @@ type stepCreateInstance struct { func (self *stepCreateInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { c := state.Get("client").(*xscommon.Connection) - config := state.Get("config").(config) + config := state.Get("config").(xscommon.Config) ui := state.Get("ui").(packer.Ui) ui.Say("Step: Create Instance") @@ -218,7 +218,7 @@ func (self *stepCreateInstance) Run(ctx context.Context, state multistep.StateBa } func (self *stepCreateInstance) Cleanup(state multistep.StateBag) { - config := state.Get("config").(config) + config := state.Get("config").(xscommon.Config) if config.ShouldKeepVM(state) { return } diff --git a/builder/xenserver/xva/builder.go b/builder/xenserver/xva/builder.go index 24a7b0e..31331ae 100644 --- a/builder/xenserver/xva/builder.go +++ b/builder/xenserver/xva/builder.go @@ -6,6 +6,7 @@ import ( "fmt" "time" + "github.com/hashicorp/hcl/v2/hcldec" "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/helper/communicator" hconfig "github.com/hashicorp/packer/helper/config" @@ -16,26 +17,14 @@ import ( xscommon "github.com/xenserver/packer-builder-xenserver/builder/xenserver/common" ) -type config struct { - common.PackerConfig `mapstructure:",squash"` - xscommon.CommonConfig `mapstructure:",squash"` - - SourcePath string `mapstructure:"source_path"` - VCPUsMax uint `mapstructure:"vcpus_max"` - VCPUsAtStartup uint `mapstructure:"vcpus_atstartup"` - VMMemory uint `mapstructure:"vm_memory"` - - PlatformArgs map[string]string `mapstructure:"platform_args"` - - ctx interpolate.Context -} - type Builder struct { - config config + config xscommon.Config runner multistep.Runner } -func (self *Builder) Prepare(raws ...interface{}) (params []string, retErr error) { +func (self *Builder) ConfigSpec() hcldec.ObjectSpec { return self.config.FlatMapstructure().HCL2Spec() } + +func (self *Builder) Prepare(raws ...interface{}) (params []string, warns []string, retErr error) { var errs *packer.MultiError @@ -53,7 +42,7 @@ func (self *Builder) Prepare(raws ...interface{}) (params []string, retErr error } errs = packer.MultiErrorAppend( - errs, self.config.CommonConfig.Prepare(&self.config.ctx, &self.config.PackerConfig)...) + errs, self.config.CommonConfig.Prepare(self.config.GetInterpContext(), &self.config.PackerConfig)...) // Set default values if self.config.VCPUsMax == 0 { @@ -93,7 +82,7 @@ func (self *Builder) Prepare(raws ...interface{}) (params []string, retErr error retErr = errors.New(errs.Error()) } - return nil, retErr + return nil, nil, retErr } @@ -158,7 +147,7 @@ func (self *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (p new(xscommon.StepSetVmHostSshAddress), new(xscommon.StepBootWait), &xscommon.StepTypeBootCommand{ - Ctx: self.config.ctx, + Ctx: *self.config.GetInterpContext(), }, &xscommon.StepWaitForIP{ Chan: httpReqChan, diff --git a/builder/xenserver/xva/builder_test.go b/builder/xenserver/xva/builder_test.go index ad731a2..ce509a6 100644 --- a/builder/xenserver/xva/builder_test.go +++ b/builder/xenserver/xva/builder_test.go @@ -1,8 +1,9 @@ package xva import ( - "github.com/hashicorp/packer/packer" "testing" + + "github.com/hashicorp/packer/packer" ) func testConfig() map[string]interface{} { @@ -30,7 +31,7 @@ func TestBuilder_ImplementsBuilder(t *testing.T) { func TestBuilderPrepare_Defaults(t *testing.T) { var b Builder config := testConfig() - warns, err := b.Prepare(config) + _, warns, err := b.Prepare(config) if len(warns) > 0 { t.Fatalf("bad: %#v", warns) } @@ -61,7 +62,7 @@ func TestBuilderPrepare_Format(t *testing.T) { // Bad config["format"] = "foo" - warns, err := b.Prepare(config) + _, warns, err := b.Prepare(config) if len(warns) > 0 { t.Fatalf("bad: %#v", warns) } @@ -72,7 +73,7 @@ func TestBuilderPrepare_Format(t *testing.T) { // Good config["format"] = "vdi_raw" b = Builder{} - warns, err = b.Prepare(config) + _, warns, err = b.Prepare(config) if len(warns) > 0 { t.Fatalf("bad: %#v", warns) } @@ -88,7 +89,7 @@ func TestBuilderPrepare_HTTPPort(t *testing.T) { // Bad config["http_port_min"] = 1000 config["http_port_max"] = 500 - warns, err := b.Prepare(config) + _, warns, err := b.Prepare(config) if len(warns) > 0 { t.Fatalf("bad: %#v", warns) } @@ -99,7 +100,7 @@ func TestBuilderPrepare_HTTPPort(t *testing.T) { // Bad config["http_port_min"] = -500 b = Builder{} - warns, err = b.Prepare(config) + _, warns, err = b.Prepare(config) if len(warns) > 0 { t.Fatalf("bad: %#v", warns) } @@ -111,7 +112,7 @@ func TestBuilderPrepare_HTTPPort(t *testing.T) { config["http_port_min"] = 500 config["http_port_max"] = 1000 b = Builder{} - warns, err = b.Prepare(config) + _, warns, err = b.Prepare(config) if len(warns) > 0 { t.Fatalf("bad: %#v", warns) } @@ -126,7 +127,7 @@ func TestBuilderPrepare_InvalidKey(t *testing.T) { // Add a random key config["i_should_not_be_valid"] = true - warns, err := b.Prepare(config) + _, warns, err := b.Prepare(config) if len(warns) > 0 { t.Fatalf("bad: %#v", warns) } @@ -141,7 +142,7 @@ func TestBuilderPrepare_KeepVM(t *testing.T) { // Bad config["keep_vm"] = "foo" - warns, err := b.Prepare(config) + _, warns, err := b.Prepare(config) if len(warns) > 0 { t.Fatalf("bad: %#v", warns) } @@ -152,7 +153,7 @@ func TestBuilderPrepare_KeepVM(t *testing.T) { // Good config["keep_vm"] = "always" b = Builder{} - warns, err = b.Prepare(config) + _, warns, err = b.Prepare(config) if len(warns) > 0 { t.Fatalf("bad: %#v", warns) } @@ -167,7 +168,7 @@ func TestBuilderPrepare_SourcePath(t *testing.T) { // Bad config["source_path"] = "" - warns, err := b.Prepare(config) + _, warns, err := b.Prepare(config) if len(warns) > 0 { t.Fatalf("bad: %#v", warns) } @@ -178,7 +179,7 @@ func TestBuilderPrepare_SourcePath(t *testing.T) { // Good config["source_path"] = "." b = Builder{} - warns, err = b.Prepare(config) + _, warns, err = b.Prepare(config) if len(warns) > 0 { t.Fatalf("bad: %#v", warns) } diff --git a/builder/xenserver/xva/step_import_instance.go b/builder/xenserver/xva/step_import_instance.go index ccdadb2..78d4e54 100644 --- a/builder/xenserver/xva/step_import_instance.go +++ b/builder/xenserver/xva/step_import_instance.go @@ -19,7 +19,7 @@ type stepImportInstance struct { func (self *stepImportInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { c := state.Get("client").(*xscommon.Connection) - config := state.Get("config").(config) + config := state.Get("config").(xscommon.Config) ui := state.Get("ui").(packer.Ui) ui.Say("Step: Import Instance") diff --git a/examples/centos8.json b/examples/centos8.json index c0c778b..ba3231e 100644 --- a/examples/centos8.json +++ b/examples/centos8.json @@ -34,6 +34,7 @@ "ssh_username": "vagrant", "ssh_password": "vagrant", "ssh_wait_timeout": "10000s", + "ssh_agent_auth": false, "vm_name": "packer-centos-8.3-x86_64-{{isotime}}", "vm_description": "Build time: {{isotime}}", "keep_vm": "always" diff --git a/examples/http/centos8/ks-centos8.cfg b/examples/http/centos8/ks-centos8.cfg index 5556361..90f8aa2 100644 --- a/examples/http/centos8/ks-centos8.cfg +++ b/examples/http/centos8/ks-centos8.cfg @@ -24,21 +24,11 @@ user --name=vagrant --plaintext --password=vagrant --groups=wheel reboot %packages --ignoremissing --excludedocs -@base -@core -@Development Tools openssh-clients sudo -openssl-devel -readline-devel -zlib-devel -kernel-headers -kernel-devel net-tools -vim wget curl -rsync # unnecessary firmware -aic94xx-firmware