From 1c9620ba5b173574f04416352796025b734e63bf Mon Sep 17 00:00:00 2001 From: thesayyn Date: Tue, 21 Nov 2023 15:41:41 -0800 Subject: [PATCH 1/5] feat: allow port and disk path to be overriden --- cmd/crane/cmd/serve.go | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/cmd/crane/cmd/serve.go b/cmd/crane/cmd/serve.go index 61d031b39..aef1744f7 100644 --- a/cmd/crane/cmd/serve.go +++ b/cmd/crane/cmd/serve.go @@ -37,11 +37,11 @@ func newCmdRegistry() *cobra.Command { } func newCmdServe() *cobra.Command { - var disk bool + var address, disk string cmd := &cobra.Command{ Use: "serve", Short: "Serve an in-memory registry implementation", - Long: `This sub-command serves an in-memory registry implementation on an automatically chosen port (or $PORT) + Long: `This sub-command serves an in-memory registry implementation on an automatically chosen port (:0), $PORT or --address The command blocks while the server accepts pushes and pulls. @@ -54,7 +54,12 @@ Contents are only stored in memory, and when the process exits, pushed data is l if port == "" { port = "0" } - listener, err := net.Listen("tcp", ":"+port) + listenOn := ":" + port + if address != "" { + listenOn = address + } + + listener, err := net.Listen("tcp", listenOn) if err != nil { log.Fatalln(err) } @@ -62,12 +67,14 @@ Contents are only stored in memory, and when the process exits, pushed data is l port = fmt.Sprintf("%d", porti) bh := registry.NewInMemoryBlobHandler() - if disk { - tmp := os.TempDir() - log.Printf("storing blobs in %s", tmp) - bh = registry.NewDiskBlobHandler(tmp) + if cmd.Flags().Changed("blobs-to-disk") { + path := os.TempDir() + if disk != "" && disk != " " { + path = disk + } + log.Printf("storing blobs in %s", path) + bh = registry.NewDiskBlobHandler(path) } - s := &http.Server{ ReadHeaderTimeout: 5 * time.Second, // prevent slowloris, quiet linter Handler: registry.New(registry.WithBlobHandler(bh)), @@ -89,7 +96,11 @@ Contents are only stored in memory, and when the process exits, pushed data is l return nil }, } - cmd.Flags().BoolVar(&disk, "blobs-to-disk", false, "Store blobs on disk") + cmd.Flags().StringVarP(&disk, "blobs-to-disk", "", "", "Store blobs on disk") + // allow --blobs-to-disk to work without a value + cmd.Flags().Lookup("blobs-to-disk").NoOptDefVal = " " cmd.Flags().MarkHidden("blobs-to-disk") + cmd.Flags().StringVar(&address, "address", "", "Address to listen on") + return cmd } From 10d278df07f8ecd5b9a351f9a3fd4400fcf30a3b Mon Sep 17 00:00:00 2001 From: thesayyn Date: Tue, 21 Nov 2023 15:51:58 -0800 Subject: [PATCH 2/5] docs --- cmd/crane/doc/crane_registry_serve.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cmd/crane/doc/crane_registry_serve.md b/cmd/crane/doc/crane_registry_serve.md index f405688e0..02b2499c7 100644 --- a/cmd/crane/doc/crane_registry_serve.md +++ b/cmd/crane/doc/crane_registry_serve.md @@ -4,7 +4,7 @@ Serve an in-memory registry implementation ### Synopsis -This sub-command serves an in-memory registry implementation on an automatically chosen port (or $PORT) +This sub-command serves an in-memory registry implementation on an automatically chosen port (:0), $PORT or --address The command blocks while the server accepts pushes and pulls. @@ -17,7 +17,8 @@ crane registry serve [flags] ### Options ``` - -h, --help help for serve + --address string Address to listen on + -h, --help help for serve ``` ### Options inherited from parent commands From ce43b8a85a28bbb721eda358c83449060d8185a6 Mon Sep 17 00:00:00 2001 From: thesayyn Date: Wed, 29 Nov 2023 12:54:40 -0800 Subject: [PATCH 3/5] new surface --- cmd/crane/cmd/serve.go | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/cmd/crane/cmd/serve.go b/cmd/crane/cmd/serve.go index aef1744f7..786cfdf82 100644 --- a/cmd/crane/cmd/serve.go +++ b/cmd/crane/cmd/serve.go @@ -38,14 +38,15 @@ func newCmdRegistry() *cobra.Command { func newCmdServe() *cobra.Command { var address, disk string + var blobs_to_disk bool cmd := &cobra.Command{ Use: "serve", - Short: "Serve an in-memory registry implementation", - Long: `This sub-command serves an in-memory registry implementation on an automatically chosen port (:0), $PORT or --address + Short: "Serve a registry implementation", + Long: `This sub-command serves a registry implementation on an automatically chosen port (:0), $PORT or --address The command blocks while the server accepts pushes and pulls. -Contents are only stored in memory, and when the process exits, pushed data is lost.`, +Contents are can be stored in memory (when the process exits, pushed data is lost.), and disk (--disk).`, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { ctx := cmd.Context() @@ -67,14 +68,23 @@ Contents are only stored in memory, and when the process exits, pushed data is l port = fmt.Sprintf("%d", porti) bh := registry.NewInMemoryBlobHandler() + + diskp := disk if cmd.Flags().Changed("blobs-to-disk") { - path := os.TempDir() - if disk != "" && disk != " " { - path = disk + if disk != "" { + return fmt.Errorf("--disk and --blobs-to-disk can't be used together") + } + diskp, err = os.MkdirTemp(os.TempDir(), "craneregistry*") + if err != nil { + return err } - log.Printf("storing blobs in %s", path) - bh = registry.NewDiskBlobHandler(path) } + + if diskp != "" { + log.Printf("storing blobs in %s", diskp) + bh = registry.NewDiskBlobHandler(diskp) + } + s := &http.Server{ ReadHeaderTimeout: 5 * time.Second, // prevent slowloris, quiet linter Handler: registry.New(registry.WithBlobHandler(bh)), @@ -96,10 +106,11 @@ Contents are only stored in memory, and when the process exits, pushed data is l return nil }, } - cmd.Flags().StringVarP(&disk, "blobs-to-disk", "", "", "Store blobs on disk") - // allow --blobs-to-disk to work without a value - cmd.Flags().Lookup("blobs-to-disk").NoOptDefVal = " " + // TODO: remove --blobs-to-disk in a future release. + cmd.Flags().BoolVarP(&blobs_to_disk, "blobs-to-disk", "", false, "Store blobs on disk on tmpdir") cmd.Flags().MarkHidden("blobs-to-disk") + cmd.Flags().MarkDeprecated("blobs-to-disk", "and will stop working in a future release. use --disk=$(mktemp -d) instead.") + cmd.Flags().StringVarP(&disk, "disk", "", "", "Path to a directory where blobs will be stored") cmd.Flags().StringVar(&address, "address", "", "Address to listen on") return cmd From def8f05f18469ec03bf112925ae6d79132f7f237 Mon Sep 17 00:00:00 2001 From: thesayyn Date: Wed, 29 Nov 2023 13:00:34 -0800 Subject: [PATCH 4/5] lint --- cmd/crane/cmd/serve.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/crane/cmd/serve.go b/cmd/crane/cmd/serve.go index 786cfdf82..3c1af885d 100644 --- a/cmd/crane/cmd/serve.go +++ b/cmd/crane/cmd/serve.go @@ -38,7 +38,7 @@ func newCmdRegistry() *cobra.Command { func newCmdServe() *cobra.Command { var address, disk string - var blobs_to_disk bool + var blobsToDisk bool cmd := &cobra.Command{ Use: "serve", Short: "Serve a registry implementation", @@ -107,7 +107,7 @@ Contents are can be stored in memory (when the process exits, pushed data is los }, } // TODO: remove --blobs-to-disk in a future release. - cmd.Flags().BoolVarP(&blobs_to_disk, "blobs-to-disk", "", false, "Store blobs on disk on tmpdir") + cmd.Flags().BoolVarP(&blobsToDisk, "blobs-to-disk", "", false, "Store blobs on disk on tmpdir") cmd.Flags().MarkHidden("blobs-to-disk") cmd.Flags().MarkDeprecated("blobs-to-disk", "and will stop working in a future release. use --disk=$(mktemp -d) instead.") cmd.Flags().StringVarP(&disk, "disk", "", "", "Path to a directory where blobs will be stored") From 5b78fa1dca5a2db1bd767c532fb1ad7b5f283482 Mon Sep 17 00:00:00 2001 From: thesayyn Date: Wed, 29 Nov 2023 13:04:07 -0800 Subject: [PATCH 5/5] docs --- cmd/crane/doc/crane_registry.md | 2 +- cmd/crane/doc/crane_registry_serve.md | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/cmd/crane/doc/crane_registry.md b/cmd/crane/doc/crane_registry.md index d2bf920d8..a75321ee7 100644 --- a/cmd/crane/doc/crane_registry.md +++ b/cmd/crane/doc/crane_registry.md @@ -20,5 +20,5 @@ ### SEE ALSO * [crane](crane.md) - Crane is a tool for managing container images -* [crane registry serve](crane_registry_serve.md) - Serve an in-memory registry implementation +* [crane registry serve](crane_registry_serve.md) - Serve a registry implementation diff --git a/cmd/crane/doc/crane_registry_serve.md b/cmd/crane/doc/crane_registry_serve.md index 02b2499c7..a411e5c22 100644 --- a/cmd/crane/doc/crane_registry_serve.md +++ b/cmd/crane/doc/crane_registry_serve.md @@ -1,14 +1,14 @@ ## crane registry serve -Serve an in-memory registry implementation +Serve a registry implementation ### Synopsis -This sub-command serves an in-memory registry implementation on an automatically chosen port (:0), $PORT or --address +This sub-command serves a registry implementation on an automatically chosen port (:0), $PORT or --address The command blocks while the server accepts pushes and pulls. -Contents are only stored in memory, and when the process exits, pushed data is lost. +Contents are can be stored in memory (when the process exits, pushed data is lost.), and disk (--disk). ``` crane registry serve [flags] @@ -18,6 +18,7 @@ crane registry serve [flags] ``` --address string Address to listen on + --disk string Path to a directory where blobs will be stored -h, --help help for serve ```