Upgrade packer and ensure that provisioners work

This commit is contained in:
Dom Del Nano 2021-01-13 22:15:33 -08:00
parent 8526cde728
commit 906728b229
15 changed files with 99 additions and 144 deletions

View File

@ -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/

View File

@ -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
}

View File

@ -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
}

View File

@ -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

View File

@ -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

View File

@ -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
}

View File

@ -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...")

View File

@ -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
}

View File

@ -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),

View File

@ -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
}

View File

@ -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,

View File

@ -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)
}

View File

@ -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")

View File

@ -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"

View File

@ -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