From d841ec73324316c8136cd44e5b8055b0a97b6dbd Mon Sep 17 00:00:00 2001 From: barnett <576501057@qq.com> Date: Thu, 6 Dec 2018 13:41:43 +0800 Subject: [PATCH] [REV] dotnet support customize runtime version and build commands --- builder/build/netcore_build.go | 16 +++++---- .../chaos/build-app/netcore/Dockerfile.build | 5 ++- .../build-app/netcore/Dockerfile.runtime | 2 +- node/nodem/healthy/health_manager.go | 6 ++-- util/comman.go | 34 +++++++++++++++++++ util/comman_test.go | 7 ++++ worker/appm/conversion/plugin.go | 21 +++++++++--- worker/appm/conversion/version.go | 28 ++++++--------- 8 files changed, 86 insertions(+), 33 deletions(-) diff --git a/builder/build/netcore_build.go b/builder/build/netcore_build.go index 32f2b1872..0b3938e7d 100644 --- a/builder/build/netcore_build.go +++ b/builder/build/netcore_build.go @@ -25,6 +25,8 @@ import ( "os" "path" + "github.com/goodrain/rainbond/util" + "github.com/docker/engine-api/client" "github.com/Sirupsen/logrus" @@ -75,7 +77,7 @@ func (d *netcoreBuild) Build(re *Request) (*Response, error) { d.serviceID = re.ServiceID defer d.clear() //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()) } d.sourceDir = re.SourceDir @@ -114,7 +116,7 @@ func (d *netcoreBuild) Build(re *Request) (*Response, error) { return nil, err } //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()) } //build runtime image @@ -150,12 +152,14 @@ func (d *netcoreBuild) Build(re *Request) (*Response, error) { re.Logger.Info("镜像推送镜像至仓库成功", map[string]string{"step": "builder-exector"}) return d.createResponse(), nil } -func (d *netcoreBuild) writeBuildDockerfile(sourceDir string) error { - return ioutil.WriteFile(path.Join(sourceDir, "Dockerfile"), buildDockerfile, 0755) +func (d *netcoreBuild) writeBuildDockerfile(sourceDir string, envs map[string]string) error { + result := util.ParseVariable(string(buildDockerfile), envs) + return ioutil.WriteFile(path.Join(sourceDir, "Dockerfile"), []byte(result), 0755) } -func (d *netcoreBuild) writeRunDockerfile(sourceDir string) error { - return ioutil.WriteFile(path.Join(sourceDir, "Dockerfile"), runDockerfile, 0755) +func (d *netcoreBuild) writeRunDockerfile(sourceDir string, envs map[string]string) error { + 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 { diff --git a/hack/contrib/docker/chaos/build-app/netcore/Dockerfile.build b/hack/contrib/docker/chaos/build-app/netcore/Dockerfile.build index cbc49247d..2d8cc4118 100644 --- a/hack/contrib/docker/chaos/build-app/netcore/Dockerfile.build +++ b/hack/contrib/docker/chaos/build-app/netcore/Dockerfile.build @@ -1,8 +1,7 @@ -FROM microsoft/dotnet:2.1-sdk-alpine +FROM microsoft/dotnet:${DOTNET_SDK_VERSION:2.2-sdk-alpine} WORKDIR /app # copy csproj and restore as distinct layers COPY . . -RUN dotnet restore -RUN dotnet publish -c Release -o /out +RUN ${DOTNET_RESTORE_PRE} && ${DOTNET_RESTORE:dotnet restore} && dotnet publish -c Release -o /out CMD ["cp","-r","/out/","/tmp/"] \ No newline at end of file diff --git a/hack/contrib/docker/chaos/build-app/netcore/Dockerfile.runtime b/hack/contrib/docker/chaos/build-app/netcore/Dockerfile.runtime index fd0c613c6..2f4dcb33f 100644 --- a/hack/contrib/docker/chaos/build-app/netcore/Dockerfile.runtime +++ b/hack/contrib/docker/chaos/build-app/netcore/Dockerfile.runtime @@ -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 COPY . . CMD ["dotnet"] \ No newline at end of file diff --git a/node/nodem/healthy/health_manager.go b/node/nodem/healthy/health_manager.go index f00fb6dcc..44eb397fe 100644 --- a/node/nodem/healthy/health_manager.go +++ b/node/nodem/healthy/health_manager.go @@ -121,8 +121,10 @@ func (p *probeManager) updateServiceProbe() { continue } serviceProbe := probe.CreateProbe(p.ctx, p.hostNode, p.statusChan, v) - p.serviceProbe[v.Name] = serviceProbe - serviceProbe.Check() + if serviceProbe != nil { + p.serviceProbe[v.Name] = serviceProbe + serviceProbe.Check() + } } } diff --git a/util/comman.go b/util/comman.go index 2dbab7e9d..b8a807bd0 100644 --- a/util/comman.go +++ b/util/comman.go @@ -30,6 +30,7 @@ import ( "path" "path/filepath" "reflect" + "regexp" "runtime" "strconv" "strings" @@ -755,3 +756,36 @@ func IsDir(path string) (bool, error) { } 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, "" +} diff --git a/util/comman_test.go b/util/comman_test.go index a54b3306d..3c5d8be6b 100644 --- a/util/comman_test.go +++ b/util/comman_test.go @@ -100,3 +100,10 @@ func TestCopyFile(t *testing.T) { 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": ",.,.,.,.", + })) +} diff --git a/worker/appm/conversion/plugin.go b/worker/appm/conversion/plugin.go index 30d6510b8..c30d80a7e 100644 --- a/worker/appm/conversion/plugin.go +++ b/worker/appm/conversion/plugin.go @@ -20,12 +20,14 @@ package conversion import ( "fmt" - "github.com/Sirupsen/logrus" "os" "strings" + "github.com/Sirupsen/logrus" + "github.com/goodrain/rainbond/db" "github.com/goodrain/rainbond/db/model" + "github.com/goodrain/rainbond/util" typesv1 "github.com/goodrain/rainbond/worker/appm/types/v1" "github.com/jinzhu/gorm" "k8s.io/api/core/v1" @@ -76,7 +78,7 @@ func createPluginsContainer(as *typesv1.AppService, dbmanager db.Manager) ([]v1. if err != nil { return nil, nil, err } - args, err := createPluginArgs(versionInfo.ContainerCMD) + args, err := createPluginArgs(versionInfo.ContainerCMD, *envs) if err != nil { return nil, nil, err } @@ -128,11 +130,15 @@ func getPluginModel(pluginID, tenantID string, dbmanager db.Manager) (string, er return plugin.PluginModel, nil } -func createPluginArgs(cmd string) ([]string, error) { +func createPluginArgs(cmd string, envs []v1.EnvVar) ([]string, error) { if cmd == "" { 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 @@ -166,6 +172,13 @@ func createPluginEnvs(pluginID, tenantID, serviceAlias string, mainEnvs []v1.Env serviceAlias, 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 } diff --git a/worker/appm/conversion/version.go b/worker/appm/conversion/version.go index adc96eb77..1df9043a1 100644 --- a/worker/appm/conversion/version.go +++ b/worker/appm/conversion/version.go @@ -21,7 +21,6 @@ package conversion import ( "fmt" "os" - "regexp" "strconv" "strings" @@ -108,16 +107,6 @@ func getMainContainer(as *v1.AppService, version *dbmodel.VersionInfo, dv *volum }, 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 { for _, env := range envs { if env.Name == key { @@ -131,13 +120,11 @@ func createArgs(version *dbmodel.VersionInfo, envs []corev1.EnvVar) (args []stri if version.Cmd == "" { return } - cmd := version.Cmd - var reg = regexp.MustCompile(`(?U)\$\{.*\}`) - resultKey := reg.FindAllString(cmd, -1) - for _, rk := range resultKey { - value := getenv(GetConfigKey(rk), envs) - cmd = strings.Replace(cmd, rk, value, -1) + configs := make(map[string]string, len(envs)) + for _, env := range envs { + configs[env.Name] = env.Value } + cmd := util.ParseVariable(version.Cmd, configs) args = strings.Split(cmd, " ") args = util.RemoveSpaces(args) return args @@ -265,6 +252,13 @@ func createEnv(as *v1.AppService, dbmanager db.Manager) (*[]corev1.EnvVar, error 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 }