Skip to content

Commit

Permalink
✨ Add server and ssh key labels for temporary objects
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
janiskemper committed Oct 23, 2023
1 parent cd2da17 commit 67dc18a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
15 changes: 9 additions & 6 deletions builder/hcloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions builder/hcloud/step_create_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ 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 != "" {
serverCreateOpts.StartAfterCreate = hcloud.Bool(false)
}

serverCreateResult, _, err := client.Server.Create(ctx, serverCreateOpts)

if err != nil {
err := fmt.Errorf("Error creating server: %s", err)
state.Put("error", err)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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},
Expand Down
1 change: 1 addition & 0 deletions builder/hcloud/step_create_sshkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 67dc18a

Please sign in to comment.