| | |
| | | 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" |
| | |
| | | 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 |
| | | } |
| | |
| | | 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 |
| | | } |
| | | } |
| | | } |
| | |
| | | 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 { |
| | |
| | | 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("获取升级压缩包md5失败") |
| | | } |
| | | 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("升级程序解压失败,请确定上传的补丁是zip格式") |
| | | } |
| | | } else { |
| | | logger.Debug("strMd5 is", strMd5,"identifier is",identifier,"not equal") |
| | | return false,errors.New("校验升级文件失败") |
| | | } |
| | | } else { |
| | | return false,errors.New("升级文件已丢失,请重新上传") |
| | | } |
| | | } |
| | | |
| | | //更新系统程序 |
| | |
| | | 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 |
| | | cmd = exec.Command("/bin/sh","-c",fmt.Sprintf("./updatePatch.sh %s",unZipPath)) |
| | | logger.Debug("called sh updatePatch.sh ", unZipPath) |
| | | updateCmd := fmt.Sprintf("./updatePatch.sh %s %s %s &",unZipPath,zipFilePath,configPatchPath+"/"+identifier) |
| | | cmd = exec.Command("/bin/sh","-c", 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 |
| | | } |
| | |
| | | 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 |
| | | } |
| | | } |