From 7730cdcb5612f0966ba6a8358dd4e24cf7438f8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Wed, 6 Mar 2019 07:57:51 +0100 Subject: [PATCH 1/4] Fix usage of freebsd64 rescue image --- builder/hcloud/step_create_server.go | 30 ++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/builder/hcloud/step_create_server.go b/builder/hcloud/step_create_server.go index ffaa6a60e..ac3253764 100644 --- a/builder/hcloud/step_create_server.go +++ b/builder/hcloud/step_create_server.go @@ -31,7 +31,7 @@ func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) mu state.Put("error", fmt.Errorf("Problem reading user data file: %s", err)) return multistep.ActionHalt } - + userData = string(contents) } @@ -87,12 +87,18 @@ func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) mu } if c.RescueMode != "" { - if err := setRescue(context.TODO(), client, serverCreateResult.Server, c.RescueMode, sshKeys); err != nil { + rootPassword, err := setRescue(context.TODO(), client, serverCreateResult.Server, c.RescueMode, sshKeys) + if err != nil { err := fmt.Errorf("Error enabling rescue mode: %s", err) state.Put("error", err) ui.Error(err.Error()) return multistep.ActionHalt } + if c.RescueMode == "freebsd64" { + // We will set this only on freebsd + ui.Say("Using Root Password instead of SSH Keys...") + c.Comm.SSHPassword = rootPassword + } } return multistep.ActionContinue @@ -116,41 +122,45 @@ func (s *stepCreateServer) Cleanup(state multistep.StateBag) { } } -func setRescue(ctx context.Context, client *hcloud.Client, server *hcloud.Server, rescue string, sshKeys []*hcloud.SSHKey) error { +func setRescue(ctx context.Context, client *hcloud.Client, server *hcloud.Server, rescue string, sshKeys []*hcloud.SSHKey) (string, error) { rescueChanged := false if server.RescueEnabled { rescueChanged = true action, _, err := client.Server.DisableRescue(ctx, server) if err != nil { - return err + return "", err } if err := waitForAction(ctx, client, action); err != nil { - return err + return "", err } } if rescue != "" { rescueChanged = true + if rescue == "freebsd64" { + sshKeys = nil // freebsd64 doesn't allow ssh keys so we will remove them here + } res, _, err := client.Server.EnableRescue(ctx, server, hcloud.ServerEnableRescueOpts{ Type: hcloud.ServerRescueType(rescue), SSHKeys: sshKeys, }) if err != nil { - return err + return "", err } if err := waitForAction(ctx, client, res.Action); err != nil { - return err + return "", err } + return res.RootPassword, nil } if rescueChanged { action, _, err := client.Server.Reset(ctx, server) if err != nil { - return err + return "", err } if err := waitForAction(ctx, client, action); err != nil { - return err + return "", err } } - return nil + return "", nil } func waitForAction(ctx context.Context, client *hcloud.Client, action *hcloud.Action) error { From b6ee0ea736b49153988d0bab8264ef6d02dc960e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Wed, 6 Mar 2019 08:05:08 +0100 Subject: [PATCH 2/4] Fix Format --- builder/hcloud/step_create_server.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builder/hcloud/step_create_server.go b/builder/hcloud/step_create_server.go index ac3253764..63a52d0ef 100644 --- a/builder/hcloud/step_create_server.go +++ b/builder/hcloud/step_create_server.go @@ -31,7 +31,7 @@ func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) mu state.Put("error", fmt.Errorf("Problem reading user data file: %s", err)) return multistep.ActionHalt } - + userData = string(contents) } From cc2267fd2a0660211f1ca3e6214472c200bc3a1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Thu, 7 Mar 2019 07:24:22 +0100 Subject: [PATCH 3/4] Fix usage of rescue system --- builder/hcloud/step_create_server.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/builder/hcloud/step_create_server.go b/builder/hcloud/step_create_server.go index 63a52d0ef..38542ab0d 100644 --- a/builder/hcloud/step_create_server.go +++ b/builder/hcloud/step_create_server.go @@ -87,6 +87,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) if err != nil { err := fmt.Errorf("Error enabling rescue mode: %s", err) @@ -94,6 +95,20 @@ func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) mu ui.Error(err.Error()) return multistep.ActionHalt } + ui.Say("Reboot server...") + action, _, err := client.Server.Reset(context.TODO(), 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 { + err := fmt.Errorf("Error rebooting server: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } if c.RescueMode == "freebsd64" { // We will set this only on freebsd ui.Say("Using Root Password instead of SSH Keys...") From 633934369c02d76da2f889e1a457bc2455665013 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Thu, 7 Mar 2019 11:41:39 +0100 Subject: [PATCH 4/4] Use Context of run method and allow users to cancel the build --- builder/hcloud/step_create_server.go | 12 ++++++------ builder/hcloud/step_create_snapshot.go | 6 +++--- builder/hcloud/step_create_sshkey.go | 4 ++-- builder/hcloud/step_shutdown_server.go | 6 +++--- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/builder/hcloud/step_create_server.go b/builder/hcloud/step_create_server.go index 38542ab0d..d3605e254 100644 --- a/builder/hcloud/step_create_server.go +++ b/builder/hcloud/step_create_server.go @@ -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()) diff --git a/builder/hcloud/step_create_snapshot.go b/builder/hcloud/step_create_snapshot.go index f0ac324a8..f2365fc1e 100644 --- a/builder/hcloud/step_create_snapshot.go +++ b/builder/hcloud/step_create_snapshot.go @@ -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: diff --git a/builder/hcloud/step_create_sshkey.go b/builder/hcloud/step_create_sshkey.go index 222830927..398adecde 100644 --- a/builder/hcloud/step_create_sshkey.go +++ b/builder/hcloud/step_create_sshkey.go @@ -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, }) diff --git a/builder/hcloud/step_shutdown_server.go b/builder/hcloud/step_shutdown_server.go index 60f488870..4aafd5852 100644 --- a/builder/hcloud/step_shutdown_server.go +++ b/builder/hcloud/step_shutdown_server.go @@ -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: