2016-04-14 17:29:27 -07:00
|
|
|
package triton
|
|
|
|
|
|
|
|
import (
|
2019-03-22 14:53:28 +01:00
|
|
|
"context"
|
2016-04-14 17:29:27 -07:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-multierror"
|
2017-04-04 13:39:01 -07:00
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
|
|
|
"github.com/hashicorp/packer/helper/config"
|
2018-01-19 16:18:44 -08:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 13:39:01 -07:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2016-04-14 17:29:27 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
BuilderId = "joyent.triton"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Builder struct {
|
|
|
|
config Config
|
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
|
|
|
|
errs := &multierror.Error{}
|
|
|
|
|
|
|
|
err := config.Decode(&b.config, &config.DecodeOpts{
|
|
|
|
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 12:07:45 -07: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 16:35:07 +02:00
|
|
|
if b.config.AccessConfig.KeyMaterial == "" && b.config.Comm.SSHPrivateKeyFile == "" {
|
2017-04-26 12:07:45 -07:00
|
|
|
b.config.Comm.SSHAgentAuth = true
|
|
|
|
}
|
|
|
|
|
2016-04-14 17:29:27 -07:00
|
|
|
return nil, errs.ErrorOrNil()
|
|
|
|
}
|
|
|
|
|
2019-03-22 14:53:28 +01:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
|
2016-04-14 17:29:27 -07:00
|
|
|
config := b.config
|
|
|
|
|
|
|
|
driver, err := NewDriverTriton(ui, config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
state := new(multistep.BasicStateBag)
|
2018-09-18 16:17:42 +02:00
|
|
|
state.Put("config", &b.config)
|
2016-12-29 18:58:56 +01:00
|
|
|
state.Put("debug", b.config.PackerDebug)
|
2016-04-14 17:29:27 -07:00
|
|
|
state.Put("driver", driver)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
|
|
|
|
|
|
|
steps := []multistep.Step{
|
|
|
|
&StepCreateSourceMachine{},
|
|
|
|
&communicator.StepConnect{
|
2018-08-22 17:02:23 +02:00
|
|
|
Config: &config.Comm,
|
2019-07-02 13:56:28 -07:00
|
|
|
Host: commHost(b.config.Comm.SSHHost),
|
2018-08-22 17:02:23 +02:00
|
|
|
SSHConfig: b.config.Comm.SSHConfigFunc(),
|
2016-04-14 17:29:27 -07:00
|
|
|
},
|
|
|
|
&common.StepProvision{},
|
2018-09-14 11:03:23 -07:00
|
|
|
&common.StepCleanupTempKeys{
|
|
|
|
Comm: &config.Comm,
|
|
|
|
},
|
2016-04-14 17:29:27 -07:00
|
|
|
&StepStopMachine{},
|
|
|
|
&StepCreateImageFromMachine{},
|
|
|
|
&StepDeleteMachine{},
|
|
|
|
}
|
|
|
|
|
2016-12-29 18:58:56 +01:00
|
|
|
b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
|
2019-03-22 14:53:28 +01:00
|
|
|
b.runner.Run(ctx, state)
|
2016-04-14 17:29:27 -07: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,
|
|
|
|
}
|
|
|
|
|
|
|
|
return artifact, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel cancels a possibly running Builder. This should block until
|
|
|
|
// the builder actually cancels and cleans up after itself.
|