Skip to content

Commit

Permalink
Merge pull request #1780 from carolynvs/disable-logs
Browse files Browse the repository at this point in the history
Make log persistence configurable
  • Loading branch information
carolynvs authored Sep 28, 2021
2 parents 1211300 + 4badbf1 commit 43a2b2a
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 11 deletions.
8 changes: 8 additions & 0 deletions cmd/porter/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ For example, the 'debug' driver may be specified, which simply logs the info giv
"Credential to use when installing the bundle. May be either a named set of credentials or a filepath, and specified multiple times.")
f.StringVarP(&opts.Driver, "driver", "d", porter.DefaultDriver,
"Specify a driver to use. Allowed values: docker, debug")
f.BoolVar(&opts.NoLogs, "no-logs", false,
"Do not persist the bundle execution logs")
addBundlePullFlags(f, &opts.BundlePullOptions)
return cmd
}
Expand Down Expand Up @@ -196,6 +198,8 @@ For example, the 'debug' driver may be specified, which simply logs the info giv
"Credential to use when installing the bundle. May be either a named set of credentials or a filepath, and specified multiple times.")
f.StringVarP(&opts.Driver, "driver", "d", porter.DefaultDriver,
"Specify a driver to use. Allowed values: docker, debug")
f.BoolVar(&opts.NoLogs, "no-logs", false,
"Do not persist the bundle execution logs")
addBundlePullFlags(f, &opts.BundlePullOptions)

return cmd
Expand Down Expand Up @@ -245,6 +249,8 @@ For example, the 'debug' driver may be specified, which simply logs the info giv
"Credential to use when installing the bundle. May be either a named set of credentials or a filepath, and specified multiple times.")
f.StringVarP(&opts.Driver, "driver", "d", porter.DefaultDriver,
"Specify a driver to use. Allowed values: docker, debug")
f.BoolVar(&opts.NoLogs, "no-logs", false,
"Do not persist the bundle execution logs")
addBundlePullFlags(f, &opts.BundlePullOptions)

return cmd
Expand Down Expand Up @@ -298,6 +304,8 @@ For example, the 'debug' driver may be specified, which simply logs the info giv
"Delete all records associated with the installation, assuming the uninstall action succeeds")
f.BoolVar(&opts.ForceDelete, "force-delete", false,
"UNSAFE. Delete all records associated with the installation, even if uninstall fails. This is intended for cleaning up test data and is not recommended for production environments.")
f.BoolVar(&opts.NoLogs, "no-logs", false,
"Do not persist the bundle execution logs")
addBundlePullFlags(f, &opts.BundlePullOptions)

