builder.cloudstack: use c.Comm for ssh

This commit is contained in:
Adrien Delorme 2018-08-28 17:47:19 +02:00
parent ef4ca9c48e
commit e0e6b0b8f7
3 changed files with 25 additions and 31 deletions

View File

@ -64,12 +64,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
HTTPPortMax: b.config.HTTPPortMax, HTTPPortMax: b.config.HTTPPortMax,
}, },
&stepKeypair{ &stepKeypair{
Debug: b.config.PackerDebug, Debug: b.config.PackerDebug,
DebugKeyPath: fmt.Sprintf("cs_%s.pem", b.config.PackerBuildName), Comm: &b.config.Comm,
KeyPair: b.config.Keypair, DebugKeyPath: fmt.Sprintf("cs_%s.pem", b.config.PackerBuildName),
PrivateKeyFile: b.config.Comm.SSHPrivateKeyFile,
SSHAgentAuth: b.config.Comm.SSHAgentAuth,
TemporaryKeyPairName: b.config.TemporaryKeypairName,
}, },
&stepCreateSecurityGroup{}, &stepCreateSecurityGroup{},
&stepCreateInstance{ &stepCreateInstance{

View File

@ -126,9 +126,9 @@ func NewConfig(raws ...interface{}) (*Config, error) {
// If we are not given an explicit keypair, ssh_password or ssh_private_key_file, // If we are not given an explicit keypair, ssh_password or ssh_private_key_file,
// then create a temporary one, but only if the temporary_keypair_name has not // then create a temporary one, but only if the temporary_keypair_name has not
// been provided. // been provided.
if c.Keypair == "" && c.TemporaryKeypairName == "" && if c.Keypair == "" && c.Comm.SSHTemporaryKeyPairName == "" &&
c.Comm.SSHPrivateKeyFile == "" && c.Comm.SSHPassword == "" { c.Comm.SSHPrivateKeyFile == "" && c.Comm.SSHPassword == "" {
c.TemporaryKeypairName = fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID()) c.Comm.SSHTemporaryKeyPairName = fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID())
} }
// Process required parameters. // Process required parameters.

View File

@ -7,49 +7,46 @@ import (
"os" "os"
"runtime" "runtime"
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
"github.com/xanzy/go-cloudstack/cloudstack" "github.com/xanzy/go-cloudstack/cloudstack"
) )
type stepKeypair struct { type stepKeypair struct {
Debug bool Debug bool
DebugKeyPath string Comm *communicator.Config
KeyPair string DebugKeyPath string
PrivateKeyFile string
SSHAgentAuth bool
TemporaryKeyPairName string
} }
func (s *stepKeypair) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { func (s *stepKeypair) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
if s.PrivateKeyFile != "" { if s.Comm.SSHPrivateKeyFile != "" {
privateKeyBytes, err := ioutil.ReadFile(s.PrivateKeyFile) privateKeyBytes, err := ioutil.ReadFile(s.Comm.SSHPrivateKeyFile)
if err != nil { if err != nil {
state.Put("error", fmt.Errorf( state.Put("error", fmt.Errorf(
"Error loading configured private key file: %s", err)) "Error loading configured private key file: %s", err))
return multistep.ActionHalt return multistep.ActionHalt
} }
state.Put("keypair", s.KeyPair) s.Comm.SSHPrivateKey = privateKeyBytes
state.Put("privateKey", string(privateKeyBytes))
return multistep.ActionContinue return multistep.ActionContinue
} }
if s.SSHAgentAuth && s.KeyPair == "" { if s.Comm.SSHAgentAuth && s.Comm.SSHKeyPairName == "" {
ui.Say("Using SSH Agent with keypair in Source image") ui.Say("Using SSH Agent with keypair in Source image")
return multistep.ActionContinue return multistep.ActionContinue
} }
if s.SSHAgentAuth && s.KeyPair != "" { if s.Comm.SSHAgentAuth && s.Comm.SSHKeyPairName != "" {
ui.Say(fmt.Sprintf("Using SSH Agent for existing keypair %s", s.KeyPair)) ui.Say(fmt.Sprintf("Using SSH Agent for existing keypair %s", s.Comm.SSHKeyPairName))
state.Put("keypair", s.KeyPair) state.Put("keypair", s.Comm.SSHKeyPairName)
return multistep.ActionContinue return multistep.ActionContinue
} }
if s.TemporaryKeyPairName == "" { if s.Comm.SSHTemporaryKeyPairName == "" {
ui.Say("Not using a keypair") ui.Say("Not using a keypair")
state.Put("keypair", "") state.Put("keypair", "")
return multistep.ActionContinue return multistep.ActionContinue
@ -57,9 +54,9 @@ func (s *stepKeypair) Run(_ context.Context, state multistep.StateBag) multistep
client := state.Get("client").(*cloudstack.CloudStackClient) client := state.Get("client").(*cloudstack.CloudStackClient)
ui.Say(fmt.Sprintf("Creating temporary keypair: %s ...", s.TemporaryKeyPairName)) ui.Say(fmt.Sprintf("Creating temporary keypair: %s ...", s.Comm.SSHTemporaryKeyPairName))
p := client.SSH.NewCreateSSHKeyPairParams(s.TemporaryKeyPairName) p := client.SSH.NewCreateSSHKeyPairParams(s.Comm.SSHTemporaryKeyPairName)
cfg := state.Get("config").(*Config) cfg := state.Get("config").(*Config)
if cfg.Project != "" { if cfg.Project != "" {
@ -81,7 +78,7 @@ func (s *stepKeypair) Run(_ context.Context, state multistep.StateBag) multistep
return multistep.ActionHalt return multistep.ActionHalt
} }
ui.Say(fmt.Sprintf("Created temporary keypair: %s", s.TemporaryKeyPairName)) ui.Say(fmt.Sprintf("Created temporary keypair: %s", s.Comm.SSHTemporaryKeyPairName))
// If we're in debug mode, output the private key to the working directory. // If we're in debug mode, output the private key to the working directory.
if s.Debug { if s.Debug {
@ -113,14 +110,14 @@ func (s *stepKeypair) Run(_ context.Context, state multistep.StateBag) multistep
} }
// Set some state data for use in future steps // Set some state data for use in future steps
state.Put("keypair", s.TemporaryKeyPairName) state.Put("keypair", s.Comm.SSHTemporaryKeyPairName)
state.Put("privateKey", keypair.Privatekey) state.Put("privateKey", keypair.Privatekey)
return multistep.ActionContinue return multistep.ActionContinue
} }
func (s *stepKeypair) Cleanup(state multistep.StateBag) { func (s *stepKeypair) Cleanup(state multistep.StateBag) {
if s.TemporaryKeyPairName == "" { if s.Comm.SSHTemporaryKeyPairName == "" {
return return
} }
@ -128,17 +125,17 @@ func (s *stepKeypair) Cleanup(state multistep.StateBag) {
client := state.Get("client").(*cloudstack.CloudStackClient) client := state.Get("client").(*cloudstack.CloudStackClient)
cfg := state.Get("config").(*Config) cfg := state.Get("config").(*Config)
p := client.SSH.NewDeleteSSHKeyPairParams(s.TemporaryKeyPairName) p := client.SSH.NewDeleteSSHKeyPairParams(s.Comm.SSHTemporaryKeyPairName)
if cfg.Project != "" { if cfg.Project != "" {
p.SetProjectid(cfg.Project) p.SetProjectid(cfg.Project)
} }
ui.Say(fmt.Sprintf("Deleting temporary keypair: %s ...", s.TemporaryKeyPairName)) ui.Say(fmt.Sprintf("Deleting temporary keypair: %s ...", s.Comm.SSHTemporaryKeyPairName))
_, err := client.SSH.DeleteSSHKeyPair(p) _, err := client.SSH.DeleteSSHKeyPair(p)
if err != nil { if err != nil {
ui.Error(err.Error()) ui.Error(err.Error())
ui.Error(fmt.Sprintf( ui.Error(fmt.Sprintf(
"Error cleaning up keypair. Please delete the key manually: %s", s.TemporaryKeyPairName)) "Error cleaning up keypair. Please delete the key manually: %s", s.Comm.SSHTemporaryKeyPairName))
} }
} }