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{
&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),

View File

@ -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")

View File

@ -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)

View File

@ -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.