packer-cn/builder/lxd/builder.go

99 lines
2.1 KiB
Go
Raw Normal View History

2016-05-26 23:21:55 -04:00
package lxd
import (
"errors"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/packer"
"github.com/mitchellh/packer/template/interpolate"
"log"
"runtime"
)
// The unique ID for this builder
const BuilderId = "lxd"
type wrappedCommandTemplate struct {
Command string
}
type Builder struct {
config *Config
runner multistep.Runner
}
func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
c, errs := NewConfig(raws...)
if errs != nil {
return nil, errs
}
b.config = c
return nil, nil
}
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
if runtime.GOOS != "linux" {
return nil, errors.New("The lxc builder only works on linux environments.")
}
wrappedCommand := func(command string) (string, error) {
b.config.ctx.Data = &wrappedCommandTemplate{Command: command}
return interpolate.Render(b.config.CommandWrapper, &b.config.ctx)
}
steps := []multistep.Step{
new(stepLxdLaunch),
new(StepProvision),
2016-05-30 19:13:59 -04:00
new(stepPublish),
2016-05-26 23:21:55 -04:00
}
// Setup the state bag
state := new(multistep.BasicStateBag)
state.Put("config", b.config)
state.Put("cache", cache)
state.Put("hook", hook)
state.Put("ui", ui)
state.Put("wrappedCommand", CommandWrapper(wrappedCommand))
// Run
if b.config.PackerDebug {
b.runner = &multistep.DebugRunner{
Steps: steps,
PauseFn: common.MultistepDebugFn(ui),
}
} else {
b.runner = &multistep.BasicRunner{Steps: steps}
}
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{
2016-05-30 19:13:59 -04:00
// dir: b.config.OutputDir,
// f: files,
2016-05-26 23:21:55 -04:00
}
return artifact, nil
}
func (b *Builder) Cancel() {
if b.runner != nil {
log.Println("Cancelling the step runner...")
b.runner.Cancel()
}
}