Skip to content

Instantly share code, notes, and snippets.

@vancluever
Created December 5, 2016 15:43
Show Gist options
  • Select an option

  • Save vancluever/51b19bf900faab62c87a750b3bf79f06 to your computer and use it in GitHub Desktop.

Select an option

Save vancluever/51b19bf900faab62c87a750b3bf79f06 to your computer and use it in GitHub Desktop.
NewDockerAPIDriver Function for Packer
// NewDockerAPIDriver loads an instance of the Docker API driver. It will
// also log into a docker registry if the appropriate options are defined.
//
// Configuration is handled in the following order:
// * tls_verify on: Use NewTLSClient
// * This breaks if ca_certificate, client_certificate, and client_key are
// not provided.
// * NewClient, if endpoint is supplied
// * NewEnvClient if all other options have been exhausted.
//
// Login is handled as follows:
// * ECR login is attempted first and then handed off to Login if ECR is
// defined.
// * Login otherwise proceeds with the defined login credentials if they are
// supplied.
func NewDockerAPIDriver(ctx *interpolate.Context, ui packer.Ui, c *Config) (*DockerApiDriver, error) {
var driver DockerApiDriver
if c.TLSVerify {
if c.CACertifciate == "" || c.ClientCertificate == "" || c.ClientKey == "" {
return nil, errors.New("ca_certifiate, client_certificate and client_key need to be defined when using tls_verify")
}
var endpoint string
if c.Endpoint == "" {
endpoint = "unix:///var/run/docker.sock"
} else {
endpoint = c.Endpoint
}
log.Debugf("[DEBUG] New Docker connection via TLS to %s", endpoint)
driver.client, err := godocker.NewTLSClient(endpoint, c.ClientCertificate, c.ClientKey, c.CACertifciate)
if err != nil {
return nil, fmt.Errorf("Cannot connect to Docker on %s: %v", endpoint, err)
}
} else if c.Endpoint{
log.Debugf("[DEBUG] New Docker connection (non-TLS) to %s", c.Endpoint)
driver.client, err := godocker.NewClient(c.Endpoint)
if err != nil {
return nil, fmt.Errorf("Cannot connect to Docker on %s: %v", c.Endpoint, err)
}
} else {
log.Debugf("[DEBUG] New Docker connection via default env chain", endpoint)
driver.client, err := godocker.NewClientFromEnv(endpoint)
if err != nil {
return nil, fmt.Errorf("Cannot make Docker connection through env: %v", err)
}
}
// Populate auth with login information if it exists and verify it.
login := c.Login
switch {
case c.ECRLogin:
if c.LoginServer == "" {
return nil, errors.New("login_server needs to be defined when using erc_login")
}
log.Debugf("[DEBUG] Fetching ECR credentials")
driver.auth, err := c.AwsAccessConfig.EcrGetLogin(c.LoginServer)
if err != nil {
return nil, fmt.Errorf("Cannot get ECR credentials: %v", err)
}
login = true
fallthrough
case login:
if driver.auth == nil {
if c.LoginUsername == nil || c.LoginPassword == nil || c.c.LoginEmail == nil || c.LoginServer == nil {
return nil, errors.New("login_email, login_password, login_server, and login_user need to be defined when using login")
}
driver.auth = &godocker.AuthConfiguration{
Username: c.LoginUsername,
Password: c.LoginPassword,
Email: c.LoginEmail,
ServerAddress: c.LoginServer,
}
}
log.Debugf("[DEBUG] Checking Docker auth configuration for registry %s", driver.auth.ServerAddress)
status, err := driver.client.AuthCheck(driver.auth)
if err != nil {
return nil, fmt.Errorf("Error verifying Docker registry login: %v", err)
}
log.Debugf("[DEBUG] Auth OK: %s", status.Status)
}
return &driver, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment