From 2de93c5ae699838cccb9720f912fc55a2a3e8c9b Mon Sep 17 00:00:00 2001 From: Edouard BONLIEU Date: Wed, 19 Apr 2017 11:10:52 +0200 Subject: [PATCH] Add existing SSH key support Update documentation --- builder/scaleway/builder.go | 5 +++-- builder/scaleway/step_create_server.go | 7 ++++++- builder/scaleway/step_create_ssh_key.go | 21 +++++++++++++++++-- website/source/docs/builders/scaleway.html.md | 8 ++++++- 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/builder/scaleway/builder.go b/builder/scaleway/builder.go index e6265395a..2277808e0 100644 --- a/builder/scaleway/builder.go +++ b/builder/scaleway/builder.go @@ -43,8 +43,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe steps := []multistep.Step{ &stepCreateSSHKey{ - Debug: b.config.PackerDebug, - DebugKeyPath: fmt.Sprintf("scw_%s.pem", b.config.PackerBuildName), + Debug: b.config.PackerDebug, + DebugKeyPath: fmt.Sprintf("scw_%s.pem", b.config.PackerBuildName), + PrivateKeyFile: b.config.Comm.SSHPrivateKey, }, new(stepCreateServer), new(stepServerInfo), diff --git a/builder/scaleway/step_create_server.go b/builder/scaleway/step_create_server.go index e1e8b88c6..3cf46e1ed 100644 --- a/builder/scaleway/step_create_server.go +++ b/builder/scaleway/step_create_server.go @@ -18,15 +18,20 @@ func (s *stepCreateServer) Run(state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) c := state.Get("config").(Config) sshPubKey := state.Get("ssh_pubkey").(string) + tags := []string{} ui.Say("Creating server...") + if sshPubKey != "" { + tags = []string{fmt.Sprintf("AUTHORIZED_KEY=%s", strings.TrimSpace(sshPubKey))} + } + server, err := client.PostServer(api.ScalewayServerDefinition{ Name: c.ServerName, Image: &c.Image, Organization: c.Organization, CommercialType: c.CommercialType, - Tags: []string{fmt.Sprintf("AUTHORIZED_KEY=%s", strings.TrimSpace(sshPubKey))}, + Tags: tags, }) err = client.PostServerAction(server, "poweron") diff --git a/builder/scaleway/step_create_ssh_key.go b/builder/scaleway/step_create_ssh_key.go index 8a3618619..dab8f710b 100644 --- a/builder/scaleway/step_create_ssh_key.go +++ b/builder/scaleway/step_create_ssh_key.go @@ -6,6 +6,7 @@ import ( "crypto/x509" "encoding/pem" "fmt" + "io/ioutil" "log" "os" "runtime" @@ -17,13 +18,29 @@ import ( ) type stepCreateSSHKey struct { - Debug bool - DebugKeyPath string + Debug bool + DebugKeyPath string + PrivateKeyFile string } func (s *stepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction { 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("privateKey", string(privateKeyBytes)) + state.Put("ssh_pubkey", "") + + return multistep.ActionContinue + } + ui.Say("Creating temporary ssh key for server...") priv, err := rsa.GenerateKey(rand.Reader, 2014) diff --git a/website/source/docs/builders/scaleway.html.md b/website/source/docs/builders/scaleway.html.md index 4e0a513fc..fa1edd827 100644 --- a/website/source/docs/builders/scaleway.html.md +++ b/website/source/docs/builders/scaleway.html.md @@ -68,6 +68,8 @@ builder. - `snapshot_name` (string) - The name of the resulting snapshot that will appear in your account. +- `ssh_private_key_file` (string) - Path to a PEM encoded private key file to use to authentiate with SSH. + ## Basic Example Here is a basic example. It is completely valid as soon as you enter your own @@ -81,6 +83,10 @@ access tokens: "image": "f01f8a48-c026-48ac-9771-a70eaac0890e", "region": "par1", "commercial_type": "X64-2GB", - "ssh_username": "root" + "ssh_username": "root", + "ssh_private_key_file": "~/.ssh/id_rsa", } ``` + +When you do not specified the `ssh_private_key_file`, a temporarily SSH keypair is generated to connect the server. +This key will only allows the `root` user to connect the server. \ No newline at end of file