From 72d15015513150f2009f8c3612909841fa7bfd89 Mon Sep 17 00:00:00 2001 From: Stephen Fox Date: Sun, 3 Feb 2019 12:16:43 -0500 Subject: [PATCH] Made SSH public key available in vbox boot command. The SSH public key (in authorized_keys format) can be retreived in the build template by specifying '{{ .SSHPublicKey }}'. The key is URL encoded to escape spaces. --- .../common/step_type_boot_command.go | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/builder/virtualbox/common/step_type_boot_command.go b/builder/virtualbox/common/step_type_boot_command.go index 859e425ff..0cb433903 100644 --- a/builder/virtualbox/common/step_type_boot_command.go +++ b/builder/virtualbox/common/step_type_boot_command.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/common/bootcommand" + "github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/template/interpolate" @@ -14,10 +15,21 @@ import ( const KeyLeftShift uint32 = 0xFFE1 +// TODO: Should this be made available for other builders? +// It is copy pasted in the VMWare builder as well. type bootCommandTemplateData struct { - HTTPIP string + // HTTPIP is the HTTP server's IP address. + HTTPIP string + + // HTTPPort is the HTTP server port. HTTPPort uint - Name string + + // Name is the VM's name. + Name string + + // SSHPublicKey is the URL encoded public key in + // authorized_keys format. + SSHPublicKey string } type StepTypeBootCommand struct { @@ -26,6 +38,7 @@ type StepTypeBootCommand struct { VMName string Ctx interpolate.Context GroupInterval time.Duration + Comm *communicator.Config } func (s *StepTypeBootCommand) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { @@ -54,9 +67,10 @@ func (s *StepTypeBootCommand) Run(ctx context.Context, state multistep.StateBag) hostIP := "10.0.2.2" common.SetHTTPIP(hostIP) s.Ctx.Data = &bootCommandTemplateData{ - hostIP, - httpPort, - s.VMName, + HTTPIP: hostIP, + HTTPPort: httpPort, + Name: s.VMName, + SSHPublicKey: s.Comm.SSHPublicKeyUrlEncoded(), } sendCodes := func(codes []string) error {