From 9d88c7c467f8d93af4aab9ba0b6d6c01c2ffc546 Mon Sep 17 00:00:00 2001 From: sunty <1172534965@qq.com> Date: 星期四, 20 八月 2020 20:35:30 +0800 Subject: [PATCH] add get buckets contrl --- service/SdkInstall.go | 425 ++++++++++++++++++++++++++++++++++++++++++---------- 1 files changed, 343 insertions(+), 82 deletions(-) diff --git a/service/SdkInstall.go b/service/SdkInstall.go index b930aab..b0ed7f6 100644 --- a/service/SdkInstall.go +++ b/service/SdkInstall.go @@ -2,6 +2,8 @@ import ( "basic.com/dbapi.git" + "basic.com/pubsub/protomsg.git" + "basic.com/valib/licence.git" "basic.com/valib/logger.git" "encoding/json" "errors" @@ -9,12 +11,49 @@ "os" "path" "path/filepath" + "strconv" + "strings" "webserver/extend/config" "webserver/extend/util" + reqUtil "webserver/extend/util" ) type SdkInstallService struct { +} + +func (sv SdkInstallService) Active(cod string, sdkId string) (interface{},error) { + //浣跨敤婵�娲荤爜婵�娲荤畻娉� + url := "http://"+config.ShopConf.Url+"/data/api-s/sdk/activeByAINode" + machineCode := licence.GetMachineCode() + if machineCode == "" { + logger.Debug("鑾峰彇鏈哄櫒鐮佸け璐�") + return nil, errors.New("鑾峰彇鏈哄櫒鐮佸け璐�") + } + paramBody := map[string]interface{}{ + "code": cod, + "sdkId": sdkId, + "machineCode": machineCode, + } + header := map[string]string { + "Authorization": token, + } + respBody, err := reqUtil.DoPostRequest(url, reqUtil.CONTENT_TYPE_JSON, paramBody, nil, header) + if err != nil { + logger.Debug("DoPostRequest err:", err) + return nil, err + } + var res dbapi.Result + if err = json.Unmarshal(respBody, &res); err != nil { + logger.Debug("unmarshal err:", err) + return nil, err + } + if !res.Success { + logger.Debug("res.Data:", res.Data) + return nil, errors.New("璇锋眰鍟嗗煄澶辫触") + } + + return res.Data, nil } //绠楁硶瀹夎鍖呭畨瑁� @@ -23,17 +62,25 @@ if config.Server.PatchPath != "" { configPatchPath = config.Server.PatchPath } else { - configPatchPath = "/opt/vasystem/patch" + configPatchPath = "../patch" } filenameWithSuffix := path.Base(filename) ext := path.Ext(filenameWithSuffix) zipFilePath := configPatchPath + "/"+identifier+ext + unPackTargetPath := configPatchPath+"/"+identifier+"_basic/" + md5DirPath := configPatchPath+"/"+identifier if util.Exists(zipFilePath) { strMd5, e := util.FileMd5(zipFilePath) if e !=nil || strMd5 == "" { go os.Remove(zipFilePath) + if util.DirExists(unPackTargetPath) { + go os.RemoveAll(unPackTargetPath) + } + if util.DirExists(md5DirPath) { + go os.RemoveAll(md5DirPath) + } return false,errors.New("鑾峰彇瀹夎鍖卪d5澶辫触") } if strMd5 == identifier { @@ -44,6 +91,12 @@ } else { go os.Remove(zipFilePath) + if util.DirExists(unPackTargetPath) { + go os.RemoveAll(unPackTargetPath) + } + if util.DirExists(md5DirPath) { + go os.RemoveAll(md5DirPath) + } logger.Debug("strMd5 is", strMd5,"identifier is",identifier,"not equal") return false,errors.New("鏍¢獙瀹夎鏂囦欢澶辫触") } @@ -52,25 +105,17 @@ } } -//瀹夎sdk -func installSdk(identifier string, ext string) (bool, error) { +//瑙e帇涓婁紶鐨勭畻娉曞畨瑁呭寘 +func unPackPatchPackage(identifier string, ext string) (bool,error) { configPatchPath := "" if config.Server.PatchPath != "" { configPatchPath = config.Server.PatchPath } else { - configPatchPath = "/opt/vasystem/patch" + configPatchPath = "../patch" } //1.瑙e帇缂╂洿鏂板寘 unPackTargetPath := configPatchPath+"/"+identifier+"_basic/" unPackFilePath := configPatchPath+"/"+identifier+ext - defer func() { - if util.Exists(unPackTargetPath) { - os.RemoveAll(unPackTargetPath) - } - if util.Exists(unPackFilePath) { - os.RemoveAll(unPackFilePath) - } - }() if util.Exists(unPackTargetPath) { //姝ょ増鏈凡缁忔洿鏂拌繃 @@ -82,15 +127,115 @@ if !util.CreateDirectory(unPackTargetPath) { return false, errors.New("鍒涘缓鍘嬬缉鏂囦欢澶瑰け璐�") } - logger.Debug("unPackFilePath:", unPackFilePath, "unPackPath:", unPackTargetPath) _,err := util.UnTarGzByCmd(unPackFilePath, unPackTargetPath) if err !=nil { logger.Debug("UnPack err:",err,"unPackFile:",unPackFilePath) return false, err } + return true, nil +} + +func (sv SdkInstallService) ShowInstallInfo(identifier string, filename string) (bool, map[string]interface{}, error) { + configPatchPath := "" + if config.Server.PatchPath != "" { + configPatchPath = config.Server.PatchPath + } else { + configPatchPath = "../patch" + } + unPackTargetPath := configPatchPath+"/"+identifier+"_basic/" + isValid := false + defer func() { + if !isValid { + filenameWithSuffix := path.Base(filename) + ext := path.Ext(filenameWithSuffix) + unPackFilePath := configPatchPath+"/"+identifier+ext + md5DirPath := configPatchPath+"/"+identifier + + if util.Exists(unPackTargetPath) { + os.RemoveAll(unPackTargetPath) + } + if util.Exists(unPackFilePath) { + os.RemoveAll(unPackFilePath) + } + if util.DirExists(md5DirPath) { + go os.RemoveAll(md5DirPath) + } + + } + }() + if util.Exists(unPackTargetPath) { + targetFileName := "" + err := filepath.Walk(unPackTargetPath, func(path string, f os.FileInfo, err error) error { + if f == nil { + return err + } + if f.IsDir() { + targetFileName = f.Name() + } + return nil + }) + if err != nil { + return isValid, nil, errors.New("闈炴硶鐨勫畨瑁呭寘") + } else { + unPackPath := unPackTargetPath + targetFileName +"/" + //瑙e帇瀹屾垚锛岃幏鍙栧畨瑁呭寘涓殑鏂囦欢锛屽紑濮嬪畨瑁� + //1.瑙f瀽瀹夎璇存槑ins.inc + incPath := unPackPath+"ins.inc" + if util.Exists(incPath) { + if incB, err := ioutil.ReadFile(incPath); err == nil { + var ins InsInc + if err = json.Unmarshal(incB, &ins); err == nil { + return true, map[string]interface{} { + "sdkName": ins.ProductName, + "installVersion": ins.InstallVersion, + "installContent": ins.InstallContent, + }, nil + } else { + return false, nil, errors.New("闈炴硶鐨勫畨瑁呭寘") + } + } else { + return false, nil, errors.New("闈炴硶鐨勫畨瑁呭寘") + } + } else { + return false, nil, errors.New("闈炴硶鐨勫畨瑁呭寘") + } + } + } else { + return isValid, nil, errors.New("瀹夎鍖呭凡涓嶅瓨鍦�") + } +} + +//瀹夎sdk +func installSdk(identifier string, ext string) (bool, error) { + configPatchPath := "" + if config.Server.PatchPath != "" { + configPatchPath = config.Server.PatchPath + } else { + configPatchPath = "../patch" + } + //1.瑙e帇缂╂洿鏂板寘 + unPackTargetPath := configPatchPath+"/"+identifier+"_basic/" + unPackFilePath := configPatchPath+"/"+identifier+ext + md5DirPath := configPatchPath+"/"+identifier + defer func() { + if util.Exists(unPackTargetPath) { + os.RemoveAll(unPackTargetPath) + } + if util.Exists(unPackFilePath) { + os.RemoveAll(unPackFilePath) + } + if util.DirExists(md5DirPath) { + go os.RemoveAll(md5DirPath) + } + }() + + if !util.Exists(unPackTargetPath) {//瀹夎鍖呬笉瀛樺湪 + return false, errors.New("瀹夎鏂囦欢宸蹭笉瀛樺湪") + } + targetFileName := "" - err = filepath.Walk(unPackTargetPath, func(path string, f os.FileInfo, err error) error { + err := filepath.Walk(unPackTargetPath, func(path string, f os.FileInfo, err error) error { if f == nil { return err } @@ -103,6 +248,9 @@ return false, err } unPackPath := unPackTargetPath + targetFileName +"/" + if !util.Exists(unPackPath) { + return false, errors.New("瀹夎鏂囦欢宸蹭笉瀛樺湪") + } //瑙e帇瀹屾垚锛岃幏鍙栧畨瑁呭寘涓殑鏂囦欢锛屽紑濮嬪畨瑁� //1.瑙f瀽瀹夎璇存槑ins.inc incPath := unPackPath+"ins.inc" @@ -122,44 +270,69 @@ //3.灏嗙畻娉晄o銆佷緷璧栨枃浠躲�亃conf銆� soM := make(map[string]SdkDef) var skDefArr []SdkDef + nInsM := make(map[string]string) + aInsM := make(map[string]string) if err = json.Unmarshal(defB, &skDefArr);err == nil { - //娉ㄥ唽绠楁硶淇℃伅鍜岀畻娉曞弬鏁板埌dbserver var sdkApi dbapi.SdkApi for _,skd := range skDefArr { + //娉ㄥ唽绠楁硶淇℃伅鍜岀畻娉曞弬鏁板埌dbserver + //鍏堢湅姝ょ畻娉曟湁娌℃湁娉ㄥ唽锛屽鏋滃凡瀹夎锛屾瘮瀵圭増鏈彿 + bf, curDbSdk := sdkApi.GetById(skd.Def.Id) + if bf { + bytes, _ := json.Marshal(curDbSdk) + var sm map[string]interface{} + unE := json.Unmarshal(bytes, &sm) + if unE != nil { + continue + } + sdkVersion, ok := sm["version"] + if ok { //鏈夌増鏈彿 + vStr := sdkVersion.(string) + if shouldVersionBeUpgrade(vStr, skd.Def.Version) { //姣斿鐗堟湰鍙风‘瀹氭槸鍚﹂渶瑕佸畨瑁� + nInsM[skd.Def.SdkType] = skd.Def.SdkType + aInsM[skd.Def.Id] = skd.Def.Id + } + } else { + nInsM[skd.Def.SdkType] = skd.Def.SdkType + aInsM[skd.Def.Id] = skd.Def.Id + } + } else { + nInsM[skd.Def.SdkType] = skd.Def.SdkType + aInsM[skd.Def.Id] = skd.Def.Id + } + + if _,ok := soM[skd.Def.SdkType];!ok { soM[skd.Def.SdkType] = skd } - srv := SdkRegisterVo{ - Id: skd.Def.Id, - SdkType: skd.Def.SdkType, - SdkName: skd.Def.SdkName, - Icon: skd.Def.Icon, - Url: skd.Def.Url, - } - for _,ag := range skd.Args { - sra := SdkRegisterArgVo{ - Scope: ag.Scope, + if _,ok := aInsM[skd.Def.Id]; ok { + srv := SdkRegisterVo{} + srv.SdkCommon = skd.Def.SdkCommon + for _,ag := range skd.Args { + sra := SdkRegisterArgVo{ + Scope: ag.Scope, + } + sra.SdkArg = SdkArg{ + Alias: ag.Alias, + Name: ag.Name, + Type: ag.Type, + ArgType: ag.ArgType, + Must: ag.Must, + Unit: ag.Unit, + Range: ag.Range, + DefaultValue: ag.DefaultValue, + DefaultOperator: ag.DefaultOperator, + Sort: ag.Sort, + } + srv.Args = append(srv.Args, sra) } - sra.SdkArg = SdkArg{ - Alias: ag.Alias, - Name: ag.Name, - Type: ag.Type, - ArgType: ag.ArgType, - Must: ag.Must, - Unit: ag.Unit, - Range: ag.Range, - DefaultValue: ag.DefaultValue, - DefaultOperator: ag.DefaultOperator, - Sort: ag.Sort, - } - srv.Args = append(srv.Args, sra) + paramBody := util.Struct2Map(srv) + logger.Debug("sdkApi.Register paramBody:", paramBody) + sdkApi.Register(paramBody) //灏嗙畻娉曟敞鍐屽埌鏁版嵁搴撲腑 } - paramBody := util.Struct2Map(srv) - logger.Debug("sdkApi.Register paramBody:", paramBody) - sdkApi.Register(paramBody) //灏嗙畻娉曟敞鍐屽埌鏁版嵁搴撲腑 } - zconfPath := "/opt/vasystem/bin/zconf/" - libPath := "/opt/vasystem/libs/" + zconfPath := "./zconf/" + libPath := "../libs/" if !util.DirExists(zconfPath) { os.MkdirAll(zconfPath, 0777) } @@ -167,47 +340,105 @@ os.MkdirAll(libPath, 0777) } for sdkType,_ := range soM { - //鍏堣В鍘婼dkType.tar.gz鏂囦欢 - if unTarGzE := util.UnTarGz(unPackPath+sdkType+".tar.gz", unPackPath);unTarGzE == nil { - //澶嶅埗json鍚姩鏂囦欢 - if util.Exists(unPackPath+sdkType+"/zconf/"+sdkType+".json") { - util.CopyFile(unPackPath+sdkType+"/zconf/"+sdkType+".json", zconfPath+sdkType+".json") - } - if util.DirExists(unPackPath+sdkType) { - if _,cE := util.CopyDirByCmd(unPackPath+sdkType, libPath);cE != nil { - return false, cE + if _,iOk := nInsM[sdkType];iOk { + //鍏堣В鍘婼dkType.tar.gz鏂囦欢 + if unTarGzE := util.UnTarGz(unPackPath+sdkType+".tar.gz", unPackPath);unTarGzE == nil { + //澶嶅埗json鍚姩鏂囦欢 + if util.Exists(unPackPath+sdkType+"/zconf/"+sdkType+".json") { + util.CopyFile(unPackPath+sdkType+"/zconf/"+sdkType+".json", zconfPath+sdkType+".json") } - } - if util.DirExists(unPackPath+sdkType+"/models") { - if _,cE := util.CopyDirByCmd(unPackPath+sdkType+"/models", "/opt/vasystem/bin/zconf"); cE != nil { - return false, cE + if util.DirExists(unPackPath+sdkType) { + if _,cE := util.CopyDirByCmd(unPackPath+sdkType, libPath);cE != nil { + return false, cE + } } + if util.DirExists(unPackPath+sdkType+"/models") { + if _,cE := util.CopyDirByCmd(unPackPath+sdkType+"/models", "./"); cE != nil { + return false, cE + } + //鎷疯礉瀹屾垚鍚庡垹闄ibs涓媠dkType鏂囦欢澶逛笅models + } + if util.DirExists(libPath+sdkType+"/models") { + os.RemoveAll(libPath+sdkType+"/models") + } + if util.DirExists(libPath+sdkType+"/zconf") { + os.RemoveAll(libPath+sdkType+"/zconf") + } + } else { + logger.Debug("unTarGzE sdkType.tar.gz err:", unTarGzE) } } else { - logger.Debug("unTarGzE sdkType.tar.gz err:", unTarGzE) + logger.Debug("upper version has been installed") } } - } else { - return false, errors.New("鍙嶅簭鍒楀寲绠楁硶瀹氫箟淇℃伅澶辫触") + logger.Debug("鍙嶅簭鍒楀寲绠楁硶瀹氫箟淇℃伅澶辫触") + return false, errors.New("閿欒鐨勫畨瑁呭寘") } } else { - return false, errors.New("璇诲彇绠楁硶瀹氫箟淇℃伅澶辫触") + logger.Debug("璇诲彇绠楁硶瀹氫箟淇℃伅澶辫触") + return false, errors.New("閿欒鐨勫畨瑁呭寘") } } else { - return false, errors.New("绠楁硶瀹氫箟淇℃伅涓㈠け") + logger.Debug("绠楁硶瀹氫箟淇℃伅涓㈠け") + return false, errors.New("閿欒鐨勫畨瑁呭寘") } } else { - return false, errors.New("鍙嶅簭鍒楀寲鎺堟潈淇℃伅澶辫触") + logger.Debug("鍙嶅簭鍒楀寲鎺堟潈淇℃伅澶辫触") + return false, errors.New("閿欒鐨勫畨瑁呭寘") } } else { - return false, errors.New("璇诲彇鎺堟潈鏂囦欢澶辫触") + logger.Debug("璇诲彇鎺堟潈鏂囦欢澶辫触") + return false, errors.New("閿欒鐨勫畨瑁呭寘") } } else { - return false, errors.New("鎺堟潈鏂囦欢涓嶅瓨鍦�") + return false, errors.New("閿欒鐨勫畨瑁呭寘") } return true, nil +} + +func shouldVersionBeUpgrade(curVersion,dstVersion string) bool { + if curVersion == "" { + return true + } + if dstVersion == "" { + return false + } + curIVArr := strings.Split(curVersion, ".") + dstIVArr := strings.Split(dstVersion, ".") + if len(curIVArr) !=3 || len(dstIVArr) != 3{ + return false + } + cH,cEH := strconv.Atoi(curIVArr[0]) + cM,cEM := strconv.Atoi(curIVArr[1]) + cL,cEL := strconv.Atoi(curIVArr[2]) + + dH,dEH := strconv.Atoi(dstIVArr[0]) + dM,dEM := strconv.Atoi(dstIVArr[1]) + dL,dEL := strconv.Atoi(dstIVArr[2]) + if cEH !=nil || cEM != nil || cEL != nil || dEH != nil ||dEM !=nil || dEL !=nil { + return false + } + if cH > dH { + return false + } else if cH == dH { + if cM > dM { + return false + } else if cM == dM { + if cL > dL { + return false + } else if cL == dL { + return false + } else { + return true + } + } else { + return true + } + } else { + return true + } } @@ -222,6 +453,10 @@ ChCount int `json:"chCount"` AuthCount int `json:"authCount"` ServeYear int `json:"serveYear"` + + ProductName string `json:"productName"` + InstallVersion string `json:"installVersion"` + InstallContent string `json:"installContent"` } //绠楁硶鍜屽弬鏁板畾涔� @@ -231,18 +466,48 @@ } type Sdk struct { - Id string `gorm:"column:id;primary_key;type:varchar(50);unique;not null;" json:"id"` - IpcId string `gorm:"column:ipc_id" json:"ipc_id"` - SdkType string `gorm:"column:sdk_type" json:"sdk_type"` - SdkName string `gorm:"column:sdk_name" json:"sdk_name" example:"浜鸿劯妫�娴�"` - Icon string `gorm:"column:icon" json:"icon,omitempty"` - Url string `gorm:"column:url" json:"url,omitempty" example:"http://ip:port/govideo/sdk/1"` + SdkCommon CreateTime string `gorm:"column:create_time" json:"create_time"` CreateBy string `gorm:"column:create_by" json:"create_by"` UpdateTime string `gorm:"column:update_time" json:"update_time"` Enable bool `gorm:"column:enable;default:1" json:"enable"` DelFlag int `gorm:"column:del_flag;default:0" json:"del_flag"` - Env string `gorm:"column:env" json:"env"` //杩愯鐜鍙婄粨鏋滆鏄庯紝json鏍煎紡锛屽寘鍚玸o_file_path,runtime,param,depends(cuda鐗堟湰锛宱pencv鐗堟湰锛宼ensorflow鐗堟湰绛�) + + Args []SdkArg `json:"args"` +} + +func (sc *SdkCommon) copyFromProto(pSdk protomsg.Sdk) { + sc.Id = pSdk.Id + sc.IpcId = pSdk.IpcId + sc.SdkType = pSdk.SdkType + sc.SdkName = pSdk.SdkName + sc.Icon = pSdk.Icon + sc.Url = pSdk.Url + sc.IconBlob = pSdk.IconBlob + sc.Version = pSdk.Version + sc.ArgDef = pSdk.ArgDef + sc.RuleSo = pSdk.RuleSo +} + +type SdkCommon struct { + Id string `json:"id"` + IpcId string `json:"ipc_id"` + SdkType string `json:"sdk_type"`//浜鸿劯妫�娴嬶細FaceDetect,浜鸿劯鎻愬彇锛欶aceExtract,浜鸿劯姣斿锛欶aceCompare,琛屼负锛歒olo + SdkName string `json:"sdk_name"` //绠楁硶鍚嶇О + Icon string `json:"icon"` //绠楁硶鍥炬爣 + Url string `json:"url"` //绠楁硶涓嬭浇鍦板潃 + IconBlob string `json:"iconBlob"` //鍥剧墖 + Version string `json:"version"` //鐗堟湰鍙� + EnTrack bool `json:"enTrack"` //鏄惁寮�鍚窡韪� + ArgDef string `json:"argDef"` //绠楁硶榛樿鍙傛暟瀹氫箟 + RuleSo string `json:"rule_so"` +} + +type SdkInsOrUpgrade struct { + Sdk + RemoteVersion string `json:"remoteVersion"` //鍟嗗煄浠撳簱鐗堟湰鍙� + Installed bool `json:"installed"` //鏄惁宸插畨瑁� + IsUpgrade bool `json:"isUpgrade"` //鏄惁闇�瑕佸崌绾� } type SdkArgEntity struct { @@ -267,23 +532,19 @@ type SdkRegisterVo struct { - Id string `json:"id"` - SdkType string `json:"sdk_type"`//浜鸿劯妫�娴嬶細FaceDetect,浜鸿劯鎻愬彇锛欶aceExtract,浜鸿劯姣斿锛欶aceCompare,琛屼负锛歒olo - SdkName string `json:"sdk_name"` //绠楁硶鍚嶇О - Args []SdkRegisterArgVo `json:"args"` //绠楁硶鍙傛暟 - Icon string `json:"icon"` //绠楁硶鍥炬爣 - Url string `json:"url"` //绠楁硶涓嬭浇鍦板潃 + SdkCommon + Args []SdkRegisterArgVo `json:"args"` //绠楁硶鍙傛暟 } type SdkRegisterArgVo struct { - Scope string `json:"scope"` + Scope string `json:"scope"` SdkArg - Dics []SdkArgDic `json:"dics"` //濡傛灉姝ょ畻娉曞弬鏁版槸琚�夐」锛岄渶瑕佸皢鍙傛暟鏋氫妇鍊煎啓鍏ュ埌瀛楀吀琛ㄤ腑 + Dics []SdkArgDic `json:"dics"` //濡傛灉姝ょ畻娉曞弬鏁版槸琚�夐」锛岄渶瑕佸皢鍙傛暟鏋氫妇鍊煎啓鍏ュ埌瀛楀吀琛ㄤ腑 } type SdkArgDic struct { - Value string `json:"value"` - Name string `json:"name"` - Sort int `json:"sort"` + Value string `json:"value"` + Name string `json:"name"` + Sort int `json:"sort"` } \ No newline at end of file -- Gitblit v1.8.0