2016-04-14 20:29:27 -04:00
|
|
|
package triton
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:53:28 -04:00
|
|
|
"context"
|
2016-04-14 20:29:27 -04:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-multierror"
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
|
|
|
"github.com/hashicorp/packer/helper/config"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2016-04-14 20:29:27 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
BuilderId = "joyent.triton"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Builder struct {
|
|
|
|
config Config
|
|
|
|
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 00:23:05 -05:00
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
|
2016-04-14 20:29:27 -04:00
|
|
|
errs := &multierror.Error{}
|
|
|
|
|
|
|
|
err := config.Decode(&b.config, &config.DecodeOpts{
|
2020-10-09 20:01:55 -04:00
|
|
|
PluginType: BuilderId,
|
2016-04-14 20:29:27 -04:00
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &b.config.ctx,
|
|
|
|
}, raws...)
|
|
|
|
if err != nil {
|
|
|
|
errs = multierror.Append(errs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
errs = multierror.Append(errs, b.config.AccessConfig.Prepare(&b.config.ctx)...)
|
|
|
|
errs = multierror.Append(errs, b.config.SourceMachineConfig.Prepare(&b.config.ctx)...)
|
|
|
|
errs = multierror.Append(errs, b.config.Comm.Prepare(&b.config.ctx)...)
|
|
|
|
errs = multierror.Append(errs, b.config.TargetImageConfig.Prepare(&b.config.ctx)...)
|
|
|
|
|
2017-04-26 15:07:45 -04:00
|
|
|
// If we are using an SSH agent to sign requests, and no private key has been
|
|
|
|
// specified for SSH, use the agent for connecting for provisioning.
|
2018-08-23 10:35:07 -04:00
|
|
|
if b.config.AccessConfig.KeyMaterial == "" && b.config.Comm.SSHPrivateKeyFile == "" {
|
2017-04-26 15:07:45 -04:00
|
|
|
b.config.Comm.SSHAgentAuth = true
|
|
|
|
}
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, nil, errs.ErrorOrNil()
|
2016-04-14 20:29:27 -04: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) {
|
2016-04-14 20:29:27 -04:00
|
|
|
config := b.config
|
|
|
|
|
|
|
|
driver, err := NewDriverTriton(ui, config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
state := new(multistep.BasicStateBag)
|
2018-09-18 10:17:42 -04:00
|
|
|
state.Put("config", &b.config)
|
2016-12-29 12:58:56 -05:00
|
|
|
state.Put("debug", b.config.PackerDebug)
|
2016-04-14 20:29:27 -04:00
|
|
|
state.Put("driver", driver)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
|
|
|
|
|
|
|
steps := []multistep.Step{
|
|
|
|
&StepCreateSourceMachine{},
|
|
|
|
&communicator.StepConnect{
|
2018-08-22 11:02:23 -04:00
|
|
|
Config: &config.Comm,
|
2020-01-30 18:22:22 -05:00
|
|
|
Host: commHost(b.config.Comm.Host()),
|
2018-08-22 11:02:23 -04:00
|
|
|
SSHConfig: b.config.Comm.SSHConfigFunc(),
|
2016-04-14 20:29:27 -04:00
|
|
|
},
|
|
|
|
&common.StepProvision{},
|
2018-09-14 14:03:23 -04:00
|
|
|
&common.StepCleanupTempKeys{
|
|
|
|
Comm: &config.Comm,
|
|
|
|
},
|
2016-04-14 20:29:27 -04:00
|
|
|
&StepStopMachine{},
|
|
|
|
&StepCreateImageFromMachine{},
|
|
|
|
&StepDeleteMachine{},
|
|
|
|
}
|
|
|
|
|
2016-12-29 12:58:56 -05:00
|
|
|
b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
|
2019-03-22 09:53:28 -04:00
|
|
|
b.runner.Run(ctx, state)
|
2016-04-14 20:29:27 -04:00
|
|
|
|
|
|
|
// If there was an error, return that
|
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is no image, just return
|
|
|
|
if _, ok := state.GetOk("image"); !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
artifact := &Artifact{
|
|
|
|
ImageID: state.Get("image").(string),
|
|
|
|
BuilderIDValue: BuilderId,
|
|
|
|
Driver: driver,
|
2020-01-30 05:27:58 -05:00
|
|
|
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
|
2016-04-14 20:29:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return artifact, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel cancels a possibly running Builder. This should block until
|
|
|
|
// the builder actually cancels and cleans up after itself.
|