packer-cn/builder.go

102 lines
2.1 KiB
Go
Raw Normal View History

package main
import (
"errors"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/communicator"
2017-08-25 01:08:08 -04:00
"github.com/hashicorp/packer/packer"
2017-08-23 20:06:50 -04:00
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
2017-08-25 01:08:08 -04:00
"github.com/mitchellh/multistep"
)
type Builder struct {
config *Config
runner multistep.Runner
}
func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
c, warnings, errs := NewConfig(raws...)
if errs != nil {
return warnings, errs
}
b.config = c
return warnings, nil
}
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
state := new(multistep.BasicStateBag)
2017-08-25 01:08:08 -04:00
state.Put("config", b.config)
state.Put("hook", hook)
state.Put("ui", ui)
var steps []multistep.Step
steps = append(steps,
&StepConnect{
config: &b.config.ConnectConfig,
},
&StepCloneVM{
config: &b.config.CloneConfig,
},
2017-07-01 18:34:50 -04:00
&StepConfigureHardware{
config: &b.config.HardwareConfig,
},
)
if b.config.Comm.Type != "none" {
steps = append(steps,
&StepRun{},
&communicator.StepConnect{
Config: &b.config.Comm,
Host: commHost,
SSHConfig: sshConfig,
},
&common.StepProvision{},
&StepShutdown{
config: &b.config.ShutdownConfig,
},
)
}
steps = append(steps,
2017-06-27 03:32:59 -04:00
&StepCreateSnapshot{
createSnapshot: b.config.CreateSnapshot,
},
&StepConvertToTemplate{
ConvertToTemplate: b.config.ConvertToTemplate,
},
)
// Run!
b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
b.runner.Run(state)
// If there was an error, return that
if rawErr, ok := state.GetOk("error"); ok {
return nil, rawErr.(error)
}
// If we were interrupted or cancelled, then just exit.
if _, ok := state.GetOk(multistep.StateCancelled); ok {
return nil, errors.New("Build was cancelled.")
}
if _, ok := state.GetOk(multistep.StateHalted); ok {
return nil, errors.New("Build was halted.")
}
artifact := &Artifact{
2017-07-02 16:29:50 -04:00
Name: b.config.VMName,
2017-08-23 20:06:50 -04:00
VM: state.Get("vm").(*driver.VirtualMachine),
}
return artifact, nil
}
func (b *Builder) Cancel() {
if b.runner != nil {
b.runner.Cancel()
}
}