liuxiaolong
2020-06-05 7c811247ecf143e08c576986a884bedadc57dd66
service/SysService.go
@@ -9,6 +9,7 @@
   "mime/multipart"
   "os"
   "os/exec"
   "path"
   "strings"
   "webserver/extend/config"
   "webserver/extend/util"
@@ -76,7 +77,7 @@
      dirFiles, _ := ioutil.ReadDir(fileTmpPath)
      if dirFiles != nil && len(dirFiles) == arg.TotalChunks {
         //表示所有分块都上传了,需要merge
         if !sv.MergeChunks(fileTmpPath, mergedFilePath) {
         if !MergeChunks(fileTmpPath, mergedFilePath) {
            return false
         }
      }
@@ -84,7 +85,7 @@
   return true
}
func (sv SysService) PatchUpload(arg *FileUploadVo) bool {
func (sv SysService) PatchUpload(arg *FileUploadVo) (bool,bool) {
   configPatchPath := ""
   if config.Server.PatchPath != "" {
      configPatchPath = config.Server.PatchPath
@@ -93,19 +94,17 @@
   }
   defer (*arg.File).Close()
   if !util.CreateDirectory(configPatchPath) {
      return false
      return false, false
   }
   index := strings.LastIndex(arg.Header.Filename, ".")
   if index < 0 {
      return false
   }
   subfix := arg.Header.Filename[index:]
   filenameWithSuffix := path.Base(arg.Header.Filename)
   subfix := path.Ext(filenameWithSuffix)
   MD5Str := arg.Identifier
   logger.Debug("Identifier:",MD5Str)
   fileTmpPath := configPatchPath + "/"+MD5Str
   if !util.Exists(fileTmpPath) {
      if !util.CreateDirectory(fileTmpPath) {
         return false
         return false, false
      }
   }
   chunkAlignNum := util.FormatNum(arg.TotalChunks, arg.ChunkNumber)
@@ -114,13 +113,13 @@
      rmErr := os.Remove(fileSavePath)
      if rmErr != nil {
         logger.Debug("rmErr:",rmErr)
         return false
         return false, false
      }
   }
   file, e := os.Create(fileSavePath)
   if e !=nil {
      logger.Debug("os.Create err:",e,"fileSavePath:",fileSavePath)
      return false
      return false, false
   }
   defer file.Close()
   writer := bufio.NewWriter(file)
@@ -128,28 +127,29 @@
   n, err := (*arg.File).ReadAt(chunkData, 0)
   if n ==0 || err !=nil {
      logger.Debug("read chunkData err:",err,"n:",n)
      return false
      return false, false
   }
   logger.Debug("read currentChunkData len:",n,"header data len:",arg.Header.Size)
   nn, err2 := writer.Write(chunkData)
   if nn ==0 || err2 !=nil {
      logger.Debug("write chunkData err:",err2,"nn:",nn)
      return false
      return false, false
   }
   logger.Debug("write chunkData len:",nn)
   if err = writer.Flush(); err != nil {
      logger.Debug("write flush err:",err)
   }
   isComplete := false
   dirFiles, _ := ioutil.ReadDir(fileTmpPath)
   if dirFiles != nil && len(dirFiles) == arg.TotalChunks {
      isComplete = true
   }
   if isComplete {
      if sv.MergeChunks(fileTmpPath,fileTmpPath + subfix) {
      if MergeChunks(fileTmpPath,fileTmpPath + subfix) {
         logger.Debug("merge all chunks success,identifier:",MD5Str,"fileName:",arg.FileName)
      } else {
         return false
         return false, isComplete
      }
   }
   return true
   return true, isComplete
}
//upgrade
@@ -163,11 +163,10 @@
   } else {
      configPatchPath = "/opt/vasystem/patch"
   }
   index := strings.LastIndex(filename, ".")
   if index < 0 {
      return false,errors.New("非法的升级压缩包文件")
   }
   ext := filename[index:]
   filenameWithSuffix := path.Base(filename)
   ext := path.Ext(filenameWithSuffix)
   zipFilePath := configPatchPath + "/"+identifier+ext
   if util.Exists(zipFilePath) {
      //校验md5
@@ -176,15 +175,11 @@
         return false,errors.New("获取升级压缩包md5失败")
      }
      if strMd5 == identifier {
         if util.ZipCheck(zipFilePath) {
            if !updatePatch(identifier, ext) {
               return false,errors.New("执行升级过程异常")
            }
            return true,nil
         } else {
            logger.Debug("not a valid zip file,path:",zipFilePath)
            return false,errors.New("升级程序解压失败,请确定上传的补丁是zip格式")
         if !updatePatch(identifier, ext) {
            return false,errors.New("执行升级过程异常,请确定上传的补丁是tar.gz格式")
         }
         return true,nil
      } else {
         logger.Debug("strMd5 is", strMd5,"identifier is",identifier,"not equal")
         return false,errors.New("校验升级文件失败")
@@ -227,28 +222,28 @@
      configPatchPath = "/opt/vasystem/patch"
   }
   //1.解压缩更新包
   unZipPath := configPatchPath+"/"+identifier+"_basic/"
   if util.Exists(unZipPath) {
   unPackPath := configPatchPath+"/"+identifier+"_basic/"
   if util.Exists(unPackPath) {
      //此版本已经更新过
      rmErr := os.RemoveAll(unZipPath)
      rmErr := os.RemoveAll(unPackPath)
      if rmErr !=nil {
         return false
      }
   }
   if !util.CreateDirectory(unZipPath) {
   if !util.CreateDirectory(unPackPath) {
      return false
   }
   zipFilePath := configPatchPath+"/"+identifier+ext
   err := util.UnZip(zipFilePath, unZipPath)
   unPackFilePath := configPatchPath+"/"+identifier+ext
   err := util.UnTarGz(unPackFilePath, unPackPath)
   if err !=nil {
      logger.Debug("UnZip err:",err,"zipFile:",zipFilePath)
      logger.Debug("UnPack err:",err,"unPackFile:",unPackFilePath)
      return false
   }
   //如果通用脚本有更新,则更新通用脚本
   if util.Exists(unZipPath+"updatePatch.sh") {
      cpStr := fmt.Sprintf("cp %s /opt/vasystem/bin",unZipPath+"updatePatch.sh")
   if util.Exists(unPackPath+"updatePatch.sh") {
      cpStr := fmt.Sprintf("cp %s /opt/vasystem/bin",unPackPath+"updatePatch.sh")
      b, err := ExecCmd(cpStr)
      if err != nil {
         logger.Debug("cp updatePatch.sh to bin err:",err,"result:",string(b))
@@ -257,9 +252,9 @@
   }
   //判断更新包里是否有补丁脚本,如果有则执行,否则执行updatePatch.sh
   updateCmd := fmt.Sprintf("./updatePatch.sh %s %s %s &",unZipPath,zipFilePath,configPatchPath+"/"+identifier)
   if util.Exists(unZipPath+"upgrade.sh") {
      updateCmd = fmt.Sprintf("%supgrade.sh %s %s %s &",unZipPath,unZipPath,zipFilePath,configPatchPath+"/"+identifier)
   updateCmd := fmt.Sprintf("./updatePatch.sh %s %s %s &",unPackPath,unPackFilePath,configPatchPath+"/"+identifier)
   if util.Exists(unPackPath+"upgrade.sh") {
      updateCmd = fmt.Sprintf("%supgrade.sh %s %s %s &",unPackPath,unPackPath,unPackFilePath,configPatchPath+"/"+identifier)
   }
   //2.更新系统
   b,err := ExecCmd(updateCmd)
@@ -272,7 +267,7 @@
   return true
}
func (sv SysService) MergeChunks(chunkPath string, storePath string) bool {
func MergeChunks(chunkPath string, storePath string) bool {
   var cmd *exec.Cmd
   cmd = exec.Command("/bin/sh", "-c", fmt.Sprintf("./mergeAll.sh %s %s", chunkPath, storePath))
   if b, err := cmd.Output(); err != nil {