Skip to content

Commit

Permalink
feat: add shell type for APP instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
kakzhou719 committed Nov 22, 2022
1 parent b3aec1e commit 7ff3358
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 25 deletions.
19 changes: 3 additions & 16 deletions build/kubefile/parser/app_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ import (
"path/filepath"
"strings"

"github.com/sealerio/sealer/pkg/define/application"

"github.com/sealerio/sealer/build/kubefile/command"

"github.com/pkg/errors"

"github.com/sealerio/sealer/build/kubefile/command"
v1 "github.com/sealerio/sealer/pkg/define/application/v1"
)

Expand Down Expand Up @@ -99,24 +95,15 @@ func (kp *KubefileParser) processApp(node *Node, result *KubefileResult) error {
result.Dockerfile = mergeLines(result.Dockerfile, tmpLine)
result.legacyContext.apps2Files[appName] = append([]string{}, filesToCopy...)

isH, err := isHelm(filesToCopy...)
appType, launchFiles, err := getApplicationType(filesToCopy)
if err != nil {
return fmt.Errorf("error in judging the application type: %v", err)
}

appType := ""
switch isH {
case true:
appType = application.HelmApp
case false:
appType = application.KubeApp
default:
return errors.Errorf("error in identifying the type of app:%s", appName)
}

v1App := v1.NewV1Application(
appName,
appType,
launchFiles,
).(*v1.Application)

result.Applications[v1App.Name()] = v1App
Expand Down
2 changes: 2 additions & 0 deletions build/kubefile/parser/image_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ var testExtensionWithApp = v1.ImageExtension{
v12.NewV1Application(
"es",
"helm",
[]string{},
),
v12.NewV1Application(
"ts",
"kube",
[]string{},
),
},
}
Expand Down
6 changes: 6 additions & 0 deletions build/kubefile/parser/kubefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ func (kp *KubefileParser) processLaunch(node *Node, result *KubefileResult) erro
return fmt.Sprintf("kubectl apply -f %s", path), nil
case application.HelmApp:
return fmt.Sprintf("helm install %s %s", v1app.Name(), path), nil
case application.ShellApp:
var cmds []string
for _, file := range v1app.LaunchFiles() {
cmds = append(cmds, fmt.Sprintf("bash %s", filepath.Join(path, file)))
}
return strings.Join(cmds, " && "), nil
default:
return "", errors.Errorf("unexpected application type %s", v1app.Type())
}
Expand Down
2 changes: 2 additions & 0 deletions build/kubefile/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ copy %s %s
app1Name: v1.NewV1Application(
app1Name,
application.KubeApp,
[]string{},
),
},
}
Expand Down Expand Up @@ -154,6 +155,7 @@ copy %s %s
app1Name: v1.NewV1Application(
app1Name,
application.HelmApp,
[]string{},
),
},
}
Expand Down
126 changes: 126 additions & 0 deletions build/kubefile/parser/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"path/filepath"
"strings"

"github.com/sealerio/sealer/pkg/define/application"
osi "github.com/sealerio/sealer/utils/os"
)

Expand Down Expand Up @@ -119,3 +120,128 @@ func isHelm(sources ...string) (bool, error) {

return chartInTargetsRoot == 7, nil
}

// isYaml sources slice only has one element
func isYaml(sources ...string) (bool, error) {
isYamlType := func(fileName string) bool {
ext := strings.ToLower(filepath.Ext(fileName))
if ext == ".yaml" || ext == ".yml" {
return true
}
return false
}

for _, source := range sources {
s, err := os.Stat(source)
if err != nil {
return false, fmt.Errorf("failed to stat %s: %v", source, err)
}

if s.IsDir() {
isAllYamlFiles := true
err = filepath.Walk(source, func(path string, f fs.FileInfo, err error) error {
if err != nil {
return err
}
if f.IsDir() {
return nil
}
// make sure all files under source dir is yaml type.
if !isYamlType(f.Name()) {
isAllYamlFiles = false
return filepath.SkipDir
}
return nil
})
if err != nil {
return false, fmt.Errorf("failed to walk yaml dir %s: %v", source, err)
}

if isAllYamlFiles {
return true, nil
}
return false, nil
}
if isYamlType(source) {
return true, nil
}
}

return false, nil
}

// isShell sources slice only has one element
func isShell(sources ...string) (bool, []string, error) {
var launchFiles []string
isShellType := func(fileName string) bool {
ext := strings.ToLower(filepath.Ext(fileName))
return ext == ".sh"
}

for _, source := range sources {
s, err := os.Stat(source)
if err != nil {
return false, nil, fmt.Errorf("failed to stat %s: %v", source, err)
}
if s.IsDir() {
err = filepath.Walk(source, func(path string, f fs.FileInfo, err error) error {
if err != nil {
return err
}
if f.IsDir() {
return nil
}
// todo optimize: use more accurate methods to determine file types.
if !isShellType(f.Name()) {
return filepath.SkipDir
}

launchFiles = append(launchFiles, path)
return nil
})

if err != nil {
return false, nil, fmt.Errorf("failed to walk shell dir %s: %v", source, err)
}

if len(launchFiles) > 0 {
return true, launchFiles, nil
}
return false, nil, nil
}
if isShellType(source) {
return true, []string{source}, nil
}
}

return false, nil, nil
}

func getApplicationType(sources []string) (string, []string, error) {
isy, yamlErr := isYaml(sources...)
if isy {
return application.KubeApp, nil, nil
}

iss, files, shellErr := isShell(sources...)
if iss {
return application.ShellApp, files, nil
}

ish, helmErr := isHelm(sources...)
if ish {
return application.HelmApp, nil, nil
}

if yamlErr != nil {
return "", nil, yamlErr
}
if shellErr != nil {
return "", nil, shellErr
}
if helmErr != nil {
return "", nil, helmErr
}

return "", nil, fmt.Errorf("unsupported application type in %s,%s,%s", application.KubeApp, application.HelmApp, application.ShellApp)
}
5 changes: 3 additions & 2 deletions pkg/define/application/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package application

const (
KubeApp string = "kube"
HelmApp string = "helm"
KubeApp string = "kube"
HelmApp string = "helm"
ShellApp string = "shell"
)
20 changes: 13 additions & 7 deletions pkg/define/application/v1/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import (
)

type Application struct {
NameVar string `json:"name"`
TypeVar string `json:"type,omitempty"`
VersionVar string `json:"version,omitempty"`
NameVar string `json:"name"`
TypeVar string `json:"type,omitempty"`
LaunchFilesVar []string `json:"launchfiles,omitempty"`
VersionVar string `json:"version,omitempty"`
}

func (app *Application) Version() string {
Expand All @@ -36,12 +37,17 @@ func (app *Application) Type() string {
return app.TypeVar
}

func (app *Application) LaunchFiles() []string {
return app.LaunchFilesVar
}

func NewV1Application(
name string,
appType string) version.VersionedApplication {
appType string, launchFiles []string) version.VersionedApplication {
return &Application{
NameVar: name,
TypeVar: appType,
VersionVar: "v1",
NameVar: name,
TypeVar: appType,
LaunchFilesVar: launchFiles,
VersionVar: "v1",
}
}
2 changes: 2 additions & 0 deletions pkg/define/application/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ type VersionedApplication interface {
Name() string

Type() string

LaunchFiles() []string
}

0 comments on commit 7ff3358

Please sign in to comment.