Reuse amazon/chroot/step_pre_mount_commands.go

This commit is contained in:
Paul Meyer 2019-05-29 00:40:44 +00:00
parent 45d3f28c67
commit 4d750ddefa
3 changed files with 32 additions and 2 deletions

View File

@ -168,6 +168,14 @@ type Config struct {
ctx interpolate.Context
}
func (c *Config) GetContext() interpolate.Context {
return c.ctx
}
type interpolateContextProvider interface {
GetContext() interpolate.Context
}
type wrappedCommandTemplate struct {
Command string
}

View File

@ -17,7 +17,7 @@ type StepPreMountCommands struct {
}
func (s *StepPreMountCommands) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*Config)
config := state.Get("config").(interpolateContextProvider)
device := state.Get("device").(string)
ui := state.Get("ui").(packer.Ui)
wrappedCommand := state.Get("wrappedCommand").(CommandWrapper)
@ -26,7 +26,7 @@ func (s *StepPreMountCommands) Run(ctx context.Context, state multistep.StateBag
return multistep.ActionContinue
}
ictx := config.ctx
ictx := config.GetContext()
ictx.Data = &preMountCommandsData{Device: device}
ui.Say("Running device setup commands...")

View File

@ -8,6 +8,7 @@ import (
"runtime"
"github.com/Azure/azure-sdk-for-go/profiles/latest/compute/mgmt/compute"
amznchroot "github.com/hashicorp/packer/builder/amazon/chroot"
azcommon "github.com/hashicorp/packer/builder/azure/common"
"github.com/hashicorp/packer/builder/azure/common/client"
"github.com/hashicorp/packer/common"
@ -22,6 +23,9 @@ type Config struct {
FromScratch bool `mapstructure:"from_scratch"`
CommandWrapper string `mapstructure:"command_wrapper"`
PreMountCommands []string `mapstructure:"pre_mount_commands"`
OSDiskSizeGB int32 `mapstructure:"osdisk_size_gb"`
OSDiskStorageAccountType string `mapstructure:"osdisk_storageaccounttype"`
@ -59,6 +63,10 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
errs = packer.MultiErrorAppend(
errs, errors.New("osdisk_size_gb is required with from_scratch."))
}
if len(b.config.PreMountCommands) == 0 {
errs = packer.MultiErrorAppend(
errs, errors.New("pre_mount_commands is required with from_scratch."))
}
}
if err != nil {
@ -74,11 +82,18 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
var azcli client.AzureClientSet
wrappedCommand := func(command string) (string, error) {
ictx := b.config.ctx
ictx.Data = &struct{ Command string }{Command: command}
return interpolate.Render(b.config.CommandWrapper, &ictx)
}
// Setup the state bag and initial state for the steps
state := new(multistep.BasicStateBag)
state.Put("config", &b.config)
state.Put("hook", hook)
state.Put("ui", ui)
state.Put("wrappedCommand", amznchroot.CommandWrapper(wrappedCommand))
info, err := azcli.MetadataClient().GetComputeInfo()
if err != nil {
@ -115,6 +130,13 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
})
}
steps = append(steps,
//&StepAttachDisk{},
&amznchroot.StepPreMountCommands{
Commands: b.config.PreMountCommands,
},
)
// Run!
b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
b.runner.Run(ctx, state)