Merge pull request #6768 from mvaude/fix-scaleway-ssh
scaleway: fix builder problems with ssh keys
This commit is contained in:
commit
2d899ffcee
|
@ -19,7 +19,7 @@ import (
|
||||||
const BuilderId = "hashicorp.scaleway"
|
const BuilderId = "hashicorp.scaleway"
|
||||||
|
|
||||||
type Builder struct {
|
type Builder struct {
|
||||||
config Config
|
config *Config
|
||||||
runner multistep.Runner
|
runner multistep.Runner
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
|
||||||
if errs != nil {
|
if errs != nil {
|
||||||
return warnings, errs
|
return warnings, errs
|
||||||
}
|
}
|
||||||
b.config = *c
|
b.config = c
|
||||||
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
steps := []multistep.Step{
|
steps := []multistep.Step{
|
||||||
&stepCreateSSHKey{
|
&stepCreateSSHKey{
|
||||||
Debug: b.config.PackerDebug,
|
Debug: b.config.PackerDebug,
|
||||||
Comm: &b.config.Comm,
|
|
||||||
DebugKeyPath: fmt.Sprintf("scw_%s.pem", b.config.PackerBuildName),
|
DebugKeyPath: fmt.Sprintf("scw_%s.pem", b.config.PackerBuildName),
|
||||||
},
|
},
|
||||||
new(stepCreateServer),
|
new(stepCreateServer),
|
||||||
|
|
|
@ -15,7 +15,7 @@ type stepImage struct{}
|
||||||
func (s *stepImage) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
func (s *stepImage) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
||||||
client := state.Get("client").(*api.ScalewayAPI)
|
client := state.Get("client").(*api.ScalewayAPI)
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
c := state.Get("config").(Config)
|
c := state.Get("config").(*Config)
|
||||||
snapshotID := state.Get("snapshot_id").(string)
|
snapshotID := state.Get("snapshot_id").(string)
|
||||||
bootscriptID := ""
|
bootscriptID := ""
|
||||||
|
|
||||||
|
|
|
@ -17,8 +17,7 @@ type stepCreateServer struct {
|
||||||
func (s *stepCreateServer) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
func (s *stepCreateServer) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
||||||
client := state.Get("client").(*api.ScalewayAPI)
|
client := state.Get("client").(*api.ScalewayAPI)
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
c := state.Get("config").(Config)
|
c := state.Get("config").(*Config)
|
||||||
sshPubKey := state.Get("ssh_pubkey").(string)
|
|
||||||
tags := []string{}
|
tags := []string{}
|
||||||
var bootscript *string
|
var bootscript *string
|
||||||
|
|
||||||
|
@ -28,8 +27,8 @@ func (s *stepCreateServer) Run(_ context.Context, state multistep.StateBag) mult
|
||||||
bootscript = &c.Bootscript
|
bootscript = &c.Bootscript
|
||||||
}
|
}
|
||||||
|
|
||||||
if sshPubKey != "" {
|
if c.Comm.SSHPublicKey != nil {
|
||||||
tags = []string{fmt.Sprintf("AUTHORIZED_KEY=%s", strings.TrimSpace(sshPubKey))}
|
tags = []string{fmt.Sprintf("AUTHORIZED_KEY=%s", strings.Replace(strings.TrimSpace(string(c.Comm.SSHPublicKey)), " ", "_", -1))}
|
||||||
}
|
}
|
||||||
|
|
||||||
server, err := client.PostServer(api.ScalewayServerDefinition{
|
server, err := client.PostServer(api.ScalewayServerDefinition{
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package scaleway
|
package scaleway
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
|
@ -13,7 +12,6 @@ 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"
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
|
@ -21,31 +19,31 @@ import (
|
||||||
|
|
||||||
type stepCreateSSHKey struct {
|
type stepCreateSSHKey struct {
|
||||||
Debug bool
|
Debug bool
|
||||||
Comm *communicator.Config
|
|
||||||
DebugKeyPath string
|
DebugKeyPath string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *stepCreateSSHKey) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
func (s *stepCreateSSHKey) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
||||||
ui := state.Get("ui").(packer.Ui)
|
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")
|
ui.Say("Using existing SSH private key")
|
||||||
privateKeyBytes, err := ioutil.ReadFile(s.Comm.SSHPrivateKeyFile)
|
privateKeyBytes, err := ioutil.ReadFile(config.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
|
||||||
}
|
}
|
||||||
|
|
||||||
s.Comm.SSHPrivateKey = privateKeyBytes
|
config.Comm.SSHPrivateKey = privateKeyBytes
|
||||||
s.Comm.SSHPublicKey = nil
|
config.Comm.SSHPublicKey = nil
|
||||||
|
|
||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
ui.Say("Creating temporary ssh key for server...")
|
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 {
|
if err != nil {
|
||||||
err := fmt.Errorf("Error creating temporary SSH key: %s", err)
|
err := fmt.Errorf("Error creating temporary SSH key: %s", err)
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
|
@ -61,17 +59,19 @@ func (s *stepCreateSSHKey) Run(_ context.Context, state multistep.StateBag) mult
|
||||||
Bytes: priv_der,
|
Bytes: priv_der,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the private key in the config for later
|
pub, err := ssh.NewPublicKey(&priv.PublicKey)
|
||||||
s.Comm.SSHPrivateKey = pem.EncodeToMemory(&priv_blk)
|
if err != nil {
|
||||||
|
err := fmt.Errorf("Error creating temporary SSH key: %s", err)
|
||||||
pub, _ := ssh.NewPublicKey(&priv.PublicKey)
|
state.Put("error", err)
|
||||||
pub_sshformat := ssh.MarshalAuthorizedKey(pub)
|
ui.Error(err.Error())
|
||||||
pub_sshformat = bytes.Replace(pub_sshformat, []byte(" "), []byte("_"), -1)
|
return multistep.ActionHalt
|
||||||
|
}
|
||||||
|
|
||||||
log.Printf("temporary ssh key created")
|
log.Printf("temporary ssh key created")
|
||||||
|
|
||||||
// Remember some state for the future
|
// 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 we're in debug mode, output the private key to the working directory.
|
||||||
if s.Debug {
|
if s.Debug {
|
||||||
|
|
|
@ -15,7 +15,7 @@ type stepSnapshot struct{}
|
||||||
func (s *stepSnapshot) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
func (s *stepSnapshot) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
||||||
client := state.Get("client").(*api.ScalewayAPI)
|
client := state.Get("client").(*api.ScalewayAPI)
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
c := state.Get("config").(Config)
|
c := state.Get("config").(*Config)
|
||||||
volumeID := state.Get("root_volume_id").(string)
|
volumeID := state.Get("root_volume_id").(string)
|
||||||
|
|
||||||
ui.Say(fmt.Sprintf("Creating snapshot: %v", c.SnapshotName))
|
ui.Say(fmt.Sprintf("Creating snapshot: %v", c.SnapshotName))
|
||||||
|
|
Loading…
Reference in New Issue