github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/define/application/v1/application.go (about) 1 // Copyright © 2022 Alibaba Group Holding Ltd. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package v1 16 17 import ( 18 "fmt" 19 "path/filepath" 20 "strings" 21 22 "github.com/sealerio/sealer/pkg/define/application" 23 "github.com/sealerio/sealer/pkg/define/application/version" 24 ) 25 26 type Application struct { 27 NameVar string `json:"name"` 28 TypeVar string `json:"type,omitempty"` 29 FilesVar []string `json:"files,omitempty"` 30 VersionVar string `json:"version,omitempty"` 31 32 // AppEnv is a set of key value pair. 33 // it is app level, only this app will be aware of its existence, 34 // it is used to render app files, or as an environment variable for app startup and deletion commands 35 AppEnv map[string]string `json:"env,omitempty"` 36 37 // AppCMDs defined from `appcmds` instruction 38 AppCMDs []string `json:"cmds,omitempty"` 39 } 40 41 func (app *Application) SetEnv(appEnv map[string]string) { 42 app.AppEnv = appEnv 43 } 44 45 func (app *Application) SetCmds(appCmds []string) { 46 app.AppCMDs = appCmds 47 } 48 49 func (app *Application) Version() string { 50 return app.VersionVar 51 } 52 53 func (app *Application) Name() string { 54 return app.NameVar 55 } 56 57 func (app *Application) Type() string { 58 return app.TypeVar 59 } 60 61 func (app *Application) Files() []string { 62 return app.FilesVar 63 } 64 65 // GetAppLaunchCmd : Get the real app launch cmds values in the following order. 66 // 1. appcmds instructionx defined in kubefile. 67 // 2. generated default command based on app type 68 func GetAppLaunchCmd(appRoot string, app *Application) string { 69 if len(app.AppCMDs) != 0 { 70 var cmds []string 71 cmds = append(cmds, []string{"cd", appRoot}...) 72 cmds = append(cmds, "&&") 73 cmds = append(cmds, app.AppCMDs...) 74 return strings.Join(cmds, " ") 75 } 76 switch app.Type() { 77 case application.KubeApp: 78 var cmds []string 79 for _, file := range app.FilesVar { 80 cmds = append(cmds, fmt.Sprintf("kubectl apply -f %s", filepath.Join(appRoot, file))) 81 } 82 return strings.Join(cmds, " && ") 83 case application.HelmApp: 84 return fmt.Sprintf("helm install %s %s", app.Name(), appRoot) 85 case application.ShellApp: 86 var cmds []string 87 for _, file := range app.FilesVar { 88 cmds = append(cmds, fmt.Sprintf("bash %s", filepath.Join(appRoot, file))) 89 } 90 return strings.Join(cmds, " && ") 91 default: 92 return "" 93 } 94 } 95 96 func NewV1Application( 97 name string, 98 appType string, files []string) version.VersionedApplication { 99 return &Application{ 100 NameVar: name, 101 TypeVar: appType, 102 FilesVar: files, 103 VersionVar: "v1", 104 } 105 }