WIP OCI Classic builder

This commit is contained in:
Matthew Hooker 2018-01-12 14:12:15 -08:00
parent 48e12b6bee
commit 543caf3ec5
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
6 changed files with 175 additions and 5 deletions

View File

@ -0,0 +1,4 @@
package classic
type AccessConfig struct {
}

View File

@ -0,0 +1,34 @@
package classic
// Artifact is an artifact implementation that contains a built Custom Image.
type Artifact struct {
}
// BuilderId uniquely identifies the builder.
func (a *Artifact) BuilderId() string {
return BuilderId
}
// Files lists the files associated with an artifact. We don't have any files
// as the custom image is stored server side.
func (a *Artifact) Files() []string {
return nil
}
// Id returns the OCID of the associated Image.
func (a *Artifact) Id() string {
return ""
}
func (a *Artifact) String() string {
return ""
}
func (a *Artifact) State(name string) interface{} {
return nil
}
// Destroy deletes the custom image associated with the artifact.
func (a *Artifact) Destroy() error {
return nil
}

View File

@ -0,0 +1,87 @@
package classic
import (
"fmt"
"log"
ocommon "github.com/hashicorp/packer/builder/oracle/common"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/packer"
"github.com/mitchellh/multistep"
)
// BuilderId uniquely identifies the builder
const BuilderId = "packer.oracle.classic"
// Builder is a builder implementation that creates Oracle OCI custom images.
type Builder struct {
config *Config
runner multistep.Runner
}
func (b *Builder) Prepare(rawConfig ...interface{}) ([]string, error) {
config, err := NewConfig(rawConfig...)
if err != nil {
return nil, err
}
b.config = config
return nil, nil
}
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
// Populate the state bag
state := new(multistep.BasicStateBag)
state.Put("config", b.config)
state.Put("hook", hook)
state.Put("ui", ui)
// Build the steps
steps := []multistep.Step{
&ocommon.StepKeyPair{
Debug: b.config.PackerDebug,
DebugKeyPath: fmt.Sprintf("oci_classic_%s.pem", b.config.PackerBuildName),
PrivateKeyFile: b.config.Comm.SSHPrivateKey,
},
&stepCreateInstance{},
&communicator.StepConnect{
Config: &b.config.Comm,
Host: commHost,
SSHConfig: SSHConfig(
b.config.Comm.SSHUsername,
b.config.Comm.SSHPassword),
},
&common.StepProvision{},
&stepSnapshot{},
}
// Run the steps
b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
b.runner.Run(state)
// If there was an error, return that
if rawErr, ok := state.GetOk("error"); ok {
return nil, rawErr.(error)
}
/*
// Build the artifact and return it
artifact := &Artifact{
Image: state.Get("image").(client.Image),
Region: b.config.AccessCfg.Region,
driver: driver,
}
return artifact, nil
*/
return nil, nil
}
// Cancel terminates a running build.
func (b *Builder) Cancel() {
if b.runner != nil {
log.Println("Cancelling the step runner...")
b.runner.Cancel()
}
}

View File

@ -0,0 +1,44 @@
package classic
import (
"fmt"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/template/interpolate"
)
type Config struct {
common.PackerConfig `mapstructure:",squash"`
Comm communicator.Config `mapstructure:",squash"`
Access *AccessConfig
// Access config overrides
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
IdentityDomain string `mapstructure:"identity_domain"`
APIEndpoint string `mapstructure:"api_endpoint"`
// Image
Shape string `mapstructure:"shape"`
ImageList string `json:"image_list"`
ctx interpolate.Context
}
func NewConfig(raws ...interface{}) (*Config, error) {
c := &Config{}
// Decode from template
err := config.Decode(c, &config.DecodeOpts{
Interpolate: true,
InterpolateContext: &c.ctx,
}, raws...)
if err != nil {
return nil, fmt.Errorf("Failed to mapstructure Config: %+v", err)
}
return c, nil
}

View File

@ -1,4 +1,4 @@
package oci package common
import ( import (
"context" "context"
@ -16,13 +16,13 @@ import (
"golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh"
) )
type stepKeyPair struct { type StepKeyPair struct {
Debug bool Debug bool
DebugKeyPath string DebugKeyPath string
PrivateKeyFile string PrivateKeyFile string
} }
func (s *stepKeyPair) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { func (s *StepKeyPair) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
if s.PrivateKeyFile != "" { if s.PrivateKeyFile != "" {
@ -112,6 +112,6 @@ func (s *stepKeyPair) Run(_ context.Context, state multistep.StateBag) multistep
return multistep.ActionContinue return multistep.ActionContinue
} }
func (s *stepKeyPair) Cleanup(state multistep.StateBag) { func (s *StepKeyPair) Cleanup(state multistep.StateBag) {
// Nothing to do // Nothing to do
} }

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"log" "log"
ocommon "github.com/hashicorp/packer/builder/oracle/common"
client "github.com/hashicorp/packer/builder/oracle/oci/client" client "github.com/hashicorp/packer/builder/oracle/oci/client"
"github.com/hashicorp/packer/common" "github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/communicator"
@ -50,7 +51,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
// Build the steps // Build the steps
steps := []multistep.Step{ steps := []multistep.Step{
&stepKeyPair{ &ocommon.StepKeyPair{
Debug: b.config.PackerDebug, Debug: b.config.PackerDebug,
DebugKeyPath: fmt.Sprintf("oci_%s.pem", b.config.PackerBuildName), DebugKeyPath: fmt.Sprintf("oci_%s.pem", b.config.PackerBuildName),
PrivateKeyFile: b.config.Comm.SSHPrivateKey, PrivateKeyFile: b.config.Comm.SSHPrivateKey,