Skip to content

Commit

Permalink
add an optionnal step to cleanup machine-specific data (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
yfodil committed Aug 17, 2022
1 parent f2a74c7 commit ee9f85d
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 deletions.
1 change: 1 addition & 0 deletions builder/scaleway/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
17 changes: 13 additions & 4 deletions builder/scaleway/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions builder/scaleway/config.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions builder/scaleway/step_cleanup_machine_data.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 3 additions & 0 deletions docs-partials/builder/scaleway/Config-not-required.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit ee9f85d

Please sign in to comment.