liuxiaolong
2020-07-13 c825ce5df1b3441f9ff74214b10a6d222a7c7526
service/SdkInstall.go
@@ -2,6 +2,7 @@
import (
   "basic.com/dbapi.git"
   "basic.com/valib/licence.git"
   "basic.com/valib/logger.git"
   "encoding/json"
   "errors"
@@ -13,10 +14,44 @@
   "strings"
   "webserver/extend/config"
   "webserver/extend/util"
   reqUtil "webserver/extend/util"
)
type SdkInstallService struct {
}
func (sv SdkInstallService) Active(cod string) error {
   //使用激活码激活算法
   url := "http://"+config.ShopConf.Url+"/data/api-s/sdk/activeByAINode"
   machineCode := licence.GetMachineCode()
   if machineCode == "" {
      logger.Debug("获取机器码失败")
      return errors.New("获取机器码失败")
   }
   paramBody := map[string]interface{}{
      "code": cod,
      "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 err
   }
   var res dbapi.Result
   if err = json.Unmarshal(respBody, &res); err != nil {
      logger.Debug("unmarshal err:", err)
      return err
   }
   if !res.Success {
      logger.Debug("res.Data:", res.Data)
      return errors.New("请求商城失败")
   }
   return nil
}
//算法安装包安装
@@ -33,12 +68,16 @@
   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("获取安装包md5失败")
      }
@@ -52,6 +91,9 @@
         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("校验安装文件失败")
@@ -72,12 +114,16 @@
   //1.解压缩更新包
   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)
      }
   }()
@@ -175,6 +221,7 @@
                              Url: skd.Def.Url,
                              IconBlob: skd.Def.IconBlob,
                              Version: skd.Def.Version,
                              ArgDef: skd.Def.ArgDef,
                           }
                           for _,ag := range skd.Args {
                              sra := SdkRegisterArgVo{
@@ -221,9 +268,16 @@
                                 }
                              }
                              if util.DirExists(unPackPath+sdkType+"/models") {
                                 if _,cE := util.CopyDirByCmd(unPackPath+sdkType+"/models", "./zconf"); cE != nil {
                                 if _,cE := util.CopyDirByCmd(unPackPath+sdkType+"/models", "./"); cE != nil {
                                    return false, cE
                                 }
                                 //拷贝完成后删除libs下sdkType文件夹下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)
@@ -271,9 +325,9 @@
   cM,cEM := strconv.Atoi(curIVArr[1])
   cL,cEL := strconv.Atoi(curIVArr[2])
   dH,dEH := strconv.Atoi(curIVArr[0])
   dM,dEM := strconv.Atoi(curIVArr[1])
   dL,dEL := 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
   }
@@ -333,6 +387,8 @@
   Env          string       `gorm:"column:env" json:"env"` //运行环境及结果说明,json格式,包含so_file_path,runtime,param,depends(cuda版本,opencv版本,tensorflow版本等)
   IconBlob       string       `gorm:"column:iconBlob;type:text;" json:"iconBlob"` //图片
   Version       string       `gorm:"column:version;type:varchar(50);default:'1.0';" json:"version"` //版本号
   Args          []SdkArg    `json:"args"`
   ArgDef          string       `gorm:"column:argDef" json:"argDef"` //算法参数默认配置
}
type SdkInsOrUpgrade struct {
@@ -372,6 +428,7 @@
   Url           string             `json:"url"`        //算法下载地址
   IconBlob       string             `json:"iconBlob"` //图片
   Version       string             `json:"version"` //版本号
   ArgDef          string             `json:"argDef"`
}
type SdkRegisterArgVo struct {