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 timeout configuration for all steps #49

Merged
merged 3 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 22 additions & 8 deletions builder/scaleway/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"log"
"os"
"time"

"github.com/hashicorp/packer-plugin-sdk/common"
"github.com/hashicorp/packer-plugin-sdk/communicator"
Expand All @@ -23,6 +22,12 @@ import (
"github.com/scaleway/scaleway-sdk-go/scw"
)

const (
defaultInstanceSnapshotWaitTimeout = "1h"
defaultInstanceImageTimeout = "1h"
defaultInstanceServerWaitTimeout = "10m"
)

type Config struct {
common.PackerConfig `mapstructure:",squash"`
Comm communicator.Config `mapstructure:",squash"`
Expand Down Expand Up @@ -77,11 +82,12 @@ type Config struct {

RemoveVolume bool `mapstructure:"remove_volume"`

// Shutdown timeout. Default to 5m
ShutdownTimeout string `mapstructure:"shutdown_timeout" required:"false"`
// Timeouts

// Testing timeout
SnapshotTimeout time.Duration `mapstructure:"state_timeout" required:"false"`
SnapshotTimeout string `mapstructure:"snapshot_timeout" required:"false"`
ImageTimeout string `mapstructure:"image_timeout" required:"false"`
ServerTimeout string `mapstructure:"server_timeout" required:"false"`
ShutdownTimeout string `mapstructure:"shutdown_timeout" required:"false"`

UserAgent string `mapstructure-to-hcl2:",skip"`
ctx interpolate.Context
Expand Down Expand Up @@ -265,11 +271,19 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
}

if c.ShutdownTimeout == "" {
c.ShutdownTimeout = "5m"
c.ShutdownTimeout = defaultInstanceServerWaitTimeout
yfodil marked this conversation as resolved.
Show resolved Hide resolved
}

if c.SnapshotTimeout == "" {
c.SnapshotTimeout = defaultInstanceSnapshotWaitTimeout
}

if c.ImageTimeout == "" {
c.ImageTimeout = defaultInstanceImageTimeout
}

if c.SnapshotTimeout == 0 {
c.SnapshotTimeout = 5 * time.Minute
if c.ServerTimeout == "" {
c.ServerTimeout = defaultInstanceServerWaitTimeout
}

if errs != nil && len(errs.Errors) > 0 {
Expand Down
8 changes: 6 additions & 2 deletions builder/scaleway/config.hcl2spec.go

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

33 changes: 32 additions & 1 deletion builder/scaleway/step_create_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"time"

"github.com/hashicorp/go-uuid"
"github.com/hashicorp/packer-plugin-sdk/multistep"
Expand Down Expand Up @@ -63,14 +64,44 @@ func (s *stepImage) Run(ctx context.Context, state multistep.StateBag) multistep
Name: c.ImageName,
RootVolume: snapshotID,
}, scw.WithContext(ctx))

if err != nil {
err := fmt.Errorf("error creating image: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}

waitImageRequest := &instance.WaitForImageRequest{
ImageID: createImageResp.Image.ID,
Zone: scw.Zone(c.Zone),
}
timeout := c.ImageTimeout
duration, err := time.ParseDuration(timeout)
if err != nil {
err := fmt.Errorf("error: %s could not parse string %s as a duration", err, timeout)
yfodil marked this conversation as resolved.
Show resolved Hide resolved
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
if timeout != "" {
waitImageRequest.Timeout = scw.TimeDurationPtr(duration)
}

image, err := instanceAPI.WaitForImage(waitImageRequest)
if err != nil {
err := fmt.Errorf("image is not available: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}

if image.State != instance.ImageStateAvailable {
err := fmt.Errorf("image is in error state")
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}

log.Printf("Image ID: %s", createImageResp.Image.ID)
state.Put("image_id", createImageResp.Image.ID)
state.Put("image_name", c.ImageName)
Expand Down
45 changes: 45 additions & 0 deletions builder/scaleway/step_create_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"strings"
"time"

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

waitServerRequest := &instance.WaitForServerRequest{
ServerID: createServerResp.Server.ID,
Zone: scw.Zone(c.Zone),
}
timeout := c.ServerTimeout
duration, err := time.ParseDuration(timeout)
if err != nil {
err := fmt.Errorf("error: %s could not parse string %s as a duration", err, timeout)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
if timeout != "" {
waitServerRequest.Timeout = scw.TimeDurationPtr(duration)
}

_, err = instanceAPI.ServerAction(&instance.ServerActionRequest{
Action: instance.ServerActionPoweron,
ServerID: createServerResp.Server.ID,
Expand All @@ -71,6 +88,21 @@ func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) mu
return multistep.ActionHalt
}

server, err := instanceAPI.WaitForServer(waitServerRequest)
if err != nil {
err := fmt.Errorf("server is not available: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}

if server.State != instance.ServerStateRunning {
err := fmt.Errorf("servert is in error state")
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}

s.serverID = createServerResp.Server.ID

state.Put("server_id", createServerResp.Server.ID)
Expand All @@ -82,6 +114,8 @@ func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) mu
}

func (s *stepCreateServer) Cleanup(state multistep.StateBag) {
volumeID := state.Get("root_volume_id").(string)

if s.serverID == "" {
return
}
Expand All @@ -104,4 +138,15 @@ func (s *stepCreateServer) Cleanup(state multistep.StateBag) {
"Error destroying server. Please destroy it manually: %s", err))
}
}

ui.Say("Removing volume ...")

err = instanceAPI.DeleteVolume(&instance.DeleteVolumeRequest{
VolumeID: volumeID,
})
if err != nil {
err := fmt.Errorf("error removing volume: %s", err)
state.Put("error", err)
ui.Error(fmt.Sprintf("Error removing volume: %s. Please destroy it manually", err))
}
}
12 changes: 10 additions & 2 deletions builder/scaleway/step_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"time"

"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
Expand Down Expand Up @@ -37,8 +38,15 @@ func (s *stepSnapshot) Run(ctx context.Context, state multistep.StateBag) multis
Zone: scw.Zone(c.Zone),
}
timeout := c.SnapshotTimeout
if timeout != 0 {
waitSnapshotRequest.Timeout = scw.TimeDurationPtr(timeout)
duration, err := time.ParseDuration(timeout)
yfodil marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
err := fmt.Errorf("error: %s could not parse string %s as a duration", err, timeout)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
if timeout != "" {
waitSnapshotRequest.Timeout = scw.TimeDurationPtr(duration)
}

snapshot, err := instanceAPI.WaitForSnapshot(waitSnapshotRequest)
Expand Down
8 changes: 6 additions & 2 deletions docs-partials/builder/scaleway/Config-not-required.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@

- `remove_volume` (bool) - Remove Volume

- `shutdown_timeout` (string) - Shutdown timeout. Default to 5m
- `snapshot_timeout` (string) - Snapshot Timeout

- `state_timeout` (duration string | ex: "1h5m2s") - Testing timeout
- `image_timeout` (string) - Image Timeout

- `server_timeout` (string) - Server Timeout
yfodil marked this conversation as resolved.
Show resolved Hide resolved

- `shutdown_timeout` (string) - Shutdown Timeout
yfodil marked this conversation as resolved.
Show resolved Hide resolved

- `api_token` (string) - The token to use to authenticate with your account.
It can also be specified via environment variable SCALEWAY_API_TOKEN. You
Expand Down