liuxiaolong
2020-06-04 0d858e8610a79b15cb337daf134c4a00962dd477
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package service
 
import (
    "basic.com/valib/logger.git"
    "bufio"
    "errors"
    "fmt"
    "io/ioutil"
    "mime/multipart"
    "os"
    "os/exec"
    "path"
    "strings"
    "webserver/extend/config"
    "webserver/extend/util"
)
 
type SysService struct {
 
}
 
type FileChunkCheckVo struct {
    UserId string
    ChunkNumber int //当前分片下标,从1开始
    ChunkSize int //每一块的大小
    CurrentChunkSize int //当前分块的大小
    FileName string  //文件名称
    Identifier string //整个文件唯一标识,md5
    RelativePath string //文件客户端路径
    TotalSize int64 //文件总大小
    TotalChunks int //总分片数量
}
 
type FileUploadVo struct {
    Id string
    UserId string
    ChunkNumber int //当前分片下标,从1开始
    ChunkSize int //每一块的大小
    CurrentChunkSize int //当前分块的大小
    FileName string  //文件名称
    Identifier string //整个文件唯一标识,md5
    RelativePath string //文件客户端路径
    TotalSize int64 //文件总大小
    TotalChunks int //总分片数量
    File *multipart.File //当前分片的文件内容
    Header *multipart.FileHeader
}
 
func (sv SysService) CheckUpdateFile(arg *FileChunkCheckVo) bool {
    configPatchPath := ""
    if config.Server.PatchPath != "" {
        configPatchPath = config.Server.PatchPath
    } else {
        configPatchPath = "/opt/vasystem/patch"
    }
    fileTmpPath := configPatchPath + "/"+arg.Identifier
    if !util.Exists(fileTmpPath) {
        return false
    }
    //判断合成的文件是否存在
    index := strings.LastIndex(arg.FileName, ".")
    subfix := ""
    if index >-1 {//有后缀
        subfix = arg.FileName[index:]
    }
    mergedFilePath := fileTmpPath+subfix
    if util.Exists(mergedFilePath) {
        return true
    }
    //判断分块文件是否存在
    chunkAlignNum := util.FormatNum(arg.TotalChunks, arg.ChunkNumber)
    chunkFilePath := fileTmpPath+"/"+arg.Identifier+"_"+chunkAlignNum
    if !util.Exists(chunkFilePath) {
        return false
    }
    if arg.ChunkNumber == arg.TotalChunks {
        dirFiles, _ := ioutil.ReadDir(fileTmpPath)
        if dirFiles != nil && len(dirFiles) == arg.TotalChunks {
            //表示所有分块都上传了,需要merge
            if !MergeChunks(fileTmpPath, mergedFilePath) {
                return false
            }
        }
    }
    return true
}
 
func (sv SysService) PatchUpload(arg *FileUploadVo) (bool,bool) {
    configPatchPath := ""
    if config.Server.PatchPath != "" {
        configPatchPath = config.Server.PatchPath
    } else {
        configPatchPath = "/opt/vasystem/patch"
    }
    defer (*arg.File).Close()
    if !util.CreateDirectory(configPatchPath) {
        return false, false
    }
 
    filenameWithSuffix := path.Base(arg.Header.Filename)
    subfix := path.Ext(filenameWithSuffix)
    MD5Str := arg.Identifier
    logger.Debug("Identifier:",MD5Str)
    fileTmpPath := configPatchPath + "/"+MD5Str
    if !util.Exists(fileTmpPath) {
        if !util.CreateDirectory(fileTmpPath) {
            return false, false
        }
    }
    chunkAlignNum := util.FormatNum(arg.TotalChunks, arg.ChunkNumber)
    fileSavePath := fileTmpPath+"/"+MD5Str+"_"+chunkAlignNum
    if util.Exists(fileSavePath) {
        rmErr := os.Remove(fileSavePath)
        if rmErr != nil {
            logger.Debug("rmErr:",rmErr)
            return false, false
        }
    }
    file, e := os.Create(fileSavePath)
    if e !=nil {
        logger.Debug("os.Create err:",e,"fileSavePath:",fileSavePath)
        return false, false
    }
    defer file.Close()
    writer := bufio.NewWriter(file)
    chunkData := make([]byte, arg.Header.Size)
    n, err := (*arg.File).ReadAt(chunkData, 0)
    if n ==0 || err !=nil {
        logger.Debug("read chunkData err:",err,"n:",n)
        return false, false
    }
    nn, err2 := writer.Write(chunkData)
    if nn ==0 || err2 !=nil {
        logger.Debug("write chunkData err:",err2,"nn:",nn)
        return false, false
    }
    if err = writer.Flush(); err != nil {
        logger.Debug("write flush err:",err)
    }
    isComplete := false
    dirFiles, _ := ioutil.ReadDir(fileTmpPath)
    if dirFiles != nil && len(dirFiles) == arg.TotalChunks {
        isComplete = true
    }
    if isComplete {
        if MergeChunks(fileTmpPath,fileTmpPath + subfix) {
            logger.Debug("merge all chunks success,identifier:",MD5Str,"fileName:",arg.FileName)
        } else {
            return false, isComplete
        }
    }
    return true, isComplete
}
 
