2019-10-14 10:43:59 -04:00
|
|
|
//go:generate mapstructure-to-hcl2 -type Config
|
|
|
|
|
2014-01-02 01:30:28 -05:00
|
|
|
package dockerpush
|
2014-01-01 23:29:53 -05:00
|
|
|
|
|
|
|
import (
|
2019-03-22 09:56:02 -04:00
|
|
|
"context"
|
2014-01-02 02:29:27 -05:00
|
|
|
"fmt"
|
2020-11-11 18:04:28 -05:00
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/builder/docker"
|
2020-11-12 17:44:02 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/common"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2020-11-18 13:34:59 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template/config"
|
2020-11-11 13:21:37 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template/interpolate"
|
2019-03-22 09:56:02 -04:00
|
|
|
dockerimport "github.com/hashicorp/packer/post-processor/docker-import"
|
|
|
|
dockertag "github.com/hashicorp/packer/post-processor/docker-tag"
|
2014-01-01 23:29:53 -05:00
|
|
|
)
|
|
|
|
|
2018-07-20 16:27:29 -04:00
|
|
|
const BuilderIdImport = "packer.post-processor.docker-import"
|
|
|
|
|
2014-01-01 23:29:53 -05:00
|
|
|
type Config struct {
|
2014-01-02 02:29:27 -05:00
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
|
2016-09-03 10:45:52 -04:00
|
|
|
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"`
|
2014-09-05 17:43:15 -04:00
|
|
|
|
2015-05-27 17:27:11 -04:00
|
|
|
ctx interpolate.Context
|
2014-01-01 23:29:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type PostProcessor struct {
|
2014-03-13 00:30:13 -04:00
|
|
|
Driver docker.Driver
|
|
|
|
|
2014-01-01 23:29:53 -05:00
|
|
|
config Config
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (p *PostProcessor) ConfigSpec() hcldec.ObjectSpec { return p.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
2014-01-02 02:29:27 -05:00
|
|
|
func (p *PostProcessor) Configure(raws ...interface{}) error {
|
2015-05-27 17:27:11 -04:00
|
|
|
err := config.Decode(&p.config, &config.DecodeOpts{
|
2020-10-09 20:01:55 -04:00
|
|
|
PluginType: BuilderIdImport,
|
2015-06-22 15:24:27 -04:00
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &p.config.ctx,
|
2015-05-27 17:27:11 -04:00
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{},
|
|
|
|
},
|
|
|
|
}, raws...)
|
2014-01-02 02:29:27 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-01-01 23:29:53 -05:00
|
|
|
}
|
|
|
|
|
2016-09-03 10:45:52 -04:00
|
|
|
if p.config.EcrLogin && p.config.LoginServer == "" {
|
|
|
|
return fmt.Errorf("ECR login requires login server to be provided.")
|
|
|
|
}
|
2014-01-01 23:29:53 -05:00
|
|
|
return nil
|
|
|
|
}
|
2014-01-02 01:30:28 -05:00
|
|
|
|
2020-11-19 15:17:11 -05:00
|
|
|
func (p *PostProcessor) PostProcess(ctx context.Context, ui packersdk.Ui, artifact packersdk.Artifact) (packersdk.Artifact, bool, bool, error) {
|
2014-10-14 19:04:37 -04:00
|
|
|
if artifact.BuilderId() != dockerimport.BuilderId &&
|
|
|
|
artifact.BuilderId() != dockertag.BuilderId {
|
2014-01-19 23:34:20 -05:00
|
|
|
err := fmt.Errorf(
|
2014-09-24 16:22:39 -04:00
|
|
|
"Unknown artifact type: %s\nCan only import from docker-import and docker-tag artifacts.",
|
2014-01-19 23:34:20 -05:00
|
|
|
artifact.BuilderId())
|
2019-04-02 19:51:58 -04:00
|
|
|
return nil, false, false, err
|
2014-01-02 01:30:28 -05:00
|
|
|
}
|
2014-01-02 02:29:27 -05:00
|
|
|
|
2014-03-13 00:30:13 -04:00
|
|
|
driver := p.Driver
|
|
|
|
if driver == nil {
|
|
|
|
// If no driver is set, then we use the real driver
|
2015-05-27 17:27:11 -04:00
|
|
|
driver = &docker.DockerDriver{Ctx: &p.config.ctx, Ui: ui}
|
2014-03-13 00:30:13 -04:00
|
|
|
}
|
2014-01-13 16:37:09 -05:00
|
|
|
|
2016-09-03 10:45:52 -04:00
|
|
|
if p.config.EcrLogin {
|
|
|
|
ui.Message("Fetching ECR credentials...")
|
|
|
|
|
|
|
|
username, password, err := p.config.EcrGetLogin(p.config.LoginServer)
|
|
|
|
if err != nil {
|
2019-04-02 19:51:58 -04:00
|
|
|
return nil, false, false, err
|
2016-09-03 10:45:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
p.config.LoginUsername = username
|
|
|
|
p.config.LoginPassword = password
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.Login || p.config.EcrLogin {
|
2014-09-05 17:43:15 -04:00
|
|
|
ui.Message("Logging in...")
|
|
|
|
err := driver.Login(
|
|
|
|
p.config.LoginServer,
|
|
|
|
p.config.LoginUsername,
|
|
|
|
p.config.LoginPassword)
|
|
|
|
if err != nil {
|
2019-04-02 19:51:58 -04:00
|
|
|
return nil, false, false, fmt.Errorf(
|
2014-09-05 17:43:15 -04:00
|
|
|
"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))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2020-09-29 08:25:15 -04:00
|
|
|
var tags []string
|
|
|
|
switch t := artifact.State("docker_tags").(type) {
|
|
|
|
case []string:
|
|
|
|
tags = t
|
|
|
|
case []interface{}:
|
|
|
|
for _, name := range t {
|
|
|
|
if n, ok := name.(string); ok {
|
|
|
|
tags = append(tags, n)
|
|
|
|
}
|
2020-09-28 05:18:24 -04:00
|
|
|
}
|
2020-05-06 19:39:41 -04:00
|
|
|
}
|
2014-03-13 00:30:13 -04:00
|
|
|
|
2020-09-29 08:25:15 -04:00
|
|
|
names := []string{artifact.Id()}
|
|
|
|
names = append(names, tags...)
|
|
|
|
|
2020-05-06 19:39:41 -04:00
|
|
|
// Get the name.
|
|
|
|
for _, name := range names {
|
|
|
|
ui.Message("Pushing: " + name)
|
|
|
|
if err := driver.Push(name); err != nil {
|
|
|
|
return nil, false, false, err
|
|
|
|
}
|
2014-01-01 23:29:53 -05:00
|
|
|
}
|
|
|
|
|
2018-07-20 16:27:29 -04:00
|
|
|
artifact = &docker.ImportArtifact{
|
|
|
|
BuilderIdValue: BuilderIdImport,
|
|
|
|
Driver: driver,
|
2020-05-06 19:39:41 -04:00
|
|
|
IdValue: names[0],
|
|
|
|
StateData: map[string]interface{}{"docker_tags": tags},
|
2018-07-20 16:27:29 -04:00
|
|
|
}
|
|
|
|
|
2019-04-02 19:51:58 -04:00
|
|
|
return artifact, true, false, nil
|
2014-01-01 23:29:53 -05:00
|
|
|
}
|