Skip to content

Commit

Permalink
adjust to make registry and fileserver subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
amartin120 committed Jan 22, 2024
1 parent e14453f commit d9e298b
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 83 deletions.
54 changes: 47 additions & 7 deletions cmd/hauler/cli/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,25 +108,65 @@ func addStoreLoad() *cobra.Command {
}

func addStoreServe() *cobra.Command {
o := &store.ServeOpts{RootOpts: rootStoreOpts}

cmd := &cobra.Command{
Use: "serve",
Short: "Expose the content of a local store through an OCI compliant registry or file server",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}
cmd.AddCommand(
addStoreServeRegistry(),
addStoreServeFiles(),
)

return cmd
}

// RegistryCmd serves the embedded registry
func addStoreServeRegistry() *cobra.Command {
o := &store.ServeRegistryOpts{RootOpts: rootStoreOpts}
cmd := &cobra.Command{
Use: "registry",
Short: "Serve the embedded registry",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

s, err := o.Store(ctx)
if err != nil {
return err
}

return store.ServeCmd(ctx, o, s)
},
}
o.AddFlags(cmd)
return store.ServeRegistryCmd(ctx, o, s)
},
}

return cmd
o.AddFlags(cmd)

return cmd
}

// FileServerCmd serves the file server
func addStoreServeFiles() *cobra.Command {
o := &store.ServeFilesOpts{RootOpts: rootStoreOpts}
cmd := &cobra.Command{
Use: "fileserver",
Short: "Serve the file server",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

s, err := o.Store(ctx)
if err != nil {
return err
}

return store.ServeFilesCmd(ctx, o, s)
},
}

o.AddFlags(cmd)

return cmd
}

func addStoreSave() *cobra.Command {
Expand Down
143 changes: 67 additions & 76 deletions cmd/hauler/cli/store/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,109 +20,100 @@ import (
"github.com/rancherfederal/hauler/pkg/log"
)

type ServeOpts struct {
type ServeRegistryOpts struct {
*RootOpts

Port int
RootDir string
ConfigFile string

Files bool

storedir string
}

func (o *ServeOpts) AddFlags(cmd *cobra.Command) {
func (o *ServeRegistryOpts) AddFlags(cmd *cobra.Command) {
f := cmd.Flags()

f.BoolVarP(&o.Files, "files", "f", false, "Toggle file server instead of registry")
f.IntVarP(&o.Port, "port", "p", 0, "Port to listen on. Defaults to 5000 for registry and 8080 for file server.")
f.StringVar(&o.RootDir, "directory", "", "Directory to use for backend. Defaults to $PWD/registry for registry and $PWD/store-files for file server.")
f.IntVarP(&o.Port, "port", "p", 5000, "Port to listen on.")
f.StringVar(&o.RootDir, "directory", "registry", "Directory to use for backend. Defaults to $PWD/registry")
f.StringVarP(&o.ConfigFile, "config", "c", "", "Path to a config file, will override all other configs")

cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
if o.Port == 0 {
o.Port = getDefaultPort(o.Files)
}
if o.RootDir == "" {
o.RootDir = getDefaultDirectory(o.Files)
}
return nil
}
}

func getDefaultPort(files bool) int {
if files {
return 8080
func ServeRegistryCmd(ctx context.Context, o *ServeRegistryOpts, s *store.Layout) error {
l := log.FromContext(ctx)
ctx = dcontext.WithVersion(ctx, version.Version)

tr := server.NewTempRegistry(ctx, o.RootDir)
if err := tr.Start(); err != nil {
return err
}
return 5000
}

func getDefaultDirectory(files bool) string {
if files {
return "store-files"
opts := &CopyOpts{}
if err := CopyCmd(ctx, opts, s, "registry://"+tr.Registry()); err != nil {
return err
}
return "registry"
}

// ServeCmd serves the embedded registry almost identically to how distribution/v3 does it
func ServeCmd(ctx context.Context, o *ServeOpts, s *store.Layout) error {
l := log.FromContext(ctx)
ctx = dcontext.WithVersion(ctx, version.Version)
tr.Close()

if o.Files {
opts := &CopyOpts{}
if err := CopyCmd(ctx, opts, s, "dir://"+o.RootDir); err != nil {
return err
}

cfg := server.FileConfig{
Root: o.RootDir,
Port: o.Port,
}

f, err := server.NewFile(ctx, cfg)
cfg := o.defaultRegistryConfig()
if o.ConfigFile != "" {
ucfg, err := loadConfig(o.ConfigFile)
if err != nil {
return err
}

l.Infof("starting file server on port [%d]", o.Port)
if err := f.ListenAndServe(); err != nil {
return err
}
cfg = ucfg
}

} else { // start registry
l.Infof("starting registry on port [%d]", o.Port)
r, err := server.NewRegistry(ctx, cfg)
if err != nil {
return err
}

if err = r.ListenAndServe(); err != nil {
return err
}

tr := server.NewTempRegistry(ctx, o.RootDir)
if err := tr.Start(); err != nil {
return err
}
return nil
}

opts := &CopyOpts{}
if err := CopyCmd(ctx, opts, s, "registry://"+tr.Registry()); err != nil {
return err
}
type ServeFilesOpts struct {
*RootOpts

tr.Close()
Port int
RootDir string

cfg := o.defaultConfig()
if o.ConfigFile != "" {
ucfg, err := loadConfig(o.ConfigFile)
if err != nil {
return err
}
cfg = ucfg
}
storedir string
}

l.Infof("starting registry on port [%d]", o.Port)
r, err := server.NewRegistry(ctx, cfg)
if err != nil {
return err
}

if err = r.ListenAndServe(); err != nil {
return err
}
func (o *ServeFilesOpts) AddFlags(cmd *cobra.Command) {
f := cmd.Flags()

f.IntVarP(&o.Port, "port", "p", 8080, "Port to listen on.")
f.StringVar(&o.RootDir, "directory", "store-files", "Directory to use for backend. Defaults to $PWD/store-files")
}

func ServeFilesCmd(ctx context.Context, o *ServeFilesOpts, s *store.Layout) error {
l := log.FromContext(ctx)
ctx = dcontext.WithVersion(ctx, version.Version)

opts := &CopyOpts{}
if err := CopyCmd(ctx, opts, s, "dir://"+o.RootDir); err != nil {
return err
}

cfg := server.FileConfig{
Root: o.RootDir,
Port: o.Port,
}

f, err := server.NewFile(ctx, cfg)
if err != nil {
return err
}

l.Infof("starting file server on port [%d]", o.Port)
if err := f.ListenAndServe(); err != nil {
return err
}

return nil
Expand All @@ -137,7 +128,7 @@ func loadConfig(filename string) (*configuration.Configuration, error) {
return configuration.Parse(f)
}

func (o *ServeOpts) defaultConfig() *configuration.Configuration {
func (o *ServeRegistryOpts) defaultRegistryConfig() *configuration.Configuration {
cfg := &configuration.Configuration{
Version: "0.1",
Storage: configuration.Storage{
Expand Down

0 comments on commit d9e298b

Please sign in to comment.