[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 {
return nil, util.CreateAPIHandleError(500, err)
}
if si.CheckStatus == "" {
si.CheckStatus = "Checking"
logrus.Debugf("checking is %v", si)

View File

@ -23,107 +23,125 @@ import (
"io/ioutil"
"path"
"github.com/bitly/go-simplejson"
simplejson "github.com/bitly/go-simplejson"
"github.com/goodrain/rainbond/util"
)
//ErrRuntimeNotSupport runtime not support
var ErrRuntimeNotSupport = fmt.Errorf("runtime version not support")
//CheckRuntime CheckRuntime
func CheckRuntime(buildPath string, lang Lang) bool {
func CheckRuntime(buildPath string, lang Lang) (map[string]string, error) {
switch lang {
case PHP:
if ok, _ := util.FileExists(path.Join(buildPath, "composer.json")); !ok {
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
return readPHPRuntimeInfo(buildPath)
case Python:
if ok, _ := util.FileExists(path.Join(buildPath, "runtime.txt")); ok {
//TODO:check runtime rules : python-2.7.3
return true
}
return false
case Ruby:
return true
return readPythonRuntimeInfo(buildPath)
case JavaMaven, JaveWar, JavaJar:
ok, err := util.FileExists(path.Join(buildPath, "system.properties"))
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
return readJavaRuntimeInfo(buildPath)
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:
return false
return nil, nil
}
}
/*
function detect_runtimes(){
lang=`echo $1 |tr A-Z a-z`
case $lang in
"php")
if [ -f $SOURCE_DIR/composer.json ];then
runtimes=`$JQBIN '.require.php' $SOURCE_DIR/composer.json`
[ "$runtimes" != "null" ] && echo "true" || echo "false"
else
echo "false"
fi
;;
"python")
if [ -f $SOURCE_DIR/runtime.txt ];then
runtimes=`grep -i python $SOURCE_DIR/runtime.txt | grep -E -o "[0-9]+(.[0-9]+)?(.[0-9]+)?"`
[ "$runtimes" != "" ] && echo "true" || echo "false"
else
echo "false"
fi
;;
"ruby")
# if [ -f $SOURCE_DIR/Gemfile ];then
# runtimes=`grep -E -i "^\ *ruby" $SOURCE_DIR/Gemfile | grep -E -o "[0-9]+(.[0-9]+)?(.[0-9]+)?"`
# [ "$runtimes" != "" ] && echo "true" || echo "false"
# else
# echo "false"
# fi
echo "true"
;;
"java-war|java-maven")
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"
else
echo "false"
fi
;;
"node.js")
if [ -f $SOURCE_DIR/package.json ] ;then
runtimes=`$JQBIN '.engines.node' $SOURCE_DIR/package.json`
[ "$runtimes" != "null" ] && echo "true" || echo "false"
else
echo "false"
fi
;;
"*")
echo "false";;
esac
}
*/
func readPHPRuntimeInfo(buildPath string) (map[string]string, error) {
var phpRuntimeInfo = make(map[string]string, 1)
if ok, _ := util.FileExists(path.Join(buildPath, "composer.json")); !ok {
return phpRuntimeInfo, nil
}
body, err := ioutil.ReadFile(path.Join(buildPath, "composer.json"))
if err != nil {
return phpRuntimeInfo, nil
}
json, err := simplejson.NewJson(body)
if err != nil {
return phpRuntimeInfo, nil
}
if json.Get("require") != nil {
if phpVersion := json.Get("require").Get("php"); phpVersion != nil {
version, _ := phpVersion.String()
if version != "" {
if len(version) < 4 {
return nil, ErrRuntimeNotSupport
}
if version[0] == '~' {
if !util.StringArrayContains([]string{"5.5", "5.6", "7.0", "7.1"}, version[1:3]) {
return nil, ErrRuntimeNotSupport
}
} else {
if !util.StringArrayContains([]string{"5.5", "5.6", "7.0", "7.1"}, version[0:3]) {
return nil, ErrRuntimeNotSupport
}
}
phpRuntimeInfo["RUNTIMES"] = version
}
}
if hhvmVersion := json.Get("require").Get("hhvm"); hhvmVersion != nil {
phpRuntimeInfo["RUNTIMES_HHVM"], _ = hhvmVersion.String()
}
}
return phpRuntimeInfo, nil
}
func readPythonRuntimeInfo(buildPath string) (map[string]string, error) {
var runtimeInfo = make(map[string]string, 1)
if ok, _ := util.FileExists(path.Join(buildPath, "runtime.txt")); !ok {
return runtimeInfo, nil
}
body, err := ioutil.ReadFile(path.Join(buildPath, "runtime.txt"))
if err != nil {
return runtimeInfo, nil
}
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 智能获取的应用信息
type ServiceInfo struct {
Ports []Port `json:"ports"`
Envs []Env `json:"envs"`
Volumes []Volume `json:"volumes"`
Image Image `json:"image"`
Args []string `json:"args"`
DependServices []string `json:"depends,omitempty"`
ServiceDeployType string `json:"deploy_type,omitempty"`
Branchs []string `json:"branchs,omitempty"`
Memory int `json:"memory"`
Lang code.Lang `json:"language"`
Runtime bool `json:"runtime"`
Dependencies bool `json:"dependencies"`
Procfile bool `json:"procfile"`
ImageAlias string `json:"image_alias"`
Endpoints []*discovery.Endpoint `json:"endpoints"`
Ports []Port `json:"ports"`
Envs []Env `json:"envs"`
Volumes []Volume `json:"volumes"`
Image Image `json:"image"`
Args []string `json:"args"`
DependServices []string `json:"depends,omitempty"`
ServiceDeployType string `json:"deploy_type,omitempty"`
Branchs []string `json:"branchs,omitempty"`
Memory int `json:"memory"`
Lang code.Lang `json:"language"`
ImageAlias string `json:"image_alias"`
//For third party services
Endpoints []*discovery.Endpoint `json:"endpoints"`
//deprecated
Runtime bool `json:"runtime"`
//deprecated
Dependencies bool `json:"dependencies"`
//deprecated
Procfile bool `json:"procfile"`
}
//GetServiceInfo GetServiceInfo

View File

@ -263,7 +263,17 @@ func (d *SourceCodeParse) Parse() ParseErrorList {
}
}
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.Procfile = code.CheckProcfile(buildPath, lang)
if rbdfileConfig != nil {