2015-12-22 09:56:33 -05:00
|
|
|
package lxc
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:53:28 -04:00
|
|
|
"context"
|
2018-01-22 20:21:10 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep/commonsteps"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
|
2015-12-22 09:56:33 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// The unique ID for this builder
|
|
|
|
const BuilderId = "ustream.lxc"
|
|
|
|
|
|
|
|
type wrappedCommandTemplate struct {
|
|
|
|
Command string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Builder struct {
|
2019-12-17 05:25:56 -05:00
|
|
|
config Config
|
2015-12-22 09:56:33 -05:00
|
|
|
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) {
|
2019-12-17 05:25:56 -05:00
|
|
|
errs := b.config.Prepare(raws...)
|
2015-12-22 09:56:33 -05:00
|
|
|
if errs != nil {
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, nil, errs
|
2015-12-22 09:56:33 -05:00
|
|
|
}
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, nil, nil
|
2015-12-22 09:56:33 -05:00
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) (packersdk.Artifact, error) {
|
2015-12-22 09:56:33 -05:00
|
|
|
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)
|
2019-12-19 11:01:55 -05:00
|
|
|
state.Put("config", &b.config)
|
2015-12-22 09:56:33 -05:00
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
|
|
|
state.Put("wrappedCommand", CommandWrapper(wrappedCommand))
|
|
|
|
|
|
|
|
// Run
|
2020-11-11 18:04:28 -05:00
|
|
|
b.runner = commonsteps.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
|
2019-03-22 09:53:28 -04:00
|
|
|
b.runner.Run(ctx, state)
|
2015-12-22 09:56:33 -05:00
|
|
|
|
|
|
|
// 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{
|
2020-01-30 05:27:58 -05:00
|
|
|
dir: b.config.OutputDir,
|
|
|
|
f: files,
|
|
|
|
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
|
2015-12-22 09:56:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return artifact, nil
|
|
|
|
}
|