Add existing SSH key support

Update documentation
This commit is contained in:
Edouard BONLIEU 2017-04-19 11:10:52 +02:00 committed by Matthew Hooker
parent 1fb13cc23e
commit 2de93c5ae6
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
4 changed files with 35 additions and 6 deletions

View File

@ -43,8 +43,9 @@ 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,
DebugKeyPath: fmt.Sprintf("scw_%s.pem", b.config.PackerBuildName), DebugKeyPath: fmt.Sprintf("scw_%s.pem", b.config.PackerBuildName),
PrivateKeyFile: b.config.Comm.SSHPrivateKey,
}, },
new(stepCreateServer), new(stepCreateServer),
new(stepServerInfo), new(stepServerInfo),

View File

@ -18,15 +18,20 @@ func (s *stepCreateServer) Run(state multistep.StateBag) multistep.StepAction {
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) sshPubKey := state.Get("ssh_pubkey").(string)
tags := []string{}
ui.Say("Creating server...") ui.Say("Creating server...")
if sshPubKey != "" {
tags = []string{fmt.Sprintf("AUTHORIZED_KEY=%s", strings.TrimSpace(sshPubKey))}
}
server, err := client.PostServer(api.ScalewayServerDefinition{ server, err := client.PostServer(api.ScalewayServerDefinition{
Name: c.ServerName, Name: c.ServerName,
Image: &c.Image, Image: &c.Image,
Organization: c.Organization, Organization: c.Organization,
CommercialType: c.CommercialType, CommercialType: c.CommercialType,
Tags: []string{fmt.Sprintf("AUTHORIZED_KEY=%s", strings.TrimSpace(sshPubKey))}, Tags: tags,
}) })
err = client.PostServerAction(server, "poweron") err = client.PostServerAction(server, "poweron")

View File

@ -6,6 +6,7 @@ import (
"crypto/x509" "crypto/x509"
"encoding/pem" "encoding/pem"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"os" "os"
"runtime" "runtime"
@ -17,13 +18,29 @@ import (
) )
type stepCreateSSHKey struct { type stepCreateSSHKey struct {
Debug bool Debug bool
DebugKeyPath string DebugKeyPath string
PrivateKeyFile string
} }
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("privateKey", string(privateKeyBytes))
state.Put("ssh_pubkey", "")
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, 2014)

View File

@ -68,6 +68,8 @@ builder.
- `snapshot_name` (string) - The name of the resulting snapshot that will - `snapshot_name` (string) - The name of the resulting snapshot that will
appear in your account. 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 ## Basic Example
Here is a basic example. It is completely valid as soon as you enter your own 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", "image": "f01f8a48-c026-48ac-9771-a70eaac0890e",
"region": "par1", "region": "par1",
"commercial_type": "X64-2GB", "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.