fix ssh key handling
This commit is contained in:
parent
a8a0072049
commit
6556a851dc
|
@ -6,6 +6,7 @@ import (
|
|||
"io/ioutil"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
"github.com/hashicorp/packer/helper/multistep"
|
||||
|
@ -36,7 +37,7 @@ func (s *StepKeyPair) Run(_ context.Context, state multistep.StateBag) multistep
|
|||
}
|
||||
|
||||
state.Put("keyPair", s.KeyPairName)
|
||||
state.Put("privateKey", string(privateKeyBytes))
|
||||
state.Put("privateKey", strings.TrimSpace(string(privateKeyBytes)))
|
||||
|
||||
return multistep.ActionContinue
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package classic
|
|||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/go-cleanhttp"
|
||||
"github.com/hashicorp/go-oracle-terraform/compute"
|
||||
|
@ -63,7 +64,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
|||
&ocommon.StepKeyPair{
|
||||
Debug: b.config.PackerDebug,
|
||||
DebugKeyPath: fmt.Sprintf("oci_classic_%s.pem", b.config.PackerBuildName),
|
||||
PrivateKeyFile: b.config.Comm.SSHPrivateKey,
|
||||
PrivateKeyFile: strings.TrimSpace(b.config.Comm.SSHPrivateKey),
|
||||
},
|
||||
&stepCreateIPReservation{},
|
||||
&stepCreateInstance{},
|
||||
|
|
|
@ -2,6 +2,7 @@ package classic
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/go-oracle-terraform/compute"
|
||||
"github.com/hashicorp/packer/packer"
|
||||
|
@ -16,7 +17,34 @@ func (s *stepCreateInstance) Run(state multistep.StateBag) multistep.StepAction
|
|||
ui.Say("Creating Instance...")
|
||||
config := state.Get("config").(*Config)
|
||||
client := state.Get("client").(*compute.ComputeClient)
|
||||
sshPublicKey := state.Get("publicKey").(string)
|
||||
sshPublicKey := strings.TrimSpace(state.Get("publicKey").(string))
|
||||
|
||||
// Load the dynamically-generated SSH key into the Oracle Compute cloud.
|
||||
sshKeyName := fmt.Sprintf("/Compute-%s/%s/packer_dynamic_key", config.IdentityDomain, config.Username)
|
||||
|
||||
sshKeysClient := client.SSHKeys()
|
||||
sshKeysInput := compute.CreateSSHKeyInput{
|
||||
Name: sshKeyName,
|
||||
Key: sshPublicKey,
|
||||
Enabled: true,
|
||||
}
|
||||
keyInfo, err := sshKeysClient.CreateSSHKey(&sshKeysInput)
|
||||
if err != nil {
|
||||
// Key already exists; update key instead
|
||||
if strings.Contains(err.Error(), "packer_dynamic_key already exists") {
|
||||
updateKeysInput := compute.UpdateSSHKeyInput{
|
||||
Name: sshKeyName,
|
||||
Key: sshPublicKey,
|
||||
Enabled: true,
|
||||
}
|
||||
keyInfo, err = sshKeysClient.UpdateSSHKey(&updateKeysInput)
|
||||
} else {
|
||||
err = fmt.Errorf("Problem adding Public SSH key through Oracle's API: %s", err)
|
||||
ui.Error(err.Error())
|
||||
state.Put("error", err)
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
}
|
||||
|
||||
// get instances client
|
||||
instanceClient := client.Instances()
|
||||
|
@ -26,8 +54,7 @@ func (s *stepCreateInstance) Run(state multistep.StateBag) multistep.StepAction
|
|||
Name: config.ImageName,
|
||||
Shape: config.Shape,
|
||||
ImageList: config.ImageList,
|
||||
SSHKeys: []string{sshPublicKey},
|
||||
Attributes: map[string]interface{}{},
|
||||
SSHKeys: []string{keyInfo.Name},
|
||||
}
|
||||
|
||||
instanceInfo, err := instanceClient.CreateInstance(input)
|
||||
|
|
|
@ -785,6 +785,12 @@
|
|||
"path": "github.com/hashicorp/go-cleanhttp",
|
||||
"revision": "875fb671b3ddc66f8e2f0acc33829c8cb989a38d"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "1hPCerVn1bWA+9vaAGqnIkRtSFc=",
|
||||
"path": "github.com/hashicorp/go-getter",
|
||||
"revision": "cb2c2774e9771bd9568d843be4e59298786550fd",
|
||||
"revisionTime": "2017-12-20T21:10:09Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "lrSl49G23l6NhfilxPM0XFs5rZo=",
|
||||
"path": "github.com/hashicorp/go-multierror",
|
||||
|
|
Loading…
Reference in New Issue