2017-09-12 11:30:39 -04:00
|
|
|
// Package oci contains a packer.Builder implementation that builds Oracle
|
|
|
|
// Bare Metal Cloud Services (OCI) images.
|
|
|
|
package oci
|
2017-02-13 05:35:14 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2018-01-12 17:12:15 -05:00
|
|
|
ocommon "github.com/hashicorp/packer/builder/oracle/common"
|
2017-09-12 11:30:39 -04:00
|
|
|
client "github.com/hashicorp/packer/builder/oracle/oci/client"
|
2017-04-07 06:20:33 -04:00
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-07 06:20:33 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2017-02-13 05:35:14 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// BuilderId uniquely identifies the builder
|
2017-09-12 11:30:39 -04:00
|
|
|
const BuilderId = "packer.oracle.oci"
|
2017-02-13 05:35:14 -05:00
|
|
|
|
2017-09-12 11:30:39 -04:00
|
|
|
// OCI API version
|
|
|
|
const ociAPIVersion = "20160918"
|
2017-02-13 05:35:14 -05:00
|
|
|
|
2017-09-12 11:30:39 -04:00
|
|
|
// Builder is a builder implementation that creates Oracle OCI custom images.
|
2017-02-13 05:35:14 -05:00
|
|
|
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) {
|
2017-09-12 11:30:39 -04:00
|
|
|
driver, err := NewDriverOCI(b.config)
|
2017-02-13 05:35:14 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate the state bag
|
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("config", b.config)
|
|
|
|
state.Put("driver", driver)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
|
|
|
|
|
|
|
// Build the steps
|
|
|
|
steps := []multistep.Step{
|
2018-01-12 17:12:15 -05:00
|
|
|
&ocommon.StepKeyPair{
|
2017-08-02 12:18:53 -04:00
|
|
|
Debug: b.config.PackerDebug,
|
2017-09-12 11:30:39 -04:00
|
|
|
DebugKeyPath: fmt.Sprintf("oci_%s.pem", b.config.PackerBuildName),
|
2017-08-02 12:18:53 -04:00
|
|
|
PrivateKeyFile: b.config.Comm.SSHPrivateKey,
|
2017-02-13 05:35:14 -05:00
|
|
|
},
|
|
|
|
&stepCreateInstance{},
|
|
|
|
&stepInstanceInfo{},
|
|
|
|
&communicator.StepConnect{
|
2017-08-02 12:18:53 -04:00
|
|
|
Config: &b.config.Comm,
|
|
|
|
Host: commHost,
|
|
|
|
SSHConfig: SSHConfig(
|
|
|
|
b.config.Comm.SSHUsername,
|
|
|
|
b.config.Comm.SSHPassword),
|
2017-02-13 05:35:14 -05:00
|
|
|
},
|
|
|
|
&common.StepProvision{},
|
|
|
|
&stepImage{},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the steps
|
2017-09-05 06:08:27 -04:00
|
|
|
b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
|
2017-02-13 05:35:14 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel terminates a running build.
|
|
|
|
func (b *Builder) Cancel() {
|
|
|
|
if b.runner != nil {
|
|
|
|
log.Println("Cancelling the step runner...")
|
|
|
|
b.runner.Cancel()
|
|
|
|
}
|
|
|
|
}
|