[REV] dotnet support customize runtime version and build commands

This commit is contained in:
barnett 2018-12-06 13:41:43 +08:00
parent 0400bfef40
commit d841ec7332
8 changed files with 86 additions and 33 deletions

View File

@ -25,6 +25,8 @@ import (
"os" "os"
"path" "path"
"github.com/goodrain/rainbond/util"
"github.com/docker/engine-api/client" "github.com/docker/engine-api/client"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
@ -75,7 +77,7 @@ func (d *netcoreBuild) Build(re *Request) (*Response, error) {
d.serviceID = re.ServiceID d.serviceID = re.ServiceID
defer d.clear() defer d.clear()
//write default Dockerfile for build //write default Dockerfile for build
if err := d.writeBuildDockerfile(re.SourceDir); err != nil { if err := d.writeBuildDockerfile(re.SourceDir, re.BuildEnvs); err != nil {
return nil, fmt.Errorf("write default build dockerfile error:%s", err.Error()) return nil, fmt.Errorf("write default build dockerfile error:%s", err.Error())
} }
d.sourceDir = re.SourceDir d.sourceDir = re.SourceDir
@ -114,7 +116,7 @@ func (d *netcoreBuild) Build(re *Request) (*Response, error) {
return nil, err return nil, err
} }
//write default runtime dockerfile //write default runtime dockerfile
if err := d.writeRunDockerfile(d.buildCacheDir); err != nil { if err := d.writeRunDockerfile(d.buildCacheDir, re.BuildEnvs); err != nil {
return nil, fmt.Errorf("write default runtime dockerfile error:%s", err.Error()) return nil, fmt.Errorf("write default runtime dockerfile error:%s", err.Error())
} }
//build runtime image //build runtime image
@ -150,12 +152,14 @@ func (d *netcoreBuild) Build(re *Request) (*Response, error) {
re.Logger.Info("镜像推送镜像至仓库成功", map[string]string{"step": "builder-exector"}) re.Logger.Info("镜像推送镜像至仓库成功", map[string]string{"step": "builder-exector"})
return d.createResponse(), nil return d.createResponse(), nil
} }
func (d *netcoreBuild) writeBuildDockerfile(sourceDir string) error { func (d *netcoreBuild) writeBuildDockerfile(sourceDir string, envs map[string]string) error {
return ioutil.WriteFile(path.Join(sourceDir, "Dockerfile"), buildDockerfile, 0755) result := util.ParseVariable(string(buildDockerfile), envs)
return ioutil.WriteFile(path.Join(sourceDir, "Dockerfile"), []byte(result), 0755)
} }
func (d *netcoreBuild) writeRunDockerfile(sourceDir string) error { func (d *netcoreBuild) writeRunDockerfile(sourceDir string, envs map[string]string) error {
return ioutil.WriteFile(path.Join(sourceDir, "Dockerfile"), runDockerfile, 0755) result := util.ParseVariable(string(runDockerfile), envs)
return ioutil.WriteFile(path.Join(sourceDir, "Dockerfile"), []byte(result), 0755)
} }
func (d *netcoreBuild) copyBuildOut(outDir string, sourceImage string) error { func (d *netcoreBuild) copyBuildOut(outDir string, sourceImage string) error {

View File

@ -1,8 +1,7 @@
FROM microsoft/dotnet:2.1-sdk-alpine FROM microsoft/dotnet:${DOTNET_SDK_VERSION:2.2-sdk-alpine}
WORKDIR /app WORKDIR /app
# copy csproj and restore as distinct layers # copy csproj and restore as distinct layers
COPY . . COPY . .
RUN dotnet restore RUN ${DOTNET_RESTORE_PRE} && ${DOTNET_RESTORE:dotnet restore} && dotnet publish -c Release -o /out
RUN dotnet publish -c Release -o /out
CMD ["cp","-r","/out/","/tmp/"] CMD ["cp","-r","/out/","/tmp/"]

View File

@ -1,4 +1,4 @@
FROM microsoft/dotnet:2.1-aspnetcore-runtime-alpine FROM microsoft/dotnet:${DOTNET_SDK_VERSION:2.2-aspnetcore-runtime-alpine}
WORKDIR /app WORKDIR /app
COPY . . COPY . .
CMD ["dotnet"] CMD ["dotnet"]

View File

@ -121,8 +121,10 @@ func (p *probeManager) updateServiceProbe() {
continue continue
} }
serviceProbe := probe.CreateProbe(p.ctx, p.hostNode, p.statusChan, v) serviceProbe := probe.CreateProbe(p.ctx, p.hostNode, p.statusChan, v)
p.serviceProbe[v.Name] = serviceProbe if serviceProbe != nil {
serviceProbe.Check() p.serviceProbe[v.Name] = serviceProbe
serviceProbe.Check()
}
} }
} }

View File

