[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"
"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 {

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
# 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/"]

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
COPY . .
CMD ["dotnet"]

View File

@ -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()
}
}
}

View File

@ -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, ""
}

View File

@ -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": ",.,.,.,.",
}))
}

View File

@ -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
}

View File

@ -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
}