builder/googlecompute: Use ssh_private_key_file if provided
This seemed to be missing from the googlecompute provider. Now if the ssh_private_key_file is provided, that will be used in place of a temporary key. I didn't update the googlecompute specific docs under `./website/`, since this parameter is already documented under the communicators templates page.
This commit is contained in:
parent
3ee8cbc1f1
commit
d70e783455
|
@ -54,6 +54,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
&StepCreateSSHKey{
|
&StepCreateSSHKey{
|
||||||
Debug: b.config.PackerDebug,
|
Debug: b.config.PackerDebug,
|
||||||
DebugKeyPath: fmt.Sprintf("gce_%s.pem", b.config.PackerBuildName),
|
DebugKeyPath: fmt.Sprintf("gce_%s.pem", b.config.PackerBuildName),
|
||||||
|
PrivateKeyFile: b.config.Comm.SSHPrivateKey,
|
||||||
},
|
},
|
||||||
&StepCreateInstance{
|
&StepCreateInstance{
|
||||||
Debug: b.config.PackerDebug,
|
Debug: b.config.PackerDebug,
|
||||||
|
|
|
@ -24,13 +24,17 @@ func (c *Config) createInstanceMetadata(sourceImage *Image, sshPublicKey string)
|
||||||
instanceMetadata[k] = v
|
instanceMetadata[k] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
// Merge any existing ssh keys with our public key.
|
// Merge any existing ssh keys with our public key, unless there is no
|
||||||
|
// supplied public key. This is possible if a private_key_file was
|
||||||
|
// specified.
|
||||||
|
if sshPublicKey != "" {
|
||||||
sshMetaKey := "sshKeys"
|
sshMetaKey := "sshKeys"
|
||||||
sshKeys := fmt.Sprintf("%s:%s", c.Comm.SSHUsername, sshPublicKey)
|
sshKeys := fmt.Sprintf("%s:%s", c.Comm.SSHUsername, sshPublicKey)
|
||||||
if confSshKeys, exists := instanceMetadata[sshMetaKey]; exists {
|
if confSshKeys, exists := instanceMetadata[sshMetaKey]; exists {
|
||||||
sshKeys = fmt.Sprintf("%s\n%s", sshKeys, confSshKeys)
|
sshKeys = fmt.Sprintf("%s\n%s", sshKeys, confSshKeys)
|
||||||
}
|
}
|
||||||
instanceMetadata[sshMetaKey] = sshKeys
|
instanceMetadata[sshMetaKey] = sshKeys
|
||||||
|
}
|
||||||
|
|
||||||
// Wrap any startup script with our own startup script.
|
// Wrap any startup script with our own startup script.
|
||||||
if c.StartupScriptFile != "" {
|
if c.StartupScriptFile != "" {
|
||||||
|
@ -65,9 +69,13 @@ func getImage(c *Config, d Driver) (*Image, error) {
|
||||||
func (s *StepCreateInstance) Run(state multistep.StateBag) multistep.StepAction {
|
func (s *StepCreateInstance) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
c := state.Get("config").(*Config)
|
c := state.Get("config").(*Config)
|
||||||
d := state.Get("driver").(Driver)
|
d := state.Get("driver").(Driver)
|
||||||
sshPublicKey := state.Get("ssh_public_key").(string)
|
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
|
|
||||||
|
sshPublicKey := ""
|
||||||
|
if sshPublicKeyRaw, ok := state.GetOk("ssh_public_key"); ok {
|
||||||
|
sshPublicKey = sshPublicKeyRaw.(string)
|
||||||
|
}
|
||||||
|
|
||||||
sourceImage, err := getImage(c, d)
|
sourceImage, err := getImage(c, d)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := fmt.Errorf("Error getting source image for instance creation: %s", err)
|
err := fmt.Errorf("Error getting source image for instance creation: %s", err)
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/mitchellh/multistep"
|
"github.com/mitchellh/multistep"
|
||||||
|
@ -17,6 +18,7 @@ import (
|
||||||
type StepCreateSSHKey struct {
|
type StepCreateSSHKey struct {
|
||||||
Debug bool
|
Debug bool
|
||||||
DebugKeyPath string
|
DebugKeyPath string
|
||||||
|
PrivateKeyFile string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run executes the Packer build step that generates SSH key pairs.
|
// Run executes the Packer build step that generates SSH key pairs.
|
||||||
|
@ -25,6 +27,20 @@ type StepCreateSSHKey struct {
|
||||||
func (s *StepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction {
|
func (s *StepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
|
|
||||||
|
if s.PrivateKeyFile != "" {
|
||||||
|
ui.Say("Using existing SSH private key")
|
||||||
|
privateKeyBytes, err := ioutil.ReadFile(s.PrivateKeyFile)
|
||||||
|
if err != nil {
|
||||||
|
state.Put("error", fmt.Errorf(
|
||||||
|
"Error loading configured private key file: %s", err))
|
||||||
|
return multistep.ActionHalt
|
||||||
|
}
|
||||||
|
|
||||||
|
state.Put("ssh_private_key", string(privateKeyBytes))
|
||||||
|
|
||||||
|
return multistep.ActionContinue
|
||||||
|
}
|
||||||
|
|
||||||
ui.Say("Creating temporary SSH key for instance...")
|
ui.Say("Creating temporary SSH key for instance...")
|
||||||
priv, err := rsa.GenerateKey(rand.Reader, 2048)
|
priv, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue