Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
close #20
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuriy Bogdanov committed Jan 12, 2016
1 parent d29faea commit 9046ed6
Show file tree
Hide file tree
Showing 10 changed files with 492 additions and 215 deletions.
33 changes: 23 additions & 10 deletions src/cmd/rocker-compose/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,16 +583,29 @@ func initDockerClient(ctx *cli.Context) *docker.Client {
return dockerClient
}

func initAuthConfig(ctx *cli.Context) *compose.AuthConfig {
auth := &compose.AuthConfig{}
authParam := globalString(ctx, "auth")

if strings.Contains(authParam, ":") {
userPass := strings.Split(authParam, ":")
auth.Username = userPass[0]
auth.Password = userPass[1]
func initAuthConfig(c *cli.Context) (auth *docker.AuthConfigurations) {
var err error
if c.GlobalIsSet("auth") {
// Obtain auth configuration from cli params
authParam := c.GlobalString("auth")
if strings.Contains(authParam, ":") {
userPass := strings.Split(authParam, ":")
auth = &docker.AuthConfigurations{
Configs: map[string]docker.AuthConfiguration{
"*": docker.AuthConfiguration{
Username: userPass[0],
Password: userPass[1],
},
},
}
}
return
}
return auth
// Obtain auth configuration from .docker/config.json
if auth, err = docker.NewAuthConfigurationsFromDockerCfg(); err != nil {
log.Fatal(err)
}
return
}

func initAnsubleResp(ctx *cli.Context) (ansibleResp *ansible.Response) {
Expand All @@ -607,7 +620,7 @@ func initAnsubleResp(ctx *cli.Context) (ansibleResp *ansible.Response) {
return
}

func doRemove(ctx *cli.Context, config *config.Config, dockerCli *docker.Client, auth *compose.AuthConfig) error {
func doRemove(ctx *cli.Context, config *config.Config, dockerCli *docker.Client, auth *docker.AuthConfigurations) error {
compose, err := compose.New(&compose.Config{
Manifest: config,
Docker: dockerCli,
Expand Down
31 changes: 7 additions & 24 deletions src/compose/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"
"util"

"github.com/grammarly/rocker/src/rocker/dockerclient"
"github.com/grammarly/rocker/src/rocker/imagename"
"github.com/grammarly/rocker/src/rocker/template"
"github.com/kr/pretty"
Expand Down Expand Up @@ -54,7 +55,7 @@ type DockerClient struct {
Docker *docker.Client
Attach bool
Wait time.Duration
Auth *AuthConfig
Auth *docker.AuthConfigurations
KeepImages int
Recover bool

Expand Down Expand Up @@ -83,27 +84,6 @@ func (e ErrContainerBadState) Error() string {
return str
}

// AuthConfig is a docker auth configuration object
type AuthConfig struct {
Username string
Password string
Email string
ServerAddress string
}

// ToDockerAPI converts AuthConfig to be eatable by go-dockerclient
func (a *AuthConfig) ToDockerAPI() *docker.AuthConfiguration {
if a == nil {
return &docker.AuthConfiguration{}
}
return &docker.AuthConfiguration{
Username: a.Username,
Password: a.Password,
Email: a.Email,
ServerAddress: a.ServerAddress,
}
}

// NewClient makes a new DockerClient object based on configuration params
// that is given with input DockerClient object.
func NewClient(initialClient *DockerClient) (*DockerClient, error) {
Expand Down Expand Up @@ -645,7 +625,7 @@ func (client *DockerClient) pullImageForContainers(forceUpdate bool, vars templa

if img, err = client.Docker.InspectImage(container.Image.String()); err == docker.ErrNoSuchImage || forceUpdate {
log.Infof("Pulling image: %s for %s", container.Image, container.Name)
if img, err = PullDockerImage(client.Docker, container.Image, client.Auth.ToDockerAPI()); err != nil {
if img, err = PullDockerImage(client.Docker, container.Image, client.Auth); err != nil {
err = fmt.Errorf("Failed to pull image %s for container %s, error: %s", container.Image, container.Name, err)
return
}
Expand Down Expand Up @@ -739,11 +719,14 @@ func (client *DockerClient) resolveVersions(local, hub bool, vars template.Vars,
log.Debugf("Getting list of tags for %s from the registry", container.Image)

var remote []*imagename.ImageName
if remote, err = imagename.RegistryListTags(container.Image); err != nil {
if remote, err = dockerclient.RegistryListTags(container.Image, client.Auth); err != nil {
log.Debugf("dockerclient.RegistryListTags err: %s", err)
return fmt.Errorf("Failed to list tags of image %s for container %s from the remote registry, error: %s",
container.Image, container.Name, err)
}

log.Debugf("remote: %v", remote)

// Re-Resolve having hub tags
candidate = container.Image.ResolveVersion(append(images, remote...))
}
Expand Down
2 changes: 1 addition & 1 deletion src/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type Config struct {
Remove bool
Recover bool
Wait time.Duration
Auth *AuthConfig
Auth *docker.AuthConfigurations
KeepImages int
}

Expand Down
8 changes: 5 additions & 3 deletions src/compose/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/term"
"github.com/fsouza/go-dockerclient"
"github.com/grammarly/rocker/src/rocker/dockerclient"
"github.com/grammarly/rocker/src/rocker/imagename"
)

Expand All @@ -48,7 +49,7 @@ func GetBridgeIP(client *docker.Client) (ip string, err error) {
_, err = client.InspectImage(emptyImageName)
if err != nil && err.Error() == "no such image" {
log.Infof("Pulling image %s to obtain network bridge address", emptyImageName)
if _, err := PullDockerImage(client, imagename.NewFromString(emptyImageName), &docker.AuthConfiguration{}); err != nil {
if _, err := PullDockerImage(client, imagename.NewFromString(emptyImageName), nil); err != nil {
return "", err
}
} else if err != nil {
Expand Down Expand Up @@ -89,7 +90,7 @@ func GetBridgeIP(client *docker.Client) (ip string, err error) {
}

// PullDockerImage pulls an image and streams to a logger respecting terminal features
func PullDockerImage(client *docker.Client, image *imagename.ImageName, auth *docker.AuthConfiguration) (*docker.Image, error) {
func PullDockerImage(client *docker.Client, image *imagename.ImageName, auth *docker.AuthConfigurations) (*docker.Image, error) {
pipeReader, pipeWriter := io.Pipe()

pullOpts := docker.PullImageOptions{
Expand All @@ -100,10 +101,11 @@ func PullDockerImage(client *docker.Client, image *imagename.ImageName, auth *do
RawJSONStream: true,
}

repoAuth := dockerclient.GetAuthForRegistry(auth, image.Registry)
errch := make(chan error, 1)

go func() {
err := client.PullImage(pullOpts, *auth)
err := client.PullImage(pullOpts, repoAuth)

if err := pipeWriter.Close(); err != nil {
log.Errorf("Failed to close pull image stream for %s, error: %s", image, err)
Expand Down
28 changes: 14 additions & 14 deletions vendor/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -111,27 +111,13 @@
"branch": "v1",
"path": "/src/rocker/textformatter"
},
{
"importpath": "github.com/grammarly/rocker/src/rocker/imagename",
"repository": "https://github.com/grammarly/rocker",
"revision": "a86d7310cd246ddf2a4e3dc4d6cae71d2b2f2d02",
"branch": "v1",
"path": "/src/rocker/imagename"
},
{
"importpath": "github.com/grammarly/rocker/src/rocker/template",
"repository": "https://github.com/grammarly/rocker",
"revision": "3b5905678b83b1b5cc4f22557bed523ff4d2438e",
"branch": "v1",
"path": "/src/rocker/template"
},
{
"importpath": "github.com/grammarly/rocker/src/rocker/dockerclient",
"repository": "https://github.com/grammarly/rocker",
"revision": "8632d6d097135c71dfff9b4f0b1bbe00eebf39b7",
"branch": "v1",
"path": "/src/rocker/dockerclient"
},
{
"importpath": "github.com/mitchellh/go-homedir",
"repository": "https://github.com/mitchellh/go-homedir",
Expand All @@ -151,6 +137,20 @@
"revision": "d6a4bbfc503169324de3462a2ae2bb5a5dc2f041",
"branch": "v1",
"path": "/src/rocker/debugtrap"
},
{
"importpath": "github.com/grammarly/rocker/src/rocker/dockerclient",
"repository": "https://github.com/grammarly/rocker",
"revision": "8fcd5a943f7cf3a0dee550aa4e4aacf223a4b89c",
"branch": "f-auth",
"path": "/src/rocker/dockerclient"
},
{
"importpath": "github.com/grammarly/rocker/src/rocker/imagename",
"repository": "https://github.com/grammarly/rocker",
"revision": "8fcd5a943f7cf3a0dee550aa4e4aacf223a4b89c",
"branch": "f-auth",
"path": "/src/rocker/imagename"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*-
* Copyright 2015 Grammarly, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dockerclient

import "github.com/fsouza/go-dockerclient"

// GetAuthForRegistry extracts desired docker.AuthConfiguration object from the
// list of docker.AuthConfigurations by registry hostname
func GetAuthForRegistry(auth *docker.AuthConfigurations, registry string) (result docker.AuthConfiguration) {
if auth == nil {
return
}
// The default registry is "index.docker.io"
if registry == "" {
registry = "index.docker.io"
}
if result, ok := auth.Configs[registry]; ok {
return result
}
if result, ok := auth.Configs["https://"+registry]; ok {
return result
}
if result, ok := auth.Configs["https://"+registry+"/v1/"]; ok {
return result
}
// not sure /v2/ is needed, but just in case
if result, ok := auth.Configs["https://"+registry+"/v2/"]; ok {
return result
}
if result, ok := auth.Configs["*"]; ok {
return result
}
return
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ type Config struct {
func NewConfig() *Config {
certPath := os.Getenv("DOCKER_CERT_PATH")
if certPath == "" {
certPath = "~/.docker"
homePath, err := homedir.Dir()
if err != nil {
log.Fatal(err)
Expand Down
Loading

0 comments on commit 9046ed6

Please sign in to comment.