packer-cn/post-processor/docker-import/post-processor.go

83 lines
2.0 KiB
Go
Raw Normal View History

package dockerimport
import (
2019-03-22 09:56:02 -04:00
"context"
"fmt"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/builder/docker"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/post-processor/artifice"
"github.com/hashicorp/packer/template/interpolate"
)
const BuilderId = "packer.post-processor.docker-import"
type Config struct {
common.PackerConfig `mapstructure:",squash"`
Repository string `mapstructure:"repository"`
Tag string `mapstructure:"tag"`
Changes []string `mapstructure:"changes"`
ctx interpolate.Context
}
type PostProcessor struct {
config Config
}
func (p *PostProcessor) Configure(raws ...interface{}) error {
err := config.Decode(&p.config, &config.DecodeOpts{
Interpolate: true,
InterpolateContext: &p.config.ctx,
InterpolateFilter: &interpolate.RenderFilter{
Exclude: []string{},
},
}, raws...)
if err != nil {
return err
}
return nil
}
func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, bool, error) {
switch artifact.BuilderId() {
case docker.BuilderId, artifice.BuilderId:
break
default:
err := fmt.Errorf(
"Unknown artifact type: %s\nCan only import from Docker builder and Artifice post-processor artifacts.",
artifact.BuilderId())
2019-04-02 19:51:58 -04:00
return nil, false, false, err
}
importRepo := p.config.Repository
if p.config.Tag != "" {
importRepo += ":" + p.config.Tag
}
driver := &docker.DockerDriver{Ctx: &p.config.ctx, Ui: ui}
ui.Message("Importing image: " + artifact.Id())
ui.Message("Repository: " + importRepo)
id, err := driver.Import(artifact.Files()[0], p.config.Changes, importRepo)
if err != nil {
2019-04-02 19:51:58 -04:00
return nil, false, false, err
}
ui.Message("Imported ID: " + id)
// Build the artifact
artifact = &docker.ImportArtifact{
BuilderIdValue: BuilderId,
Driver: driver,
IdValue: importRepo,
}
2019-04-02 19:51:58 -04:00
return artifact, false, false, nil
}