github.com/vijayrajah/packer@v1.3.2/post-processor/docker-push/post-processor.go (about)

     1  package dockerpush
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/packer/builder/docker"
     7  	"github.com/hashicorp/packer/common"
     8  	"github.com/hashicorp/packer/helper/config"
     9  	"github.com/hashicorp/packer/packer"
    10  	"github.com/hashicorp/packer/post-processor/docker-import"
    11  	"github.com/hashicorp/packer/post-processor/docker-tag"
    12  	"github.com/hashicorp/packer/template/interpolate"
    13  )
    14  
    15  const BuilderIdImport = "packer.post-processor.docker-import"
    16  
    17  type Config struct {
    18  	common.PackerConfig `mapstructure:",squash"`
    19  
    20  	Login                  bool
    21  	LoginUsername          string `mapstructure:"login_username"`
    22  	LoginPassword          string `mapstructure:"login_password"`
    23  	LoginServer            string `mapstructure:"login_server"`
    24  	EcrLogin               bool   `mapstructure:"ecr_login"`
    25  	docker.AwsAccessConfig `mapstructure:",squash"`
    26  
    27  	ctx interpolate.Context
    28  }
    29  
    30  type PostProcessor struct {
    31  	Driver docker.Driver
    32  
    33  	config Config
    34  }
    35  
    36  func (p *PostProcessor) Configure(raws ...interface{}) error {
    37  	err := config.Decode(&p.config, &config.DecodeOpts{
    38  		Interpolate:        true,
    39  		InterpolateContext: &p.config.ctx,
    40  		InterpolateFilter: &interpolate.RenderFilter{
    41  			Exclude: []string{},
    42  		},
    43  	}, raws...)
    44  	if err != nil {
    45  		return err
    46  	}
    47  
    48  	if p.config.EcrLogin && p.config.LoginServer == "" {
    49  		return fmt.Errorf("ECR login requires login server to be provided.")
    50  	}
    51  	return nil
    52  }
    53  
    54  func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
    55  	if artifact.BuilderId() != dockerimport.BuilderId &&
    56  		artifact.BuilderId() != dockertag.BuilderId {
    57  		err := fmt.Errorf(
    58  			"Unknown artifact type: %s\nCan only import from docker-import and docker-tag artifacts.",
    59  			artifact.BuilderId())
    60  		return nil, false, err
    61  	}
    62  
    63  	driver := p.Driver
    64  	if driver == nil {
    65  		// If no driver is set, then we use the real driver
    66  		driver = &docker.DockerDriver{Ctx: &p.config.ctx, Ui: ui}
    67  	}
    68  
    69  	if p.config.EcrLogin {
    70  		ui.Message("Fetching ECR credentials...")
    71  
    72  		username, password, err := p.config.EcrGetLogin(p.config.LoginServer)
    73  		if err != nil {
    74  			return nil, false, err
    75  		}
    76  
    77  		p.config.LoginUsername = username
    78  		p.config.LoginPassword = password
    79  	}
    80  
    81  	if p.config.Login || p.config.EcrLogin {
    82  		ui.Message("Logging in...")
    83  		err := driver.Login(
    84  			p.config.LoginServer,
    85  			p.config.LoginUsername,
    86  			p.config.LoginPassword)
    87  		if err != nil {
    88  			return nil, false, fmt.Errorf(
    89  				"Error logging in to Docker: %s", err)
    90  		}
    91  
    92  		defer func() {
    93  			ui.Message("Logging out...")
    94  			if err := driver.Logout(p.config.LoginServer); err != nil {
    95  				ui.Error(fmt.Sprintf("Error logging out: %s", err))
    96  			}
    97  		}()
    98  	}
    99  
   100  	// Get the name.
   101  	name := artifact.Id()
   102  
   103  	ui.Message("Pushing: " + name)
   104  	if err := driver.Push(name); err != nil {
   105  		return nil, false, err
   106  	}
   107  
   108  	artifact = &docker.ImportArtifact{
   109  		BuilderIdValue: BuilderIdImport,
   110  		Driver:         driver,
   111  		IdValue:        name,
   112  	}
   113  
   114  	return artifact, true, nil
   115  }