Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. // NewDockerAPIDriver loads an instance of the Docker API driver. It will
  2. // also log into a docker registry if the appropriate options are defined.
  3. //
  4. // Configuration is handled in the following order:
  5. // * tls_verify on: Use NewTLSClient
  6. // * This breaks if ca_certificate, client_certificate, and client_key are
  7. // not provided.
  8. // * NewClient, if endpoint is supplied
  9. // * NewEnvClient if all other options have been exhausted.
  10. //
  11. // Login is handled as follows:
  12. // * ECR login is attempted first and then handed off to Login if ECR is
  13. // defined.
  14. // * Login otherwise proceeds with the defined login credentials if they are
  15. // supplied.
  16. func NewDockerAPIDriver(ctx *interpolate.Context, ui packer.Ui, c *Config) (*DockerApiDriver, error) {
  17. var driver DockerApiDriver
  18.  
  19. if c.TLSVerify {
  20. if c.CACertifciate == "" || c.ClientCertificate == "" || c.ClientKey == "" {
  21. return nil, errors.New("ca_certifiate, client_certificate and client_key need to be defined when using tls_verify")
  22. }
  23. var endpoint string
  24. if c.Endpoint == "" {
  25. endpoint = "unix:///var/run/docker.sock"
  26. } else {
  27. endpoint = c.Endpoint
  28. }
  29. log.Debugf("[DEBUG] New Docker connection via TLS to %s", endpoint)
  30. driver.client, err := godocker.NewTLSClient(endpoint, c.ClientCertificate, c.ClientKey, c.CACertifciate)
  31. if err != nil {
  32. return nil, fmt.Errorf("Cannot connect to Docker on %s: %v", endpoint, err)
  33. }
  34.  
  35. } else if c.Endpoint{
  36. log.Debugf("[DEBUG] New Docker connection (non-TLS) to %s", c.Endpoint)
  37. driver.client, err := godocker.NewClient(c.Endpoint)
  38. if err != nil {
  39. return nil, fmt.Errorf("Cannot connect to Docker on %s: %v", c.Endpoint, err)
  40. }
  41. } else {
  42. log.Debugf("[DEBUG] New Docker connection via default env chain", endpoint)
  43. driver.client, err := godocker.NewClientFromEnv(endpoint)
  44. if err != nil {
  45. return nil, fmt.Errorf("Cannot make Docker connection through env: %v", err)
  46. }
  47. }
  48.  
  49. // Populate auth with login information if it exists and verify it.
  50. login := c.Login
  51. switch {
  52. case c.ECRLogin:
  53. if c.LoginServer == "" {
  54. return nil, errors.New("login_server needs to be defined when using erc_login")
  55. }
  56. log.Debugf("[DEBUG] Fetching ECR credentials")
  57. driver.auth, err := c.AwsAccessConfig.EcrGetLogin(c.LoginServer)
  58. if err != nil {
  59. return nil, fmt.Errorf("Cannot get ECR credentials: %v", err)
  60. }
  61. login = true
  62. fallthrough
  63. case login:
  64. if driver.auth == nil {
  65. if c.LoginUsername == nil || c.LoginPassword == nil || c.c.LoginEmail == nil || c.LoginServer == nil {
  66. return nil, errors.New("login_email, login_password, login_server, and login_user need to be defined when using login")
  67. }
  68. driver.auth = &godocker.AuthConfiguration{
  69. Username: c.LoginUsername,
  70. Password: c.LoginPassword,
  71. Email: c.LoginEmail,
  72. ServerAddress: c.LoginServer,
  73. }
  74. }
  75. log.Debugf("[DEBUG] Checking Docker auth configuration for registry %s", driver.auth.ServerAddress)
  76. status, err := driver.client.AuthCheck(driver.auth)
  77. if err != nil {
  78. return nil, fmt.Errorf("Error verifying Docker registry login: %v", err)
  79. }
  80. log.Debugf("[DEBUG] Auth OK: %s", status.Status)
  81. }
  82.  
  83. return &driver, nil
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement