From 849a6ed353fccdc0b2444cdd632f3cd98af1d9d4 Mon Sep 17 00:00:00 2001 From: "Nathan A. Ferch" Date: Tue, 20 Nov 2018 13:57:44 -0500 Subject: [PATCH] add ssh_keys config to hcloud builder --- builder/hcloud/config.go | 7 ++++--- builder/hcloud/step_create_server.go | 15 +++++++++++++-- .../source/docs/builders/hetzner-cloud.html.md | 3 +++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/builder/hcloud/config.go b/builder/hcloud/config.go index ef34806c4..3de43ab04 100644 --- a/builder/hcloud/config.go +++ b/builder/hcloud/config.go @@ -30,9 +30,10 @@ type Config struct { ServerType string `mapstructure:"server_type"` Image string `mapstructure:"image"` - SnapshotName string `mapstructure:"snapshot_name"` - UserData string `mapstructure:"user_data"` - UserDataFile string `mapstructure:"user_data_file"` + SnapshotName string `mapstructure:"snapshot_name"` + UserData string `mapstructure:"user_data"` + UserDataFile string `mapstructure:"user_data_file"` + SSHKeys []string `mapstructure:"ssh_keys"` ctx interpolate.Context } diff --git a/builder/hcloud/step_create_server.go b/builder/hcloud/step_create_server.go index c705c7fad..eca8d54ae 100644 --- a/builder/hcloud/step_create_server.go +++ b/builder/hcloud/step_create_server.go @@ -15,7 +15,7 @@ type stepCreateServer struct { serverId int } -func (s *stepCreateServer) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { +func (s *stepCreateServer) 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) @@ -35,11 +35,22 @@ func (s *stepCreateServer) Run(_ context.Context, state multistep.StateBag) mult userData = string(contents) } + sshKeys := []*hcloud.SSHKey{{ID: sshKeyId}} + for _, k := range c.SSHKeys { + sshKey, _, err := client.SSHKey.Get(ctx, k) + if err != nil { + ui.Error(err.Error()) + state.Put("error", fmt.Errorf("Error fetching SSH key: %s", err)) + return multistep.ActionHalt + } + sshKeys = append(sshKeys, sshKey) + } + serverCreateResult, _, err := client.Server.Create(context.TODO(), hcloud.ServerCreateOpts{ Name: c.ServerName, ServerType: &hcloud.ServerType{Name: c.ServerType}, Image: &hcloud.Image{Name: c.Image}, - SSHKeys: []*hcloud.SSHKey{{ID: sshKeyId}}, + SSHKeys: sshKeys, Location: &hcloud.Location{Name: c.Location}, UserData: userData, }) diff --git a/website/source/docs/builders/hetzner-cloud.html.md b/website/source/docs/builders/hetzner-cloud.html.md index 41bd2a4fb..4b0ab5f5e 100644 --- a/website/source/docs/builders/hetzner-cloud.html.md +++ b/website/source/docs/builders/hetzner-cloud.html.md @@ -69,6 +69,9 @@ builder. - `user_data_file` (string) - Path to a file that will be used for the user data when launching the server. +- `ssh_keys` (array of strings) - List of SSH keys by name or id to be added + to image on launch. + ## Basic Example Here is a basic example. It is completely valid as soon as you enter your own