From 67dc18a3b4d363f1c5c3e2805646c95191939ca5 Mon Sep 17 00:00:00 2001 From: janiskemper Date: Mon, 23 Oct 2023 15:51:37 +0200 Subject: [PATCH] :sparkles: Add server and ssh key labels for temporary objects Adding labels to the temporary server and ssh key objects which are needed to create the snapshot. This is needed in some use cases where e.g. packer build stops unexpectedly due to a SIGKILL and cannot shut down properly. In order to realize that the respective server and ssh key are orphaned, labels are needed. --- builder/hcloud/config.go | 15 +++++++++------ builder/hcloud/step_create_server.go | 6 ++---- builder/hcloud/step_create_sshkey.go | 1 + 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/builder/hcloud/config.go b/builder/hcloud/config.go index 8dc238b9..cccc1443 100644 --- a/builder/hcloud/config.go +++ b/builder/hcloud/config.go @@ -31,12 +31,13 @@ type Config struct { PollInterval time.Duration `mapstructure:"poll_interval"` - ServerName string `mapstructure:"server_name"` - Location string `mapstructure:"location"` - ServerType string `mapstructure:"server_type"` - UpgradeServerType string `mapstructure:"upgrade_server_type"` - Image string `mapstructure:"image"` - ImageFilter *imageFilter `mapstructure:"image_filter"` + ServerName string `mapstructure:"server_name"` + Location string `mapstructure:"location"` + ServerType string `mapstructure:"server_type"` + ServerLabels map[string]string `mapstructure:"server_labels"` + UpgradeServerType string `mapstructure:"upgrade_server_type"` + Image string `mapstructure:"image"` + ImageFilter *imageFilter `mapstructure:"image_filter"` SnapshotName string `mapstructure:"snapshot_name"` SnapshotLabels map[string]string `mapstructure:"snapshot_labels"` @@ -45,6 +46,8 @@ type Config struct { SSHKeys []string `mapstructure:"ssh_keys"` Networks []int64 `mapstructure:"networks"` + TempSSHKeyLabels map[string]string `mapstructure:"temp_sshkey_labels"` + RescueMode string `mapstructure:"rescue"` ctx interpolate.Context diff --git a/builder/hcloud/step_create_server.go b/builder/hcloud/step_create_server.go index 509c9689..65f79f08 100644 --- a/builder/hcloud/step_create_server.go +++ b/builder/hcloud/step_create_server.go @@ -82,6 +82,7 @@ func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) mu Location: &hcloud.Location{Name: c.Location}, UserData: userData, Networks: networks, + Labels: c.ServerLabels, } if c.UpgradeServerType != "" { @@ -89,7 +90,6 @@ func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) mu } serverCreateResult, _, err := client.Server.Create(ctx, serverCreateOpts) - if err != nil { err := fmt.Errorf("Error creating server: %s", err) state.Put("error", err) @@ -127,7 +127,6 @@ func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) mu ServerType: &hcloud.ServerType{Name: c.UpgradeServerType}, UpgradeDisk: false, }) - if err != nil { err := fmt.Errorf("Error changing server-type: %s", err) state.Put("error", err) @@ -144,7 +143,6 @@ func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) mu ui.Say("Starting server...") serverPoweronAction, _, err := client.Server.Poweron(ctx, serverCreateResult.Server) - if err != nil { err := fmt.Errorf("Error starting server: %s", err) state.Put("error", err) @@ -256,7 +254,7 @@ func waitForAction(ctx context.Context, client *hcloud.Client, action *hcloud.Ac func getImageWithSelectors(ctx context.Context, client *hcloud.Client, c *Config, serverType *hcloud.ServerType) (*hcloud.Image, error) { var allImages []*hcloud.Image - var selector = strings.Join(c.ImageFilter.WithSelector, ",") + selector := strings.Join(c.ImageFilter.WithSelector, ",") opts := hcloud.ImageListOpts{ ListOpts: hcloud.ListOpts{LabelSelector: selector}, Status: []hcloud.ImageStatus{hcloud.ImageStatusAvailable}, diff --git a/builder/hcloud/step_create_sshkey.go b/builder/hcloud/step_create_sshkey.go index 32a0dc13..367a712f 100644 --- a/builder/hcloud/step_create_sshkey.go +++ b/builder/hcloud/step_create_sshkey.go @@ -36,6 +36,7 @@ func (s *stepCreateSSHKey) Run(ctx context.Context, state multistep.StateBag) mu key, _, err := client.SSHKey.Create(ctx, hcloud.SSHKeyCreateOpts{ Name: name, PublicKey: string(c.Comm.SSHPublicKey), + Labels: c.TempSSHKeyLabels, }) if err != nil { err := fmt.Errorf("Error creating temporary SSH key: %s", err)