Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dry-run mode to skopeo-sync #1459

Closed
wants to merge 10 commits into from
45 changes: 28 additions & 17 deletions cmd/skopeo/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type syncOptions struct {
destination string // Destination registry name
scoped bool // When true, namespace copied images at destination using the source repository name
all bool // Copy all of the images if an image in the source is a list
dryRun bool // Don't actually copy anything, just output what it would have done
keepGoing bool // Whether or not to abort the sync if there are any errors during syncing the images
}

Expand Down Expand Up @@ -105,6 +106,7 @@ See skopeo-sync(1) for details.
flags.StringVarP(&opts.destination, "dest", "d", "", "DESTINATION transport type")
flags.BoolVar(&opts.scoped, "scoped", false, "Images at DESTINATION are prefix using the full source image path as scope")
flags.BoolVarP(&opts.all, "all", "a", false, "Copy all images if SOURCE-IMAGE is a list")
flags.BoolVar(&opts.dryRun, "dry-run", false, "Run without actually copying data")
flags.BoolVarP(&opts.keepGoing, "keep-going", "", false, "Do not abort the sync if any image copy fails")
flags.AddFlagSet(&sharedFlags)
flags.AddFlagSet(&deprecatedTLSVerifyFlags)
Expand Down Expand Up @@ -579,6 +581,11 @@ func (opts *syncOptions) run(args []string, stdout io.Writer) error {
OptimizeDestinationImageAlreadyExists: true,
ForceManifestMIMEType: manifestType,
}

if opts.dryRun {
logrus.Warn("Running in dry-run mode")
}

errorsPresent := false
imagesNumber := 0
for _, srcRepo := range srcRepoList {
Expand Down Expand Up @@ -606,26 +613,30 @@ func (opts *syncOptions) run(args []string, stdout io.Writer) error {
if err != nil {
return err
}

logrus.WithFields(logrus.Fields{
fromToFields := logrus.Fields{
"from": transports.ImageName(ref),
"to": transports.ImageName(destRef),
}).Infof("Copying image ref %d/%d", counter+1, len(srcRepo.ImageRefs))

if err = retry.RetryIfNecessary(ctx, func() error {
_, err = copy.Image(ctx, policyContext, destRef, ref, &options)
return err
}, opts.retryOpts); err != nil {
if !opts.keepGoing {
return errors.Wrapf(err, "Error copying ref %q", transports.ImageName(ref))
}
// log the error, keep a note that there was a failure and move on to the next
// image ref
errorsPresent = true
logrus.WithError(err).Errorf("Error copying ref %q", transports.ImageName(ref))
continue
}
imagesNumber++
if opts.dryRun {
logrus.WithFields(fromToFields).Infof("Would have copied image ref %d/%d", counter+1, len(srcRepo.ImageRefs))
} else {
logrus.WithFields(fromToFields).Infof("Copying image ref %d/%d", counter+1, len(srcRepo.ImageRefs))
retryFunc := func() error {
mtrmac marked this conversation as resolved.
Show resolved Hide resolved
_, err = copy.Image(ctx, policyContext, destRef, ref, &options)
return err
}
if err = retry.RetryIfNecessary(ctx, retryFunc, opts.retryOpts); err != nil {
if !opts.keepGoing {
return errors.Wrapf(err, "Error copying ref %q", transports.ImageName(ref))
}
// log the error, keep a note that there was a failure and move on to the next
// image ref
errorsPresent = true
logrus.WithError(err).Errorf("Error copying ref %q", transports.ImageName(ref))
continue
}
imagesNumber++
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions docs/skopeo-sync.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ Path of the authentication file for the source registry. Uses path given by `--a

Path of the authentication file for the destination registry. Uses path given by `--authfile`, if not provided.

**--dry-run**
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the flag to completions/bash/skopeo as well.


Run the sync without actually copying data to the destination.

**--src**, **-s** _transport_ Transport for the source repository.

**--dest**, **-d** _transport_ Destination transport.
Expand Down