2017-07-16 02:26:48 -04:00
|
|
|
package cloudstack
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2017-07-16 02:26:48 -04:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
|
2018-08-28 11:47:19 -04:00
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-07-16 02:26:48 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/xanzy/go-cloudstack/cloudstack"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepKeypair struct {
|
2018-08-28 11:47:19 -04:00
|
|
|
Debug bool
|
|
|
|
Comm *communicator.Config
|
|
|
|
DebugKeyPath string
|
2017-07-16 02:26:48 -04:00
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *stepKeypair) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2017-07-16 02:26:48 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
2018-08-28 11:47:19 -04:00
|
|
|
if s.Comm.SSHPrivateKeyFile != "" {
|
|
|
|
privateKeyBytes, err := ioutil.ReadFile(s.Comm.SSHPrivateKeyFile)
|
2017-07-16 02:26:48 -04:00
|
|
|
if err != nil {
|
|
|
|
state.Put("error", fmt.Errorf(
|
|
|
|
"Error loading configured private key file: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2018-08-28 11:47:19 -04:00
|
|
|
s.Comm.SSHPrivateKey = privateKeyBytes
|
2017-07-16 02:26:48 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2018-08-28 11:47:19 -04:00
|
|
|
if s.Comm.SSHAgentAuth && s.Comm.SSHKeyPairName == "" {
|
2017-07-16 02:26:48 -04:00
|
|
|
ui.Say("Using SSH Agent with keypair in Source image")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2018-08-28 11:47:19 -04:00
|
|
|
if s.Comm.SSHAgentAuth && s.Comm.SSHKeyPairName != "" {
|
|
|
|
ui.Say(fmt.Sprintf("Using SSH Agent for existing keypair %s", s.Comm.SSHKeyPairName))
|
|
|
|
state.Put("keypair", s.Comm.SSHKeyPairName)
|
2017-07-16 02:26:48 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2018-08-28 11:47:19 -04:00
|
|
|
if s.Comm.SSHTemporaryKeyPairName == "" {
|
2017-07-26 15:34:11 -04:00
|
|
|
ui.Say("Not using a keypair")
|
2017-07-16 02:26:48 -04:00
|
|
|
state.Put("keypair", "")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
client := state.Get("client").(*cloudstack.CloudStackClient)
|
|
|
|
|
2018-08-28 11:47:19 -04:00
|
|
|
ui.Say(fmt.Sprintf("Creating temporary keypair: %s ...", s.Comm.SSHTemporaryKeyPairName))
|
2017-07-16 02:26:48 -04:00
|
|
|
|
2018-08-28 11:47:19 -04:00
|
|
|
p := client.SSH.NewCreateSSHKeyPairParams(s.Comm.SSHTemporaryKeyPairName)
|
2018-07-13 11:56:27 -04:00
|
|
|
|
|
|
|
cfg := state.Get("config").(*Config)
|
|
|
|
if cfg.Project != "" {
|
|
|
|
p.SetProjectid(cfg.Project)
|
|
|
|
}
|
|
|
|
|
2017-07-16 02:26:48 -04:00
|
|
|
keypair, err := client.SSH.CreateSSHKeyPair(p)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating temporary keypair: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
if keypair.Privatekey == "" {
|
|
|
|
err := fmt.Errorf("The temporary keypair returned was blank")
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2018-08-28 11:47:19 -04:00
|
|
|
ui.Say(fmt.Sprintf("Created temporary keypair: %s", s.Comm.SSHTemporaryKeyPairName))
|
2017-07-16 02:26:48 -04:00
|
|
|
|
2017-07-26 15:34:11 -04:00
|
|
|
// If we're in debug mode, output the private key to the working directory.
|
2017-07-16 02:26:48 -04:00
|
|
|
if s.Debug {
|
|
|
|
ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", s.DebugKeyPath))
|
|
|
|
f, err := os.Create(s.DebugKeyPath)
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Error saving debug key: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
// Write the key out
|
|
|
|
if _, err := f.Write([]byte(keypair.Privatekey)); err != nil {
|
|
|
|
err := fmt.Errorf("Error saving debug key: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chmod it so that it is SSH ready
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
if err := f.Chmod(0600); err != nil {
|
|
|
|
err := fmt.Errorf("Error setting permissions of debug key: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set some state data for use in future steps
|
2018-08-28 11:47:19 -04:00
|
|
|
state.Put("keypair", s.Comm.SSHTemporaryKeyPairName)
|
2017-07-16 02:26:48 -04:00
|
|
|
state.Put("privateKey", keypair.Privatekey)
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepKeypair) Cleanup(state multistep.StateBag) {
|
2018-08-28 11:47:19 -04:00
|
|
|
if s.Comm.SSHTemporaryKeyPairName == "" {
|
2017-07-16 02:26:48 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
client := state.Get("client").(*cloudstack.CloudStackClient)
|
2018-07-13 11:56:27 -04:00
|
|
|
cfg := state.Get("config").(*Config)
|
|
|
|
|
2018-08-28 11:47:19 -04:00
|
|
|
p := client.SSH.NewDeleteSSHKeyPairParams(s.Comm.SSHTemporaryKeyPairName)
|
2018-07-13 11:56:27 -04:00
|
|
|
if cfg.Project != "" {
|
|
|
|
p.SetProjectid(cfg.Project)
|
|
|
|
}
|
2017-07-16 02:26:48 -04:00
|
|
|
|
2018-08-28 11:47:19 -04:00
|
|
|
ui.Say(fmt.Sprintf("Deleting temporary keypair: %s ...", s.Comm.SSHTemporaryKeyPairName))
|
2017-07-16 02:26:48 -04:00
|
|
|
|
2018-07-13 11:56:27 -04:00
|
|
|
_, err := client.SSH.DeleteSSHKeyPair(p)
|
2017-07-16 02:26:48 -04:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(err.Error())
|
|
|
|
ui.Error(fmt.Sprintf(
|
2018-08-28 11:47:19 -04:00
|
|
|
"Error cleaning up keypair. Please delete the key manually: %s", s.Comm.SSHTemporaryKeyPairName))
|
2017-07-16 02:26:48 -04:00
|
|
|
}
|
|
|
|
}
|