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

Convert xml rules to yaml for analyze #38

Merged
merged 1 commit into from
Aug 31, 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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ RUN CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -a -o windows-kantra main.g

FROM quay.io/konveyor/analyzer-lsp:latest

RUN mkdir /opt/rulesets /opt/rulesets/input /opt/openrewrite /opt/input /opt/output /opt/xmlrules /opt/shimoutput
RUN mkdir /opt/rulesets /opt/rulesets/input /opt/rulesets/convert /opt/openrewrite /opt/input /opt/output /opt/xmlrules /opt/shimoutput

COPY --from=builder /workspace/kantra /usr/local/bin/kantra
COPY --from=builder /workspace/darwin-kantra /usr/local/bin/darwin-kantra
Expand Down
107 changes: 105 additions & 2 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io/fs"
"io/ioutil"
"os"
"path"

"path/filepath"
"sort"
Expand Down Expand Up @@ -94,7 +95,12 @@ func NewAnalyzeCmd(log logr.Logger) *cobra.Command {
}
return nil
}
err := analyzeCmd.RunAnalysis(cmd.Context())
xmlOutputDir, err := analyzeCmd.ConvertXML(cmd.Context())
if err != nil {
log.V(5).Error(err, "failed to convert XML rules")
return err
}
err = analyzeCmd.RunAnalysis(cmd.Context(), xmlOutputDir)
if err != nil {
log.V(5).Error(err, "failed to execute analysis")
return err
Expand Down Expand Up @@ -389,13 +395,18 @@ func (a *analyzeCommand) getRulesVolumes() (map[string]string, error) {
}
// move rules files passed into dir to mount
if !stat.IsDir() {
// XML rules are handled outside of this func
if isXMLFile(r) {
continue
}
rulesetNeeded = true
destFile := filepath.Join(tempDir, fmt.Sprintf("rules%d.yaml", i))
err := copyFileContents(r, destFile)
if err != nil {
log.Errorf("failed to move rules file from %s to %s", r, destFile)
return nil, err
}

} else {
rulesVolumes[r] = filepath.Join(RulesMountPath, filepath.Base(r))
}
Expand Down Expand Up @@ -445,14 +456,21 @@ func createTempRuleSet(path string) error {
return nil
}

func (a *analyzeCommand) RunAnalysis(ctx context.Context) error {
func (a *analyzeCommand) RunAnalysis(ctx context.Context, xmlOutputDir string) error {
volumes := map[string]string{
// application source code
a.input: SourceMountPath,
// output directory
a.output: OutputPath,
}

if xmlOutputDir != "" {
convertPath := filepath.Join(RulesetPath, "convert")
volumes[xmlOutputDir] = convertPath
// for cleanup purposes
a.tempDirs = append(a.tempDirs, xmlOutputDir)
}

configVols, err := a.getConfigVolumes()
if err != nil {
a.log.V(5).Error(err, "failed to get config volumes for analysis")
Expand Down Expand Up @@ -632,3 +650,88 @@ func (a *analyzeCommand) getLabelSelector() string {
}
return ""
}

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

func (a *analyzeCommand) getXMLRulesVolumes(tempRuleDir string) (map[string]string, error) {
rulesVolumes := make(map[string]string)
mountTempDir := false
for _, r := range a.rules {
stat, err := os.Stat(r)
if err != nil {
a.log.V(5).Error(err, "failed to stat rules")
return nil, err
}
// move xml rule files from user into dir to mount
if !stat.IsDir() {
if !isXMLFile(r) {
continue
}
mountTempDir = true
xmlFileName := filepath.Base(r)
destFile := filepath.Join(tempRuleDir, xmlFileName)
err := copyFileContents(r, destFile)
if err != nil {
a.log.Error(err, "failed to move rules file from source to destination", "src", r, "dest", destFile)
return nil, err
}
} else {
rulesVolumes[r] = filepath.Join(XMLRulePath, filepath.Base(r))
}
}
if mountTempDir {
rulesVolumes[tempRuleDir] = XMLRulePath
}
return rulesVolumes, nil
}

func (a *analyzeCommand) ConvertXML(ctx context.Context) (string, error) {
if a.rules == nil || len(a.rules) == 0 {
return "", nil
}
tempDir, err := os.MkdirTemp("", "transform-rules-")
Copy link
Contributor

Choose a reason for hiding this comment

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

can we check if a.rules is empty / nil here and not do anything if it is?

if err != nil {
a.log.V(5).Error(err, "failed to create temp dir for rules")
return "", err
}
a.log.V(5).Info("created directory for XML rules", "dir", tempDir)
tempOutputDir, err := os.MkdirTemp("", "transform-output-")
if err != nil {
a.log.V(5).Error(err, "failed to create temp dir for rules")
return "", err
}
a.log.V(5).Info("created directory for converted XML rules", "dir", tempDir)
defer os.RemoveAll(tempDir)
volumes := map[string]string{
tempOutputDir: ShimOutputPath,
}

ruleVols, err := a.getXMLRulesVolumes(tempDir)
if err != nil {
a.log.V(5).Error(err, "failed to get XML rule volumes for analysis")
return "", err
}
maps.Copy(volumes, ruleVols)

args := []string{"convert",
fmt.Sprintf("--outputdir=%v", ShimOutputPath),
XMLRulePath,
}
err = NewContainer().Run(
ctx,
WithVolumes(volumes),
WithEntrypointArgs(args...),
WithEntrypointBin("/usr/local/bin/windup-shim"),
)
if err != nil {
return "", err
}

return tempOutputDir, nil
}