Skip to content

Commit

Permalink
Merge pull request intelops#212 from intelops/add-license-per-node
Browse files Browse the repository at this point in the history
Add license per node
  • Loading branch information
mahendraintelops authored Apr 21, 2024
2 parents 23f0f17 + 283b0a4 commit e32c008
Show file tree
Hide file tree
Showing 15 changed files with 246 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"mode": "debug",
"program": "${workspaceRoot}/main.go",
"cwd": "${workspaceRoot}",
"args": ["pullTemplates"]
"args": ["generate"]
}
]
}
23 changes: 23 additions & 0 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
"path/filepath"
)

// generateCmd represents the generate command
Expand Down Expand Up @@ -61,6 +62,28 @@ func GenerateCode() error {
return err
}

if len(coreProject.License.Path) > 0 {
// assign absolute path to the license file Path if it's not
absPath, err := filepath.Abs(coreProject.License.Path)
if err != nil {
log.Errorf("error while getting absolute path [" + err.Error() + "]")
return err
}
coreProject.License.Path = absPath
}

// assign absolute path to the license file path if it's not (if supplied for the nodes)
for _, node := range coreProject.CompageJSON.Nodes {
if len(node.License.Path) > 0 {
absPath, err := filepath.Abs(node.License.Path)
if err != nil {
log.Errorf("error while getting absolute path [" + err.Error() + "]")
return err
}
node.License.Path = absPath
}
}

// pull all required templates
// pull the common templates
err = ociregistry.PullOCIArtifact("common", project.Version)
Expand Down
7 changes: 7 additions & 0 deletions cmd/models/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,16 @@ type GitDetails struct {
Platform Platform `yaml:"platform,omitempty"`
}

type License struct {
Name string `yaml:"name,omitempty"`
URL string `yaml:"url,omitempty"`
Path string `yaml:"path,omitempty"`
}

