use public ssh key & key name from config instead of the statebag

This commit is contained in:
Adrien Delorme 2018-08-29 14:01:55 +02:00
parent 0938b640cc
commit cd851f8ac2
6 changed files with 15 additions and 22 deletions

View File

@ -20,7 +20,6 @@ type Config struct {
Token string `mapstructure:"token"`
Url string `mapstructure:"url"`
SSHKey string
SnapshotName string `mapstructure:"image_name"`
DataCenterName string `mapstructure:"data_center_name"`
DataCenterId string

View File

@ -17,10 +17,6 @@ func (s *stepCreateServer) Run(_ context.Context, state multistep.StateBag) mult
ui := state.Get("ui").(packer.Ui)
c := state.Get("config").(*Config)
if sshkey, ok := state.GetOk("publicKey"); ok {
c.SSHKey = sshkey.(string)
}
token := oneandone.SetToken(c.Token)
//Create an API client
@ -72,8 +68,8 @@ func (s *stepCreateServer) Run(_ context.Context, state multistep.StateBag) mult
if c.Comm.SSHPassword != "" {
req.Password = c.Comm.SSHPassword
}
if c.SSHKey != "" {
req.SSHKey = c.SSHKey
if len(c.Comm.SSHPublicKey) != 0 {
req.SSHKey = string(c.Comm.SSHPublicKey)
}
server_id, server, err := api.CreateServer(&req)

View File

@ -1,9 +1,9 @@
package classic
import (
"bytes"
"context"
"fmt"
"strings"
"github.com/hashicorp/go-oracle-terraform/compute"
"github.com/hashicorp/packer/common/uuid"
@ -25,7 +25,7 @@ func (s *stepAddKeysToAPI) Run(_ context.Context, state multistep.StateBag) mult
}
// grab packer-generated key from statebag context.
sshPublicKey := strings.TrimSpace(state.Get("publicKey").(string))
sshPublicKey := bytes.TrimSpace(config.Comm.SSHPublicKey)
// form API call to add key to compute cloud
sshKeyName := fmt.Sprintf("/Compute-%s/%s/packer_generated_key_%s",
@ -36,7 +36,7 @@ func (s *stepAddKeysToAPI) Run(_ context.Context, state multistep.StateBag) mult
sshKeysClient := client.SSHKeys()
sshKeysInput := compute.CreateSSHKeyInput{
Name: sshKeyName,
Key: sshPublicKey,
Key: string(sshPublicKey),
Enabled: true,
}
@ -48,20 +48,20 @@ func (s *stepAddKeysToAPI) Run(_ context.Context, state multistep.StateBag) mult
state.Put("error", err)
return multistep.ActionHalt
}
state.Put("key_name", keyInfo.Name)
config.Comm.SSHKeyPairName = keyInfo.Name
return multistep.ActionContinue
}
func (s *stepAddKeysToAPI) Cleanup(state multistep.StateBag) {
// Delete the keys we created during this run
keyName, ok := state.GetOk("key_name")
if !ok {
config := state.Get("config").(*Config)
if len(config.Comm.SSHKeyPairName) == 0 {
// No keys were generated; none need to be cleaned up.
return
}
ui := state.Get("ui").(packer.Ui)
ui.Say("Deleting SSH keys...")
deleteInput := compute.DeleteSSHKeyInput{Name: keyName.(string)}
deleteInput := compute.DeleteSSHKeyInput{Name: config.Comm.SSHKeyPairName}
client := state.Get("client").(*compute.ComputeClient)
deleteClient := client.SSHKeys()
err := deleteClient.DeleteSSHKey(&deleteInput)

View File

@ -38,8 +38,7 @@ func (s *stepCreateInstance) Run(_ context.Context, state multistep.StateBag) mu
Attributes: config.attribs,
}
if config.Comm.Type == "ssh" {
keyName := state.Get("key_name").(string)
input.SSHKeys = []string{keyName}
input.SSHKeys = []string{config.Comm.SSHKeyPairName}
}
instanceInfo, err := instanceClient.CreateInstance(input)

View File

@ -75,8 +75,7 @@ func (s *StepKeyPair) Run(_ context.Context, state multistep.StateBag) multistep
return multistep.ActionHalt
}
pubSSHFormat := string(ssh.MarshalAuthorizedKey(pub))
state.Put("publicKey", pubSSHFormat)
s.Comm.SSHPublicKey = ssh.MarshalAuthorizedKey(pub)
// If we're in debug mode, output the private key to the working
// directory.

View File

@ -14,12 +14,12 @@ func (s *stepCreateInstance) Run(ctx context.Context, state multistep.StateBag)
var (
driver = state.Get("driver").(Driver)
ui = state.Get("ui").(packer.Ui)
publicKey = state.Get("publicKey").(string)
config = state.Get("config").(*Config)
)
ui.Say("Creating instance...")
instanceID, err := driver.CreateInstance(ctx, publicKey)
instanceID, err := driver.CreateInstance(ctx, string(config.Comm.SSHPublicKey))
if err != nil {
err = fmt.Errorf("Problem creating instance: %s", err)
ui.Error(err.Error())