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

🐛 Fix no-cleanup option, disable analysis logs to stdout #99

Merged
merged 1 commit into from
Oct 23, 2023
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
74 changes: 50 additions & 24 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"io"
"io/fs"
"io/ioutil"
"os"
"path"

Expand Down Expand Up @@ -62,12 +61,15 @@ type analyzeCommand struct {
log logr.Logger
// isFileInput is set when input points to a file and not a dir
isFileInput bool
logLevel *uint32
cleanup bool
}

// analyzeCmd represents the analyze command
func NewAnalyzeCmd(log logr.Logger) *cobra.Command {
analyzeCmd := &analyzeCommand{
log: log,
log: log,
cleanup: true,
}

analyzeCommand := &cobra.Command{
Expand All @@ -91,6 +93,12 @@ func NewAnalyzeCmd(log logr.Logger) *cobra.Command {
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if val, err := cmd.Flags().GetUint32(logLevelFlag); err == nil {
analyzeCmd.logLevel = &val
}
if val, err := cmd.Flags().GetBool(noCleanupFlag); err == nil {
analyzeCmd.cleanup = !val
}
if analyzeCmd.listSources || analyzeCmd.listTargets {
err := analyzeCmd.ListLabels(cmd.Context())
if err != nil {
Expand Down Expand Up @@ -122,9 +130,6 @@ func NewAnalyzeCmd(log logr.Logger) *cobra.Command {
return nil
},
PostRunE: func(cmd *cobra.Command, args []string) error {
if cmd.PersistentFlags().Changed(noCleanupFlag) {
return nil
}
err := analyzeCmd.Clean(cmd.Context())
if err != nil {
log.Error(err, "failed to clean temporary container resources")
Expand Down Expand Up @@ -249,6 +254,7 @@ func (a *analyzeCommand) ListLabels(ctx context.Context) error {
WithVolumes(volumes),
WithEntrypointBin("/usr/local/bin/kantra"),
WithEntrypointArgs(args...),
WithCleanup(a.cleanup),
)
if err != nil {
a.log.Error(err, "failed listing labels")
Expand Down Expand Up @@ -405,7 +411,7 @@ func (a *analyzeCommand) getConfigVolumes() (map[string]string, error) {
a.log.V(1).Error(err, "failed to marshal provider config")
return nil, err
}
err = ioutil.WriteFile(filepath.Join(tempDir, "settings.json"), jsonData, os.ModePerm)
err = os.WriteFile(filepath.Join(tempDir, "settings.json"), jsonData, os.ModePerm)
if err != nil {
a.log.V(1).Error(err, "failed to write provider config", "dir", tempDir, "file", "settings.json")
return nil, err
Expand Down Expand Up @@ -491,7 +497,7 @@ func createTempRuleSet(path string) error {
if err != nil {
return err
}
err = ioutil.WriteFile(path, yamlData, os.ModePerm)
err = os.WriteFile(path, yamlData, os.ModePerm)
if err != nil {
return err
}
Expand Down Expand Up @@ -539,7 +545,9 @@ func (a *analyzeCommand) RunAnalysis(ctx context.Context, xmlOutputDir string) e
args = append(args,
fmt.Sprintf("--dep-label-selector=(!%s=open-source)", provider.DepSourceLabel))
}

if a.logLevel != nil {
args = append(args, fmt.Sprintf("--verbose=%d", *a.logLevel))
}
labelSelector := a.getLabelSelector()
if labelSelector != "" {
args = append(args, fmt.Sprintf("--label-selector=%s", labelSelector))
Expand All @@ -561,31 +569,35 @@ func (a *analyzeCommand) RunAnalysis(ctx context.Context, xmlOutputDir string) e

a.log.Info("running source code analysis", "log", analysisLogFilePath,
"input", a.input, "output", a.output, "args", strings.Join(args, " "), "volumes", volumes)
a.log.Info("generating analysis log in file", "file", analysisLogFilePath)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

// TODO (pgaikwad): run analysis & deps in parallel
err = NewContainer(a.log).Run(
ctx,
WithVolumes(volumes),
WithStdout(os.Stdout, analysisLog),
WithStderr(os.Stdout, analysisLog),
WithStdout(analysisLog),
WithStderr(analysisLog),
WithEntrypointArgs(args...),
WithEntrypointBin("/usr/bin/konveyor-analyzer"),
WithCleanup(a.cleanup),
)
if err != nil {
return err
}

a.log.Info("running dependency analysis",
"log", depsLogFilePath, "input", a.input, "output", a.output, "args", strings.Join(args, " "))
a.log.Info("generating dependency log in file", "file", depsLogFilePath)
err = NewContainer(a.log).Run(
ctx,
WithStdout(os.Stdout, dependencyLog),
WithStderr(os.Stderr, dependencyLog),
WithStdout(dependencyLog),
WithStderr(dependencyLog),
WithVolumes(volumes),
WithEntrypointBin("/usr/bin/konveyor-analyzer-dep"),
WithEntrypointArgs(
fmt.Sprintf("--output-file=%s", DepsOutputMountPath),
fmt.Sprintf("--provider-settings=%s", ProviderSettingsMountPath),
),
WithCleanup(a.cleanup),
)
if err != nil {
return err
Expand All @@ -601,7 +613,7 @@ func (a *analyzeCommand) CreateJSONOutput() error {
outputPath := filepath.Join(a.output, "output.yaml")
depPath := filepath.Join(a.output, "dependencies.yaml")

data, err := ioutil.ReadFile(outputPath)
data, err := os.ReadFile(outputPath)
if err != nil {
return err
}
Expand All @@ -617,13 +629,13 @@ func (a *analyzeCommand) CreateJSONOutput() error {
a.log.V(1).Error(err, "failed to marshal output file to json")
return err
}
err = ioutil.WriteFile(filepath.Join(a.output, "output.json"), jsonData, os.ModePerm)
err = os.WriteFile(filepath.Join(a.output, "output.json"), jsonData, os.ModePerm)
if err != nil {
a.log.V(1).Error(err, "failed to write json output", "dir", a.output, "file", "output.json")
return err
}

depData, err := ioutil.ReadFile(depPath)
depData, err := os.ReadFile(depPath)
if err != nil {
return err
}
Expand All @@ -639,7 +651,7 @@ func (a *analyzeCommand) CreateJSONOutput() error {
a.log.V(1).Error(err, "failed to marshal dependencies file to json")
return err
}
err = ioutil.WriteFile(filepath.Join(a.output, "dependencies.json"), jsonDataDep, os.ModePerm)
err = os.WriteFile(filepath.Join(a.output, "dependencies.json"), jsonDataDep, os.ModePerm)
if err != nil {
a.log.V(1).Error(err, "failed to write json dependencies output", "dir", a.output, "file", "dependencies.json")
return err
Expand Down Expand Up @@ -681,6 +693,7 @@ func (a *analyzeCommand) GenerateStaticReport(ctx context.Context) error {
WithEntrypointArgs(staticReportCmd...),
WithVolumes(volumes),
WithcFlag(true),
WithCleanup(a.cleanup),
)
if err != nil {
return err
Expand All @@ -693,6 +706,9 @@ func (a *analyzeCommand) GenerateStaticReport(ctx context.Context) error {
}

func (a *analyzeCommand) Clean(ctx context.Context) error {
if !a.cleanup {
return nil
}
for _, path := range a.tempDirs {
err := os.RemoveAll(path)
if err != nil {
Expand Down Expand Up @@ -752,11 +768,7 @@ func (a *analyzeCommand) getLabelSelector() string {
}

func isXMLFile(rule string) bool {
extension := path.Ext(rule)
if extension == ".xml" {
return true
}
return false
return path.Ext(rule) == ".xml"
}

func (a *analyzeCommand) getXMLRulesVolumes(tempRuleDir string) (map[string]string, error) {
Expand Down Expand Up @@ -806,8 +818,10 @@ func (a *analyzeCommand) ConvertXML(ctx context.Context) (string, error) {
a.log.V(1).Error(err, "failed to create temp dir for rules")
return "", err
}
a.log.V(1).Info("created directory for converted XML rules", "dir", tempDir)
defer os.RemoveAll(tempDir)
a.log.V(1).Info("created directory for converted XML rules", "dir", tempOutputDir)
if a.cleanup {
defer os.RemoveAll(tempDir)
}
volumes := map[string]string{
tempOutputDir: ShimOutputPath,
}
Expand All @@ -819,16 +833,28 @@ func (a *analyzeCommand) ConvertXML(ctx context.Context) (string, error) {
}
maps.Copy(volumes, ruleVols)

a.log.Info("running windup shim", "output", a.output)
shimLogPath := filepath.Join(a.output, "shim.log")
shimLog, err := os.Create(shimLogPath)
if err != nil {
return "", fmt.Errorf("failed creating shim log file %s", shimLogPath)
}
defer shimLog.Close()

args := []string{"convert",
fmt.Sprintf("--outputdir=%v", ShimOutputPath),
XMLRulePath,
}
a.log.Info("running windup shim",
"output", a.output, "args", strings.Join(args, " "), "volumes", volumes)
a.log.Info("generating shim log in file", "file", shimLogPath)
err = NewContainer(a.log).Run(
ctx,
WithStdout(shimLog),
WithStderr(shimLog),
WithVolumes(volumes),
WithEntrypointArgs(args...),
WithEntrypointBin("/usr/local/bin/windup-shim"),
WithCleanup(a.cleanup),
)
if err != nil {
return "", err
Expand Down
8 changes: 7 additions & 1 deletion cmd/openrewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ type openRewriteCommand struct {
goal string
miscOpts string
log logr.Logger
cleanup bool
}

func NewOpenRewriteCommand(log logr.Logger) *cobra.Command {
openRewriteCmd := &openRewriteCommand{
log: log,
log: log,
cleanup: true,
}

openRewriteCommand := &cobra.Command{
Expand All @@ -36,6 +38,9 @@ func NewOpenRewriteCommand(log logr.Logger) *cobra.Command {
}
},
RunE: func(cmd *cobra.Command, args []string) error {
if val, err := cmd.Flags().GetBool(noCleanupFlag); err == nil {
openRewriteCmd.cleanup = !val
}
err := openRewriteCmd.Validate()
if err != nil {
log.Error(err, "failed validating input args")
Expand Down Expand Up @@ -144,6 +149,7 @@ func (o *openRewriteCommand) Run(ctx context.Context) error {
WithEntrypointBin("/usr/bin/mvn"),
WithVolumes(volumes),
WithWorkDir(InputPath),
WithCleanup(o.cleanup),
)
if err != nil {
o.log.V(1).Error(err, "error running openrewrite")
Expand Down
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

const (
noCleanupFlag = "no-cleanup"
logLevelFlag = "log-level"
)

var logLevel uint32
Expand All @@ -36,7 +37,7 @@ var rootCmd = &cobra.Command{
}

func init() {
rootCmd.PersistentFlags().Uint32Var(&logLevel, "log-level", 4, "log level")
rootCmd.PersistentFlags().Uint32Var(&logLevel, logLevelFlag, 4, "log level")
rootCmd.PersistentFlags().BoolVar(&noCleanup, noCleanupFlag, false, "do not cleanup temporary resources")

logrusLog = logrus.New()
Expand Down
25 changes: 22 additions & 3 deletions cmd/shimconvert.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ type windupShimCommand struct {
input []string
output string

log logr.Logger
log logr.Logger
cleanup bool
}

func NewWindupShimCommand(log logr.Logger) *cobra.Command {
windupShimCmd := &windupShimCommand{
log: log,
log: log,
cleanup: true,
}

windupShimCommand := &cobra.Command{
Expand All @@ -45,6 +47,9 @@ func NewWindupShimCommand(log logr.Logger) *cobra.Command {
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if val, err := cmd.Flags().GetBool(noCleanupFlag); err == nil {
windupShimCmd.cleanup = !val
}
err := windupShimCmd.Run(cmd.Context())
if err != nil {
log.Error(err, "failed to execute windup shim")
Expand Down Expand Up @@ -129,7 +134,10 @@ func (w *windupShimCommand) Run(ctx context.Context) error {
w.log.V(1).Error(err, "failed to create temp dir for rules")
return err
}
defer os.RemoveAll(tempDir)
w.log.V(1).Info("created temp directory for XML rules", "dir", tempDir)
if w.cleanup {
defer os.RemoveAll(tempDir)
}
volumes := map[string]string{
w.output: ShimOutputPath,
}
Expand All @@ -140,17 +148,28 @@ func (w *windupShimCommand) Run(ctx context.Context) error {
}
maps.Copy(volumes, ruleVols)

shimLogPath := filepath.Join(w.output, "shim.log")
shimLog, err := os.Create(shimLogPath)
if err != nil {
return fmt.Errorf("failed creating shim log file %s", shimLogPath)
}
defer shimLog.Close()

args := []string{"convert",
fmt.Sprintf("--outputdir=%v", ShimOutputPath),
XMLRulePath,
}
w.log.Info("running windup-shim convert command",
"args", strings.Join(args, " "), "volumes", volumes, "output", w.output, "inputs", strings.Join(w.input, ","))
w.log.Info("generating shim log in file", "file", shimLogPath)
err = NewContainer(w.log).Run(
ctx,
WithVolumes(volumes),
WithStdout(shimLog),
WithStderr(shimLog),
WithEntrypointArgs(args...),
WithEntrypointBin("/usr/local/bin/windup-shim"),
WithCleanup(w.cleanup),
)
if err != nil {
w.log.V(1).Error(err, "failed to run convert command")
Expand Down