@ -30,6 +30,7 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"reflect" "reflect"
"regexp"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
@ -755,3 +756,36 @@ func IsDir(path string) (bool, error) {
} }
return info.IsDir(), nil return info.IsDir(), nil
} }
var reg = regexp.MustCompile(`(?U)\$\{.*\}`)
//ParseVariable parse and replace variable in source str
func ParseVariable(source string, configs map[string]string) string {
resultKey := reg.FindAllString(source, -1)
for _, sourcekey := range resultKey {
key, defaultValue := getVariableKey(sourcekey)
if value, ok := configs[key]; ok {
source = strings.Replace(source, sourcekey, value, -1)
} else {
source = strings.Replace(source, sourcekey, defaultValue, -1)
}
}
return source
}
func getVariableKey(source string) (key, value string) {
if len(source) < 4 {
return "", ""
}
left := strings.Index(source, "{")
right := strings.Index(source, "}")
k := source[left+1 : right]
if strings.Contains(k, ":") {
re := strings.Split(k, ":")
if len(re) > 1 {
return re[0], re[1]
}
return re[0], ""
}
return k, ""
}

View File

@ -100,3 +100,10 @@ func TestCopyFile(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
} }
func TestParseVariable(t *testing.T) {
t.Log(ParseVariable("sada${XXX:aaa}dasd${XXX:aaa} ${YYY:aaa} ASDASD ${ZZZ:aaa}", map[string]string{
"XXX": "123DDD",
"ZZZ": ",.,.,.,.",
}))
}

View File

@ -20,12 +20,14 @@ package conversion
import ( import (
"fmt" "fmt"
"github.com/Sirupsen/logrus"
"os" "os"
"strings" "strings"
"github.com/Sirupsen/logrus"
"github.com/goodrain/rainbond/db" "github.com/goodrain/rainbond/db"
"github.com/goodrain/rainbond/db/model" "github.com/goodrain/rainbond/db/model"
"github.com/goodrain/rainbond/util"
typesv1 "github.com/goodrain/rainbond/worker/appm/types/v1" typesv1 "github.com/goodrain/rainbond/worker/appm/types/v1"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
"k8s.io/api/core/v1" "k8s.io/api/core/v1"
@ -76,7 +78,7 @@ func createPluginsContainer(as *typesv1.AppService, dbmanager db.Manager) ([]v1.
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
args, err := createPluginArgs(versionInfo.ContainerCMD) args, err := createPluginArgs(versionInfo.ContainerCMD, *envs)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@ -128,11 +130,15 @@ func getPluginModel(pluginID, tenantID string, dbmanager db.Manager) (string, er
return plugin.PluginModel, nil return plugin.PluginModel, nil
} }
func createPluginArgs(cmd string) ([]string, error) { func createPluginArgs(cmd string, envs []v1.EnvVar) ([]string, error) {
if cmd == "" { if cmd == "" {
return nil, nil return nil, nil
} }
return strings.Split(cmd, " "), nil configs := make(map[string]string, len(envs))
for _, env := range envs {
configs[env.Name] = env.Value
}
return strings.Split(util.ParseVariable(cmd, configs), " "), nil
} }
//container envs //container envs
@ -166,6 +172,13 @@ func createPluginEnvs(pluginID, tenantID, serviceAlias string, mainEnvs []v1.Env
serviceAlias, serviceAlias,
pluginID)}) pluginID)})
envs = append(envs, v1.EnvVar{Name: "PLUGIN_ID", Value: pluginID}) envs = append(envs, v1.EnvVar{Name: "PLUGIN_ID", Value: pluginID})
var config = make(map[string]string, len(envs))
for _, env := range envs {
config[env.Name] = env.Value
}
for i, env := range envs {
envs[i].Value = util.ParseVariable(env.Value, config)
}
return &envs, nil return &envs, nil
} }

View File

@ -21,7 +21,6 @@ package conversion
import ( import (
"fmt" "fmt"
"os" "os"
"regexp"
"strconv" "strconv"
"strings" "strings"
@ -108,16 +107,6 @@ func getMainContainer(as *v1.AppService, version *dbmodel.VersionInfo, dv *volum
}, nil }, nil
} }
//GetConfigKey 获取配置key
func GetConfigKey(rk string) string {
if len(rk) < 4 {
return ""
}
left := strings.Index(rk, "{")
right := strings.Index(rk, "}")
return rk[left+1 : right]
}
func getenv(key string, envs []corev1.EnvVar) string { func getenv(key string, envs []corev1.EnvVar) string {
for _, env := range envs { for _, env := range envs {
if env.Name == key { if env.Name == key {
@ -131,13 +120,11 @@ func createArgs(version *dbmodel.VersionInfo, envs []corev1.EnvVar) (args []stri
if version.Cmd == "" { if version.Cmd == "" {
return return
} }
cmd := version.Cmd configs := make(map[string]string, len(envs))
var reg = regexp.MustCompile(`(?U)\$\{.*\}`) for _, env := range envs {
resultKey := reg.FindAllString(cmd, -1) configs[env.Name] = env.Value
for _, rk := range resultKey {
value := getenv(GetConfigKey(rk), envs)
cmd = strings.Replace(cmd, rk, value, -1)
} }
cmd := util.ParseVariable(version.Cmd, configs)
args = strings.Split(cmd, " ") args = strings.Split(cmd, " ")
args = util.RemoveSpaces(args) args = util.RemoveSpaces(args)
return args return args
@ -265,6 +252,13 @@ func createEnv(as *v1.AppService, dbmanager db.Manager) (*[]corev1.EnvVar, error
FieldPath: "status.podIP", FieldPath: "status.podIP",
}, },
}}) }})
var config = make(map[string]string, len(envs))
for _, env := range envs {
config[env.Name] = env.Value
}
for i, env := range envs {
envs[i].Value = util.ParseVariable(env.Value, config)
}
return &envs, nil return &envs, nil
} }