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"` Token string `mapstructure:"token"`
Url string `mapstructure:"url"` Url string `mapstructure:"url"`
SSHKey string
SnapshotName string `mapstructure:"image_name"` SnapshotName string `mapstructure:"image_name"`
DataCenterName string `mapstructure:"data_center_name"` DataCenterName string `mapstructure:"data_center_name"`
DataCenterId string DataCenterId string

View File

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

View File

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

View File

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

View File

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

View File

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