Files
Packer-Cn/builder/oracle/oci/builder.go
T

111 lines
2.7 KiB
Go
Raw Normal View History

2017-09-12 16:30:39 +01:00
// Package oci contains a packer.Builder implementation that builds Oracle
// Bare Metal Cloud Services (OCI) images.
package oci
import (
"context"
"fmt"
2019-12-17 11:25:56 +01:00
"github.com/hashicorp/hcl/v2/hcldec"
2018-01-12 14:12:15 -08:00
ocommon "github.com/hashicorp/packer/builder/oracle/common"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/communicator"
2018-01-19 16:18:44 -08:00
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
2018-04-11 10:20:40 +01:00
"github.com/oracle/oci-go-sdk/core"
)
// BuilderId uniquely identifies the builder
2017-09-12 16:30:39 +01:00
const BuilderId = "packer.oracle.oci"
2017-09-12 16:30:39 +01:00
// OCI API version
const ociAPIVersion = "20160918"
2017-09-12 16:30:39 +01:00
// Builder is a builder implementation that creates Oracle OCI custom images.
type Builder struct {
2019-12-17 11:25:56 +01:00
config Config
runner multistep.Runner
}
2019-12-17 11:25:56 +01:00
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
2019-12-17 11:25:56 +01:00
err := b.config.Prepare(raws...)
if err != nil {
return nil, nil, err
}
return nil, nil, nil
}
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
2019-12-17 11:25:56 +01:00
driver, err := NewDriverOCI(&b.config)
if err != nil {
return nil, err
}
// Populate the state bag
state := new(multistep.BasicStateBag)
2019-12-19 17:01:55 +01:00
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 14:12:15 -08:00
&ocommon.StepKeyPair{
2018-08-28 17:48:09 +02:00
Debug: b.config.PackerDebug,
Comm: &b.config.Comm,
DebugKeyPath: fmt.Sprintf("oci_%s.pem", b.config.PackerBuildName),
},
&stepCreateInstance{},
&stepInstanceInfo{},
&stepGetDefaultCredentials{
Debug: b.config.PackerDebug,
Comm: &b.config.Comm,
BuildName: b.config.PackerBuildName,
},
&communicator.StepConnect{
Config: &b.config.Comm,
Host: communicator.CommHost(b.config.Comm.Host(), "instance_ip"),
SSHConfig: b.config.Comm.SSHConfigFunc(),
},
&common.StepProvision{},
&common.StepCleanupTempKeys{
Comm: &b.config.Comm,
},
&stepImage{},
}
// Run the steps
b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
b.runner.Run(ctx, state)
// If there was an error, return that
if rawErr, ok := state.GetOk("error"); ok {
return nil, rawErr.(error)
}
region, err := b.config.configProvider.Region()
2018-04-11 10:20:40 +01:00
if err != nil {
return nil, err
}
image, ok := state.GetOk("image")
if !ok {
return nil, err
}
// Build the artifact and return it
artifact := &Artifact{
Image: image.(core.Image),
Region: region,
driver: driver,
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
}
return artifact, nil
}
// Cancel terminates a running build.