[ADD] code check add runtime version check

This commit is contained in:
barnett 2019-03-09 14:50:38 +08:00
parent f5fd1145f1
commit b6632e79d3
4 changed files with 139 additions and 107 deletions

View File

@ -95,7 +95,6 @@ func (s *ServiceAction) GetServiceCheckInfo(uuid string) (*exector.ServiceCheckR
if err := ffjson.Unmarshal(v, &si); err != nil { if err := ffjson.Unmarshal(v, &si); err != nil {
return nil, util.CreateAPIHandleError(500, err) return nil, util.CreateAPIHandleError(500, err)
} }
if si.CheckStatus == "" { if si.CheckStatus == "" {
si.CheckStatus = "Checking" si.CheckStatus = "Checking"
logrus.Debugf("checking is %v", si) logrus.Debugf("checking is %v", si)

View File

@ -23,107 +23,125 @@ import (
"io/ioutil" "io/ioutil"
"path" "path"
"github.com/bitly/go-simplejson" simplejson "github.com/bitly/go-simplejson"
"github.com/goodrain/rainbond/util" "github.com/goodrain/rainbond/util"
) )
//ErrRuntimeNotSupport runtime not support
var ErrRuntimeNotSupport = fmt.Errorf("runtime version not support")
//CheckRuntime CheckRuntime //CheckRuntime CheckRuntime
func CheckRuntime(buildPath string, lang Lang) bool { func CheckRuntime(buildPath string, lang Lang) (map[string]string, error) {
switch lang { switch lang {
case PHP: case PHP:
if ok, _ := util.FileExists(path.Join(buildPath, "composer.json")); !ok { return readPHPRuntimeInfo(buildPath)
return false
}
body, err := ioutil.ReadFile(path.Join(buildPath, "composer.json"))
if err != nil {
return false
}
json, err := simplejson.NewJson(body)
if err != nil {
return false
}
if json.Get("require") != nil && json.Get("require").Get("php") != nil {
return true
}
return false
case Python: case Python:
if ok, _ := util.FileExists(path.Join(buildPath, "runtime.txt")); ok { return readPythonRuntimeInfo(buildPath)
//TODO:check runtime rules : python-2.7.3
return true
}
return false
case Ruby:
return true
case JavaMaven, JaveWar, JavaJar: case JavaMaven, JaveWar, JavaJar:
ok, err := util.FileExists(path.Join(buildPath, "system.properties")) return readJavaRuntimeInfo(buildPath)
if !ok || err != nil {
return false
}
cmd := fmt.Sprintf(`grep -i "java.runtime.version" %s | grep -E -o "[0-9]+(.[0-9]+)?(.[0-9]+)?"`, path.Join(buildPath, "system.properties"))
runtime, err := util.CmdExec(cmd)
if err != nil {
return false
}
if runtime != "" {
return true
}
return false
case Nodejs: case Nodejs:
return false return readNodeRuntimeInfo(buildPath)
case NodeJSStatic:
runtime, err := readNodeRuntimeInfo(buildPath)
if err != nil {
return nil, err
}
runtime["RUNTIMES_SERVER"] = "nginx"
return runtime, nil
case Static:
return map[string]string{"RUNTIMES_SERVER": "nginx"}, nil
default: default:
return false return nil, nil
} }
} }
/* func readPHPRuntimeInfo(buildPath string) (map[string]string, error) {
function detect_runtimes(){ var phpRuntimeInfo = make(map[string]string, 1)
lang=`echo $1 |tr A-Z a-z` if ok, _ := util.FileExists(path.Join(buildPath, "composer.json")); !ok {
case $lang in return phpRuntimeInfo, nil
"php") }
if [ -f $SOURCE_DIR/composer.json ];then body, err := ioutil.ReadFile(path.Join(buildPath, "composer.json"))
runtimes=`$JQBIN '.require.php' $SOURCE_DIR/composer.json` if err != nil {
[ "$runtimes" != "null" ] && echo "true" || echo "false" return phpRuntimeInfo, nil
else }
echo "false" json, err := simplejson.NewJson(body)
fi if err != nil {
;; return phpRuntimeInfo, nil
"python") }
if [ -f $SOURCE_DIR/runtime.txt ];then if json.Get("require") != nil {
runtimes=`grep -i python $SOURCE_DIR/runtime.txt | grep -E -o "[0-9]+(.[0-9]+)?(.[0-9]+)?"` if phpVersion := json.Get("require").Get("php"); phpVersion != nil {
[ "$runtimes" != "" ] && echo "true" || echo "false" version, _ := phpVersion.String()
else if version != "" {
echo "false" if len(version) < 4 {
fi return nil, ErrRuntimeNotSupport
;; }
"ruby") if version[0] == '~' {
# if [ -f $SOURCE_DIR/Gemfile ];then if !util.StringArrayContains([]string{"5.5", "5.6", "7.0", "7.1"}, version[1:3]) {
# runtimes=`grep -E -i "^\ *ruby" $SOURCE_DIR/Gemfile | grep -E -o "[0-9]+(.[0-9]+)?(.[0-9]+)?"` return nil, ErrRuntimeNotSupport
# [ "$runtimes" != "" ] && echo "true" || echo "false" }
# else } else {
# echo "false" if !util.StringArrayContains([]string{"5.5", "5.6", "7.0", "7.1"}, version[0:3]) {
# fi return nil, ErrRuntimeNotSupport
echo "true" }
;; }
"java-war|java-maven") phpRuntimeInfo["RUNTIMES"] = version
if [ -f $SOURCE_DIR/system.properties ];then }
runtimes=`grep -i "java.runtime.version" $SOURCE_DIR/system.properties | grep -E -o "[0-9]+(.[0-9]+)?(.[0-9]+)?"` }
[ "$runtimes" != "" ] && echo "true" || echo "false" if hhvmVersion := json.Get("require").Get("hhvm"); hhvmVersion != nil {
else phpRuntimeInfo["RUNTIMES_HHVM"], _ = hhvmVersion.String()
echo "false" }
fi }
;; return phpRuntimeInfo, nil
"node.js") }
if [ -f $SOURCE_DIR/package.json ] ;then
runtimes=`$JQBIN '.engines.node' $SOURCE_DIR/package.json` func readPythonRuntimeInfo(buildPath string) (map[string]string, error) {
[ "$runtimes" != "null" ] && echo "true" || echo "false" var runtimeInfo = make(map[string]string, 1)
else if ok, _ := util.FileExists(path.Join(buildPath, "runtime.txt")); !ok {
echo "false" return runtimeInfo, nil
fi }
;; body, err := ioutil.ReadFile(path.Join(buildPath, "runtime.txt"))
"*") if err != nil {
echo "false";; return runtimeInfo, nil
esac }
} runtimeInfo["RUNTIMES"] = string(body)
*/ return runtimeInfo, nil
}
func readJavaRuntimeInfo(buildPath string) (map[string]string, error) {
var runtimeInfo = make(map[string]string, 1)
ok, err := util.FileExists(path.Join(buildPath, "system.properties"))
if !ok || err != nil {
return runtimeInfo, nil
}
cmd := fmt.Sprintf(`grep -i "java.runtime.version" %s | grep -E -o "[0-9]+(.[0-9]+)?(.[0-9]+)?"`, path.Join(buildPath, "system.properties"))
runtime, err := util.CmdExec(cmd)
if err != nil {
return runtimeInfo, nil
}
if runtime != "" {
runtimeInfo["RUNTIMES"] = runtime
}
return runtimeInfo, nil
}
func readNodeRuntimeInfo(buildPath string) (map[string]string, error) {
var runtimeInfo = make(map[string]string, 1)
if ok, _ := util.FileExists(path.Join(buildPath, "package.json")); !ok {
return runtimeInfo, nil
}
body, err := ioutil.ReadFile(path.Join(buildPath, "package.json"))
if err != nil {
return runtimeInfo, nil
}
json, err := simplejson.NewJson(body)
if err != nil {
return runtimeInfo, nil
}
if json.Get("engines") != nil {
if nodeVersion := json.Get("engines").Get("node"); nodeVersion != nil {
runtimeInfo["RUNTIMES"], _ = nodeVersion.String()
}
}
return runtimeInfo, nil
}

View File

@ -131,21 +131,26 @@ type Lang string
//ServiceInfo 智能获取的应用信息 //ServiceInfo 智能获取的应用信息
type ServiceInfo struct { type ServiceInfo struct {
Ports []Port `json:"ports"` Ports []Port `json:"ports"`
Envs []Env `json:"envs"` Envs []Env `json:"envs"`
Volumes []Volume `json:"volumes"` Volumes []Volume `json:"volumes"`
Image Image `json:"image"` Image Image `json:"image"`
Args []string `json:"args"` Args []string `json:"args"`
DependServices []string `json:"depends,omitempty"` DependServices []string `json:"depends,omitempty"`
ServiceDeployType string `json:"deploy_type,omitempty"` ServiceDeployType string `json:"deploy_type,omitempty"`
Branchs []string `json:"branchs,omitempty"` Branchs []string `json:"branchs,omitempty"`
Memory int `json:"memory"` Memory int `json:"memory"`
Lang code.Lang `json:"language"` Lang code.Lang `json:"language"`
Runtime bool `json:"runtime"` ImageAlias string `json:"image_alias"`
Dependencies bool `json:"dependencies"` //For third party services
Procfile bool `json:"procfile"` Endpoints []*discovery.Endpoint `json:"endpoints"`
ImageAlias string `json:"image_alias"`
Endpoints []*discovery.Endpoint `json:"endpoints"` //deprecated
Runtime bool `json:"runtime"`
//deprecated
Dependencies bool `json:"dependencies"`
//deprecated
Procfile bool `json:"procfile"`
} }
//GetServiceInfo GetServiceInfo //GetServiceInfo GetServiceInfo

View File

@ -263,7 +263,17 @@ func (d *SourceCodeParse) Parse() ParseErrorList {
} }
} }
d.Dependencies = code.CheckDependencies(buildPath, lang) d.Dependencies = code.CheckDependencies(buildPath, lang)
d.Runtime = code.CheckRuntime(buildPath, lang) runtimeInfo, err := code.CheckRuntime(buildPath, lang)
if err != nil && err == code.ErrRuntimeNotSupport {
d.errappend(ErrorAndSolve(FatalError, "代码选择的运行时版本不支持", "请参考文档查看平台各语言支持的Runtime版本"))
return d.errors
}
for k, v := range runtimeInfo {
d.envs["BUILD_"+k] = &Env{
Name: "BUILD_" + k,
Value: v,
}
}
d.memory = getRecommendedMemory(lang) d.memory = getRecommendedMemory(lang)
d.Procfile = code.CheckProcfile(buildPath, lang) d.Procfile = code.CheckProcfile(buildPath, lang)
if rbdfileConfig != nil { if rbdfileConfig != nil {