packer-cn/builder/lxc/builder.go

96 lines
2.0 KiB
Go
Raw Normal View History

2015-12-22 09:56:33 -05:00
package lxc
import (
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
2015-12-22 09:56:33 -05:00
"github.com/mitchellh/multistep"
"log"
"os"
"path/filepath"
)
// The unique ID for this builder
const BuilderId = "ustream.lxc"
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) {
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(stepPrepareOutputDir),
new(stepLxcCreate),
&StepWaitInit{
WaitTimeout: b.config.InitTimeout,
},
new(StepProvision),
new(stepExport),
}
// 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
b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
2015-12-22 09:56:33 -05:00
b.runner.Run(state)
// If there was an error, return that
if rawErr, ok := state.GetOk("error"); ok {
return nil, rawErr.(error)
}
// Compile the artifact list
files := make([]string, 0, 5)
visit := func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
files = append(files, path)
}
return err
}
if err := filepath.Walk(b.config.OutputDir, visit); err != nil {
return nil, err
}
artifact := &Artifact{
dir: b.config.OutputDir,
f: files,
}
return artifact, nil
}
func (b *Builder) Cancel() {
if b.runner != nil {
log.Println("Cancelling the step runner...")
b.runner.Cancel()
}
}