Files

127 lines
3.3 KiB
Go
Raw Permalink Normal View History

2013-11-08 16:55:02 -08:00
package docker
import (
"context"
2015-05-29 09:29:59 -07:00
"log"
2019-12-17 11:25:56 +01:00
"github.com/hashicorp/hcl/v2/hcldec"
2020-12-17 13:29:25 -08:00
"github.com/hashicorp/packer-plugin-sdk/communicator"
"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/packerbuilderdata"
2013-11-08 16:55:02 -08:00
)
const (
BuilderId = "packer.docker"
BuilderIdImport = "packer.post-processor.docker-import"
)
2013-11-08 16:55:02 -08:00
type Builder struct {
2019-12-17 11:25:56 +01:00
config Config
2013-11-08 16:55:02 -08:00
runner multistep.Runner
}
2019-12-17 11:25:56 +01:00
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
2019-12-17 11:25:56 +01:00
warnings, errs := b.config.Prepare(raws...)
2013-11-09 17:07:14 -08:00
if errs != nil {
return nil, warnings, errs
2013-11-08 16:55:02 -08:00
}
return []string{
"ImageSha256",
}, warnings, nil
2013-11-08 16:55:02 -08:00
}
func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) (packersdk.Artifact, error) {
driver := &DockerDriver{Ctx: &b.config.ctx, Ui: ui}
if err := driver.Verify(); err != nil {
return nil, err
}
2015-05-29 09:29:59 -07:00
version, err := driver.Version()
if err != nil {
return nil, err
}
log.Printf("[DEBUG] Docker version: %s", version.String())
// Setup the state bag and initial state for the steps
state := new(multistep.BasicStateBag)
state.Put("config", &b.config)
state.Put("hook", hook)
state.Put("ui", ui)
generatedData := &packerbuilderdata.GeneratedData{State: state}
// Setup the driver that will talk to Docker
state.Put("driver", driver)
2013-11-08 22:00:57 -08:00
steps := []multistep.Step{
2013-11-08 23:43:41 -08:00
&StepTempDir{},
2013-11-08 22:00:57 -08:00
&StepPull{},
2013-11-08 22:17:46 -08:00
&StepRun{},
&communicator.StepConnect{
Config: &b.config.Comm,
Host: commHost(b.config.Comm.Host()),
SSHConfig: b.config.Comm.SSHConfigFunc(),
CustomConnect: map[string]multistep.Step{
"docker": &StepConnectDocker{},
"dockerWindowsContainer": &StepConnectDocker{},
},
},
&commonsteps.StepProvision{},
&commonsteps.StepCleanupTempKeys{
Comm: &b.config.Comm,
},
}
if b.config.Discard {
log.Print("[DEBUG] Container will be discarded")
} else if b.config.Commit {
log.Print("[DEBUG] Container will be committed")
steps = append(steps,
new(StepCommit),
&StepSetGeneratedData{ // Adds ImageSha256 variable available after StepCommit
GeneratedData: generatedData,
})
} else if b.config.ExportPath != "" {
log.Printf("[DEBUG] Container will be exported to %s", b.config.ExportPath)
steps = append(steps, new(StepExport))
} else {
2015-08-19 13:12:16 -07:00
return nil, errArtifactNotUsed
2013-11-08 22:00:57 -08:00
}
2013-11-08 16:55:02 -08:00
// Run!
b.runner = commonsteps.NewRunner(steps, b.config.PackerConfig, ui)
b.runner.Run(ctx, state)
2013-11-08 16:55:02 -08:00
// If there was an error, return that
if rawErr, ok := state.GetOk("error"); ok {
return nil, rawErr.(error)
}
// If it was cancelled, then just return
if _, ok := state.GetOk(multistep.StateCancelled); ok {
return nil, nil
}
2013-11-09 13:22:13 -08:00
// No errors, must've worked
2020-11-19 12:17:11 -08:00
var artifact packersdk.Artifact
if b.config.Commit {
artifact = &ImportArtifact{
IdValue: state.Get("image_id").(string),
BuilderIdValue: BuilderIdImport,
Driver: driver,
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
}
} else {
artifact = &ExportArtifact{
path: b.config.ExportPath,
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
}
}
2013-11-09 13:22:13 -08:00
return artifact, nil
2013-11-08 16:55:02 -08:00
}