use public ssh key & key name from config instead of the statebag
This commit is contained in:
parent
0938b640cc
commit
cd851f8ac2
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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())
|
||||
|
|
Loading…
Reference in New Issue