Skip to content

Commit

Permalink
Merge pull request #3 from mattiarossi/packer-1.5
Browse files Browse the repository at this point in the history
Refactor to support packer 1.5 new plugin interface
  • Loading branch information
mattiarossi authored Apr 29, 2020
2 parents b5e2712 + 51d2a89 commit 3715f9f
Show file tree
Hide file tree
Showing 10 changed files with 877 additions and 105 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.DS_Store
/bin
/local
packer-builder-oracle-ocisurrogate
/vendor
/dist
26 changes: 26 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This is an example goreleaser.yaml file with some sane defaults.
# Make sure to check the documentation at http://goreleaser.com
before:
hooks:
# You may remove this if you don't use go modules.
- go mod download
builds:
- env:
- CGO_ENABLED=0
archives:
- replacements:
darwin: Darwin
linux: Linux
windows: Windows
386: i386
amd64: x86_64
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ .Tag }}-next"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module github.com/mattiarossi/packer-builder-oracle-ocisurrogate
go 1.13

require (
github.com/hashicorp/packer v1.4.5
github.com/oracle/oci-go-sdk v17.2.0+incompatible
github.com/hashicorp/hcl/v2 v2.4.0
github.com/hashicorp/packer v1.5.5
github.com/oracle/oci-go-sdk v19.0.0+incompatible
github.com/zclconf/go-cty v1.4.0
)
506 changes: 506 additions & 0 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ func main() {
if err != nil {
panic(err)
}
server.RegisterBuilder(ocisurrogate.NewBuilder())
server.RegisterBuilder(new(ocisurrogate.Builder))
server.Serve()
}
6 changes: 5 additions & 1 deletion pkg/ocisurrogate/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ type Artifact struct {
Image core.Image
Region string
driver Driver

// StateData should store data such as GeneratedData
// to be shared with post-processors
StateData map[string]interface{}
}

// BuilderId uniquely identifies the builder.
Expand Down Expand Up @@ -44,7 +48,7 @@ func (a *Artifact) String() string {

// State ...
func (a *Artifact) State(name string) interface{} {
return nil
return a.StateData[name]
}

// Destroy deletes the custom image associated with the artifact.
Expand Down
40 changes: 16 additions & 24 deletions pkg/ocisurrogate/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"fmt"

"github.com/hashicorp/hcl/v2/hcldec"
ocommon "github.com/hashicorp/packer/builder/oracle/common"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/communicator"
Expand All @@ -22,40 +23,30 @@ const ociAPIVersion = "20160918"

// Builder is a builder implementation that creates Oracle OCI custom images.
type Builder struct {
config *Config
config Config
runner multistep.Runner
cancel context.CancelFunc
context context.Context
}

func NewBuilder() *Builder {
ctx, cancel := context.WithCancel(context.Background())
return &Builder{
context: ctx,
cancel: cancel,
}
}
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }


func (b *Builder) Prepare(rawConfig ...interface{}) ([]string, error) {
config, err := NewConfig(rawConfig...)
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
err := b.config.Prepare(raws...)
if err != nil {
return nil, err
return nil, nil, err
}
b.config = config

return nil, nil
return nil, nil, nil
}

func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
driver, err := NewDriverOCI(b.config)
driver, err := NewDriverOCI(&b.config)
if err != nil {
return nil, err
}

// Populate the state bag
state := new(multistep.BasicStateBag)
state.Put("config", b.config)
state.Put("config", &b.config)
state.Put("driver", driver)
state.Put("hook", hook)
state.Put("ui", ui)
Expand All @@ -76,7 +67,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
},
&communicator.StepConnect{
Config: &b.config.Comm,
Host: communicator.CommHost(b.config.Comm.SSHHost, "instance_ip"),
Host: communicator.CommHost(b.config.Comm.Host(), "instance_ip"),
SSHConfig: b.config.Comm.SSHConfigFunc(),
},
&common.StepProvision{},
Expand All @@ -95,7 +86,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
return nil, rawErr.(error)
}

region, err := b.config.ConfigProvider.Region()
region, err := b.config.configProvider.Region()
if err != nil {
return nil, err
}
Expand All @@ -107,9 +98,10 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack

// Build the artifact and return it
artifact := &Artifact{
Image: image.(core.Image),
Region: region,
driver: driver,
Image: image.(core.Image),
Region: region,
driver: driver,
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
}

return artifact, nil
Expand All @@ -118,6 +110,6 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
// Cancel terminates a running build.
func (b *Builder) Cancel() {
if b.runner != nil {
b.cancel()
b.Cancel()
}
}
Loading

0 comments on commit 3715f9f

Please sign in to comment.