Skip to content

Commit

Permalink
fix: use tmpdir if provided (#431)
Browse files Browse the repository at this point in the history
Co-authored-by: unclegedd <gedd@defenseunicorns.com>
  • Loading branch information
decleaver and UncleGedd committed Feb 20, 2024
1 parent 823ac5e commit ef4b577
Show file tree
Hide file tree
Showing 18 changed files with 139 additions and 52 deletions.
4 changes: 2 additions & 2 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var (

var rootCmd = &cobra.Command{
Use: "uds COMMAND",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
// Skip for vendor-only commands
if common.CheckVendorOnlyFromPath(cmd) {
return
Expand All @@ -46,7 +46,7 @@ var rootCmd = &cobra.Command{
cliSetup()
},
Short: lang.RootCmdShort,
Run: func(cmd *cobra.Command, args []string) {
Run: func(cmd *cobra.Command, _ []string) {
_, _ = fmt.Fprintln(os.Stderr)
err := cmd.Help()
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/uds.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var createCmd = &cobra.Command{
Aliases: []string{"c"},
Args: cobra.MaximumNArgs(1),
Short: lang.CmdBundleCreateShort,
PreRun: func(cmd *cobra.Command, args []string) {
PreRun: func(_ *cobra.Command, args []string) {
pathToBundleFile := ""
if len(args) > 0 {
if !zarfUtils.IsDir(args[0]) {
Expand All @@ -44,7 +44,7 @@ var createCmd = &cobra.Command{
message.Fatalf(err, "Neither %s or %s found", config.BundleYAML, bundleYml)
}
},
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, args []string) {
srcDir, err := os.Getwd()
if err != nil {
message.Fatalf(err, "error reading the current working directory")
Expand All @@ -69,7 +69,7 @@ var deployCmd = &cobra.Command{
Aliases: []string{"d"},
Short: lang.CmdBundleDeployShort,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, args []string) {
bundleCfg.DeployOpts.Source = chooseBundle(args)
configureZarf()

Expand Down
9 changes: 1 addition & 8 deletions src/pkg/bundle/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func New(cfg *types.BundleConfig) (*Bundle, error) {
}
)

tmp, err := utils.MakeTempDir("")
tmp, err := utils.MakeTempDir(config.CommonOptions.TempDirectory)
if err != nil {
return nil, fmt.Errorf("bundler unable to create temp directory: %w", err)
}
Expand Down Expand Up @@ -102,11 +102,6 @@ func (b *Bundle) ValidateBundleResources(bundle *types.UDSBundle, spinner *messa
return fmt.Errorf("error validating bundle vars: %s", err)
}

tmp, err := utils.MakeTempDir("")
if err != nil {
return err
}

// validate access to packages as well as components referenced in the package
for idx, pkg := range bundle.Packages {
spinner.Updatef("Validating Bundle Package: %s", pkg.Name)
Expand Down Expand Up @@ -180,8 +175,6 @@ func (b *Bundle) ValidateBundleResources(bundle *types.UDSBundle, spinner *messa

message.Debug("Validating package:", message.JSONValue(pkg))

defer os.RemoveAll(tmp)

// todo: need to packager.ValidatePackageSignature (or come up with a bundle-level signature scheme)
publicKeyPath := filepath.Join(b.tmp, config.PublicKeyFile)
if pkg.PublicKey != "" {
Expand Down
20 changes: 2 additions & 18 deletions src/pkg/bundle/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package bundle

import (
"fmt"
"os"
"path/filepath"

"github.com/AlecAivazis/survey/v2"
Expand All @@ -21,25 +20,9 @@ import (

// Create creates a bundle
func (b *Bundle) Create() error {
// get the current working directory
cwd, err := os.Getwd()
if err != nil {
return err
}

// cd into base
if err := os.Chdir(b.cfg.CreateOpts.SourceDirectory); err != nil {
return err
}
defer func() {
err := os.Chdir(cwd)
if err != nil {
fmt.Println("Error changing back to the original directory:", err)
}
}()

// read the bundle's metadata into memory
if err := utils.ReadYaml(b.cfg.CreateOpts.BundleFile, &b.bundle); err != nil {
if err := utils.ReadYaml(filepath.Join(b.cfg.CreateOpts.SourceDirectory, b.cfg.CreateOpts.BundleFile), &b.bundle); err != nil {
return err
}

Expand Down Expand Up @@ -94,6 +77,7 @@ func (b *Bundle) Create() error {
Bundle: &b.bundle,
Output: b.cfg.CreateOpts.Output,
TmpDstDir: b.tmp,
SourceDir: b.cfg.CreateOpts.SourceDirectory,
}
bundlerClient := bundler.NewBundler(&opts)
return bundlerClient.Create()
Expand Down
5 changes: 4 additions & 1 deletion src/pkg/bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Bundler struct {
bundle *types.UDSBundle
output string
tmpDstDir string
sourceDir string
}

type Pusher interface {
Expand All @@ -21,20 +22,22 @@ type Options struct {
Bundle *types.UDSBundle
Output string
TmpDstDir string
SourceDir string
}

func NewBundler(opts *Options) *Bundler {
b := Bundler{
bundle: opts.Bundle,
output: opts.Output,
tmpDstDir: opts.TmpDstDir,
sourceDir: opts.SourceDir,
}
return &b
}

func (b *Bundler) Create() error {
if b.output == "" {
localBundle := NewLocalBundle(&LocalBundleOpts{Bundle: b.bundle, TmpDstDir: b.tmpDstDir})
localBundle := NewLocalBundle(&LocalBundleOpts{Bundle: b.bundle, TmpDstDir: b.tmpDstDir, SourceDir: b.sourceDir})
err := localBundle.create(nil)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/bundler/fetcher/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type localFetcher struct {
func (f *localFetcher) Fetch() ([]ocispec.Descriptor, error) {
fetchSpinner := message.NewProgressSpinner("Fetching package %s", f.pkg.Name)
defer fetchSpinner.Stop()
pkgTmp, err := zarfUtils.MakeTempDir("")
pkgTmp, err := zarfUtils.MakeTempDir(config.CommonOptions.TempDirectory)
defer os.RemoveAll(pkgTmp)
if err != nil {
return nil, err
Expand All @@ -64,7 +64,7 @@ func (f *localFetcher) Fetch() ([]ocispec.Descriptor, error) {

// GetPkgMetadata grabs metadata from a local Zarf package's zarf.yaml
func (f *localFetcher) GetPkgMetadata() (zarfTypes.ZarfPackage, error) {
tmpDir, err := zarfUtils.MakeTempDir("")
tmpDir, err := zarfUtils.MakeTempDir(config.CommonOptions.TempDirectory)
if err != nil {
return zarfTypes.ZarfPackage{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/bundler/fetcher/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (f *remoteFetcher) GetPkgMetadata() (zarfTypes.ZarfPackage, error) {
if err != nil {
return zarfTypes.ZarfPackage{}, err
}
tmpDir, err := zarfUtils.MakeTempDir("")
tmpDir, err := zarfUtils.MakeTempDir(config.CommonOptions.TempDirectory)
if err != nil {
return zarfTypes.ZarfPackage{}, fmt.Errorf("bundler unable to create temp directory: %w", err)
}
Expand Down
14 changes: 7 additions & 7 deletions src/pkg/bundler/localbundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,20 @@ import (
type LocalBundleOpts struct {
Bundle *types.UDSBundle
TmpDstDir string
SourceDir string
}

type LocalBundle struct {
bundle *types.UDSBundle
tmpDstDir string
sourceDir string
}

func NewLocalBundle(opts *LocalBundleOpts) *LocalBundle {
return &LocalBundle{
bundle: opts.Bundle,
tmpDstDir: opts.TmpDstDir,
sourceDir: opts.SourceDir,
}
}

Expand Down Expand Up @@ -153,7 +156,7 @@ func (lo *LocalBundle) create(signature []byte) error {
}

// tarball the bundle
err = writeTarball(bundle, artifactPathMap)
err = writeTarball(bundle, artifactPathMap, lo.sourceDir)
if err != nil {
return err
}
Expand Down Expand Up @@ -199,17 +202,14 @@ func pushManifestConfig(store *ocistore.Store, metadata types.UDSMetadata, build
}

// writeTarball builds and writes a bundle tarball to disk based on a file map
func writeTarball(bundle *types.UDSBundle, artifactPathMap types.PathMap) error {
func writeTarball(bundle *types.UDSBundle, artifactPathMap types.PathMap, sourceDir string) error {
format := archiver.CompressedArchive{
Compression: archiver.Zstd{},
Archival: archiver.Tar{},
}
filename := fmt.Sprintf("%s%s-%s-%s.tar.zst", config.BundlePrefix, bundle.Metadata.Name, bundle.Metadata.Architecture, bundle.Metadata.Version)
cwd, err := os.Getwd()
if err != nil {
return err
}
dst := filepath.Join(cwd, filename)

dst := filepath.Join(sourceDir, filename)

_ = os.RemoveAll(dst)

Expand Down
4 changes: 3 additions & 1 deletion src/pkg/bundler/pusher/remote.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023-Present The UDS Authors

// Package pusher contains functionality to push Zarf pkgs to remote bundles
package pusher

import (
Expand All @@ -23,6 +24,7 @@ type RemotePusher struct {
cfg Config
}

// Config contains the configuration for the remote pusher
type Config struct {
PkgRootManifest *oci.ZarfOCIManifest
RemoteSrc *oci.OrasRemote
Expand Down Expand Up @@ -81,7 +83,7 @@ func (p *RemotePusher) PushManifest() (ocispec.Descriptor, error) {
return zarfManifestDesc, nil
}

// todo: what does this fn return?
// LayersToRemoteBundle pushes the Zarf pkg's layers to a remote bundle
func (p *RemotePusher) LayersToRemoteBundle(spinner *message.Spinner, currentPackageIter int, totalPackages int) ([]ocispec.Descriptor, error) {
spinner.Updatef("Fetching %s package layer metadata (package %d of %d)", p.pkg.Name, currentPackageIter, totalPackages)
// get only the layers that are required by the components
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (r *Runner) importTasks(includes []map[string]string, dir string, setVariab
// check if included file is a url
if helpers.IsURL(includeFilename) {
// If file is a url download it to a tmp directory
tmpDir, err := zarfUtils.MakeTempDir("")
tmpDir, err := zarfUtils.MakeTempDir(config.CommonOptions.TempDirectory)
defer os.RemoveAll(tmpDir)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion src/test/bundles/03-local-and-remote/uds-bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ packages:
repository: ghcr.io/defenseunicorns/uds-cli/nginx
ref: 0.0.1
- name: podinfo
path: "../../packages/podinfo"
path: "src/test/packages/podinfo"
ref: 0.0.1
2 changes: 1 addition & 1 deletion src/test/bundles/04-init/uds-bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ metadata:
# cannot do uds remove on this pkg due to having the same name
packages:
- name: init
path: "../../packages"
path: "src/test/packages"
# renovate: datasource=github-tags depName=defenseunicorns/zarf
ref: v0.32.3
optionalComponents:
Expand Down
2 changes: 1 addition & 1 deletion src/test/bundles/05-gitrepo/uds-bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ metadata:

packages:
- name: gitrepo
path: "../../packages/gitrepo"
path: "src/test/packages/gitrepo"
ref: 0.0.1
2 changes: 1 addition & 1 deletion src/test/bundles/07-helm-overrides/uds-bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ metadata:

packages:
- name: helm-overrides
path: "../../packages/helm"
path: "src/test/packages/helm"
ref: 0.0.1

overrides:
Expand Down
4 changes: 2 additions & 2 deletions src/test/bundles/08-var-precedence/uds-bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ metadata:

packages:
- name: helm-overrides
path: "../../packages/helm"
path: "src/test/packages/helm"
ref: 0.0.1
overrides:
podinfo-component:
Expand All @@ -22,5 +22,5 @@ packages:
default: "uds.dev"

- name: output-var
path: "../../packages/no-cluster/output-var"
path: "src/test/packages/no-cluster/output-var"
ref: 0.0.1
2 changes: 1 addition & 1 deletion src/test/bundles/09-uds-bundle-yml/uds-bundle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ metadata:

packages:
- name: nginx
path: "../../packages/nginx"
path: "src/test/packages/nginx"
ref: 0.0.1
2 changes: 1 addition & 1 deletion src/test/bundles/11-real-simple/uds-bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ metadata:

packages:
- name: real-simple
path: "../../packages/no-cluster/real-simple"
path: "src/test/packages/no-cluster/real-simple"
ref: 0.0.1
Loading

0 comments on commit ef4b577

Please sign in to comment.