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 (
|
2019-03-22 09:53:28 -04:00
|
|
|
"context"
|
2017-02-13 05:35:14 -05:00
|
|
|
"fmt"
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2018-01-12 17:12:15 -05:00
|
|
|
ocommon "github.com/hashicorp/packer/builder/oracle/common"
|
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"
|
2018-04-11 05:20:40 -04:00
|
|
|
"github.com/oracle/oci-go-sdk/core"
|
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 {
|
2019-12-17 05:25:56 -05:00
|
|
|
config Config
|
2017-02-13 05:35:14 -05:00
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
2019-12-17 05:57:09 -05:00
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
|
2019-12-17 05:25:56 -05:00
|
|
|
err := b.config.Prepare(raws...)
|
2017-02-13 05:35:14 -05:00
|
|
|
if err != nil {
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, nil, err
|
2017-02-13 05:35:14 -05:00
|
|
|
}
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, nil, nil
|
2017-02-13 05:35:14 -05:00
|
|
|
}
|
|
|
|
|
2019-03-22 09:53:28 -04:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
|
2019-12-17 05:25:56 -05: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)
|
2019-12-19 11:01:55 -05:00
|
|
|
state.Put("config", &b.config)
|
2017-02-13 05:35:14 -05:00
|
|
|
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{
|
2018-08-28 11:48:09 -04:00
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
Comm: &b.config.Comm,
|
|
|
|
DebugKeyPath: fmt.Sprintf("oci_%s.pem", b.config.PackerBuildName),
|
2017-02-13 05:35:14 -05:00
|
|
|
},
|
|
|
|
&stepCreateInstance{},
|
|
|
|
&stepInstanceInfo{},
|
2018-05-24 13:47:37 -04:00
|
|
|
&stepGetDefaultCredentials{
|
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
Comm: &b.config.Comm,
|
|
|
|
BuildName: b.config.PackerBuildName,
|
|
|
|
},
|
2017-02-13 05:35:14 -05:00
|
|
|
&communicator.StepConnect{
|
2018-08-22 11:02:23 -04:00
|
|
|
Config: &b.config.Comm,
|
2020-01-30 18:22:22 -05:00
|
|
|
Host: communicator.CommHost(b.config.Comm.Host(), "instance_ip"),
|
2018-08-22 11:02:23 -04:00
|
|
|
SSHConfig: b.config.Comm.SSHConfigFunc(),
|
2017-02-13 05:35:14 -05:00
|
|
|
},
|
|
|
|
&common.StepProvision{},
|
2018-09-14 14:03:23 -04:00
|
|
|
&common.StepCleanupTempKeys{
|
|
|
|
Comm: &b.config.Comm,
|
|
|
|
},
|
2017-02-13 05:35:14 -05:00
|
|
|
&stepImage{},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the steps
|
2017-09-05 06:08:27 -04:00
|
|
|
b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
|
2019-03-22 09:53:28 -04:00
|
|
|
b.runner.Run(ctx, state)
|
2017-02-13 05:35:14 -05:00
|
|
|
|
|
|
|
// If there was an error, return that
|
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
|
|
|
|
2019-10-14 10:08:16 -04:00
|
|
|
region, err := b.config.configProvider.Region()
|
2018-04-11 05:20:40 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-17 17:54:27 -05:00
|
|
|
image, ok := state.GetOk("image")
|
|
|
|
if !ok {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-02-13 05:35:14 -05:00
|
|
|
// Build the artifact and return it
|
|
|
|
artifact := &Artifact{
|
2020-01-30 05:27:58 -05:00
|
|
|
Image: image.(core.Image),
|
|
|
|
Region: region,
|
|
|
|
driver: driver,
|
|
|
|
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
|
2017-02-13 05:35:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return artifact, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel terminates a running build.
|