Skip to content

Commit

Permalink
Merge pull request #6 from tomaszkiewicz/rename-arguments
Browse files Browse the repository at this point in the history
Rename arguments
  • Loading branch information
mdlavin authored Aug 8, 2018
2 parents b0a258d + 58f02b5 commit c3fcd0e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
# move-docker-image - Copy docker images without a full docker installation
# copy-docker-image - Copy docker images without a full docker installation
[![Build Status](https://travis-ci.org/mdlavin/copy-docker-image.svg?branch=master)](https://travis-ci.org/mdlavin/copy-docker-image)

## Overview

When doing automated deployments, especially when using AWS ECR in multiple accounts, you might want to copy images from one registry to another without the need for a full docker installation. At LifeOmic we wanted to orchestrate the copying of images while executing inside a container without exposing a full Docker socker just for image manipulation.
When doing automated deployments, especially when using AWS ECR in multiple accounts, you might want to copy images from one registry to another without the need for a full docker installation. At LifeOmic we wanted to orchestrate the copying of images while executing inside a container without exposing a full Docker socket just for image manipulation.

To copy an image between two anonymous repositories, you can use a command line like:

```
$ copy-docker-image --srcRepo http://registry1/ --destRepo http://registry2 --repo project
$ copy-docker-image --src-repo http://registry1/ --dest-repo http://registry2 --repo project
```

To specify an image tag, just add a --tag argument like:

```
$ copy-docker-image --srcRepo http://registry1/ --destRepo http://registry2 --repo project --tag v1
$ copy-docker-image --src-repo http://registry1/ --dest-repo http://registry2 --repo project --tag v1
```

## Integration with AWS ECR

Because copy to AWS ECR was common a special URL format was added to automatically look up the right HTTPS URL and authorization token. Assuming a AWS CLI profile has been created for your account you can use a command like:

```
$ copy-docker-image --srcRepo http://registry1/ --destRepo ecr:<account-id> --repo project
$ copy-docker-image --src-repo http://registry1/ --dest-repo ecr:<account-id> --repo project
```

## Installation
Expand Down
28 changes: 14 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func moveLayerUsingFile(srcHub *registry.Registry, destHub *registry.Registry, s

imageReadStream, err := os.Open(file.Name())
if err != nil {
return fmt.Errorf("Failed to open the temporary image layer for uploading. %v", err)
return fmt.Errorf("Failed to open temporary image layer for uploading. %v", err)
}
err = destHub.UploadLayer(destRepo, layerDigest, imageReadStream)
imageReadStream.Close()
Expand All @@ -58,19 +58,19 @@ func moveLayerUsingFile(srcHub *registry.Registry, destHub *registry.Registry, s
}

func migrateLayer(srcHub *registry.Registry, destHub *registry.Registry, srcRepo string, destRepo string, layer schema1.FSLayer) error {
fmt.Println("Checking if manifest layer exists in destiation registery")
fmt.Println("Checking if manifest layer exists in destination registery")

layerDigest := layer.BlobSum
hasLayer, err := destHub.HasLayer(destRepo, layerDigest)
if err != nil {
return fmt.Errorf("Failure while checking if the destiation registry contained an image layer. %v", err)
return fmt.Errorf("Failure while checking if the destination registry contained an image layer. %v", err)
}

if !hasLayer {
fmt.Println("Need to upload layer", layerDigest, "to the destiation")
fmt.Println("Need to upload layer", layerDigest, "to the destination")
tempFile, err := ioutil.TempFile("", "docker-image")
if err != nil {
return fmt.Errorf("Failure while a creating temporary file for an image layer download. %v", err)
return fmt.Errorf("Failure while creating temporary file for an image layer download. %v", err)
}

err = moveLayerUsingFile(srcHub, destHub, srcRepo, destRepo, layer, tempFile)
Expand All @@ -94,15 +94,15 @@ type RepositoryArguments struct {
}

func buildRegistryArguments(argPrefix string, argDescription string) RepositoryArguments {
registryURLName := fmt.Sprintf("%sURL", argPrefix)
registryURLName := fmt.Sprintf("%s-url", argPrefix)
registryURLDescription := fmt.Sprintf("URL of %s registry", argDescription)
registryURLArg := kingpin.Flag(registryURLName, registryURLDescription).String()

repositoryName := fmt.Sprintf("%sRepo", argPrefix)
repositoryName := fmt.Sprintf("%s-repo", argPrefix)
repositoryDescription := fmt.Sprintf("Name of the %s repository", argDescription)
repositoryArg := kingpin.Flag(repositoryName, repositoryDescription).String()

tagName := fmt.Sprintf("%sTag", argPrefix)
tagName := fmt.Sprintf("%s-tag", argPrefix)
tagDescription := fmt.Sprintf("Name of the %s tag", argDescription)
tagArg := kingpin.Flag(tagName, tagDescription).String()

Expand Down Expand Up @@ -157,7 +157,7 @@ func connectToRegistry(args RepositoryArguments) (*registry.Registry, error) {

err = registry.Ping()
if err != nil {
return nil, fmt.Errorf("Failed to to ping registry %s as a connection test. %v", origUrl, err)
return nil, fmt.Errorf("Failed to ping registry %s as a connection test. %v", origUrl, err)
}

return registry, nil
Expand All @@ -170,9 +170,9 @@ func main() {
}()

srcArgs := buildRegistryArguments("src", "source")
destArgs := buildRegistryArguments("dest", "destiation")
repoArg := kingpin.Flag("repo", "The repository in the source and the destiation. Values provided by --srcRepo or --destTag will override this value").String()
tagArg := kingpin.Flag("tag", "The tag name in the source and the destiation. Values provided by --srcTag or --destTag will override this value").Default("latest").String()
destArgs := buildRegistryArguments("dest", "destination")
repoArg := kingpin.Flag("repo", "The repository in the source and the destination. Values provided by --src-repo or --dest-tag will override this value").String()
tagArg := kingpin.Flag("tag", "The tag name in the source and the destination. Values provided by --src-tag or --dest-tag will override this value").Default("latest").String()
kingpin.Parse()

if *srcArgs.Repository == "" {
Expand All @@ -190,13 +190,13 @@ func main() {
}

if *srcArgs.Repository == "" {
fmt.Printf("A source repository name is required either with --srcRepo or --repo")
fmt.Printf("A source repository name is required either with --src-repo or --repo")
exitCode = -1
return
}

if *destArgs.Repository == "" {
fmt.Printf("A destiation repository name is required either with --destRepo or --repo")
fmt.Printf("A destination repository name is required either with --dest-repo or --repo")
exitCode = -1
return
}
Expand Down

0 comments on commit c3fcd0e

Please sign in to comment.