1package registry 2 3import ( 4 "fmt" 5 "os" 6 7 dcontext "github.com/docker/distribution/context" 8 "github.com/docker/distribution/registry/storage" 9 "github.com/docker/distribution/registry/storage/driver/factory" 10 "github.com/docker/distribution/version" 11 "github.com/docker/libtrust" 12 "github.com/spf13/cobra" 13) 14 15var showVersion bool 16 17func init() { 18 RootCmd.AddCommand(ServeCmd) 19 RootCmd.AddCommand(GCCmd) 20 GCCmd.Flags().BoolVarP(&dryRun, "dry-run", "d", false, "do everything except remove the blobs") 21 GCCmd.Flags().BoolVarP(&removeUntagged, "delete-untagged", "m", false, "delete manifests that are not currently referenced via tag") 22 RootCmd.Flags().BoolVarP(&showVersion, "version", "v", false, "show the version and exit") 23} 24 25// RootCmd is the main command for the 'registry' binary. 26var RootCmd = &cobra.Command{ 27 Use: "registry", 28 Short: "`registry`", 29 Long: "`registry`", 30 Run: func(cmd *cobra.Command, args []string) { 31 if showVersion { 32 version.PrintVersion() 33 return 34 } 35 cmd.Usage() 36 }, 37} 38 39var dryRun bool 40var removeUntagged bool 41 42// GCCmd is the cobra command that corresponds to the garbage-collect subcommand 43var GCCmd = &cobra.Command{ 44 Use: "garbage-collect <config>", 45 Short: "`garbage-collect` deletes layers not referenced by any manifests", 46 Long: "`garbage-collect` deletes layers not referenced by any manifests", 47 Run: func(cmd *cobra.Command, args []string) { 48 config, err := resolveConfiguration(args) 49 if err != nil { 50 fmt.Fprintf(os.Stderr, "configuration error: %v\n", err) 51 cmd.Usage() 52 os.Exit(1) 53 } 54 55 driver, err := factory.Create(config.Storage.Type(), config.Storage.Parameters()) 56 if err != nil { 57 fmt.Fprintf(os.Stderr, "failed to construct %s driver: %v", config.Storage.Type(), err) 58 os.Exit(1) 59 } 60 61 ctx := dcontext.Background() 62 ctx, err = configureLogging(ctx, config) 63 if err != nil { 64 fmt.Fprintf(os.Stderr, "unable to configure logging with config: %s", err) 65 os.Exit(1) 66 } 67 68 k, err := libtrust.GenerateECP256PrivateKey() 69 if err != nil { 70 fmt.Fprint(os.Stderr, err) 71 os.Exit(1) 72 } 73 74 registry, err := storage.NewRegistry(ctx, driver, storage.Schema1SigningKey(k)) 75 if err != nil { 76 fmt.Fprintf(os.Stderr, "failed to construct registry: %v", err) 77 os.Exit(1) 78 } 79 80 err = storage.MarkAndSweep(ctx, driver, registry, storage.GCOpts{ 81 DryRun: dryRun, 82 RemoveUntagged: removeUntagged, 83 }) 84 if err != nil { 85 fmt.Fprintf(os.Stderr, "failed to garbage collect: %v", err) 86 os.Exit(1) 87 } 88 }, 89} 90