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

---
 service/SysService.go |   56 +++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 49 insertions(+), 7 deletions(-)

diff --git a/service/SysService.go b/service/SysService.go
index 5ff8a95..4b14658 100644
--- a/service/SysService.go
+++ b/service/SysService.go
@@ -124,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)
@@ -152,6 +152,9 @@
 
 //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
@@ -189,6 +192,30 @@
 	}
 }
 
+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
+}
+
 //鏇存柊绯荤粺绋嬪簭
 func updatePatch(identifier string, ext string) bool {
 	configPatchPath := ""
@@ -216,20 +243,29 @@
 		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)
-		logger.Debug("upgrade use ",unZipPath,"upgrade.sh")
 	}
 	//2.鏇存柊绯荤粺
-	var cmd *exec.Cmd
-	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
 }
@@ -244,4 +280,10 @@
 		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