-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
335 lines (283 loc) · 7.18 KB
/
main.go
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"runtime"
"strings"
log "github.com/Sirupsen/logrus"
"path/filepath"
"archive/zip"
"crypto/sha256"
"io/ioutil"
"time"
"os/exec"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
// LomoUpdateVersion lomoUpdate version auto generated
const LomoUpdateVersion = "2019_12_09.23_41_36.0.b28a902"
type platform struct {
URL string
SHA256 string
Version string
PreCmds []string
PostCmds []string
}
type releases map[string]platform
func main() {
cli.VersionPrinter = func(c *cli.Context) {
fmt.Printf("%s\n", c.App.Version)
}
app := cli.NewApp()
app.Version = LomoUpdateVersion
app.Usage = "Lomorage upgrade app"
app.Authors = []*cli.Author{
&cli.Author{
Name: "Jeromy Fu",
Email: "[email protected]",
},
}
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "app-dir, a",
Usage: "app directory to uncompress lomorage zip",
Value: dir,
},
&cli.StringFlag{
Name: "backup-dir, b",
Usage: "directory to back up downloaded zip file and old release",
},
&cli.StringFlag{
Name: "curr-version, c",
Usage: "current version of lomorage app",
Value: dir,
},
&cli.StringFlag{
Name: "url, u",
Usage: "url for release json",
Value: "http://lomorage.github.io/release.json",
},
&cli.StringFlag{
Name: "precmd, prc",
Usage: "PreCmd for upgrading",
Value: "c:/stopLomoagent.bat",
},
&cli.StringFlag{
Name: "precmdarg, prca",
Usage: "PreCmd args for upgrading",
Value: "",
},
&cli.StringFlag{
Name: "postcmd, psc",
Usage: "PostCmd for upgrading",
Value: "c:/startLomoagent.bat",
},
&cli.StringFlag{
Name: "postcmdarg, psca",
Usage: "PostCmdArgs for upgrading",
Value: "c:/lomoagent.exe",
},
&cli.StringFlag{
Name: "log-dir, l",
Usage: "logfile directory",
Value: dir,
},
}
app.Action = bootService
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func bootService(ctx *cli.Context) error {
if ctx.String("app-dir") == "" {
return errors.New("invalid log dir")
}
logFile := filepath.Join(ctx.String("log-dir"), "lomoupg.log")
logFileHandler, err := os.OpenFile(logFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
Formatter := new(log.TextFormatter)
Formatter.TimestampFormat = "2006-02-02 15:04:06"
Formatter.FullTimestamp = true
log.SetFormatter(Formatter)
if err != nil {
// Cannot open log file. Logging to stderr
fmt.Println(err)
} else {
mw := io.MultiWriter(os.Stdout, logFileHandler)
log.SetOutput(mw)
}
if ctx.String("app-dir") == "" {
return errors.New("invalid app dir")
}
if ctx.String("curr-version") == "" {
return errors.New("invalid current version")
}
if ctx.String("url") == "" {
return errors.New("invalid url")
}
p, err := downloadReleaseMeta(ctx.String("url"))
if err != nil {
log.Errorf("downloadReleaseMeta error, %v", err)
return err
}
if p.Version == ctx.String("curr-version") {
log.Info("No new version, skip upgrade")
return nil
}
log.Info("Got new version, start upgrade")
tempRoot := ctx.String("backup-dir")
if tempRoot == "" {
tempRoot, err = ioutil.TempDir("", "lomod-temp")
if err != nil {
return err
}
defer os.RemoveAll(tempRoot)
}
f, err := downloadReleaseBin(p.URL, p.SHA256, tempRoot)
if err != nil {
return err
}
defer os.Remove(f)
tempUncompress := filepath.Join(tempRoot, "uncompress")
if err := uncompress(f, tempUncompress); err != nil {
return err
}
tempPreCmd := ctx.String("precmd")
tempPreCmdArg := ctx.String("precmdarg")
log.Info("start preUpgrade...")
if err := preUpgrade(tempPreCmd, tempPreCmdArg); err != nil {
// return err
log.Errorf("preUpgrade fail..., %v", err)
}
log.Info("start upgrade...")
if err := upgrade(ctx.String("app-dir"), tempRoot, tempUncompress); err != nil {
log.Errorf("upgrade fail..., %v", err)
// return err
}
tempPostCmd := ctx.String("postcmd")
tempPostCmdArg := ctx.String("postcmdarg")
log.Info("start postUpgrade...")
return postUpgrade(tempPostCmd, tempPostCmdArg)
}
func downloadReleaseMeta(url string) (*platform, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
d := json.NewDecoder(resp.Body)
v := make(releases)
err = d.Decode(&v)
if err != nil {
return nil, err
}
p, ok := v[runtime.GOOS]
if !ok {
return nil, errors.Errorf("Unsupported platform: %s", runtime.GOOS)
}
return &p, nil
}
func downloadReleaseBin(url, expectedSHA, tmpdir string) (string, error) {
tmpfile, err := ioutil.TempFile(tmpdir, "")
if err != nil {
return "", err
}
defer tmpfile.Close()
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
sha := sha256.New()
mw := io.MultiWriter(sha, tmpfile)
size, err := io.Copy(mw, resp.Body)
if err != nil {
return "", err
}
if size == 0 {
return "", errors.New("Empty file size")
}
// sha256Temp := fmt.Sprintf("%x", sha.Sum(nil))
// fmt.Println("the file's SHA256=", sha256Temp)
if strings.ToUpper(fmt.Sprintf("%x", sha.Sum(nil))) != strings.ToUpper(expectedSHA) {
return "", errors.New("Hash not match")
}
return tmpfile.Name(), nil
}
func uncompress(src, dst string) error {
r, err := zip.OpenReader(src)
if err != nil {
return err
}
defer r.Close()
// Iterate through the files in the archive,
// printing some of their contents.
for _, f := range r.File {
fpath := filepath.Join(dst, f.Name)
// Check for ZipSlip. More Info: http://bit.ly/2MsjAWE
if !strings.HasPrefix(fpath, filepath.Clean(dst)+string(os.PathSeparator)) {
return fmt.Errorf("%s: illegal file path", fpath)
}
if f.FileInfo().IsDir() {
// Make Folder
if err := os.MkdirAll(fpath, f.Mode()); err != nil {
return err
}
continue
}
// Make File
if err = os.MkdirAll(filepath.Dir(fpath), f.Mode()); err != nil {
return err
}
outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
rc, err := f.Open()
if err != nil {
return err
}
_, err = io.Copy(outFile, rc)
if err != nil {
return nil
}
if err := rc.Close(); err != nil {
return err
}
if err := outFile.Close(); err != nil {
return err
}
}
return nil
}
func preUpgrade(preCmd string, preCmdArg string) error {
cmd := exec.Command(preCmd, preCmdArg)
//log.Printf("Running command and waiting for it to finish...")
err := cmd.Run()
return err
}
func upgrade(appDir, bakDir, downloadDir string) error {
now := time.Now()
tmpFolder := fmt.Sprintf("lomod-bak-%d%02d%02d_%02d%02d%02d", now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second())
tmpFolderPath := filepath.Join(bakDir, tmpFolder)
log.Infof("rename %v to %v", appDir, tmpFolderPath)
if err := os.Rename(appDir, tmpFolderPath); err != nil {
return err
}
// move new one to specified app-dir
newAppPath := filepath.Join(downloadDir, filepath.Base(appDir))
log.Infof("rename %v to %v", newAppPath, appDir)
return os.Rename(newAppPath, appDir)
}
func postUpgrade(postCmd string, postCmdArg string) error {
cmd := exec.Command(postCmd, postCmdArg)
//log.Printf("Running command and waiting for it to finish...")
err := cmd.Start()
return err
}