return cmd
Expand Down
1 change: 1 addition & 0 deletions docs/content/cli/bundles_install.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ porter bundles install [INSTALLATION] [flags]
--force Force a fresh pull of the bundle
-h, --help help for install
--insecure-registry Don't require TLS for the registry
--no-logs Do not persist the bundle execution logs
--param strings Define an individual parameter in the form NAME=VALUE. Overrides parameters otherwise set via --parameter-set. May be specified multiple times.
-p, --parameter-set strings Name of a parameter set file for the bundle. May be either a named set of parameters or a filepath, and specified multiple times.
-r, --reference string Use a bundle in an OCI registry specified by the given reference.
Expand Down
1 change: 1 addition & 0 deletions docs/content/cli/bundles_invoke.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ porter bundles invoke [INSTALLATION] --action ACTION [flags]
--force Force a fresh pull of the bundle
-h, --help help for invoke
--insecure-registry Don't require TLS for the registry
--no-logs Do not persist the bundle execution logs
--param strings Define an individual parameter in the form NAME=VALUE. Overrides parameters otherwise set via --parameter-set. May be specified multiple times.
-p, --parameter-set strings Name of a parameter set file for the bundle. May be either a named set of parameters or a filepath, and specified multiple times.
-r, --reference string Use a bundle in an OCI registry specified by the given reference.
Expand Down
1 change: 1 addition & 0 deletions docs/content/cli/bundles_uninstall.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ porter bundles uninstall [INSTALLATION] [flags]
--force-delete UNSAFE. Delete all records associated with the installation, even if uninstall fails. This is intended for cleaning up test data and is not recommended for production environments.
-h, --help help for uninstall
--insecure-registry Don't require TLS for the registry
--no-logs Do not persist the bundle execution logs
--param strings Define an individual parameter in the form NAME=VALUE. Overrides parameters otherwise set via --parameter-set. May be specified multiple times.
-p, --parameter-set strings Name of a parameter set file for the bundle. May be either a named set of parameters or a filepath, and specified multiple times.
-r, --reference string Use a bundle in an OCI registry specified by the given reference.
Expand Down
1 change: 1 addition & 0 deletions docs/content/cli/bundles_upgrade.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ porter bundles upgrade [INSTALLATION] [flags]
--force Force a fresh pull of the bundle
-h, --help help for upgrade
--insecure-registry Don't require TLS for the registry
--no-logs Do not persist the bundle execution logs
--param strings Define an individual parameter in the form NAME=VALUE. Overrides parameters otherwise set via --parameter-set. May be specified multiple times.
-p, --parameter-set strings Name of a parameter set file for the bundle. May be either a named set of parameters or a filepath, and specified multiple times.
-r, --reference string Use a bundle in an OCI registry specified by the given reference.
Expand Down
1 change: 1 addition & 0 deletions docs/content/cli/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ porter install [INSTALLATION] [flags]
--force Force a fresh pull of the bundle
-h, --help help for install
--insecure-registry Don't require TLS for the registry
--no-logs Do not persist the bundle execution logs
--param strings Define an individual parameter in the form NAME=VALUE. Overrides parameters otherwise set via --parameter-set. May be specified multiple times.
-p, --parameter-set strings Name of a parameter set file for the bundle. May be either a named set of parameters or a filepath, and specified multiple times.
-r, --reference string Use a bundle in an OCI registry specified by the given reference.
Expand Down
1 change: 1 addition & 0 deletions docs/content/cli/invoke.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ porter invoke [INSTALLATION] --action ACTION [flags]
--force Force a fresh pull of the bundle
-h, --help help for invoke
--insecure-registry Don't require TLS for the registry
--no-logs Do not persist the bundle execution logs
--param strings Define an individual parameter in the form NAME=VALUE. Overrides parameters otherwise set via --parameter-set. May be specified multiple times.
-p, --parameter-set strings Name of a parameter set file for the bundle. May be either a named set of parameters or a filepath, and specified multiple times.
-r, --reference string Use a bundle in an OCI registry specified by the given reference.
Expand Down
1 change: 1 addition & 0 deletions docs/content/cli/uninstall.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ porter uninstall [INSTALLATION] [flags]
--force-delete UNSAFE. Delete all records associated with the installation, even if uninstall fails. This is intended for cleaning up test data and is not recommended for production environments.
-h, --help help for uninstall
--insecure-registry Don't require TLS for the registry
--no-logs Do not persist the bundle execution logs
--param strings Define an individual parameter in the form NAME=VALUE. Overrides parameters otherwise set via --parameter-set. May be specified multiple times.
-p, --parameter-set strings Name of a parameter set file for the bundle. May be either a named set of parameters or a filepath, and specified multiple times.
-r, --reference string Use a bundle in an OCI registry specified by the given reference.
Expand Down
1 change: 1 addition & 0 deletions docs/content/cli/upgrade.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ porter upgrade [INSTALLATION] [flags]
--force Force a fresh pull of the bundle
-h, --help help for upgrade
--insecure-registry Don't require TLS for the registry
--no-logs Do not persist the bundle execution logs
--param strings Define an individual parameter in the form NAME=VALUE. Overrides parameters otherwise set via --parameter-set. May be specified multiple times.
-p, --parameter-set strings Name of a parameter set file for the bundle. May be either a named set of parameters or a filepath, and specified multiple times.
-r, --reference string Use a bundle in an OCI registry specified by the given reference.
Expand Down
5 changes: 4 additions & 1 deletion pkg/cnab/provider/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ type ActionArguments struct {

// Give the bundle privileged access to the docker daemon.
AllowDockerHostAccess bool

// PersistLogs specifies if the invocation image output should be saved as an output.
PersistLogs bool
}

