liuxiaolong
2019-12-19 94bcda3ef533ac0ab0f5c0786554a9efe4c27f4d
add upgrade action,do nothing after patch upload
5个文件已修改
106 ■■■■ 已修改文件
controllers/syssetcont.go 27 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
extend/code/code.go 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
extend/util/util.go 18 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
router/router.go 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/SysService.go 57 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
controllers/syssetcont.go
@@ -543,4 +543,31 @@
    } else {
        util.ResponseFormat(c, code.ComError, "")
    }
}
// @Security ApiKeyAuth
// @Summary 开始升级
// @Description 开始升级
// @Accept multipart/form-data
// @Produce json
// @Tags sysset
// @Param identifier formData string true "整个文件的唯一标识,目前是md5"
// @Param filename formData string true "文件名称"
// @Success 200 {string} json "{"code":200, msg:"", success:true}"
// @Failure 500 {string} json "{"code":500, msg:"", success:false}"
// @Router /data/api-v/sysset/upgrade [post]
func (sset SysSetController) Upgrade(c *gin.Context) {
    identifier := c.Request.FormValue("identifier")
    filename := c.Request.FormValue("filename")
    if identifier == "" || filename == "" {
        util.ResponseFormat(c,code.RequestParamError,"")
        return
    }
    var sv service.SysService
    if b,err := sv.Upgrade(identifier, filename);b {
        util.ResponseFormat(c,code.UpgradeSuccess,"升级成功")
    } else {
        util.ResponseFormat(c,code.UpgradeFail,err.Error())
    }
}
extend/code/code.go
@@ -65,4 +65,7 @@
    CreateFirstNodeErr  = &Code{http.StatusInternalServerError, false, "创建节点失败!"}
    QueryClusterInfoErr = &Code{http.StatusInternalServerError, false, "查询失败,请确认您的ip是正确的!"}
    AddClusterInfoErr   = &Code{http.StatusInternalServerError, false, "加入节点失败!"}
    UpgradeSuccess = &Code{http.StatusOK, true, "升级成功"}
    UpgradeFail = &Code{http.StatusInternalServerError, false, "升级失败"}
)
extend/util/util.go
@@ -3,6 +3,8 @@
import (
    "archive/zip"
    "bytes"
    "crypto/md5"
    "encoding/hex"
    "encoding/json"
    "image"
    "io"
@@ -309,4 +311,20 @@
    }
    fmtStr := "%0"+strconv.Itoa(m)+"d"
    return fmt.Sprintf(fmtStr, n)
}
func FileMd5(path string) (string,error){
    file, err := os.Open(path)
    if err !=nil {
        return "",err
    }
    defer file.Close()
    _md5 := md5.New()
    if _,err := io.Copy(_md5, file);err != nil {
        return "",err
    }
    return hex.EncodeToString(_md5.Sum(nil)),nil
}
router/router.go
@@ -233,6 +233,7 @@
        vsset.GET("/patchUpdate", ssController.PatchUpdateCheck)
        vsset.POST("/patchUpdate", ssController.PatchUpdate)
        vsset.POST("/upgrade", ssController.Upgrade)
    }
    //算法库操作
service/SysService.go
@@ -3,6 +3,7 @@
import (
    "basic.com/valib/logger.git"
    "bufio"
    "errors"
    "fmt"
    "io/ioutil"
    "mime/multipart"
@@ -75,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
            }
        }
    }
@@ -146,18 +143,52 @@
    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 {
                logger.Debug("not a valid zip file,path:",fileTmpPath+subfix)
            }
        } else {
            return false
        }
    }
    return true
}
//upgrade
func (sv SysService) Upgrade(identifier string,filename string) (bool,error) {
    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 + ext) {
                if !updatePatch(identifier, ext) {
                    return false,errors.New("执行升级过程异常")
                }
                return true,nil
            } else {
                logger.Debug("not a valid zip file,path:",zipFilePath+ext)
                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 updatePatch(identifier string, ext string) bool {
    configPatchPath := ""