From ab4f205bf6339ffb3c9d12931d49a357ef48c08c Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Fri, 26 Oct 2018 15:47:49 -0700 Subject: [PATCH] correctly building linux/windows now --- builder/oracle/classic/builder.go | 39 ++++++------------- builder/oracle/classic/pv_config.go | 24 +++++++++++- builder/oracle/classic/step_add_keys.go | 9 ++--- .../oracle/classic/step_connect_builder.go | 22 +++++++++++ 4 files changed, 60 insertions(+), 34 deletions(-) create mode 100644 builder/oracle/classic/step_connect_builder.go diff --git a/builder/oracle/classic/builder.go b/builder/oracle/classic/builder.go index 767ebe470..d3a304c10 100644 --- a/builder/oracle/classic/builder.go +++ b/builder/oracle/classic/builder.go @@ -13,7 +13,6 @@ import ( "github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" - "golang.org/x/crypto/ssh" ) // BuilderId uniquely identifies the builder @@ -25,24 +24,6 @@ type Builder struct { runner multistep.Runner } -// TODO: rename, comment -type builderConfig struct { - *communicator.Config -} - -func (c *builderConfig) sshConfigFunc(username string) func(multistep.StateBag) (*ssh.ClientConfig, error) { - return func(state multistep.StateBag) (*ssh.ClientConfig, error) { - // copy communicator config by value at the last possible moment, - // so we get any values written by prior steps, then change the static - // details to make it work correctly. - s := *c - s.SSHPty = true - s.Type = "ssh" - s.SSHUsername = username - return s.SSHConfigFunc()(state) - } -} - func (b *Builder) Prepare(rawConfig ...interface{}) ([]string, error) { config, err := NewConfig(rawConfig...) if err != nil { @@ -90,8 +71,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe var steps []multistep.Step if b.config.IsPV() { - bc := builderConfig{&b.config.Comm} - steps = []multistep.Step{ &ocommon.StepKeyPair{ Debug: b.config.PackerDebug, @@ -99,7 +78,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe DebugKeyPath: fmt.Sprintf("oci_classic_%s.pem", b.config.PackerBuildName), }, &stepCreateIPReservation{}, - &stepAddKeysToAPI{}, + &stepAddKeysToAPI{ + KeyName: fmt.Sprintf("packer-generated-key_%s", runID), + }, &stepSecurity{ CommType: b.config.Comm.Type, SecurityListKey: "security_list_master", @@ -144,10 +125,13 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe Index: 2, InstanceInfoKey: "builder_instance_info", }, - &communicator.StepConnectSSH{ - Config: &b.config.Comm, - Host: ocommon.CommHost, - SSHConfig: bc.sshConfigFunc(b.config.BuilderSSHUsername), + &stepConnectBuilder{ + KeyName: fmt.Sprintf("packer-generated-key_%s", runID), + StepConnectSSH: &communicator.StepConnectSSH{ + Config: &b.config.BuilderComm, + Host: ocommon.CommHost, + SSHConfig: b.config.BuilderComm.SSHConfigFunc(), + }, }, &stepUploadImage{ UploadImageCommand: b.config.BuilderUploadImageCommand, @@ -168,7 +152,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe }, &stepCreateIPReservation{}, &stepAddKeysToAPI{ - Skip: b.config.Comm.Type != "ssh", + Skip: b.config.Comm.Type != "ssh", + KeyName: fmt.Sprintf("packer-generated-key_%s", runID), }, &stepSecurity{ SecurityListKey: "security_list", diff --git a/builder/oracle/classic/pv_config.go b/builder/oracle/classic/pv_config.go index de55b130e..9c141f716 100644 --- a/builder/oracle/classic/pv_config.go +++ b/builder/oracle/classic/pv_config.go @@ -3,6 +3,7 @@ package classic import ( "fmt" + "github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/template/interpolate" ) @@ -21,6 +22,8 @@ type PVConfig struct { BuilderImageList string `mapstructure:"builder_image_list"` BuilderImageListEntry int `mapstructure:"builder_image_list_entry"` BuilderSSHUsername string `mapstructure:"builder_ssh_username"` + + BuilderComm communicator.Config `mapstructure:"builder_communicator"` /* TODO: * Documentation * split master/builder image/connection config. i.e. build anything, master only linux @@ -48,12 +51,25 @@ func (c *PVConfig) Prepare(ctx *interpolate.Context) (errs *packer.MultiError) { return errs } + c.BuilderComm.SSHPty = true + if c.BuilderComm.Type == "winrm" { + errs = packer.MultiErrorAppend(errs, + fmt.Errorf("`ssh` is the only valid builder communicator type.")) + } + + /* + s.SSHPty = true + s.Type = "ssh" + s.SSHUsername = username + return s.SSHConfigFunc()(state) + */ + if c.BuilderShape == "" { c.BuilderShape = shapeDefault } - if c.BuilderSSHUsername == "" { - c.BuilderSSHUsername = usernameDefault + if c.BuilderComm.SSHUsername == "" { + c.BuilderComm.SSHUsername = usernameDefault } if c.BuilderImageList == "" { @@ -120,5 +136,9 @@ curl -I -X HEAD \ ` } + if es := c.BuilderComm.Prepare(ctx); len(es) > 0 { + errs = packer.MultiErrorAppend(errs, es...) + } + return } diff --git a/builder/oracle/classic/step_add_keys.go b/builder/oracle/classic/step_add_keys.go index a68093f32..489448633 100644 --- a/builder/oracle/classic/step_add_keys.go +++ b/builder/oracle/classic/step_add_keys.go @@ -6,13 +6,13 @@ import ( "fmt" "github.com/hashicorp/go-oracle-terraform/compute" - "github.com/hashicorp/packer/common/uuid" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" ) type stepAddKeysToAPI struct { - Skip bool + Skip bool + KeyName string } func (s *stepAddKeysToAPI) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { @@ -30,13 +30,12 @@ func (s *stepAddKeysToAPI) Run(_ context.Context, state multistep.StateBag) mult sshPublicKey := bytes.TrimSpace(config.Comm.SSHPublicKey) // form API call to add key to compute cloud - sshKeyName := config.Identifier(fmt.Sprintf("packer_generated_key_%s", uuid.TimeOrderedUUID())) - ui.Say(fmt.Sprintf("Creating temporary key: %s", sshKeyName)) + ui.Say(fmt.Sprintf("Creating temporary key: %s", s.KeyName)) sshKeysClient := client.SSHKeys() sshKeysInput := compute.CreateSSHKeyInput{ - Name: sshKeyName, + Name: s.KeyName, Key: string(sshPublicKey), Enabled: true, } diff --git a/builder/oracle/classic/step_connect_builder.go b/builder/oracle/classic/step_connect_builder.go new file mode 100644 index 000000000..1e8aa021e --- /dev/null +++ b/builder/oracle/classic/step_connect_builder.go @@ -0,0 +1,22 @@ +package classic + +import ( + "context" + + "github.com/hashicorp/packer/helper/communicator" + "github.com/hashicorp/packer/helper/multistep" +) + +type stepConnectBuilder struct { + *communicator.StepConnectSSH + KeyName string +} + +func (s *stepConnectBuilder) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { + s.Config.SSHKeyPairName = s.KeyName + return s.StepConnectSSH.Run(ctx, state) +} + +func (s *stepConnectBuilder) Cleanup(state multistep.StateBag) { + s.StepConnectSSH.Cleanup(state) +}