liuxiaolong
2019-12-20 e0f0035a7d02098f14e9d800a16d42d4c9380e9c
service/SysService.go
@@ -1,13 +1,14 @@
package service
import (
   "basic.com/valib/logger.git"
   "bufio"
   "errors"
   "fmt"
   "io/ioutil"
   "mime/multipart"
   "os"
   "os/exec"
   "strconv"
   "strings"
   "webserver/extend/config"
   "webserver/extend/util"
@@ -66,7 +67,8 @@
      return true
   }
   //判断分块文件是否存在
   chunkFilePath := fileTmpPath+"/"+arg.Identifier+"_"+strconv.Itoa(arg.ChunkNumber)
   chunkAlignNum := util.FormatNum(arg.TotalChunks, arg.ChunkNumber)
   chunkFilePath := fileTmpPath+"/"+arg.Identifier+"_"+chunkAlignNum
   if !util.Exists(chunkFilePath) {
      return false
   }
@@ -74,12 +76,8 @@
      dirFiles, _ := ioutil.ReadDir(fileTmpPath)
      if dirFiles != nil && len(dirFiles) == arg.TotalChunks {
         //表示所有分块都上传了,需要merge
         if sv.MergeChunks(fileTmpPath, mergedFilePath) {
            if util.ZipCheck(mergedFilePath) {
               if !updatePatch(arg.Identifier, subfix) {
                  return false
               }
            }
         if !sv.MergeChunks(fileTmpPath, mergedFilePath) {
            return false
         }
      }
   }
@@ -110,7 +108,8 @@
         return false
      }
   }
   fileSavePath := fileTmpPath+"/"+MD5Str+"_"+strconv.Itoa(arg.ChunkNumber)
   chunkAlignNum := util.FormatNum(arg.TotalChunks, arg.ChunkNumber)
   fileSavePath := fileTmpPath+"/"+MD5Str+"_"+chunkAlignNum
   if util.Exists(fileSavePath) {
      rmErr := os.Remove(fileSavePath)
      if rmErr != nil {
@@ -125,7 +124,7 @@
   }
   defer file.Close()
   writer := bufio.NewWriter(file)
   chunkData := make([]byte, arg.Header.Size)
   chunkData := make([]byte, arg.CurrentChunkSize)
   n, err := (*arg.File).ReadAt(chunkData, 0)
   if n ==0 || err !=nil {
      logger.Debug("read chunkData err:",err,"n:",n)
@@ -144,12 +143,75 @@
   if isComplete {
      if sv.MergeChunks(fileTmpPath,fileTmpPath + subfix) {
         logger.Debug("merge all chunks success,identifier:",MD5Str,"fileName:",arg.FileName)
         if util.ZipCheck(fileTmpPath + subfix) {
            if !updatePatch(arg.Identifier, subfix) {
               return false
            }
         }
      } else {
         return false
      }
   }
   return true
}
//upgrade
func (sv SysService) Upgrade(identifier string,filename string) (bool,error) {
   if !bakBeforeUpgrade() {
      return false,errors.New("更新前备份失败")
   }
   configPatchPath := ""
   if config.Server.PatchPath != "" {
      configPatchPath = config.Server.PatchPath
   } else {
      configPatchPath = "/opt/vasystem/patch"
   }
   index := strings.LastIndex(filename, ".")
   if index < 0 {
      return false,errors.New("非法的升级压缩包文件")
   }
   ext := filename[index:]
   zipFilePath := configPatchPath + "/"+identifier+ext
   if util.Exists(zipFilePath) {
      //校验md5
      strMd5, e := util.FileMd5(zipFilePath)
      if e !=nil || strMd5 == "" {
         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格式")
         }
      } else {
         logger.Debug("strMd5 is", strMd5,"identifier is",identifier,"not equal")
         return false,errors.New("校验升级文件失败")
      }
   } else {
      return false,errors.New("升级文件已丢失,请重新上传")
   }
}
func bakBeforeUpgrade() bool {
   configBakPath := ""
   if config.Server.BakPath != "" {
      configBakPath = config.Server.BakPath
   } else {
      configBakPath = "/opt/vasystem/bak"
   }
   if util.Exists(configBakPath) {
      //只保留最新的版本
      if err := os.RemoveAll(configBakPath);err != nil {
         return false
      }
   }
   if !util.CreateDirectory(configBakPath) {
      return false
   }
   b, err := ExecCmd("cp -r /opt/vasystem/bin /opt/vasystem/bak")
   if err != nil {
      logger.Debug("bakBeforeUpgrade result:",string(b),"err:",err)
      return false
   }
   return true
}
@@ -166,24 +228,44 @@
   unZipPath := configPatchPath+"/"+identifier+"_basic/"
   if util.Exists(unZipPath) {
      //此版本已经更新过
      return true
   } else {
      if !util.CreateDirectory(unZipPath) {
      rmErr := os.RemoveAll(unZipPath)
      if rmErr !=nil {
         return false
      }
   }
   err := util.UnZip(configPatchPath+"/"+identifier+ext, unZipPath)
   if err !=nil {
      logger.Debug("UnZip err:",err,"zipFile:",configPatchPath+"/"+identifier+ext)
   if !util.CreateDirectory(unZipPath) {
      return false
   }
   //2.更新系统
   var cmd *exec.Cmd
   cmd = exec.Command("/bin/sh","-c",fmt.Sprintf("./updatePatch.sh %s",unZipPath))
   logger.Debug("called sh updatePatch.sh ", unZipPath)
   if b, err := cmd.Output(); err != nil {
      logger.Debug("updatePatch err:",err,"result:",string(b))
   zipFilePath := configPatchPath+"/"+identifier+ext
   err := util.UnZip(zipFilePath, unZipPath)
   if err !=nil {
      logger.Debug("UnZip err:",err,"zipFile:",zipFilePath)
      return false
   }
   //如果通用脚本有更新,则更新通用脚本
   if util.Exists(unZipPath+"updatePatch.sh") {
      cpStr := fmt.Sprintf("cp %s /opt/vasystem/bin",unZipPath+"updatePatch.sh")
      b, err := ExecCmd(cpStr)
      if err != nil {
         logger.Debug("cp updatePatch.sh to bin err:",err,"result:",string(b))
         return false
      }
   }
   //判断更新包里是否有补丁脚本,如果有则执行,否则执行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)
   }
   //2.更新系统
   b,err := ExecCmd(updateCmd)
   if err != nil {
      logger.Debug("upgrade err:",err,"result:",string(b),"cmd:",updateCmd)
      return false
   } else {
      logger.Debug("upgrade result:",string(b),"cmd:",updateCmd)
   }
   return true
}
@@ -191,11 +273,17 @@
func (sv SysService) MergeChunks(chunkPath string, storePath string) bool {
   var cmd *exec.Cmd
   cmd = exec.Command("/bin/sh", "-c", fmt.Sprintf("./mergeAll.sh %s %s", chunkPath, storePath))
   logger.Debug("called sh mergeAll.sh ", chunkPath, storePath)
   if b, err := cmd.Output(); err != nil {
      logger.Debug("mergeChunks err:", err, "result:", string(b))
      return false
   } else {
      logger.Debug("mergeChunks result:",string(b),"cmd: ./mergeAll.sh ", chunkPath, storePath)
      return true
   }
}
func ExecCmd(cmdStr string) ([]byte,error) {
   var cmd *exec.Cmd
   cmd = exec.Command("/bin/sh", "-c",cmdStr)
   return cmd.Output()
}