type Project struct {
Name string `yaml:"name"`
Version string `yaml:"version"`
License License `yaml:"license"`
GitDetails GitDetails `yaml:"git"`
CompageJSON map[string]interface{} `yaml:"compageJSON"`
ProjectMetadata string `yaml:"projectMetadata"`
Expand Down
1 change: 1 addition & 0 deletions internal/converter/cmd/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func GetProject(input *models.Project) (*core.Project, error) {
CompageJSON: compageJSON,
Name: input.Name,
Version: input.Version,
License: &input.License,
GitPlatformName: input.GitDetails.Platform.Name,
GitPlatformURL: input.GitDetails.Platform.URL,
GitPlatformUserName: input.GitDetails.Platform.UserName,
Expand Down
2 changes: 2 additions & 0 deletions internal/core/models.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package core

import (
"github.com/intelops/compage/cmd/models"
coreedge "github.com/intelops/compage/internal/core/edge"
corenode "github.com/intelops/compage/internal/core/node"
"time"
Expand All @@ -19,6 +20,7 @@ type ModificationDetails struct {
type Project struct {
Name string `json:"name"`
Version string `json:"version"`
License *models.License `json:"license"`
CompageJSON *CompageJSON `json:"compageJSON"`
GitRepositoryName string `json:"gitRepositoryName"`
GitRepositoryURL string `json:"gitRepositoryURL"`
Expand Down
8 changes: 8 additions & 0 deletions internal/core/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ type Node struct {
Language string `json:"language"`
// Name of a component (required, this will be service and deployment name).
Name string `json:"name"`
// License of the component.
License *License `json:"license"`
// RestConfig holds all config related to REST. If nil, it means that the node is not REST server or has REST clients.
RestConfig *RestConfig `json:"restConfig,omitempty"`
// GrpcConfig holds all config related to gRPC. If nil, it means that the node is not gRPC server or has gRPC clients.
Expand All @@ -19,6 +21,12 @@ type Node struct {
Annotations map[string]string `json:"annotations,omitempty"`
}

type License struct {
Name string `yaml:"name,omitempty"`
URL string `yaml:"url,omitempty"`
Path string `yaml:"path,omitempty"`
}

type RestServer struct {
Port string `json:"port"`
SQLDB string `json:"sqlDB"`
Expand Down
2 changes: 0 additions & 2 deletions internal/integrations/deepsource/copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ type Copier struct {
}

func NewCopier(project *core.Project) (*Copier, error) {
// retrieve project named directory
//gitPlatformUserName, gitRepositoryName, projectDirectoryName, templatesRootPath string
// populate map to replace templates
data := map[string]interface{}{
"GitRepositoryName": project.GitRepositoryName,
Expand Down
22 changes: 19 additions & 3 deletions internal/integrations/license/copier.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package license

import (
"github.com/intelops/compage/cmd/models"
"github.com/intelops/compage/internal/core"
"github.com/intelops/compage/internal/languages/executor"
"github.com/intelops/compage/internal/utils"
Expand All @@ -14,12 +15,11 @@ type Copier struct {
ProjectDirectoryName string
GitRepositoryName string
TemplatesRootPath string
License *models.License
Data map[string]interface{}
}

func NewCopier(project *core.Project) (*Copier, error) {
// retrieve project named directory
//gitPlatformUserName, gitRepositoryName, projectDirectoryName, templatesRootPath string
// populate map to replace templates
data := map[string]interface{}{
"GitRepositoryName": project.GitRepositoryName,
Expand All @@ -37,6 +37,7 @@ func NewCopier(project *core.Project) (*Copier, error) {
ProjectDirectoryName: utils.GetProjectDirectoryName(project.Name),
GitRepositoryName: project.GitRepositoryName,
Data: data,
License: project.License,
}, nil
}

Expand All @@ -47,7 +48,22 @@ func (c Copier) CreateLicenseFiles() error {
log.Errorf("error while creating directories [" + err.Error() + "]")
return err
}

// copy license file if it's been supplied
if c.License != nil && len(c.License.URL) > 0 {
// read file from url in c.License.URL. This is applicable for both config.yaml file and ui flow.
return utils.DownloadFile(c.ProjectDirectoryName+"/LICENCE", c.License.URL)
} else if c.License != nil && len(c.License.Path) > 0 {
// local license file sent via config.yaml file.
// get the absolute path of the license file
_, err := utils.CopyFile(c.ProjectDirectoryName+"/LICENCE", c.License.Path)
if err != nil {
log.Errorf("error while copying file [" + err.Error() + "]")
return err
}
// return from here as the license file has been copied
return nil
}
// copy license file from templates (the default one)
var filePaths []*string
// copy deployment files to generated license config files
targetLicenseFileName := c.ProjectDirectoryName + "/" + File
Expand Down
2 changes: 0 additions & 2 deletions internal/integrations/readme/copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ type Copier struct {
}

func NewCopier(project *core.Project) (*Copier, error) {
// retrieve project named directory
//gitPlatformUserName, gitRepositoryName, projectDirectoryName, templatesRootPath string
// populate map to replace templates
data := map[string]interface{}{
"GitRepositoryName": project.GitRepositoryName,
Expand Down
13 changes: 13 additions & 0 deletions internal/languages/dotnet/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/intelops/compage/internal/languages/dotnet/integrations/docker"
"github.com/intelops/compage/internal/languages/dotnet/integrations/githubactions"
"github.com/intelops/compage/internal/languages/dotnet/integrations/kubernetes"
"github.com/intelops/compage/internal/languages/dotnet/integrations/license"
"github.com/intelops/compage/internal/languages/templates"
"github.com/intelops/compage/internal/utils"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -113,6 +114,14 @@ func generateIntegrationConfig(dotNetValues *DotNetValues) error {
log.Errorf("error while getting the integrations copier [" + err.Error() + "]")
return err
}

// license files need to be generated for the whole project so, it should be here.
licenseCopier := m["license"].(*license.Copier)
if err = licenseCopier.CreateLicenseFiles(); err != nil {
log.Errorf("err : %s", err)
return err
}

// dockerfile needs to be generated for the whole project, so it should be here.
dockerCopier := m["docker"].(*docker.Copier)
if err = dockerCopier.CreateDockerFile(); err != nil {
Expand Down Expand Up @@ -159,6 +168,9 @@ func getIntegrationsCopier(dotNetValues *DotNetValues) (map[string]interface{},
restServerPort = ""
}

// create dotnet specific licenseCopier
licenseCopier := license.NewCopier(gitPlatformUserName, gitRepositoryName, nodeName, nodeDirectoryName, dotNetTemplatesRootPath, dotNetValues.LDotNetLangNode.License)

// create dotnet specific dockerCopier
dockerCopier := docker.NewCopier(gitPlatformUserName, gitRepositoryName, nodeName, nodeDirectoryName, dotNetTemplatesRootPath, isRestServer, restServerPort)

Expand All @@ -172,5 +184,6 @@ func getIntegrationsCopier(dotNetValues *DotNetValues) (map[string]interface{},
"docker": dockerCopier,
"k8s": k8sCopier,
"githubActions": githubActionsCopier,
"license": licenseCopier,
}, nil
}
56 changes: 56 additions & 0 deletions internal/languages/dotnet/integrations/license/copier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package license

import (
corenode "github.com/intelops/compage/internal/core/node"
"github.com/intelops/compage/internal/utils"
log "github.com/sirupsen/logrus"
)

// Copier integrations specific copier
type Copier struct {
NodeName string
NodeDirectoryName string
TemplatesRootPath string
License *corenode.License
Data map[string]interface{}
}

func NewCopier(gitRepositoryName, gitPlatformUserName, nodeName, nodeDirectoryName, templatesRootPath string, license *corenode.License) *Copier {
// populate map to replace templates
data := map[string]interface{}{
"GitRepositoryName": gitRepositoryName,
"GitPlatformUserName": gitPlatformUserName,
}

return &Copier{
TemplatesRootPath: templatesRootPath,
NodeDirectoryName: nodeDirectoryName,
NodeName: nodeName,
Data: data,
License: license,
}
}

// CreateLicenseFiles creates the required directory and copies files from language template.
func (c Copier) CreateLicenseFiles() error {
destDirectory := c.NodeDirectoryName
if err := utils.CreateDirectories(destDirectory); err != nil {
log.Errorf("error while creating directories [" + err.Error() + "]")
return err
}
// copy license file if it's been supplied
if c.License != nil && len(c.License.URL) > 0 {
// read file from url in c.License.URL. This is applicable for both config.yaml file and ui flow.
return utils.DownloadFile(c.NodeDirectoryName+"/LICENCE", c.License.URL)
} else if c.License != nil && len(c.License.Path) > 0 {
// local license file sent via config.yaml file.
// get the absolute path of the license file
_, err := utils.CopyFile(c.NodeDirectoryName+"/LICENCE", c.License.Path)
if err != nil {
log.Errorf("error while copying file [" + err.Error() + "]")
return err
}
}
// return from here as the license file has been copied
return nil
}
13 changes: 13 additions & 0 deletions internal/languages/golang/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/intelops/compage/internal/languages/golang/integrations/docker"
"github.com/intelops/compage/internal/languages/golang/integrations/githubactions"
"github.com/intelops/compage/internal/languages/golang/integrations/kubernetes"
"github.com/intelops/compage/internal/languages/golang/integrations/license"
"github.com/intelops/compage/internal/languages/templates"
"github.com/intelops/compage/internal/utils"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -68,6 +69,14 @@ func generateIntegrationConfig(goValues *GoValues) error {
log.Errorf("error while getting the integrations copier [" + err.Error() + "]")
return err
}

// license files need to be generated for the whole project so, it should be here.
licenseCopier := m["license"].(*license.Copier)
if err = licenseCopier.CreateLicenseFiles(); err != nil {
log.Errorf("err : %s", err)
return err
}

// dockerfile needs to be generated for the whole project, so it should be here.
dockerCopier := m["docker"].(*docker.Copier)
if err = dockerCopier.CreateDockerFile(); err != nil {
Expand Down Expand Up @@ -389,6 +398,9 @@ func getIntegrationsCopier(goValues *GoValues) (map[string]interface{}, error) {
projectDirectoryName := utils.GetProjectDirectoryName(goValues.Values.ProjectName)
projectName := goValues.Values.ProjectName

// create dotnet specific licenseCopier
licenseCopier := license.NewCopier(gitPlatformUserName, gitRepositoryName, nodeName, nodeDirectoryName, goTemplatesRootPath, goValues.LGoLangNode.License)

// create golang specific dockerCopier
dockerCopier := docker.NewCopier(gitPlatformUserName, gitRepositoryName, nodeName, nodeDirectoryName, goTemplatesRootPath, isRestServer, restServerPort, isGrpcServer, grpcServerPort)

Expand All @@ -405,6 +417,7 @@ func getIntegrationsCopier(goValues *GoValues) (map[string]interface{}, error) {
devContainerCopier := devcontainer.NewCopier(gitPlatformUserName, gitRepositoryName, projectName, nodeName, nodeDirectoryName, goTemplatesRootPath, isRestServer, restServerPort, isGrpcServer, grpcServerPort)

return map[string]interface{}{
"license": licenseCopier,
"docker": dockerCopier,
"k8s": k8sCopier,
"githubActions": githubActionsCopier,
Expand Down
56 changes: 56 additions & 0 deletions internal/languages/golang/integrations/license/copier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package license

import (
corenode "github.com/intelops/compage/internal/core/node"
"github.com/intelops/compage/internal/utils"
log "github.com/sirupsen/logrus"
)

// Copier integrations specific copier
type Copier struct {
NodeName string
NodeDirectoryName string
TemplatesRootPath string
License *corenode.License
Data map[string]interface{}
}

func NewCopier(gitRepositoryName, gitPlatformUserName, nodeName, nodeDirectoryName, templatesRootPath string, license *corenode.License) *Copier {
// populate map to replace templates
data := map[string]interface{}{
"GitRepositoryName": gitRepositoryName,
"GitPlatformUserName": gitPlatformUserName,
}

return &Copier{
TemplatesRootPath: templatesRootPath,
NodeDirectoryName: nodeDirectoryName,
NodeName: nodeName,
Data: data,
License: license,
}
}

// CreateLicenseFiles creates the required directory and copies files from language template.
func (c Copier) CreateLicenseFiles() error {
destDirectory := c.NodeDirectoryName
if err := utils.CreateDirectories(destDirectory); err != nil {
log.Errorf("error while creating directories [" + err.Error() + "]")
return err
}
// copy license file if it's been supplied
if c.License != nil && len(c.License.URL) > 0 {
// read file from url in c.License.URL. This is applicable for both config.yaml file and ui flow.
return utils.DownloadFile(c.NodeDirectoryName+"/LICENCE", c.License.URL)
} else if c.License != nil && len(c.License.Path) > 0 {
// local license file sent via config.yaml file.
// get the absolute path of the license file
_, err := utils.CopyFile(c.NodeDirectoryName+"/LICENCE", c.License.Path)
if err != nil {
log.Errorf("error while copying file [" + err.Error() + "]")
return err
}
}
// return from here as the license file has been copied
return nil
}
9 changes: 5 additions & 4 deletions internal/languages/languages.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ type LanguageNode struct {
Metadata map[string]interface{} `json:"metadata,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
Language string `json:"language"`

RestConfig *corenode.RestConfig `json:"addRestConfig"`
GrpcConfig *corenode.GrpcConfig `json:"grpcConfig"`
WsConfig *corenode.WsConfig `json:"wsConfig"`
License *corenode.License `json:"license"`
RestConfig *corenode.RestConfig `json:"addRestConfig"`
GrpcConfig *corenode.GrpcConfig `json:"grpcConfig"`
WsConfig *corenode.WsConfig `json:"wsConfig"`
}

// NewLanguageNode converts node to LanguageNode struct
Expand All @@ -39,6 +39,7 @@ func NewLanguageNode(compageJSON *core.CompageJSON, node *corenode.Node) (*Langu
Metadata: node.Metadata,
Annotations: node.Annotations,
Language: node.Language,
License: node.License,
}

addRestConfig(node, languageNode)
Expand Down
Loading

0 comments on commit e32c008

Please sign in to comment.