2016-01-19 23:26:15 +01:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
2016-04-27 20:49:01 +02:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2017-08-12 00:31:16 +02:00
|
|
|
dcontext "github.com/docker/distribution/context"
|
2016-04-27 20:49:01 +02:00
|
|
|
"github.com/docker/distribution/registry/storage"
|
|
|
|
"github.com/docker/distribution/registry/storage/driver/factory"
|
2016-01-19 23:26:15 +01:00
|
|
|
"github.com/docker/distribution/version"
|
2016-04-27 20:49:01 +02:00
|
|
|
"github.com/docker/libtrust"
|
2016-01-19 23:26:15 +01:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var showVersion bool
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RootCmd.AddCommand(ServeCmd)
|
|
|
|
RootCmd.AddCommand(GCCmd)
|
2016-04-27 20:49:01 +02:00
|
|
|
GCCmd.Flags().BoolVarP(&dryRun, "dry-run", "d", false, "do everything except remove the blobs")
|
2016-01-19 23:26:15 +01:00
|
|
|
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()
|
|
|
|
},
|
|
|
|
}
|
2016-04-27 20:49:01 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-08-12 00:31:16 +02:00
|
|
|
ctx := dcontext.Background()
|
2016-04-27 20:49:01 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-04-07 02:01:30 +02:00
|
|
|
registry, err := storage.NewRegistry(ctx, driver, storage.Schema1SigningKey(k))
|
2016-04-27 20:49:01 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|