Merge pull request #8269 from paulmey/paulmey/generalize-chroot

[amazon/chroot] Move common/generic chroot builder steps to common directory
This commit is contained in:
Megan Marsh 2019-10-24 14:32:01 -07:00 committed by GitHub
commit a783a09ea3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 79 additions and 51 deletions

View File

@ -15,6 +15,7 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"
awscommon "github.com/hashicorp/packer/builder/amazon/common"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/common/chroot"
"github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
@ -173,10 +174,6 @@ func (c *Config) GetContext() interpolate.Context {
return c.ctx
}
type interpolateContextProvider interface {
GetContext() interpolate.Context
}
type wrappedCommandTemplate struct {
Command string
}
@ -356,7 +353,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
state.Put("awsSession", session)
state.Put("hook", hook)
state.Put("ui", ui)
state.Put("wrappedCommand", CommandWrapper(wrappedCommand))
state.Put("wrappedCommand", common.CommandWrapper(wrappedCommand))
// Build the steps
steps := []multistep.Step{
@ -391,24 +388,24 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
},
&StepAttachVolume{},
&StepEarlyUnflock{},
&StepPreMountCommands{
&chroot.StepPreMountCommands{
Commands: b.config.PreMountCommands,
},
&StepMountDevice{
MountOptions: b.config.MountOptions,
MountPartition: b.config.MountPartition,
},
&StepPostMountCommands{
&chroot.StepPostMountCommands{
Commands: b.config.PostMountCommands,
},
&StepMountExtra{
&chroot.StepMountExtra{
ChrootMounts: b.config.ChrootMounts,
},
&StepCopyFiles{
&chroot.StepCopyFiles{
Files: b.config.CopyFiles,
},
&StepChrootProvision{},
&StepEarlyCleanup{},
&chroot.StepChrootProvision{},
&chroot.StepEarlyCleanup{},
&StepSnapshot{},
&awscommon.StepDeregisterAMI{
AccessConfig: &b.config.AccessConfig,

View File

@ -6,6 +6,8 @@ import (
"os"
"runtime"
"testing"
"github.com/hashicorp/packer/common"
)
func TestCopyFile(t *testing.T) {
@ -26,7 +28,7 @@ func TestCopyFile(t *testing.T) {
}
first.Sync()
cmd := ShellCommand(fmt.Sprintf("cp %s %s", first.Name(), newName))
cmd := common.ShellCommand(fmt.Sprintf("cp %s %s", first.Name(), newName))
if err := cmd.Run(); err != nil {
t.Fatalf("Couldn't copy file")
}

View File

@ -1,11 +1,15 @@
package chroot
import "testing"
import (
"testing"
"github.com/hashicorp/packer/common/chroot"
)
func TestAttachVolumeCleanupFunc_ImplementsCleanupFunc(t *testing.T) {
var raw interface{}
raw = new(StepAttachVolume)
if _, ok := raw.(Cleanup); !ok {
if _, ok := raw.(chroot.Cleanup); !ok {
t.Fatalf("cleanup func should be a CleanupFunc")
}
}

View File

@ -5,6 +5,7 @@ import (
"fmt"
"log"
"github.com/hashicorp/packer/common/chroot"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
)
@ -13,7 +14,7 @@ import (
type StepEarlyUnflock struct{}
func (s *StepEarlyUnflock) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
cleanup := state.Get("flock_cleanup").(Cleanup)
cleanup := state.Get("flock_cleanup").(chroot.Cleanup)
ui := state.Get("ui").(packer.Ui)
log.Println("Unlocking file lock...")

View File

@ -1,11 +1,15 @@
package chroot
import "testing"
import (
"testing"
"github.com/hashicorp/packer/common/chroot"
)
func TestFlockCleanupFunc_ImplementsCleanupFunc(t *testing.T) {
var raw interface{}
raw = new(StepFlock)
if _, ok := raw.(Cleanup); !ok {
if _, ok := raw.(chroot.Cleanup); !ok {
t.Fatalf("cleanup func should be a CleanupFunc")
}
}

View File

@ -10,6 +10,7 @@ import (
"strings"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
@ -39,7 +40,7 @@ func (s *StepMountDevice) Run(ctx context.Context, state multistep.StateBag) mul
// customizable device path for mounting NVME block devices on c5 and m5 HVM
device = config.NVMEDevicePath
}
wrappedCommand := state.Get("wrappedCommand").(CommandWrapper)
wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper)
var virtualizationType string
if config.FromScratch || config.AMIVirtType != "" {
@ -104,7 +105,7 @@ func (s *StepMountDevice) Run(ctx context.Context, state multistep.StateBag) mul
return multistep.ActionHalt
}
log.Printf("[DEBUG] (step mount) mount command is %s", mountCommand)
cmd := ShellCommand(mountCommand)
cmd := common.ShellCommand(mountCommand)
cmd.Stderr = stderr
if err := cmd.Run(); err != nil {
err := fmt.Errorf(
@ -135,7 +136,7 @@ func (s *StepMountDevice) CleanupFunc(state multistep.StateBag) error {
}
ui := state.Get("ui").(packer.Ui)
wrappedCommand := state.Get("wrappedCommand").(CommandWrapper)
wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper)
ui.Say("Unmounting the root device...")
unmountCommand, err := wrappedCommand(fmt.Sprintf("umount %s", s.mountPath))
@ -143,7 +144,7 @@ func (s *StepMountDevice) CleanupFunc(state multistep.StateBag) error {
return fmt.Errorf("Error creating unmount command: %s", err)
}
cmd := ShellCommand(unmountCommand)
cmd := common.ShellCommand(unmountCommand)
if err := cmd.Run(); err != nil {
return fmt.Errorf("Error unmounting root device: %s", err)
}

View File

@ -1,11 +1,15 @@
package chroot
import "testing"
import (
"testing"
"github.com/hashicorp/packer/common/chroot"
)
func TestMountDeviceCleanupFunc_ImplementsCleanupFunc(t *testing.T) {
var raw interface{}
raw = new(StepMountDevice)
if _, ok := raw.(Cleanup); !ok {
if _, ok := raw.(chroot.Cleanup); !ok {
t.Fatalf("cleanup func should be a CleanupFunc")
}
}

View File

@ -14,10 +14,10 @@ import (
"runtime"
"strings"
"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"
"github.com/hashicorp/packer/common/chroot"
"github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
@ -317,7 +317,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
state.Put("hook", hook)
state.Put("ui", ui)
state.Put("azureclient", azcli)
state.Put("wrappedCommand", chroot.CommandWrapper(wrappedCommand))
state.Put("wrappedCommand", common.CommandWrapper(wrappedCommand))
info, err := azcli.MetadataClient().GetComputeInfo()
if err != nil {

View File

@ -6,14 +6,15 @@ import (
"bytes"
"context"
"fmt"
"github.com/hashicorp/packer/builder/amazon/chroot"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
"log"
"os"
"path/filepath"
"strings"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
)
var _ multistep.Step = &StepMountDevice{}
@ -30,7 +31,7 @@ func (s *StepMountDevice) Run(ctx context.Context, state multistep.StateBag) mul
ui := state.Get("ui").(packer.Ui)
device := state.Get("device").(string)
config := state.Get("config").(*Config)
wrappedCommand := state.Get("wrappedCommand").(chroot.CommandWrapper)
wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper)
ictx := config.ctx
@ -83,7 +84,7 @@ func (s *StepMountDevice) Run(ctx context.Context, state multistep.StateBag) mul
return multistep.ActionHalt
}
log.Printf("[DEBUG] (step mount) mount command is %s", mountCommand)
cmd := chroot.ShellCommand(mountCommand)
cmd := common.ShellCommand(mountCommand)
cmd.Stderr = stderr
if err := cmd.Run(); err != nil {
err := fmt.Errorf(
@ -114,7 +115,7 @@ func (s *StepMountDevice) CleanupFunc(state multistep.StateBag) error {
}
ui := state.Get("ui").(packer.Ui)
wrappedCommand := state.Get("wrappedCommand").(chroot.CommandWrapper)
wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper)
ui.Say("Unmounting the root device...")
unmountCommand, err := wrappedCommand(fmt.Sprintf("umount %s", s.mountPath))
@ -122,7 +123,7 @@ func (s *StepMountDevice) CleanupFunc(state multistep.StateBag) error {
return fmt.Errorf("error creating unmount command: %s", err)
}
cmd := chroot.ShellCommand(unmountCommand)
cmd := common.ShellCommand(unmountCommand)
if err := cmd.Run(); err != nil {
return fmt.Errorf("error unmounting root device: %s", err)
}

View File

@ -13,6 +13,7 @@ import (
"strings"
"syscall"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/packer/tmp"
)
@ -21,7 +22,7 @@ import (
// commands locally but within a chroot.
type Communicator struct {
Chroot string
CmdWrapper CommandWrapper
CmdWrapper common.CommandWrapper
}
func (c *Communicator) Start(ctx context.Context, cmd *packer.RemoteCmd) error {
@ -33,7 +34,7 @@ func (c *Communicator) Start(ctx context.Context, cmd *packer.RemoteCmd) error {
return err
}
localCmd := ShellCommand(command)
localCmd := common.ShellCommand(command)
localCmd.Stdin = cmd.Stdin
localCmd.Stdout = cmd.Stdout
localCmd.Stderr = cmd.Stderr
@ -83,7 +84,7 @@ func (c *Communicator) Upload(dst string, r io.Reader, fi *os.FileInfo) error {
return err
}
return ShellCommand(cpCmd).Run()
return common.ShellCommand(cpCmd).Run()
}
func (c *Communicator) UploadDir(dst string, src string, exclude []string) error {
@ -105,7 +106,7 @@ func (c *Communicator) UploadDir(dst string, src string, exclude []string) error
}
var stderr bytes.Buffer
cmd := ShellCommand(cpCmd)
cmd := common.ShellCommand(cpCmd)
cmd.Env = append(cmd.Env, "LANG=C")
cmd.Env = append(cmd.Env, os.Environ()...)
cmd.Stderr = &stderr

View File

@ -0,0 +1,7 @@
package chroot
import "github.com/hashicorp/packer/template/interpolate"
type interpolateContextProvider interface {
GetContext() interpolate.Context
}

View File

@ -4,12 +4,13 @@ import (
"context"
"fmt"
"github.com/hashicorp/packer/common"
sl "github.com/hashicorp/packer/common/shell-local"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
)
func RunLocalCommands(commands []string, wrappedCommand CommandWrapper, ictx interpolate.Context, ui packer.Ui) error {
func RunLocalCommands(commands []string, wrappedCommand common.CommandWrapper, ictx interpolate.Context, ui packer.Ui) error {
ctx := context.TODO()
for _, rawCmd := range commands {
intCmd, err := interpolate.Render(rawCmd, &ictx)

View File

@ -4,6 +4,7 @@ import (
"context"
"log"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
)
@ -16,7 +17,7 @@ func (s *StepChrootProvision) Run(ctx context.Context, state multistep.StateBag)
hook := state.Get("hook").(packer.Hook)
mountPath := state.Get("mount_path").(string)
ui := state.Get("ui").(packer.Ui)
wrappedCommand := state.Get("wrappedCommand").(CommandWrapper)
wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper)
// Create our communicator
comm := &Communicator{

View File

@ -7,6 +7,7 @@ import (
"log"
"path/filepath"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
)
@ -24,7 +25,7 @@ type StepCopyFiles struct {
func (s *StepCopyFiles) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
mountPath := state.Get("mount_path").(string)
ui := state.Get("ui").(packer.Ui)
wrappedCommand := state.Get("wrappedCommand").(CommandWrapper)
wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper)
stderr := new(bytes.Buffer)
s.files = make([]string, 0, len(s.Files))
@ -44,7 +45,7 @@ func (s *StepCopyFiles) Run(ctx context.Context, state multistep.StateBag) multi
}
stderr.Reset()
cmd := ShellCommand(cmdText)
cmd := common.ShellCommand(cmdText)
cmd.Stderr = stderr
if err := cmd.Run(); err != nil {
err := fmt.Errorf(
@ -70,7 +71,7 @@ func (s *StepCopyFiles) Cleanup(state multistep.StateBag) {
}
func (s *StepCopyFiles) CleanupFunc(state multistep.StateBag) error {
wrappedCommand := state.Get("wrappedCommand").(CommandWrapper)
wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper)
if s.files != nil {
for _, file := range s.files {
log.Printf("Removing: %s", file)
@ -79,7 +80,7 @@ func (s *StepCopyFiles) CleanupFunc(state multistep.StateBag) error {
return err
}
localCmd := ShellCommand(localCmdText)
localCmd := common.ShellCommand(localCmdText)
if err := localCmd.Run(); err != nil {
return err
}

View File

@ -8,6 +8,7 @@ import (
"os/exec"
"syscall"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
)
@ -24,7 +25,7 @@ type StepMountExtra struct {
func (s *StepMountExtra) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
mountPath := state.Get("mount_path").(string)
ui := state.Get("ui").(packer.Ui)
wrappedCommand := state.Get("wrappedCommand").(CommandWrapper)
wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper)
s.mounts = make([]string, 0, len(s.ChrootMounts))
@ -58,7 +59,7 @@ func (s *StepMountExtra) Run(ctx context.Context, state multistep.StateBag) mult
return multistep.ActionHalt
}
cmd := ShellCommand(mountCommand)
cmd := common.ShellCommand(mountCommand)
cmd.Stderr = stderr
if err := cmd.Run(); err != nil {
err := fmt.Errorf(
@ -89,7 +90,7 @@ func (s *StepMountExtra) CleanupFunc(state multistep.StateBag) error {
return nil
}
wrappedCommand := state.Get("wrappedCommand").(CommandWrapper)
wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper)
for len(s.mounts) > 0 {
var path string
lastIndex := len(s.mounts) - 1
@ -103,7 +104,7 @@ func (s *StepMountExtra) CleanupFunc(state multistep.StateBag) error {
// Before attempting to unmount,
// check to see if path is already unmounted
stderr := new(bytes.Buffer)
cmd := ShellCommand(grepCommand)
cmd := common.ShellCommand(grepCommand)
cmd.Stderr = stderr
if err := cmd.Run(); err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
@ -124,7 +125,7 @@ func (s *StepMountExtra) CleanupFunc(state multistep.StateBag) error {
}
stderr = new(bytes.Buffer)
cmd = ShellCommand(unmountCommand)
cmd = common.ShellCommand(unmountCommand)
cmd.Stderr = stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf(

View File

@ -3,6 +3,7 @@ package chroot
import (
"context"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
)
@ -23,7 +24,7 @@ func (s *StepPostMountCommands) Run(ctx context.Context, state multistep.StateBa
device := state.Get("device").(string)
mountPath := state.Get("mount_path").(string)
ui := state.Get("ui").(packer.Ui)
wrappedCommand := state.Get("wrappedCommand").(CommandWrapper)
wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper)
if len(s.Commands) == 0 {
return multistep.ActionContinue

View File

@ -3,6 +3,7 @@ package chroot
import (
"context"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
)
@ -20,7 +21,7 @@ func (s *StepPreMountCommands) Run(ctx context.Context, state multistep.StateBag
config := state.Get("config").(interpolateContextProvider)
device := state.Get("device").(string)
ui := state.Get("ui").(packer.Ui)
wrappedCommand := state.Get("wrappedCommand").(CommandWrapper)
wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper)
if len(s.Commands) == 0 {
return multistep.ActionContinue

View File

@ -1,4 +1,4 @@
package chroot
package common
import (
"os/exec"