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

122 lines
3.2 KiB
Go
Raw Normal View History

2019-10-14 16:43:59 +02:00
//go:generate mapstructure-to-hcl2 -type Config
package dockerpush
2014-01-01 20:29:53 -08:00
import (
2019-03-22 14:56:02 +01:00
"context"
2014-01-01 23:29:27 -08:00
"fmt"
2019-12-17 11:25:56 +01:00
"github.com/hashicorp/hcl/v2/hcldec"
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"
2019-03-22 14:56:02 +01:00
dockerimport "github.com/hashicorp/packer/post-processor/docker-import"
dockertag "github.com/hashicorp/packer/post-processor/docker-tag"
2017-04-04 13:39:01 -07:00
"github.com/hashicorp/packer/template/interpolate"
2014-01-01 20:29:53 -08:00
)
const BuilderIdImport = "packer.post-processor.docker-import"
2014-01-01 20:29:53 -08:00
type Config struct {
2014-01-01 23:29:27 -08:00
common.PackerConfig `mapstructure:",squash"`
Login bool
LoginUsername string `mapstructure:"login_username"`
LoginPassword string `mapstructure:"login_password"`
LoginServer string `mapstructure:"login_server"`
EcrLogin bool `mapstructure:"ecr_login"`
docker.AwsAccessConfig `mapstructure:",squash"`
ctx interpolate.Context
2014-01-01 20:29:53 -08:00
}
type PostProcessor struct {
Driver docker.Driver
2014-01-01 20:29:53 -08:00
config Config
}
2019-12-17 11:25:56 +01:00
func (p *PostProcessor) ConfigSpec() hcldec.ObjectSpec { return p.config.FlatMapstructure().HCL2Spec() }
2014-01-01 23:29:27 -08:00
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-01 23:29:27 -08:00
if err != nil {
2014-01-01 20:29:53 -08:00
return err
}
if p.config.EcrLogin && p.config.LoginServer == "" {
return fmt.Errorf("ECR login requires login server to be provided.")
}
2014-01-01 20:29:53 -08:00
return nil
}
func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, bool, error) {
2014-10-14 16:04:37 -07:00
if artifact.BuilderId() != dockerimport.BuilderId &&
artifact.BuilderId() != dockertag.BuilderId {
2014-01-19 20:34:20 -08:00
err := fmt.Errorf(
"Unknown artifact type: %s\nCan only import from docker-import and docker-tag artifacts.",
2014-01-19 20:34:20 -08:00
artifact.BuilderId())
2019-04-02 16:51:58 -07:00
return nil, false, false, err
}
2014-01-01 23:29:27 -08:00
driver := p.Driver
if driver == nil {
// If no driver is set, then we use the real driver
driver = &docker.DockerDriver{Ctx: &p.config.ctx, Ui: ui}
}
if p.config.EcrLogin {
ui.Message("Fetching ECR credentials...")
username, password, err := p.config.EcrGetLogin(p.config.LoginServer)
if err != nil {
2019-04-02 16:51:58 -07:00
return nil, false, false, err
}
p.config.LoginUsername = username
p.config.LoginPassword = password
}
if p.config.Login || p.config.EcrLogin {
ui.Message("Logging in...")
err := driver.Login(
p.config.LoginServer,
p.config.LoginUsername,
p.config.LoginPassword)
if err != nil {
2019-04-02 16:51:58 -07:00
return nil, false, false, fmt.Errorf(
"Error logging in to Docker: %s", err)
}
defer func() {
ui.Message("Logging out...")
if err := driver.Logout(p.config.LoginServer); err != nil {
ui.Error(fmt.Sprintf("Error logging out: %s", err))
}
}()
}
// Get the name.
2014-01-19 20:34:20 -08:00
name := artifact.Id()
2014-01-19 20:34:20 -08:00
ui.Message("Pushing: " + name)
if err := driver.Push(name); err != nil {
2019-04-02 16:51:58 -07:00
return nil, false, false, err
2014-01-01 20:29:53 -08:00
}
artifact = &docker.ImportArtifact{
BuilderIdValue: BuilderIdImport,
Driver: driver,
IdValue: name,
}
2019-04-02 16:51:58 -07:00
return artifact, true, false, nil
2014-01-01 20:29:53 -08:00
}