github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/post-processor/docker-push/post-processor.go (about)

     1  package dockerpush
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/mitchellh/packer/builder/docker"
     7  	"github.com/mitchellh/packer/common"
     8  	"github.com/mitchellh/packer/helper/config"
     9  	"github.com/mitchellh/packer/packer"
    10  	"github.com/mitchellh/packer/post-processor/docker-import"
    11  	"github.com/mitchellh/packer/post-processor/docker-tag"
    12  	"github.com/mitchellh/packer/template/interpolate"
    13  )
    14  
    15  type Config struct {
    16  	common.PackerConfig `mapstructure:",squash"`
    17  
    18  	Login                  bool
    19  	LoginEmail             string `mapstructure:"login_email"`
    20  	LoginUsername          string `mapstructure:"login_username"`
    21  	LoginPassword          string `mapstructure:"login_password"`
    22  	LoginServer            string `mapstructure:"login_server"`
    23  	EcrLogin               bool   `mapstructure:"ecr_login"`
    24  	docker.AwsAccessConfig `mapstructure:",squash"`
    25  
    26  	ctx interpolate.Context
    27  }
    28  
    29  type PostProcessor struct {
    30  	Driver docker.Driver
    31  
    32  	config Config
    33  }
    34  
    35  func (p *PostProcessor) Configure(raws ...interface{}) error {
    36  	err := config.Decode(&p.config, &config.DecodeOpts{
    37  		Interpolate:        true,
    38  		InterpolateContext: &p.config.ctx,
    39  		InterpolateFilter: &interpolate.RenderFilter{
    40  			Exclude: []string{},
    41  		},
    42  	}, raws...)
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	if p.config.EcrLogin && p.config.LoginServer == "" {
    48  		return fmt.Errorf("ECR login requires login server to be provided.")
    49  	}
    50  	return nil
    51  }
    52  
    53  func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
    54  	if artifact.BuilderId() != dockerimport.BuilderId &&
    55  		artifact.BuilderId() != dockertag.BuilderId {
    56  		err := fmt.Errorf(
    57  			"Unknown artifact type: %s\nCan only import from docker-import and docker-tag artifacts.",
    58  			artifact.BuilderId())
    59  		return nil, false, err
    60  	}
    61  
    62  	driver := p.Driver
    63  	if driver == nil {
    64  		// If no driver is set, then we use the real driver
    65  		driver = &docker.DockerDriver{Ctx: &p.config.ctx, Ui: ui}
    66  	}
    67  
    68  	if p.config.EcrLogin {
    69  		ui.Message("Fetching ECR credentials...")
    70  
    71  		username, password, err := p.config.EcrGetLogin(p.config.LoginServer)
    72  		if err != nil {
    73  			return nil, false, err
    74  		}
    75  
    76  		p.config.LoginUsername = username
    77  		p.config.LoginPassword = password
    78  	}
    79  
    80  	if p.config.Login || p.config.EcrLogin {
    81  		ui.Message("Logging in...")
    82  		err := driver.Login(
    83  			p.config.LoginServer,
    84  			p.config.LoginEmail,
    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  	return nil, false, nil
   109  }