correctly building linux/windows now

This commit is contained in:
Matthew Hooker 2018-10-26 15:47:49 -07:00
parent 09de194d8e
commit ab4f205bf6
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
4 changed files with 60 additions and 34 deletions

View File

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

View File

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

View File

@ -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,
}

View File

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