From a789f7bd273fb5659c0583c45b2e9cdb7102f1d3 Mon Sep 17 00:00:00 2001
From: liuxiaolong <736321739@qq.com>
Date: 星期四, 19 十二月 2019 17:48:46 +0800
Subject: [PATCH] update shell

---
 service/SysService.go |   84 +++++++++++++++++++++++++++++++-----------
 1 files changed, 62 insertions(+), 22 deletions(-)

diff --git a/service/SysService.go b/service/SysService.go
index 5b6b985..c50339f 100644
--- a/service/SysService.go
+++ b/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 {
 			//琛ㄧず鎵�鏈夊垎鍧楅兘涓婁紶浜嗭紝闇�瑕乵erge
-			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 {
@@ -144,14 +143,50 @@
 	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) {
+	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("鍗囩骇鏂囦欢宸蹭涪澶憋紝璇烽噸鏂颁笂浼�")
+	}
 }
 
 //鏇存柊绯荤粺绋嬪簭
@@ -166,25 +201,30 @@
 	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 !util.CreateDirectory(unZipPath) {
+		return false
+	}
+
+	zipFilePath := configPatchPath+"/"+identifier+ext
+	err := util.UnZip(zipFilePath, unZipPath)
 	if err !=nil {
-		logger.Debug("UnZip err:",err,"zipFile:",configPatchPath+"/"+identifier+ext)
+		logger.Debug("UnZip err:",err,"zipFile:",zipFilePath)
 		return false
 	}
 	//2.鏇存柊绯荤粺
 	var cmd *exec.Cmd
-	updateCmd := fmt.Sprintf("setsid updatePatch.sh %s",unZipPath)
+	updateCmd := fmt.Sprintf("./updatePatch.sh %s %s %s &",unZipPath,zipFilePath,configPatchPath+"/"+identifier)
 	cmd = exec.Command("/bin/sh","-c", updateCmd)
-	logger.Debug("called sh updatePatch.sh,updateCmd:",updateCmd)
 	if b, err := cmd.Output(); err != nil {
 		logger.Debug("updatePatch err:",err,"result:",string(b))
 		return false
+	} else {
+		logger.Debug("updatePatch result:",string(b),"cmd:",updateCmd)
 	}
 	return true
 }
@@ -192,11 +232,11 @@
 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
 	}
 }
\ No newline at end of file

--
Gitblit v1.8.0