func (r *Runtime) ApplyConfig(args ActionArguments) action.OperationConfigs {
Expand Down Expand Up @@ -178,7 +181,7 @@ func (r *Runtime) Execute(args ActionArguments) error {

a := cnabaction.New(driver, r.claims)
a.SaveAllOutputs = true
a.SaveLogs = true
a.SaveLogs = args.PersistLogs

modifies, err := c.IsModifyingAction()
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/porter/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type BundleActionOptions struct {
sharedOptions
BundlePullOptions
AllowAccessToDockerHost bool
NoLogs bool
}

func (o *BundleActionOptions) Validate(args []string, porter *Porter) error {
Expand Down Expand Up @@ -68,6 +69,7 @@ func (p *Porter) BuildActionArgs(action BundleAction) (cnabprovider.ActionArgume
Driver: opts.Driver,
RelocationMapping: opts.RelocationMapping,
AllowDockerHostAccess: opts.AllowAccessToDockerHost,
PersistLogs: !opts.NoLogs,
}

err := opts.LoadParameters(p)
Expand Down
7 changes: 6 additions & 1 deletion tests/smoke/hello_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@ func TestHelloBundle(t *testing.T) {

test.RequirePorter("install", "--reference", ref)
test.RequirePorter("installation", "show", "mybuns")
test.RequirePorter("installation", "logs", "show", "-i=mybuns")

test.RequirePorter("upgrade", "mybuns")
test.RequirePorter("installation", "show", "mybuns")

test.RequirePorter("uninstall", "mybuns")
// Check that we can disable logs from persisting
test.RequirePorter("uninstall", "mybuns", "--no-logs")
test.RequirePorter("installation", "show", "mybuns")
_, _, err = test.RunPorter("installation", "logs", "show", "-i=mybuns")
require.Error(t, err, "expected log retrieval to fail")
require.Contains(t, err.Error(), "no logs found")

// Verify file permissions on PORTER_HOME
test.RequireFileMode(filepath.Join(test.PorterHomeDir, "claims", "*"), 0600)
Expand Down
40 changes: 31 additions & 9 deletions tests/smoke/main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package smoke

import (
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -45,12 +47,15 @@ func NewTest(t *testing.T) (Test, error) {
return *test, err
}

err = test.PorterE("help")
os.Setenv("PORTER_HOME", test.PorterHomeDir)
os.Setenv("PATH", test.PorterHomeDir+string(os.PathListSeparator)+os.Getenv("PATH"))

_, _, err = test.RunPorter("help")
if err != nil {
return *test, err
}

err = test.PorterE("version")
_, _, err = test.RunPorter("version")
if err != nil {
return *test, err
}
Expand All @@ -60,17 +65,34 @@ func NewTest(t *testing.T) (Test, error) {

// Run a porter command and fail the test if the command returns an error.
func (t Test) RequirePorter(args ...string) {
err := t.PorterE(args...)
_, _, err := t.RunPorter(args...)
require.NoError(t.T, err)
}

// Run a porter command, printing stderr when the command fails.
func (t Test) PorterE(args ...string) error {
// Run a porter command returning stderr when it fails
func (t Test) RunPorter(args ...string) (stdout string, combinedoutput string, err error) {
t.T.Helper()
// Copy stderr to stdout so we can return the "full" output printed to the console
stdoutBuf := &bytes.Buffer{}
stderrBuf := &bytes.Buffer{}
output := &bytes.Buffer{}
cmd := t.buildPorterCommand(args...)
t.T.Log(cmd.String())
ran, _, err := cmd.Stdout(io.MultiWriter(stdoutBuf, output)).Stderr(io.MultiWriter(stderrBuf, output)).Exec()
if err != nil {
if ran {
err = errors.New(stderrBuf.String())
}
return stdoutBuf.String(), output.String(), err
}
return stdoutBuf.String(), output.String(), nil
}

// Build a porter command, ready to be executed or further customized.
func (t Test) buildPorterCommand(args ...string) shx.PreparedCommand {
args = append(args, "--debug")
p := shx.Command(t.PorterPath, args...).Stdout(nil)
p.Cmd.Env = []string{"PORTER_HOME=" + t.PorterHomeDir}
err := p.Run()
return errors.Wrapf(err, "error running porter %s", args)
return shx.Command("porter", args...).
Env("PORTER_HOME=" + t.PorterHomeDir)
}

func (t Test) Teardown() {
Expand Down

0 comments on commit 43a2b2a

Please sign in to comment.