From c2f8c24f1ddb67f890f9ac4af5328e65a46aab75 Mon Sep 17 00:00:00 2001
From: liuxiaolong <736321739@qq.com>
Date: 星期五, 20 十二月 2019 16:35:46 +0800
Subject: [PATCH] test

---
 service/SysService.go |  136 ++++++++++++++++++++++++++++++++++++--------
 1 files changed, 110 insertions(+), 26 deletions(-)

diff --git a/service/SysService.go b/service/SysService.go
index 5de188c..92957eb 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
 			}
 		}
 	}
@@ -133,11 +130,13 @@
 		logger.Debug("read chunkData err:",err,"n:",n)
 		return false
 	}
+	logger.Debug("read currentChunkData len:",n,"header data len:",arg.Header.Size,"data:",string(chunkData))
 	nn, err2 := writer.Write(chunkData)
 	if nn ==0 || err2 !=nil {
 		logger.Debug("write chunkData err:",err2,"nn:",nn)
 		return false
 	}
+	logger.Debug("write chunkData len:",nn)
 	isComplete := false
 	dirFiles, _ := ioutil.ReadDir(fileTmpPath)
 	if dirFiles != nil && len(dirFiles) == arg.TotalChunks {
@@ -146,14 +145,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 {
-				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) {
+	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("鑾峰彇鍗囩骇鍘嬬缉鍖卪d5澶辫触")
+		}
+		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("鍗囩骇绋嬪簭瑙e帇澶辫触锛岃纭畾涓婁紶鐨勮ˉ涓佹槸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
 }
@@ -170,26 +230,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
 	}
+
+	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
+		}
+	}
+
+	//鍒ゆ柇鏇存柊鍖呴噷鏄惁鏈夎ˉ涓佽剼鏈紝濡傛灉鏈夊垯鎵ц锛屽惁鍒欐墽琛寀pdatePatch.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.鏇存柊绯荤粺
-	var cmd *exec.Cmd
-	updateCmd := fmt.Sprintf("setsid ./updatePatch.sh %s %s %s",unZipPath,configPatchPath+"/"+identifier+ext,configPatchPath+"/"+identifier)
-	cmd = exec.Command("/bin/sh","-c", updateCmd)
-	if b, err := cmd.Output(); err != nil {
-		logger.Debug("updatePatch err:",err,"result:",string(b))
+	b,err := ExecCmd(updateCmd)
+	if err != nil {
+		logger.Debug("upgrade err:",err,"result:",string(b),"cmd:",updateCmd)
 		return false
 	} else {
-		logger.Debug("updatePatch result:",string(b),"cmd:",updateCmd)
+		logger.Debug("upgrade result:",string(b),"cmd:",updateCmd)
 	}
 	return true
 }
@@ -201,7 +279,13 @@
 		logger.Debug("mergeChunks err:", err, "result:", string(b))
 		return false
 	} else {
-		logger.Debug("mergeChunks success,cmd: mergeAll.sh ", chunkPath, storePath)
+		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()
 }
\ No newline at end of file

--
Gitblit v1.8.0