//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"
    }
 
    filenameWithSuffix := path.Base(filename)
    ext := path.Ext(filenameWithSuffix)
 
    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 !updatePatch(identifier, ext) {
                return false,errors.New("执行升级过程异常,请确定上传的补丁是tar.gz格式")
            }
            return true,nil
 
        } 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
}
 
//更新系统程序
func updatePatch(identifier string, ext string) bool {
    configPatchPath := ""
    if config.Server.PatchPath != "" {
        configPatchPath = config.Server.PatchPath
    } else {
        configPatchPath = "/opt/vasystem/patch"
    }
    //1.解压缩更新包
    unPackPath := configPatchPath+"/"+identifier+"_basic/"
    if util.Exists(unPackPath) {
        //此版本已经更新过
        rmErr := os.RemoveAll(unPackPath)
        if rmErr !=nil {
            return false
        }
    }
    if !util.CreateDirectory(unPackPath) {
        return false
    }
 
    unPackFilePath := configPatchPath+"/"+identifier+ext
    err := util.UnTarGz(unPackFilePath, unPackPath)
    if err !=nil {
        logger.Debug("UnPack err:",err,"unPackFile:",unPackFilePath)
        return false
    }
 
    //如果通用脚本有更新,则更新通用脚本
    if util.Exists(unPackPath+"updatePatch.sh") {
        cpStr := fmt.Sprintf("cp %s /opt/vasystem/bin",unPackPath+"updatePatch.sh")
        b, err := ExecCmd(cpStr)
        if err != nil {
            logger.Debug("cp updatePatch.sh to bin err:",err,"result:",string(b))
            return false
        }
    }
 
    //判断更新包里是否有补丁脚本,如果有则执行,否则执行updatePatch.sh
    updateCmd := fmt.Sprintf("./updatePatch.sh %s %s %s &",unPackPath,unPackFilePath,configPatchPath+"/"+identifier)
    if util.Exists(unPackPath+"upgrade.sh") {
        updateCmd = fmt.Sprintf("%supgrade.sh %s %s %s &",unPackPath,unPackPath,unPackFilePath,configPatchPath+"/"+identifier)
    }
    //2.更新系统
    b,err := ExecCmd(updateCmd)
    if err != nil {
        logger.Debug("upgrade err:",err,"result:",string(b),"cmd:",updateCmd)
        return false
    } else {
        logger.Debug("upgrade result:",string(b),"cmd:",updateCmd)
    }
    return true
}
 
func MergeChunks(chunkPath string, storePath string) bool {
    var cmd *exec.Cmd
    cmd = exec.Command("/bin/sh", "-c", fmt.Sprintf("./mergeAll.sh %s %s", 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
    }
}
 
func ExecCmd(cmdStr string) ([]byte,error) {
    var cmd *exec.Cmd
    cmd = exec.Command("/bin/sh", "-c",cmdStr)
    return cmd.Output()
}