Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add user data support #52

Merged
merged 2 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions builder/scaleway/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
&commonsteps.StepCleanupTempKeys{
Comm: &b.config.Comm,
},
new(stepWaitUserData),
new(stepShutdown),
new(stepSnapshot),
new(stepImage),
Expand Down
10 changes: 10 additions & 0 deletions builder/scaleway/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
defaultInstanceSnapshotWaitTimeout = 1 * time.Hour
defaultInstanceImageWaitTimeout = 1 * time.Hour
defaultInstanceServerWaitTimeout = 10 * time.Minute
defaultUserDataWaitTimeout = 0 * time.Second
)

type Config struct {
Expand Down Expand Up @@ -92,6 +93,11 @@ type Config struct {
// The time to wait for server shutdown. Defaults to "10m"
ServerShutdownTimeout time.Duration `mapstructure:"server_shutdown_timeout" required:"false"`

// User data to apply when launching the instance
UserData map[string]string `mapstructure:"user_data" required:"false"`
// A custom timeout for user data to assure its completion. Defaults to "0s"
UserDataTimeout time.Duration `mapstructure:"user_data_timeout" required:"false"`

UserAgent string `mapstructure-to-hcl2:",skip"`
ctx interpolate.Context

Expand Down Expand Up @@ -289,6 +295,10 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
c.ImageCreationTimeout = defaultInstanceImageWaitTimeout
}

if c.UserDataTimeout == 0 {
c.UserDataTimeout = defaultUserDataWaitTimeout
}

if errs != nil && len(errs.Errors) > 0 {
return warnings, errs
}
Expand Down
4 changes: 4 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.

23 changes: 23 additions & 0 deletions builder/scaleway/step_create_server.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package scaleway

import (
"bytes"
"context"
"fmt"
"io"
"strings"

"github.com/hashicorp/packer-plugin-sdk/multistep"
Expand Down Expand Up @@ -60,6 +62,27 @@ func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) mu
return multistep.ActionHalt
}

if len(c.UserData) > 0 {
m := map[string]io.Reader{}
for k, v := range c.UserData {
m[k] = bytes.NewBufferString(v)
}

createUserDataReq := &instance.SetAllServerUserDataRequest{
Zone: scw.Zone(c.Zone),
ServerID: createServerResp.Server.ID,
UserData: m,
}

err = instanceAPI.SetAllServerUserData(createUserDataReq, scw.WithContext(ctx))
if err != nil {
err := fmt.Errorf("error creating server: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}

waitServerRequest := &instance.WaitForServerRequest{
ServerID: createServerResp.Server.ID,
Zone: scw.Zone(c.Zone),
Expand Down
26 changes: 26 additions & 0 deletions builder/scaleway/step_wait_user_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package scaleway

import (
"context"
"time"

"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
)

type stepWaitUserData struct{}

func (s *stepWaitUserData) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packersdk.Ui)
c := state.Get("config").(*Config)

ui.Say("Waiting for any user data apply to finish if provided...")

time.Sleep(c.UserDataTimeout)

return multistep.ActionContinue
}

func (s *stepWaitUserData) Cleanup(_ multistep.StateBag) {
// no cleanup
}
4 changes: 4 additions & 0 deletions docs-partials/builder/scaleway/Config-not-required.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@

- `server_shutdown_timeout` (duration string | ex: "1h5m2s") - The time to wait for server shutdown. Defaults to "10m"

- `user_data` (map[string]string) - User data to apply when launching the instance

- `user_data_timeout` (duration string | ex: "1h5m2s") - A custom timeout for user data to assure its completion. Defaults to "0s"

- `api_token` (string) - The token to use to authenticate with your account.
It can also be specified via environment variable SCALEWAY_API_TOKEN. You
can see and generate tokens in the "Credentials"
Expand Down