Files
Packer-Cn/post-processor/docker-import/post-processor.go
T

81 lines
1.8 KiB
Go
Raw Normal View History

2014-01-02 14:49:14 -08:00
package dockerimport
import (
"fmt"
2017-04-04 13:39:01 -07: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"
2014-01-02 14:49:14 -08:00
)
const BuilderId = "packer.post-processor.docker-import"
2014-01-02 14:49:14 -08:00
type Config struct {
common.PackerConfig `mapstructure:",squash"`
Repository string `mapstructure:"repository"`
Tag string `mapstructure:"tag"`
ctx interpolate.Context
2014-01-02 14:49:14 -08:00
}
type PostProcessor struct {
config Config
}
func (p *PostProcessor) Configure(raws ...interface{}) error {
err := config.Decode(&p.config, &config.DecodeOpts{
2015-06-22 12:24:27 -07:00
Interpolate: true,
InterpolateContext: &p.config.ctx,
InterpolateFilter: &interpolate.RenderFilter{
Exclude: []string{},
},
}, raws...)
2014-01-02 14:49:14 -08:00
if err != nil {
return err
}
return nil
}
func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
2015-06-23 14:28:03 +03:00
switch artifact.BuilderId() {
2015-11-02 11:21:15 +00:00
case docker.BuilderId, artifice.BuilderId:
2015-06-23 14:28:03 +03:00
break
default:
err := fmt.Errorf(
2015-11-02 11:21:15 +00:00
"Unknown artifact type: %s\nCan only import from Docker builder and Artifice post-processor artifacts.",
artifact.BuilderId())
return nil, false, err
}
2014-01-19 19:15:25 -08:00
importRepo := p.config.Repository
if p.config.Tag != "" {
importRepo += ":" + p.config.Tag
2014-01-02 14:49:14 -08:00
}
driver := &docker.DockerDriver{Ctx: &p.config.ctx, Ui: ui}
2014-01-19 19:15:25 -08:00
ui.Message("Importing image: " + artifact.Id())
ui.Message("Repository: " + importRepo)
id, err := driver.Import(artifact.Files()[0], importRepo)
if err != nil {
2014-01-19 19:15:25 -08:00
return nil, false, err
}
ui.Message("Imported ID: " + id)
// Build the artifact
artifact = &docker.ImportArtifact{
BuilderIdValue: BuilderId,
Driver: driver,
IdValue: importRepo,
}
return artifact, false, nil
2014-01-02 14:49:14 -08:00
}