diff --git a/builder/amazon/common/step_key_pair.go b/builder/amazon/common/step_key_pair.go index 927101fcf..55f5aace7 100644 --- a/builder/amazon/common/step_key_pair.go +++ b/builder/amazon/common/step_key_pair.go @@ -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 } diff --git a/builder/oracle/classic/builder.go b/builder/oracle/classic/builder.go index 4501456d2..b7773ee19 100644 --- a/builder/oracle/classic/builder.go +++ b/builder/oracle/classic/builder.go @@ -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{}, diff --git a/builder/oracle/classic/step_create_instance.go b/builder/oracle/classic/step_create_instance.go index c13338928..fd91ea930 100644 --- a/builder/oracle/classic/step_create_instance.go +++ b/builder/oracle/classic/step_create_instance.go @@ -2,6 +2,7 @@ package classic import ( "fmt" + "strings" "github.com/hashicorp/go-oracle-terraform/compute" "github.com/hashicorp/packer/packer" @@ -16,18 +17,44 @@ 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() // Instances Input input := &compute.CreateInstanceInput{ - Name: config.ImageName, - Shape: config.Shape, - ImageList: config.ImageList, - SSHKeys: []string{sshPublicKey}, - Attributes: map[string]interface{}{}, + Name: config.ImageName, + Shape: config.Shape, + ImageList: config.ImageList, + SSHKeys: []string{keyInfo.Name}, } instanceInfo, err := instanceClient.CreateInstance(input) diff --git a/vendor/vendor.json b/vendor/vendor.json index fe36fe283..256c2e1d2 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -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",