From 235218cfff46233a2ecb51aebcc3c32e65fd7bf5 Mon Sep 17 00:00:00 2001 From: Zack Brady Date: Wed, 4 Dec 2024 14:26:53 -0500 Subject: [PATCH 1/2] added store env var (#370) * added strictmode flag and consts * updated code with strictmode * added flag for retryoperation * updated registry short flag * added store env var * added/fixed more env var code --------- Signed-off-by: Zack Brady --- cmd/hauler/cli/store/load.go | 14 ++++++++++++-- cmd/hauler/cli/store/save.go | 14 ++++++++++++-- internal/flags/store.go | 13 ++++++++++--- pkg/consts/consts.go | 1 + pkg/cosign/cosign.go | 1 + 5 files changed, 36 insertions(+), 7 deletions(-) diff --git a/cmd/hauler/cli/store/load.go b/cmd/hauler/cli/store/load.go index 913e248f..8cace695 100644 --- a/cmd/hauler/cli/store/load.go +++ b/cmd/hauler/cli/store/load.go @@ -18,9 +18,19 @@ import ( func LoadCmd(ctx context.Context, o *flags.LoadOpts, archiveRefs ...string) error { l := log.FromContext(ctx) + storeDir := o.StoreDir + + if storeDir == "" { + storeDir = os.Getenv(consts.HaulerStoreDir) + } + + if storeDir == "" { + storeDir = consts.DefaultStoreName + } + for _, archiveRef := range archiveRefs { - l.Infof("loading content from [%s] to [%s]", archiveRef, o.StoreDir) - err := unarchiveLayoutTo(ctx, archiveRef, o.StoreDir, o.TempOverride) + l.Infof("loading content from [%s] to [%s]", archiveRef, storeDir) + err := unarchiveLayoutTo(ctx, archiveRef, storeDir, o.TempOverride) if err != nil { return err } diff --git a/cmd/hauler/cli/store/save.go b/cmd/hauler/cli/store/save.go index ae083030..22827dfe 100644 --- a/cmd/hauler/cli/store/save.go +++ b/cmd/hauler/cli/store/save.go @@ -28,6 +28,16 @@ import ( func SaveCmd(ctx context.Context, o *flags.SaveOpts, outputFile string) error { l := log.FromContext(ctx) + storeDir := o.StoreDir + + if storeDir == "" { + storeDir = os.Getenv(consts.HaulerStoreDir) + } + + if storeDir == "" { + storeDir = consts.DefaultStoreName + } + // TODO: Support more formats? a := archiver.NewTarZstd() a.OverwriteExisting = true @@ -42,7 +52,7 @@ func SaveCmd(ctx context.Context, o *flags.SaveOpts, outputFile string) error { return err } defer os.Chdir(cwd) - if err := os.Chdir(o.StoreDir); err != nil { + if err := os.Chdir(storeDir); err != nil { return err } @@ -55,7 +65,7 @@ func SaveCmd(ctx context.Context, o *flags.SaveOpts, outputFile string) error { return err } - l.Infof("saved store [%s] -> [%s]", o.StoreDir, absOutputfile) + l.Infof("saved store [%s] -> [%s]", storeDir, absOutputfile) return nil } diff --git a/internal/flags/store.go b/internal/flags/store.go index 65a62d85..202b255c 100644 --- a/internal/flags/store.go +++ b/internal/flags/store.go @@ -19,7 +19,7 @@ type StoreRootOpts struct { func (o *StoreRootOpts) AddFlags(cmd *cobra.Command) { pf := cmd.PersistentFlags() - pf.StringVarP(&o.StoreDir, "store", "s", consts.DefaultStoreName, "Set the directory to use for the content store") + pf.StringVarP(&o.StoreDir, "store", "s", "", "Set the directory to use for the content store") pf.IntVarP(&o.Retries, "retries", "r", consts.DefaultRetries, "Set the number of retries for operations") } @@ -28,6 +28,14 @@ func (o *StoreRootOpts) Store(ctx context.Context) (*store.Layout, error) { storeDir := o.StoreDir + if storeDir == "" { + storeDir = os.Getenv(consts.HaulerStoreDir) + } + + if storeDir == "" { + storeDir = consts.DefaultStoreName + } + abs, err := filepath.Abs(storeDir) if err != nil { return nil, err @@ -36,8 +44,7 @@ func (o *StoreRootOpts) Store(ctx context.Context) (*store.Layout, error) { l.Debugf("using store at %s", abs) if _, err := os.Stat(abs); errors.Is(err, os.ErrNotExist) { - err := os.Mkdir(abs, os.ModePerm) - if err != nil { + if err := os.MkdirAll(abs, os.ModePerm); err != nil { return nil, err } } else if err != nil { diff --git a/pkg/consts/consts.go b/pkg/consts/consts.go index 6c0a1ee9..a2922588 100644 --- a/pkg/consts/consts.go +++ b/pkg/consts/consts.go @@ -64,6 +64,7 @@ const ( // environment variables HaulerDir = "HAULER_DIR" HaulerTempDir = "HAULER_TEMP_DIR" + HaulerStoreDir = "HAULER_STORE_DIR" HaulerIgnoreErrors = "HAULER_IGNORE_ERRORS" // container files and directories diff --git a/pkg/cosign/cosign.go b/pkg/cosign/cosign.go index c803d51e..4156b8b1 100644 --- a/pkg/cosign/cosign.go +++ b/pkg/cosign/cosign.go @@ -277,6 +277,7 @@ func EnsureBinaryExists(ctx context.Context, bin embed.FS, ro *flags.CliRootOpts // getCosignPath returns the binary path func getCosignPath(haulerDir string) (string, error) { + if haulerDir == "" { haulerDir = os.Getenv(consts.HaulerDir) } From 01faf396bba83bf43867dc804a22abd0e92da073 Mon Sep 17 00:00:00 2001 From: Zack Brady Date: Wed, 4 Dec 2024 14:29:26 -0500 Subject: [PATCH 2/2] renamed incorrectly named consts (#371) --- cmd/hauler/cli/store/save.go | 2 +- internal/mapper/mappers.go | 4 ++-- pkg/consts/consts.go | 10 +++++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cmd/hauler/cli/store/save.go b/cmd/hauler/cli/store/save.go index 22827dfe..886d74fa 100644 --- a/cmd/hauler/cli/store/save.go +++ b/cmd/hauler/cli/store/save.go @@ -171,7 +171,7 @@ func writeExportsManifest(ctx context.Context, dir string, platformStr string) e return err } - return oci.WriteFile(consts.OCIImageManifestFile, buf.Bytes(), 0666) + return oci.WriteFile(consts.ImageManifestFile, buf.Bytes(), 0666) } func (x *exports) describe() tarball.Manifest { diff --git a/internal/mapper/mappers.go b/internal/mapper/mappers.go index 1cfb5867..154daefb 100644 --- a/internal/mapper/mappers.go +++ b/internal/mapper/mappers.go @@ -36,7 +36,7 @@ func Images() map[string]Fn { m := make(map[string]Fn) manifestMapperFn := Fn(func(desc ocispec.Descriptor) (string, error) { - return consts.OCIImageManifestFile, nil + return consts.ImageManifestFile, nil }) for _, l := range []string{consts.DockerManifestSchema2, consts.DockerManifestListSchema2, consts.OCIManifestSchema1} { @@ -52,7 +52,7 @@ func Images() map[string]Fn { } configMapperFn := Fn(func(desc ocispec.Descriptor) (string, error) { - return consts.OCIImageConfigFile, nil + return consts.ImageConfigFile, nil }) for _, l := range []string{consts.DockerConfigJSON} { diff --git a/pkg/consts/consts.go b/pkg/consts/consts.go index a2922588..454ba194 100644 --- a/pkg/consts/consts.go +++ b/pkg/consts/consts.go @@ -68,11 +68,11 @@ const ( HaulerIgnoreErrors = "HAULER_IGNORE_ERRORS" // container files and directories - OCIImageIndexFile = "index.json" - OCIImageManifestFile = "manifest.json" - OCIImageConfigFile = "config.json" - OCIImageLayoutFile = "oci-layout" - OCIImageBlobsDir = "blobs" + OCIImageIndexFile = "index.json" + OCIImageLayoutFile = "oci-layout" + OCIImageBlobsDir = "blobs" + ImageManifestFile = "manifest.json" + ImageConfigFile = "config.json" // other constraints CarbideRegistry = "rgcrprod.azurecr.us"