Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add labels options to server and ssh keys #128

Merged
merged 2 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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