mirror of
https://github.com/distribution/distribution
synced 2024-11-12 05:45:51 +01:00
9c88801a12
Back in the before time, the best practices surrounding usage of Context weren't quite worked out. We defined our own type to make usage easier. As this packaged was used elsewhere, it make it more and more challenging to integrate with the forked `Context` type. Now that it is available in the standard library, we can just use that one directly. To make usage more consistent, we now use `dcontext` when referring to the distribution context package. Signed-off-by: Stephen J Day <stephen.day@docker.com>
85 lines
2.2 KiB
Go
85 lines
2.2 KiB
Go
package registry
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
dcontext "github.com/docker/distribution/context"
|
|
"github.com/docker/distribution/registry/storage"
|
|
"github.com/docker/distribution/registry/storage/driver/factory"
|
|
"github.com/docker/distribution/version"
|
|
"github.com/docker/libtrust"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var showVersion bool
|
|
|
|
func init() {
|
|
RootCmd.AddCommand(ServeCmd)
|
|
RootCmd.AddCommand(GCCmd)
|
|
GCCmd.Flags().BoolVarP(&dryRun, "dry-run", "d", false, "do everything except remove the blobs")
|
|
RootCmd.Flags().BoolVarP(&showVersion, "version", "v", false, "show the version and exit")
|
|
}
|
|
|
|
// RootCmd is the main command for the 'registry' binary.
|
|
var RootCmd = &cobra.Command{
|
|
Use: "registry",
|
|
Short: "`registry`",
|
|
Long: "`registry`",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if showVersion {
|
|
version.PrintVersion()
|
|
return
|
|
}
|
|
cmd.Usage()
|
|
},
|
|
}
|
|
|
|
var dryRun bool
|
|
|
|
// GCCmd is the cobra command that corresponds to the garbage-collect subcommand
|
|
var GCCmd = &cobra.Command{
|
|
Use: "garbage-collect <config>",
|
|
Short: "`garbage-collect` deletes layers not referenced by any manifests",
|
|
Long: "`garbage-collect` deletes layers not referenced by any manifests",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
config, err := resolveConfiguration(args)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "configuration error: %v\n", err)
|
|
cmd.Usage()
|
|
os.Exit(1)
|
|
}
|
|
|
|
driver, err := factory.Create(config.Storage.Type(), config.Storage.Parameters())
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "failed to construct %s driver: %v", config.Storage.Type(), err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
ctx := dcontext.Background()
|
|
ctx, err = configureLogging(ctx, config)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "unable to configure logging with config: %s", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
k, err := libtrust.GenerateECP256PrivateKey()
|
|
if err != nil {
|
|
fmt.Fprint(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
registry, err := storage.NewRegistry(ctx, driver, storage.Schema1SigningKey(k))
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "failed to construct registry: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
err = storage.MarkAndSweep(ctx, driver, registry, dryRun)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "failed to garbage collect: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
},
|
|
}
|