Use Context of run method and allow users to cancel the build

This commit is contained in:
Lukas Kämmerling 2019-03-07 11:41:39 +01:00
parent cc2267fd2a
commit 633934369c
No known key found for this signature in database
GPG Key ID: AA5C71B0F2BF3DB2
4 changed files with 14 additions and 14 deletions

View File

@ -50,7 +50,7 @@ func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) mu
sshKeys = append(sshKeys, sshKey)
}
serverCreateResult, _, err := client.Server.Create(context.TODO(), hcloud.ServerCreateOpts{
serverCreateResult, _, err := client.Server.Create(ctx, hcloud.ServerCreateOpts{
Name: c.ServerName,
ServerType: &hcloud.ServerType{Name: c.ServerType},
Image: &hcloud.Image{Name: c.Image},
@ -71,14 +71,14 @@ func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) mu
// Store the server id for later
state.Put("server_id", serverCreateResult.Server.ID)
if err := waitForAction(context.TODO(), client, serverCreateResult.Action); err != nil {
if err := waitForAction(ctx, client, serverCreateResult.Action); err != nil {
err := fmt.Errorf("Error creating server: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
for _, nextAction := range serverCreateResult.NextActions {
if err := waitForAction(context.TODO(), client, nextAction); err != nil {
if err := waitForAction(ctx, client, nextAction); err != nil {
err := fmt.Errorf("Error creating server: %s", err)
state.Put("error", err)
ui.Error(err.Error())
@ -88,7 +88,7 @@ func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) mu
if c.RescueMode != "" {
ui.Say("Enabling Rescue Mode...")
rootPassword, err := setRescue(context.TODO(), client, serverCreateResult.Server, c.RescueMode, sshKeys)
rootPassword, err := setRescue(ctx, client, serverCreateResult.Server, c.RescueMode, sshKeys)
if err != nil {
err := fmt.Errorf("Error enabling rescue mode: %s", err)
state.Put("error", err)
@ -96,14 +96,14 @@ func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) mu
return multistep.ActionHalt
}
ui.Say("Reboot server...")
action, _, err := client.Server.Reset(context.TODO(), serverCreateResult.Server)
action, _, err := client.Server.Reset(ctx, serverCreateResult.Server)
if err != nil {
err := fmt.Errorf("Error rebooting server: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
if err := waitForAction(context.TODO(), client, action); err != nil {
if err := waitForAction(ctx, client, action); err != nil {
err := fmt.Errorf("Error rebooting server: %s", err)
state.Put("error", err)
ui.Error(err.Error())

View File

@ -11,7 +11,7 @@ import (
type stepCreateSnapshot struct{}
func (s *stepCreateSnapshot) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
func (s *stepCreateSnapshot) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
client := state.Get("hcloudClient").(*hcloud.Client)
ui := state.Get("ui").(packer.Ui)
c := state.Get("config").(*Config)
@ -19,7 +19,7 @@ func (s *stepCreateSnapshot) Run(_ context.Context, state multistep.StateBag) mu
ui.Say("Creating snapshot ...")
ui.Say("This can take some time")
result, _, err := client.Server.CreateImage(context.TODO(), &hcloud.Server{ID: serverID}, &hcloud.ServerCreateImageOpts{
result, _, err := client.Server.CreateImage(ctx, &hcloud.Server{ID: serverID}, &hcloud.ServerCreateImageOpts{
Type: hcloud.ImageTypeSnapshot,
Labels: c.SnapshotLabels,
Description: hcloud.String(c.SnapshotName),
@ -32,7 +32,7 @@ func (s *stepCreateSnapshot) Run(_ context.Context, state multistep.StateBag) mu
}
state.Put("snapshot_id", result.Image.ID)
state.Put("snapshot_name", c.SnapshotName)
_, errCh := client.Action.WatchProgress(context.TODO(), result.Action)
_, errCh := client.Action.WatchProgress(ctx, result.Action)
for {
select {
case err1 := <-errCh:

View File

@ -25,7 +25,7 @@ type stepCreateSSHKey struct {
keyId int
}
func (s *stepCreateSSHKey) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
func (s *stepCreateSSHKey) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
client := state.Get("hcloudClient").(*hcloud.Client)
ui := state.Get("ui").(packer.Ui)
c := state.Get("config").(*Config)
@ -53,7 +53,7 @@ func (s *stepCreateSSHKey) Run(_ context.Context, state multistep.StateBag) mult
name := fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID())
// Create the key!
key, _, err := client.SSHKey.Create(context.TODO(), hcloud.SSHKeyCreateOpts{
key, _, err := client.SSHKey.Create(ctx, hcloud.SSHKeyCreateOpts{
Name: name,
PublicKey: pubSSHFormat,
})

View File

@ -11,14 +11,14 @@ import (
type stepShutdownServer struct{}
func (s *stepShutdownServer) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
func (s *stepShutdownServer) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
client := state.Get("hcloudClient").(*hcloud.Client)
ui := state.Get("ui").(packer.Ui)
serverID := state.Get("server_id").(int)
ui.Say("Shutting down server...")
action, _, err := client.Server.Shutdown(context.TODO(), &hcloud.Server{ID: serverID})
action, _, err := client.Server.Shutdown(ctx, &hcloud.Server{ID: serverID})
if err != nil {
err := fmt.Errorf("Error stopping server: %s", err)
@ -27,7 +27,7 @@ func (s *stepShutdownServer) Run(_ context.Context, state multistep.StateBag) mu
return multistep.ActionHalt
}
_, errCh := client.Action.WatchProgress(context.TODO(), action)
_, errCh := client.Action.WatchProgress(ctx, action)
for {
select {
case err1 := <-errCh: