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