Skip to content

Commit

Permalink
feat: add labels options to server and ssh keys (#128)
Browse files Browse the repository at this point in the history
Continuation of #92 

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.

---------

Signed-off-by: janiskemper <janis.kemper@syself.com>
Co-authored-by: janiskemper <janis.kemper@syself.com>
  • Loading branch information
jooola and janiskemper committed Dec 12, 2023
1 parent b824a67 commit 3f7dcae
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .web-docs/components/builder/hcloud/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ builder.
- `server_name` (string) - The name assigned to the server. The Hetzner Cloud
sets the hostname of the machine to this value.

- `server_labels` (map of key/value strings) - Key/value pair labels to
apply to the created server.

- `snapshot_name` (string) - The name of the resulting snapshot that will
appear in your account as image description. Defaults to `packer-{{timestamp}}` (see
[configuration templates](/packer/docs/templates/legacy_json_templates/engine) for more info).
Expand All @@ -87,6 +90,9 @@ builder.
- `user_data_file` (string) - Path to a file that will be used for the user
data when launching the server.

- `ssh_keys_labels` (map of key/value strings) - Key/value pair labels to
apply to the created ssh keys.

- `ssh_keys` (array of strings) - List of SSH keys by name or id to be added
to image on launch.

Expand Down
14 changes: 8 additions & 6 deletions builder/hcloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,20 @@ 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"`
UserData string `mapstructure:"user_data"`
UserDataFile string `mapstructure:"user_data_file"`
SSHKeys []string `mapstructure:"ssh_keys"`
SSHKeysLabels map[string]string `mapstructure:"ssh_keys_labels"`
Networks []int64 `mapstructure:"networks"`

RescueMode string `mapstructure:"rescue"`
Expand Down
4 changes: 4 additions & 0 deletions builder/hcloud/config.hcl2spec.go

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

6 changes: 2 additions & 4 deletions builder/hcloud/step_create_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,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.Ptr(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 @@ -128,7 +128,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 @@ -145,7 +144,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 @@ -257,7 +255,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 @@ -37,6 +37,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.SSHKeysLabels,
})
if err != nil {
err := fmt.Errorf("Error creating temporary SSH key: %s", err)
Expand Down
6 changes: 6 additions & 0 deletions docs/builders/hcloud.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ builder.
- `server_name` (string) - The name assigned to the server. The Hetzner Cloud
sets the hostname of the machine to this value.

- `server_labels` (map of key/value strings) - Key/value pair labels to
apply to the created server.

- `snapshot_name` (string) - The name of the resulting snapshot that will
appear in your account as image description. Defaults to `packer-{{timestamp}}` (see
[configuration templates](/packer/docs/templates/legacy_json_templates/engine) for more info).
Expand All @@ -100,6 +103,9 @@ builder.
- `user_data_file` (string) - Path to a file that will be used for the user
data when launching the server.

- `ssh_keys_labels` (map of key/value strings) - Key/value pair labels to
apply to the created ssh keys.

- `ssh_keys` (array of strings) - List of SSH keys by name or id to be added
to image on launch.

Expand Down

0 comments on commit 3f7dcae

Please sign in to comment.