diff --git a/builder/amazon/chroot/builder.go b/builder/amazon/chroot/builder.go index 840ac1561..82338cb2b 100644 --- a/builder/amazon/chroot/builder.go +++ b/builder/amazon/chroot/builder.go @@ -18,10 +18,6 @@ import ( // The unique ID for this builder const BuilderId = "mitchellh.amazon.chroot" -// CleanupFunc is a type that is strung throughout the state bag in -// order to perform cleanup at earlier points. -type CleanupFunc func(map[string]interface{}) error - // Config is the configuration that is chained through the steps and // settable from the template. type Config struct { diff --git a/builder/amazon/chroot/cleanup.go b/builder/amazon/chroot/cleanup.go new file mode 100644 index 000000000..14c47aac3 --- /dev/null +++ b/builder/amazon/chroot/cleanup.go @@ -0,0 +1,6 @@ +package chroot + +// Cleanup is an interface that some steps implement for early cleanup. +type Cleanup interface { + CleanupFunc(map[string]interface{}) error +} diff --git a/builder/amazon/chroot/step_attach_volume.go b/builder/amazon/chroot/step_attach_volume.go index 9ab3b92f2..0617adca0 100644 --- a/builder/amazon/chroot/step_attach_volume.go +++ b/builder/amazon/chroot/step_attach_volume.go @@ -71,7 +71,7 @@ func (s *StepAttachVolume) Run(state map[string]interface{}) multistep.StepActio } state["device"] = config.AttachedDevicePath - state["attach_cleanup"] = s.CleanupFunc + state["attach_cleanup"] = s return multistep.ActionContinue } diff --git a/builder/amazon/chroot/step_attach_volume_test.go b/builder/amazon/chroot/step_attach_volume_test.go new file mode 100644 index 000000000..63f629b77 --- /dev/null +++ b/builder/amazon/chroot/step_attach_volume_test.go @@ -0,0 +1,11 @@ +package chroot + +import "testing" + +func TestAttachVolumeCleanupFunc_ImplementsCleanupFunc(t *testing.T) { + var raw interface{} + raw = new(StepAttachVolume) + if _, ok := raw.(Cleanup); !ok { + t.Fatalf("cleanup func should be a CleanupFunc") + } +} diff --git a/builder/amazon/chroot/step_copy_files.go b/builder/amazon/chroot/step_copy_files.go index eab0ee6e8..45ec09db3 100644 --- a/builder/amazon/chroot/step_copy_files.go +++ b/builder/amazon/chroot/step_copy_files.go @@ -43,7 +43,7 @@ func (s *StepCopyFiles) Run(state map[string]interface{}) multistep.StepAction { } } - state["copy_files_cleanup"] = s.CleanupFunc + state["copy_files_cleanup"] = s return multistep.ActionContinue } diff --git a/builder/amazon/chroot/step_copy_files_test.go b/builder/amazon/chroot/step_copy_files_test.go new file mode 100644 index 000000000..281613e6f --- /dev/null +++ b/builder/amazon/chroot/step_copy_files_test.go @@ -0,0 +1,11 @@ +package chroot + +import "testing" + +func TestCopyFilesCleanupFunc_ImplementsCleanupFunc(t *testing.T) { + var raw interface{} + raw = new(StepCopyFiles) + if _, ok := raw.(Cleanup); !ok { + t.Fatalf("cleanup func should be a CleanupFunc") + } +} diff --git a/builder/amazon/chroot/step_early_cleanup.go b/builder/amazon/chroot/step_early_cleanup.go index 421e7ba27..01ccb48d7 100644 --- a/builder/amazon/chroot/step_early_cleanup.go +++ b/builder/amazon/chroot/step_early_cleanup.go @@ -21,9 +21,9 @@ func (s *StepEarlyCleanup) Run(state map[string]interface{}) multistep.StepActio } for _, key := range cleanupKeys { - f := state[key].(CleanupFunc) + c := state[key].(Cleanup) log.Printf("Running cleanup func: %s", key) - if err := f(state); err != nil { + if err := c.CleanupFunc(state); err != nil { err := fmt.Errorf("Error cleaning up: %s", err) state["error"] = err ui.Error(err.Error()) diff --git a/builder/amazon/chroot/step_mount_device.go b/builder/amazon/chroot/step_mount_device.go index b5733eefd..9dc4f1743 100644 --- a/builder/amazon/chroot/step_mount_device.go +++ b/builder/amazon/chroot/step_mount_device.go @@ -62,7 +62,7 @@ func (s *StepMountDevice) Run(state map[string]interface{}) multistep.StepAction // Set the mount path so we remember to unmount it later s.mountPath = mountPath state["mount_path"] = s.mountPath - state["mount_device_cleanup"] = s.CleanupFunc + state["mount_device_cleanup"] = s return multistep.ActionContinue } diff --git a/builder/amazon/chroot/step_mount_device_test.go b/builder/amazon/chroot/step_mount_device_test.go new file mode 100644 index 000000000..2eeb850eb --- /dev/null +++ b/builder/amazon/chroot/step_mount_device_test.go @@ -0,0 +1,11 @@ +package chroot + +import "testing" + +func TestMountDeviceCleanupFunc_ImplementsCleanupFunc(t *testing.T) { + var raw interface{} + raw = new(StepMountDevice) + if _, ok := raw.(Cleanup); !ok { + t.Fatalf("cleanup func should be a CleanupFunc") + } +} diff --git a/builder/amazon/chroot/step_mount_extra.go b/builder/amazon/chroot/step_mount_extra.go index 165804bea..6a306fdf9 100644 --- a/builder/amazon/chroot/step_mount_extra.go +++ b/builder/amazon/chroot/step_mount_extra.go @@ -61,7 +61,7 @@ func (s *StepMountExtra) Run(state map[string]interface{}) multistep.StepAction s.mounts = append(s.mounts, innerPath) } - state["mount_extra_cleanup"] = s.CleanupFunc + state["mount_extra_cleanup"] = s return multistep.ActionContinue } diff --git a/builder/amazon/chroot/step_mount_extra_test.go b/builder/amazon/chroot/step_mount_extra_test.go new file mode 100644 index 000000000..d53cc7056 --- /dev/null +++ b/builder/amazon/chroot/step_mount_extra_test.go @@ -0,0 +1,11 @@ +package chroot + +import "testing" + +func TestMountExtraCleanupFunc_ImplementsCleanupFunc(t *testing.T) { + var raw interface{} + raw = new(StepMountExtra) + if _, ok := raw.(Cleanup); !ok { + t.Fatalf("cleanup func should be a CleanupFunc") + } +}