From 94bcda3ef533ac0ab0f5c0786554a9efe4c27f4d Mon Sep 17 00:00:00 2001
From: liuxiaolong <736321739@qq.com>
Date: 星期四, 19 十二月 2019 13:37:13 +0800
Subject: [PATCH] add upgrade action,do nothing after patch upload

---
 controllers/syssetcont.go |   27 +++++++++++++
 extend/code/code.go       |    3 +
 extend/util/util.go       |   18 +++++++++
 service/SysService.go     |   57 ++++++++++++++++++++++------
 router/router.go          |    1 
 5 files changed, 93 insertions(+), 13 deletions(-)

diff --git a/controllers/syssetcont.go b/controllers/syssetcont.go
index 2dd3ccd..bdd04a8 100644
--- a/controllers/syssetcont.go
+++ b/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())
+	}
 }
\ No newline at end of file
diff --git a/extend/code/code.go b/extend/code/code.go
index 3ef6457..4e7ef5a 100644
--- a/extend/code/code.go
+++ b/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, "鍗囩骇澶辫触"}
 )
diff --git a/extend/util/util.go b/extend/util/util.go
index 224d086..89fc165 100644
--- a/extend/util/util.go
+++ b/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
 }
\ No newline at end of file
diff --git a/router/router.go b/router/router.go
index 0d0d123..05180ce 100644
--- a/router/router.go
+++ b/router/router.go
@@ -233,6 +233,7 @@
 
 		vsset.GET("/patchUpdate", ssController.PatchUpdateCheck)
 		vsset.POST("/patchUpdate", ssController.PatchUpdate)
+		vsset.POST("/upgrade", ssController.Upgrade)
 	}
 
 	//绠楁硶搴撴搷浣�
diff --git a/service/SysService.go b/service/SysService.go
index 4d6f5ea..b4a6bc6 100644
--- a/service/SysService.go
+++ b/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 {
 			//琛ㄧず鎵�鏈夊垎鍧楅兘涓婁紶浜嗭紝闇�瑕乵erge
-			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("鑾峰彇鍗囩骇鍘嬬缉鍖卪d5澶辫触")
+		}
+		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("鍗囩骇绋嬪簭瑙e帇澶辫触锛岃纭畾涓婁紶鐨勮ˉ涓佹槸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 := ""

--
Gitblit v1.8.0