add ssh_keys config to hcloud builder

This commit is contained in:
Nathan A. Ferch 2018-11-20 13:57:44 -05:00
parent fbde30c8c0
commit 849a6ed353
3 changed files with 20 additions and 5 deletions

View File

@ -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
}

View File

@ -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,
})

View File

@ -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