Unify steps, fix some typos

This commit is contained in:
Lukas Kämmerling 2018-10-18 08:03:04 +02:00
parent 16453173db
commit bad1b95a49
5 changed files with 15 additions and 18 deletions

View File

@ -52,24 +52,22 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
Debug: b.config.PackerDebug, Debug: b.config.PackerDebug,
DebugKeyPath: fmt.Sprintf("ssh_key_%s.pem", b.config.PackerBuildName), DebugKeyPath: fmt.Sprintf("ssh_key_%s.pem", b.config.PackerBuildName),
}, },
new(stepCreateServer), &stepCreateServer{},
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.Comm, Config: &b.config.Comm,
Host: getServerIP, Host: getServerIP,
SSHConfig: b.config.Comm.SSHConfigFunc(), SSHConfig: b.config.Comm.SSHConfigFunc(),
}, },
new(common.StepProvision), &common.StepProvision{},
&common.StepCleanupTempKeys{ &common.StepCleanupTempKeys{
Comm: &b.config.Comm, Comm: &b.config.Comm,
}, },
new(stepShutdownServer), &stepShutdownServer{},
new(stepCreateSnapshot), &stepCreateSnapshot{},
} }
ui.Say("Steps OK")
// Run the steps // Run the steps
b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
b.runner.Run(state) b.runner.Run(state)
ui.Say("Run OK")
// If there was an error, return that // If there was an error, return that
if rawErr, ok := state.GetOk("error"); ok { if rawErr, ok := state.GetOk("error"); ok {
return nil, rawErr.(error) return nil, rawErr.(error)

View File

@ -44,7 +44,7 @@ func (s *stepCreateServer) Run(_ context.Context, state multistep.StateBag) mult
UserData: userData, UserData: userData,
}) })
if err != nil { if err != nil {
err := fmt.Errorf("1Error creating server: %s", err) err := fmt.Errorf("Error creating server: %s", err)
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt

View File

@ -34,28 +34,28 @@ func (s *stepCreateSSHKey) Run(_ context.Context, state multistep.StateBag) mult
priv, err := rsa.GenerateKey(rand.Reader, 2014) priv, err := rsa.GenerateKey(rand.Reader, 2014)
// ASN.1 DER encoded form // ASN.1 DER encoded form
priv_der := x509.MarshalPKCS1PrivateKey(priv) privDER := x509.MarshalPKCS1PrivateKey(priv)
priv_blk := pem.Block{ privBLK := pem.Block{
Type: "RSA PRIVATE KEY", Type: "RSA PRIVATE KEY",
Headers: nil, Headers: nil,
Bytes: priv_der, Bytes: privDER,
} }
// Set the private key in the config for later // Set the private key in the config for later
c.Comm.SSHPrivateKey = pem.EncodeToMemory(&priv_blk) c.Comm.SSHPrivateKey = pem.EncodeToMemory(&privBLK)
// Marshal the public key into SSH compatible format // Marshal the public key into SSH compatible format
// TODO properly handle the public key error // TODO properly handle the public key error
pub, _ := ssh.NewPublicKey(&priv.PublicKey) pub, _ := ssh.NewPublicKey(&priv.PublicKey)
pub_sshformat := string(ssh.MarshalAuthorizedKey(pub)) pubSSHFormat := string(ssh.MarshalAuthorizedKey(pub))
// The name of the public key on DO // The name of the public key on the Hetzner Cloud
name := fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID()) name := fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID())
// Create the key! // Create the key!
key, _, err := client.SSHKey.Create(context.TODO(), hcloud.SSHKeyCreateOpts{ key, _, err := client.SSHKey.Create(context.TODO(), hcloud.SSHKeyCreateOpts{
Name: name, Name: name,
PublicKey: pub_sshformat, PublicKey: pubSSHFormat,
}) })
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)
@ -83,7 +83,7 @@ func (s *stepCreateSSHKey) Run(_ context.Context, state multistep.StateBag) mult
defer f.Close() defer f.Close()
// Write the key out // Write the key out
if _, err := f.Write(pem.EncodeToMemory(&priv_blk)); err != nil { if _, err := f.Write(pem.EncodeToMemory(&privBLK)); err != nil {
state.Put("error", fmt.Errorf("Error saving debug key: %s", err)) state.Put("error", fmt.Errorf("Error saving debug key: %s", err))
return multistep.ActionHalt return multistep.ActionHalt
} }
@ -96,7 +96,6 @@ func (s *stepCreateSSHKey) Run(_ context.Context, state multistep.StateBag) mult
} }
} }
} }
ui.Say("SSH Key OK")
return multistep.ActionContinue return multistep.ActionContinue
} }

View File

@ -39,7 +39,7 @@ builder.
can also be specified via environment variable `HCLOUD_TOKEN`, can also be specified via environment variable `HCLOUD_TOKEN`,
if set. if set.
- `image` (string) - ID or name of image to create server in. - `image` (string) - ID or name of image to launch server from.
- `location` (string) - The name of the location to launch the server in. - `location` (string) - The name of the location to launch the server in.

View File

@ -80,7 +80,7 @@ description: |-
<p> <p>
Out of the box Packer comes with support to build images for Out of the box Packer comes with support to build images for
Amazon EC2, CloudStack, DigitalOcean, Docker, Google Compute Amazon EC2, CloudStack, DigitalOcean, Docker, Google Compute
Engine, Hetzner Cloud, Microsoft Azure, QEMU, VirtualBox, VMware, and more. Engine, Microsoft Azure, QEMU, VirtualBox, VMware, and more.
Support for more platforms is on the way, and anyone can add Support for more platforms is on the way, and anyone can add
new platforms via plugins. new platforms via plugins.
</p> </p>