Merge pull request #6768 from mvaude/fix-scaleway-ssh

scaleway: fix builder problems with ssh keys
This commit is contained in:
Adrien Delorme 2018-09-27 16:23:44 +02:00 committed by GitHub
commit 2d899ffcee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 24 deletions

View File

@ -19,7 +19,7 @@ import (
const BuilderId = "hashicorp.scaleway"
type Builder struct {
config Config
config *Config
runner multistep.Runner
}
@ -28,7 +28,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
if errs != nil {
return warnings, errs
}
b.config = *c
b.config = c
return nil, nil
}
@ -50,7 +50,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
steps := []multistep.Step{
&stepCreateSSHKey{
Debug: b.config.PackerDebug,
Comm: &b.config.Comm,
DebugKeyPath: fmt.Sprintf("scw_%s.pem", b.config.PackerBuildName),
},
new(stepCreateServer),

View File

@ -15,7 +15,7 @@ type stepImage struct{}
func (s *stepImage) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*api.ScalewayAPI)
ui := state.Get("ui").(packer.Ui)
c := state.Get("config").(Config)
c := state.Get("config").(*Config)
snapshotID := state.Get("snapshot_id").(string)
bootscriptID := ""

View File

@ -17,8 +17,7 @@ type stepCreateServer struct {
func (s *stepCreateServer) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*api.ScalewayAPI)
ui := state.Get("ui").(packer.Ui)
c := state.Get("config").(Config)
sshPubKey := state.Get("ssh_pubkey").(string)
c := state.Get("config").(*Config)
tags := []string{}
var bootscript *string
@ -28,8 +27,8 @@ func (s *stepCreateServer) Run(_ context.Context, state multistep.StateBag) mult
bootscript = &c.Bootscript
}
if sshPubKey != "" {
tags = []string{fmt.Sprintf("AUTHORIZED_KEY=%s", strings.TrimSpace(sshPubKey))}
if c.Comm.SSHPublicKey != nil {
tags = []string{fmt.Sprintf("AUTHORIZED_KEY=%s", strings.Replace(strings.TrimSpace(string(c.Comm.SSHPublicKey)), " ", "_", -1))}
}
server, err := client.PostServer(api.ScalewayServerDefinition{

View File

@ -1,7 +1,6 @@
package scaleway
import (
"bytes"
"context"
"crypto/rand"
"crypto/rsa"
@ -13,7 +12,6 @@ import (
"os"
"runtime"
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"golang.org/x/crypto/ssh"
@ -21,31 +19,31 @@ import (
type stepCreateSSHKey struct {
Debug bool
Comm *communicator.Config
DebugKeyPath string
}
func (s *stepCreateSSHKey) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
config := state.Get("config").(*Config)
if s.Comm.SSHPrivateKeyFile != "" {
if config.Comm.SSHPrivateKeyFile != "" {
ui.Say("Using existing SSH private key")
privateKeyBytes, err := ioutil.ReadFile(s.Comm.SSHPrivateKeyFile)
privateKeyBytes, err := ioutil.ReadFile(config.Comm.SSHPrivateKeyFile)
if err != nil {
state.Put("error", fmt.Errorf(
"Error loading configured private key file: %s", err))
return multistep.ActionHalt
}
s.Comm.SSHPrivateKey = privateKeyBytes
s.Comm.SSHPublicKey = nil
config.Comm.SSHPrivateKey = privateKeyBytes
config.Comm.SSHPublicKey = nil
return multistep.ActionContinue
}
ui.Say("Creating temporary ssh key for server...")
priv, err := rsa.GenerateKey(rand.Reader, 2014)
priv, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
err := fmt.Errorf("Error creating temporary SSH key: %s", err)
state.Put("error", err)
@ -61,17 +59,19 @@ func (s *stepCreateSSHKey) Run(_ context.Context, state multistep.StateBag) mult
Bytes: priv_der,
}
// Set the private key in the config for later
s.Comm.SSHPrivateKey = pem.EncodeToMemory(&priv_blk)
pub, _ := ssh.NewPublicKey(&priv.PublicKey)
pub_sshformat := ssh.MarshalAuthorizedKey(pub)
pub_sshformat = bytes.Replace(pub_sshformat, []byte(" "), []byte("_"), -1)
pub, err := ssh.NewPublicKey(&priv.PublicKey)
if err != nil {
err := fmt.Errorf("Error creating temporary SSH key: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
log.Printf("temporary ssh key created")
// Remember some state for the future
s.Comm.SSHPublicKey = pub_sshformat
config.Comm.SSHPrivateKey = pem.EncodeToMemory(&priv_blk)
config.Comm.SSHPublicKey = ssh.MarshalAuthorizedKey(pub)
// If we're in debug mode, output the private key to the working directory.
if s.Debug {

View File

@ -15,7 +15,7 @@ type stepSnapshot struct{}
func (s *stepSnapshot) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*api.ScalewayAPI)
ui := state.Get("ui").(packer.Ui)
c := state.Get("config").(Config)
c := state.Get("config").(*Config)
volumeID := state.Get("root_volume_id").(string)
ui.Say(fmt.Sprintf("Creating snapshot: %v", c.SnapshotName))