diff --git a/builder/scaleway/builder.go b/builder/scaleway/builder.go index 5fc5b48..d13da2f 100644 --- a/builder/scaleway/builder.go +++ b/builder/scaleway/builder.go @@ -91,6 +91,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) Comm: &b.config.Comm, }, new(stepWaitUserData), + new(stepCleanupMachineData), new(stepShutdown), new(stepSnapshot), new(stepImage), diff --git a/builder/scaleway/config.go b/builder/scaleway/config.go index 99ccb88..8277196 100644 --- a/builder/scaleway/config.go +++ b/builder/scaleway/config.go @@ -24,10 +24,11 @@ import ( ) const ( - defaultInstanceSnapshotWaitTimeout = 1 * time.Hour - defaultInstanceImageWaitTimeout = 1 * time.Hour - defaultInstanceServerWaitTimeout = 10 * time.Minute - defaultUserDataWaitTimeout = 0 * time.Second + defaultInstanceSnapshotWaitTimeout = 1 * time.Hour + defaultInstanceImageWaitTimeout = 1 * time.Hour + defaultInstanceServerWaitTimeout = 10 * time.Minute + defaultUserDataWaitTimeout = 0 * time.Second + defaultCleanupMachineRelatedDataStatus = "false" ) type Config struct { @@ -84,6 +85,10 @@ type Config struct { RemoveVolume bool `mapstructure:"remove_volume"` + // This value allows the user to remove information + // that is particular to the instance used to build the image + CleanupMachineRelatedData string `mapstructure:"cleanup_machine_related_data" required:"false"` + // The time to wait for snapshot creation. Defaults to "1h" SnapshotCreationTimeout time.Duration `mapstructure:"snapshot_creation_timeout" required:"false"` // The time to wait for image creation. Defaults to "1h" @@ -299,6 +304,10 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) { c.UserDataTimeout = defaultUserDataWaitTimeout } + if c.CleanupMachineRelatedData == "" { + c.CleanupMachineRelatedData = defaultCleanupMachineRelatedDataStatus + } + if errs != nil && len(errs.Errors) > 0 { return warnings, errs } diff --git a/builder/scaleway/config.hcl2spec.go b/builder/scaleway/config.hcl2spec.go index c7f6bab..cc187a2 100644 --- a/builder/scaleway/config.hcl2spec.go +++ b/builder/scaleway/config.hcl2spec.go @@ -81,6 +81,7 @@ type FlatConfig struct { Bootscript *string `mapstructure:"bootscript" required:"false" cty:"bootscript" hcl:"bootscript"` BootType *string `mapstructure:"boottype" required:"false" cty:"boottype" hcl:"boottype"` RemoveVolume *bool `mapstructure:"remove_volume" cty:"remove_volume" hcl:"remove_volume"` + CleanupMachineRelatedData *string `mapstructure:"cleanup_machine_related_data" required:"false" cty:"cleanup_machine_related_data" hcl:"cleanup_machine_related_data"` SnapshotCreationTimeout *string `mapstructure:"snapshot_creation_timeout" required:"false" cty:"snapshot_creation_timeout" hcl:"snapshot_creation_timeout"` ImageCreationTimeout *string `mapstructure:"image_creation_timeout" required:"false" cty:"image_creation_timeout" hcl:"image_creation_timeout"` ServerCreationTimeout *string `mapstructure:"server_creation_timeout" required:"false" cty:"server_creation_timeout" hcl:"server_creation_timeout"` @@ -175,6 +176,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "bootscript": &hcldec.AttrSpec{Name: "bootscript", Type: cty.String, Required: false}, "boottype": &hcldec.AttrSpec{Name: "boottype", Type: cty.String, Required: false}, "remove_volume": &hcldec.AttrSpec{Name: "remove_volume", Type: cty.Bool, Required: false}, + "cleanup_machine_related_data": &hcldec.AttrSpec{Name: "cleanup_machine_related_data", Type: cty.String, Required: false}, "snapshot_creation_timeout": &hcldec.AttrSpec{Name: "snapshot_creation_timeout", Type: cty.String, Required: false}, "image_creation_timeout": &hcldec.AttrSpec{Name: "image_creation_timeout", Type: cty.String, Required: false}, "server_creation_timeout": &hcldec.AttrSpec{Name: "server_creation_timeout", Type: cty.String, Required: false}, diff --git a/builder/scaleway/step_cleanup_machine_data.go b/builder/scaleway/step_cleanup_machine_data.go new file mode 100644 index 0000000..f2f8953 --- /dev/null +++ b/builder/scaleway/step_cleanup_machine_data.go @@ -0,0 +1,59 @@ +package scaleway + +import ( + "context" + "fmt" + "log" + "strconv" + + "github.com/hashicorp/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer-plugin-sdk/packer" +) + +type stepCleanupMachineData struct{} + +// Machine ID file locations +var ( + sysdID = "/etc/machine-id" + dbusID = "/var/lib/dbus/machine-id" +) + +func (s *stepCleanupMachineData) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { + ui := state.Get("ui").(packersdk.Ui) + comm := state.Get("communicator").(packersdk.Communicator) + c := state.Get("config").(*Config) + cmd := new(packersdk.RemoteCmd) + + str, err := strconv.ParseBool(c.CleanupMachineRelatedData) + if err != nil { + err := fmt.Errorf("value must be: 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + + if !str { + return multistep.ActionContinue + } + + ui.Say("Trying to remove machine-related data...") + + // Remove the machine-id file under /etc + cmd.Command = fmt.Sprintf("sudo truncate -s 0 %s", sysdID) + if err := cmd.RunWithUi(ctx, comm, ui); err != nil { + log.Printf("Error cleaning up %s: %s", sysdID, err) + } + + // Remove the machine-id file under /var/lib/dbus + cmd = new(packersdk.RemoteCmd) + cmd.Command = fmt.Sprintf("sudo truncate -s 0 %s", dbusID) + if err := cmd.RunWithUi(ctx, comm, ui); err != nil { + log.Printf("Error cleaning up %s: %s", dbusID, err) + } + + return multistep.ActionContinue +} + +func (s *stepCleanupMachineData) Cleanup(_ multistep.StateBag) { + // no cleanup +} diff --git a/docs-partials/builder/scaleway/Config-not-required.mdx b/docs-partials/builder/scaleway/Config-not-required.mdx index 9d01440..7714214 100644 --- a/docs-partials/builder/scaleway/Config-not-required.mdx +++ b/docs-partials/builder/scaleway/Config-not-required.mdx @@ -23,6 +23,9 @@ - `remove_volume` (bool) - Remove Volume +- `cleanup_machine_related_data` (string) - This value allows the user to remove information + that is particular to the instance used to build the image + - `snapshot_creation_timeout` (duration string | ex: "1h5m2s") - The time to wait for snapshot creation. Defaults to "1h" - `image_creation_timeout` (duration string | ex: "1h5m2s") - The time to wait for image creation. Defaults to "1h"