Skip to content

Commit

Permalink
containerless support for java
Browse files Browse the repository at this point in the history
Signed-off-by: Emily McMullan <[email protected]>
  • Loading branch information
eemcmullan committed Sep 24, 2024
1 parent dcbb845 commit 84bd9e1
Show file tree
Hide file tree
Showing 18 changed files with 4,268 additions and 113 deletions.
841 changes: 841 additions & 0 deletions cmd/analyze-bin.go

Large diffs are not rendered by default.

99 changes: 14 additions & 85 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"bufio"
"bytes"
"context"
"encoding/json"
Expand All @@ -17,7 +16,6 @@ import (

"path/filepath"
"slices"
"sort"
"strings"

"github.com/devfile/alizer/pkg/apis/model"
Expand Down Expand Up @@ -70,6 +68,17 @@ var (
}
)

// analyzer container paths
const (
RulesetPath = "/opt/rulesets"
OpenRewriteRecipesPath = "/opt/openrewrite"
InputPath = "/opt/input"
OutputPath = "/opt/output"
XMLRulePath = "/opt/xmlrules"
ShimOutputPath = "/opt/shimoutput"
CustomRulePath = "/opt/input/rules"
)

// supported providers
const (
javaProvider = "java"
Expand All @@ -88,15 +97,6 @@ const (
ClassFile = ".class"
)

// provider config options
const (
mavenSettingsFile = "mavenSettingsFile"
lspServerPath = "lspServerPath"
lspServerName = "lspServerName"
workspaceFolders = "workspaceFolders"
dependencyProviderPath = "dependencyProviderPath"
)

// TODO add network and volume w/ interface
type ProviderInit struct {
port int
Expand Down Expand Up @@ -664,7 +664,7 @@ func (a *analyzeCommand) fetchLabels(ctx context.Context, listSources, listTarge
runModeContainer := "container"
if os.Getenv(runMode) == runModeContainer {
if listSources {
sourceSlice, err := readRuleFilesForLabels(sourceLabel)
sourceSlice, err := a.readRuleFilesForLabels(sourceLabel)
if err != nil {
a.log.Error(err, "failed to read rule labels")
return err
Expand All @@ -673,7 +673,7 @@ func (a *analyzeCommand) fetchLabels(ctx context.Context, listSources, listTarge
return nil
}
if listTargets {
targetsSlice, err := readRuleFilesForLabels(targetLabel)
targetsSlice, err := a.readRuleFilesForLabels(targetLabel)
if err != nil {
a.log.Error(err, "failed to read rule labels")
return err
Expand Down Expand Up @@ -713,7 +713,7 @@ func (a *analyzeCommand) fetchLabels(ctx context.Context, listSources, listTarge
return nil
}

func readRuleFilesForLabels(label string) ([]string, error) {
func (a *analyzeCommand) readRuleFilesForLabels(label string) ([]string, error) {
labelsSlice := []string{}
err := filepath.WalkDir(RulesetPath, walkRuleSets(RulesetPath, label, &labelsSlice))
if err != nil {
Expand All @@ -722,77 +722,6 @@ func readRuleFilesForLabels(label string) ([]string, error) {
return labelsSlice, nil
}

func walkRuleSets(root string, label string, labelsSlice *[]string) fs.WalkDirFunc {
return func(path string, d fs.DirEntry, err error) error {
if !d.IsDir() {
*labelsSlice, err = readRuleFile(path, labelsSlice, label)
if err != nil {
return err
}
}
return err
}
}

func readRuleFile(filePath string, labelsSlice *[]string, label string) ([]string, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()

scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanWords)

for scanner.Scan() {
// add source/target labels to slice
label := getSourceOrTargetLabel(scanner.Text(), label)
if len(label) > 0 && !slices.Contains(*labelsSlice, label) {
*labelsSlice = append(*labelsSlice, label)
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return *labelsSlice, nil
}

func getSourceOrTargetLabel(text string, label string) string {
if strings.Contains(text, label) {
return text
}
return ""
}

func listOptionsFromLabels(sl []string, label string, out io.Writer) {
var newSl []string
l := label + "="

for _, label := range sl {
newSt := strings.TrimPrefix(label, l)

if newSt != label {
newSt = strings.TrimSuffix(newSt, "+")
newSt = strings.TrimSuffix(newSt, "-")

if !slices.Contains(newSl, newSt) {
newSl = append(newSl, newSt)

}
}
}
sort.Strings(newSl)

if label == outputv1.SourceTechnologyLabel {
fmt.Fprintln(out, "available source technologies:")
} else {
fmt.Fprintln(out, "available target technologies:")
}
for _, tech := range newSl {
fmt.Fprintln(out, tech)
}
}

func (a *analyzeCommand) getDepsFolders() (map[string]string, []string) {
vols := map[string]string{}
dependencyFolders := []string{}
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func init() {
rootCmd.AddCommand(NewAnalyzeCmd(logger))
rootCmd.AddCommand(NewTestCommand(logger))
rootCmd.AddCommand(NewVersionCommand())
rootCmd.AddCommand(NewAnalyzeBinCmd(logger))
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand Down
10 changes: 0 additions & 10 deletions cmd/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@ import (

var Settings = &Config{}

const (
RulesetPath = "/opt/rulesets"
OpenRewriteRecipesPath = "/opt/openrewrite"
InputPath = "/opt/input"
OutputPath = "/opt/output"
XMLRulePath = "/opt/xmlrules"
ShimOutputPath = "/opt/shimoutput"
CustomRulePath = "/opt/input/rules"
)

type Config struct {
RootCommandName string `env:"CMD_NAME" default:"kantra"`
ContainerBinary string `env:"CONTAINER_TOOL" default:"/usr/bin/podman"`
Expand Down
104 changes: 104 additions & 0 deletions cmd/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package cmd

import (
"bufio"
"fmt"
"io"
"io/fs"
"os"
"slices"
"sort"
"strings"

outputv1 "github.com/konveyor/analyzer-lsp/output/v1/konveyor"
)

var (
RootCommandName = "kantra"
JavaBundlesLocation = "/jdtls/java-analyzer-bundle/java-analyzer-bundle.core/target/java-analyzer-bundle.core-1.0.0-SNAPSHOT.jar"
JDTLSBinLocation = "/jdtls/bin/jdtls"
RulesetsLocation = "rulesets/default/generated"
JavaProviderImage = "quay.io/konveyor/java-external-provider"
GenericProviderImage = "quay.io/konveyor/generic-external-provider"
DotnetProviderImage = "quay.io/konveyor/dotnet-external-provider"
)

// provider config options
const (
mavenSettingsFile = "mavenSettingsFile"
lspServerPath = "lspServerPath"
lspServerName = "lspServerName"
workspaceFolders = "workspaceFolders"
dependencyProviderPath = "dependencyProviderPath"
)

func walkRuleSets(root string, label string, labelsSlice *[]string) fs.WalkDirFunc {
return func(path string, d fs.DirEntry, err error) error {
if !d.IsDir() {
*labelsSlice, err = readRuleFile(path, labelsSlice, label)
if err != nil {
return err
}
}
return err
}
}

func readRuleFile(filePath string, labelsSlice *[]string, label string) ([]string, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()

scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanWords)

for scanner.Scan() {
// add source/target labels to slice
label := getSourceOrTargetLabel(scanner.Text(), label)
if len(label) > 0 && !slices.Contains(*labelsSlice, label) {
*labelsSlice = append(*labelsSlice, label)
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return *labelsSlice, nil
}

func getSourceOrTargetLabel(text string, label string) string {
if strings.Contains(text, label) {
return text
}
return ""
}

func listOptionsFromLabels(sl []string, label string, out io.Writer) {
var newSl []string
l := label + "="

for _, label := range sl {
newSt := strings.TrimPrefix(label, l)

if newSt != label {
newSt = strings.TrimSuffix(newSt, "+")
newSt = strings.TrimSuffix(newSt, "-")

if !slices.Contains(newSl, newSt) {
newSl = append(newSl, newSt)

}
}
}
sort.Strings(newSl)

if label == outputv1.SourceTechnologyLabel {
fmt.Fprintln(out, "available source technologies:")
} else {
fmt.Fprintln(out, "available target technologies:")
}
for _, tech := range newSl {
fmt.Fprintln(out, tech)
}
}
11 changes: 3 additions & 8 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@ import (
)

var (
BuildCommit = ""
Version = "latest"
RunnerImage = "quay.io/konveyor/kantra"
RootCommandName = "kantra"
JavaBundlesLocation = "/jdtls/java-analyzer-bundle/java-analyzer-bundle.core/target/java-analyzer-bundle.core-1.0.0-SNAPSHOT.jar"
JavaProviderImage = "quay.io/konveyor/java-external-provider"
GenericProviderImage = "quay.io/konveyor/generic-external-provider"
DotnetProviderImage = "quay.io/konveyor/dotnet-external-provider"
BuildCommit = ""
Version = "latest"
RunnerImage = "quay.io/konveyor/kantra"
)

// Use build flags to set correct Version and BuildCommit
Expand Down
29 changes: 29 additions & 0 deletions docs/containerless.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## Test and Run Containerless Kantra

### Clone the required packages:

```sh
git clone https://github.com/eemcmullan/containerless-kantra-deps.git
```

## Move them to where kantra will look for packged binaries:

```sh
mv $HOME/containerless-kantra-deps $HOME/.kantra
```

## Build the static report output parser:

```sh
cd $HOME/.kantra/static-report/analyzer-output-parser
```

```sh
go build -o analyzer-output-parser main.go
```

### From kantra, run:

```sh
go run main.go analyze-bin --input <java-app> --output <output-dir> --rules <java-rules>
```
Loading

0 comments on commit 84bd9e1

Please sign in to comment.