fix ssh key handling

This commit is contained in:
Megan Marsh 2018-01-18 13:44:00 -08:00 committed by Matthew Hooker
parent a8a0072049
commit 6556a851dc
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
4 changed files with 43 additions and 8 deletions

View File

@ -6,6 +6,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"runtime" "runtime"
"strings"
"github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/packer/helper/multistep" "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("keyPair", s.KeyPairName)
state.Put("privateKey", string(privateKeyBytes)) state.Put("privateKey", strings.TrimSpace(string(privateKeyBytes)))
return multistep.ActionContinue return multistep.ActionContinue
} }

View File

@ -3,6 +3,7 @@ package classic
import ( import (
"fmt" "fmt"
"log" "log"
"strings"
"github.com/hashicorp/go-cleanhttp" "github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/go-oracle-terraform/compute" "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{ &ocommon.StepKeyPair{
Debug: b.config.PackerDebug, Debug: b.config.PackerDebug,
DebugKeyPath: fmt.Sprintf("oci_classic_%s.pem", b.config.PackerBuildName), DebugKeyPath: fmt.Sprintf("oci_classic_%s.pem", b.config.PackerBuildName),
PrivateKeyFile: b.config.Comm.SSHPrivateKey, PrivateKeyFile: strings.TrimSpace(b.config.Comm.SSHPrivateKey),
}, },
&stepCreateIPReservation{}, &stepCreateIPReservation{},
&stepCreateInstance{}, &stepCreateInstance{},

View File

@ -2,6 +2,7 @@ package classic
import ( import (
"fmt" "fmt"
"strings"
"github.com/hashicorp/go-oracle-terraform/compute" "github.com/hashicorp/go-oracle-terraform/compute"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
@ -16,18 +17,44 @@ func (s *stepCreateInstance) Run(state multistep.StateBag) multistep.StepAction
ui.Say("Creating Instance...") ui.Say("Creating Instance...")
config := state.Get("config").(*Config) config := state.Get("config").(*Config)
client := state.Get("client").(*compute.ComputeClient) 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 // get instances client
instanceClient := client.Instances() instanceClient := client.Instances()
// Instances Input // Instances Input
input := &compute.CreateInstanceInput{ input := &compute.CreateInstanceInput{
Name: config.ImageName, Name: config.ImageName,
Shape: config.Shape, Shape: config.Shape,
ImageList: config.ImageList, ImageList: config.ImageList,
SSHKeys: []string{sshPublicKey}, SSHKeys: []string{keyInfo.Name},
Attributes: map[string]interface{}{},
} }
instanceInfo, err := instanceClient.CreateInstance(input) instanceInfo, err := instanceClient.CreateInstance(input)

6
vendor/vendor.json vendored
View File

@ -785,6 +785,12 @@
"path": "github.com/hashicorp/go-cleanhttp", "path": "github.com/hashicorp/go-cleanhttp",
"revision": "875fb671b3ddc66f8e2f0acc33829c8cb989a38d" "revision": "875fb671b3ddc66f8e2f0acc33829c8cb989a38d"
}, },
{
"checksumSHA1": "1hPCerVn1bWA+9vaAGqnIkRtSFc=",
"path": "github.com/hashicorp/go-getter",
"revision": "cb2c2774e9771bd9568d843be4e59298786550fd",
"revisionTime": "2017-12-20T21:10:09Z"
},
{ {
"checksumSHA1": "lrSl49G23l6NhfilxPM0XFs5rZo=", "checksumSHA1": "lrSl49G23l6NhfilxPM0XFs5rZo=",
"path": "github.com/hashicorp/go-multierror", "path": "github.com/hashicorp/go-multierror",