Merge pull request #7028 from nferch/hcloud_sshkeys

add ssh_keys config to hcloud builder
This commit is contained in:
Megan Marsh 2018-11-20 15:46:20 -08:00 committed by GitHub
commit 1fab55ad89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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