Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add --cleanup flag (default true) #1529

Merged
merged 3 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/md/melange_build.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ melange build [flags]
--build-option strings build options to enable
--cache-dir string directory used for cached inputs (default "./melange-cache/")
--cache-source string directory or bucket used for preloading the cache
--cleanup when enabled, the temp dir used for the guest will be cleaned up after completion (default true)
--cpu string default CPU resources to use for builds
--create-build-log creates a package.log file containing a list of packages that were built by the command
--debug enables debug logging of build pipelines
Expand Down
1 change: 1 addition & 0 deletions docs/md/melange_test.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ melange test [flags]
--arch strings architectures to build for (e.g., x86_64,ppc64le,arm64) -- default is all, unless specified in config
--cache-dir string directory used for cached inputs
--cache-source string directory or bucket used for preloading the cache
--cleanup when enabled, the temp dir used for the guest will be cleaned up after completion (default true)
--debug enables debug logging of test pipelines (sets -x for steps)
--debug-runner when enabled, the builder pod will persist after the build succeeds or fails
--env-file string file to use for preloaded environment variables
Expand Down
5 changes: 5 additions & 0 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type Build struct {
PipelineDirs []string
SourceDir string
GuestDir string
Cleanup bool
SigningKey string
SigningPassphrase string
Namespace string
Expand Down Expand Up @@ -703,6 +704,10 @@ func (b *Build) BuildPackage(ctx context.Context) error {
return fmt.Errorf("unable to make guest directory: %w", err)
}
b.GuestDir = guestDir

if b.Cleanup {
defer os.RemoveAll(guestDir)
}
}

log.Infof("evaluating pipelines for package requirements")
Expand Down
8 changes: 8 additions & 0 deletions pkg/build/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,11 @@ func WithIgnoreSignatures(ignore bool) Option {
return nil
}
}

// WithCleanup indicates whether to clean up the build environment after the build is complete.
func WithCleanup(c bool) Option {
return func(b *Build) error {
b.Cleanup = c
return nil
}
}
5 changes: 5 additions & 0 deletions pkg/build/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Test struct {
PipelineDirs []string
SourceDir string
GuestDir string
Cleanup bool
Arch apko_types.Architecture
ExtraKeys []string
ExtraRepos []string
Expand Down Expand Up @@ -376,6 +377,10 @@ func (t *Test) TestPackage(ctx context.Context) error {
return fmt.Errorf("unable to make guest directory: %w", err)
}
t.GuestDir = guestDir

if t.Cleanup {
defer os.RemoveAll(guestDir)
}
}

imgRef := ""
Expand Down
8 changes: 8 additions & 0 deletions pkg/build/test_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,11 @@ func WithTestAuth(domain, user, pass string) TestOption {
return nil
}
}

// If true, the test will clean up the test environment after the test is complete.
func WithTestCleanup(c bool) TestOption {
return func(t *Test) error {
t.Cleanup = c
return nil
}
}
3 changes: 3 additions & 0 deletions pkg/cli/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func buildCmd() *cobra.Command {
var libc string
var lintRequire, lintWarn []string
var ignoreSignatures bool
var cleanup bool

var traceFile string

Expand Down Expand Up @@ -157,6 +158,7 @@ func buildCmd() *cobra.Command {
build.WithTimeout(timeout),
build.WithLibcFlavorOverride(libc),
build.WithIgnoreSignatures(ignoreSignatures),
build.WithCleanup(cleanup),
}

if len(args) > 0 {
Expand Down Expand Up @@ -224,6 +226,7 @@ func buildCmd() *cobra.Command {
cmd.Flags().StringSliceVar(&lintRequire, "lint-require", linter.DefaultRequiredLinters(), "linters that must pass")
cmd.Flags().StringSliceVar(&lintWarn, "lint-warn", linter.DefaultWarnLinters(), "linters that will generate warnings")
cmd.Flags().BoolVar(&ignoreSignatures, "ignore-signatures", false, "ignore repository signature verification")
cmd.Flags().BoolVar(&cleanup, "cleanup", true, "when enabled, the temp dir used for the guest will be cleaned up after completion")

_ = cmd.Flags().Bool("fail-on-lint-warning", false, "DEPRECATED: DO NOT USE")
_ = cmd.Flags().MarkDeprecated("fail-on-lint-warning", "use --lint-require and --lint-warn instead")
Expand Down
3 changes: 3 additions & 0 deletions pkg/cli/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func test() *cobra.Command {
var interactive bool
var runner string
var extraTestPackages []string
var cleanup bool

cmd := &cobra.Command{
Use: "test",
Expand Down Expand Up @@ -78,6 +79,7 @@ func test() *cobra.Command {
build.WithTestDebug(debug),
build.WithTestDebugRunner(debugRunner),
build.WithTestInteractive(interactive),
build.WithTestCleanup(cleanup),
}

if len(args) > 0 {
Expand Down Expand Up @@ -129,6 +131,7 @@ func test() *cobra.Command {
cmd.Flags().BoolVarP(&interactive, "interactive", "i", false, "when enabled, attaches stdin with a tty to the pod on failure")
cmd.Flags().StringSliceVarP(&extraRepos, "repository-append", "r", []string{}, "path to extra repositories to include in the build environment")
cmd.Flags().StringSliceVar(&extraTestPackages, "test-package-append", []string{}, "extra packages to install for each of the test environments")
cmd.Flags().BoolVar(&cleanup, "cleanup", true, "when enabled, the temp dir used for the guest will be cleaned up after completion")

return cmd
}
Expand Down
Loading