2018-01-22 19:54:49 -05:00
|
|
|
package classic
|
|
|
|
|
|
|
|
import (
|
2018-08-29 08:01:55 -04:00
|
|
|
"bytes"
|
2018-01-25 17:43:55 -05:00
|
|
|
"context"
|
2018-01-22 19:54:49 -05:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-oracle-terraform/compute"
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2018-01-22 19:54:49 -05:00
|
|
|
)
|
|
|
|
|
2018-10-26 16:00:45 -04:00
|
|
|
type stepAddKeysToAPI struct {
|
2018-10-26 18:47:49 -04:00
|
|
|
Skip bool
|
|
|
|
KeyName string
|
2018-10-26 16:00:45 -04:00
|
|
|
}
|
2018-01-22 19:54:49 -05:00
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepAddKeysToAPI) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-01-22 19:54:49 -05:00
|
|
|
// get variables from state
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2018-01-22 19:54:49 -05:00
|
|
|
config := state.Get("config").(*Config)
|
2018-10-24 17:08:11 -04:00
|
|
|
client := state.Get("client").(*compute.Client)
|
2018-01-22 19:54:49 -05:00
|
|
|
|
2018-10-26 16:00:45 -04:00
|
|
|
if s.Skip {
|
|
|
|
ui.Say("Skipping generating SSH keys...")
|
2018-02-08 17:55:04 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
2018-01-22 19:54:49 -05:00
|
|
|
// grab packer-generated key from statebag context.
|
2018-10-26 16:00:45 -04:00
|
|
|
// Always check configured communicator for keys
|
2018-08-29 08:01:55 -04:00
|
|
|
sshPublicKey := bytes.TrimSpace(config.Comm.SSHPublicKey)
|
2018-01-22 19:54:49 -05:00
|
|
|
|
|
|
|
// form API call to add key to compute cloud
|
|
|
|
|
2018-10-26 18:47:49 -04:00
|
|
|
ui.Say(fmt.Sprintf("Creating temporary key: %s", s.KeyName))
|
2018-01-26 16:12:35 -05:00
|
|
|
|
2018-01-22 19:54:49 -05:00
|
|
|
sshKeysClient := client.SSHKeys()
|
|
|
|
sshKeysInput := compute.CreateSSHKeyInput{
|
2018-10-26 18:47:49 -04:00
|
|
|
Name: s.KeyName,
|
2018-08-29 08:01:55 -04:00
|
|
|
Key: string(sshPublicKey),
|
2018-01-22 19:54:49 -05:00
|
|
|
Enabled: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the packer-generated SSH key into the Oracle Compute cloud.
|
|
|
|
keyInfo, err := sshKeysClient.CreateSSHKey(&sshKeysInput)
|
|
|
|
if err != nil {
|
2018-01-26 12:51:16 -05:00
|
|
|
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
|
2018-01-22 19:54:49 -05:00
|
|
|
}
|
2018-08-29 08:01:55 -04:00
|
|
|
config.Comm.SSHKeyPairName = keyInfo.Name
|
2018-01-22 19:54:49 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepAddKeysToAPI) Cleanup(state multistep.StateBag) {
|
2018-10-26 16:00:45 -04:00
|
|
|
if s.Skip {
|
|
|
|
return
|
|
|
|
}
|
2018-08-29 08:01:55 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2018-10-26 16:00:45 -04:00
|
|
|
// Delete the keys we created during this run
|
2018-08-29 08:01:55 -04:00
|
|
|
if len(config.Comm.SSHKeyPairName) == 0 {
|
2018-02-08 17:55:04 -05:00
|
|
|
// No keys were generated; none need to be cleaned up.
|
|
|
|
return
|
|
|
|
}
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2018-01-26 12:51:16 -05:00
|
|
|
ui.Say("Deleting SSH keys...")
|
2018-08-29 08:01:55 -04:00
|
|
|
deleteInput := compute.DeleteSSHKeyInput{Name: config.Comm.SSHKeyPairName}
|
2018-10-24 17:08:11 -04:00
|
|
|
client := state.Get("client").(*compute.Client)
|
2018-01-26 12:51:16 -05:00
|
|
|
deleteClient := client.SSHKeys()
|
|
|
|
err := deleteClient.DeleteSSHKey(&deleteInput)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error deleting SSH keys: %s", err.Error()))
|
|
|
|
}
|
|
|
|
return
|
2018-01-22 19:54:49 -05:00
|
|
|
}
|