-
Notifications
You must be signed in to change notification settings - Fork 1
/
upgrade.go
138 lines (117 loc) · 3.11 KB
/
upgrade.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
// Copyright 2019 The vogo Authors. All rights reserved.
// author: wongoo
package gracego
import (
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
)
// Upgrade gracefully upgrade server
// - version: the new version of the server
// - path: the relative path of the command in the upgrade compress file
// - upgradeUrl: the url of the upgrade file, which must be a zip format file with a suffix `.jar` or `.zip`
func Upgrade(version, path, upgradeURL string) error {
if err := upgradeServerCmd(version, path, upgradeURL); err != nil {
return err
}
go restart()
return nil
}
// upgradeServerCmd grace up server bin file
func upgradeServerCmd(version, path, upgradeURL string) error {
if server == nil || serverBin == "" {
return errors.New("server not started")
}
versionDir := filepath.Join(serverDir, version)
err := os.Mkdir(versionDir, 0770)
if err != nil && !strings.Contains(err.Error(), "file exists") {
return err
}
fileName, err := parseFileName(upgradeURL)
if err != nil {
return err
}
upgradeCmd := filepath.Join(versionDir, path)
_, err = os.Open(upgradeCmd)
if err == nil {
graceLog("found upgrade command file: %s", upgradeCmd)
return link(upgradeCmd, serverCmdPath)
}
downloadPath := filepath.Join(versionDir, fileName)
err = downloadFile(downloadPath, upgradeURL)
if err != nil {
return err
}
err = unzip(downloadPath, versionDir)
if err != nil {
return err
}
return link(upgradeCmd, serverCmdPath)
}
func parseFileName(upgradeURL string) (string, error) {
u, err := url.Parse(upgradeURL)
if err != nil {
return "", err
}
uri := u.RequestURI()
index := strings.LastIndex(uri, "/")
if index < 0 {
return "", fmt.Errorf("invalid download url: %s", u)
}
fileName := uri[index+1:]
if !acceptFileSuffix(fileName) {
return "", fmt.Errorf("invalid suffix for download url: %s", u)
}
return fileName, nil
}
func link(src, dest string) error {
_ = os.Remove(dest)
graceLog("link %s to %s", src, dest)
return os.Link(src, dest)
}
func acceptFileSuffix(f string) bool {
return strings.HasSuffix(f, ".jar") || strings.HasSuffix(f, ".zip")
}
// downloadFile will download a url to a local file. It's efficient because it will
// write as it downloads and not load the whole file into memory.
func downloadFile(filePath, upgradeURL string) error {
u, err := url.Parse(upgradeURL)
if err != nil {
return err
}
graceLog("download %s to %s", upgradeURL, filePath)
_ = os.Remove(filePath)
// Get the data
resp, err := http.Get(u.String())
if err != nil {
return err
}
defer resp.Body.Close()
switch resp.StatusCode {
case 200:
case 404:
return fmt.Errorf("file not found: %s", upgradeURL)
default:
buf := make([]byte, 1024)
result := ""
if n, readErr := resp.Body.Read(buf); n > 0 && readErr == nil {
result = string(buf[:n])
}
return fmt.Errorf("download failed, status code: %d, result: %s", resp.StatusCode, result)
}
// Create the file
out, err := os.Create(filePath)
if err != nil {
graceLog("can't create file: %v", err)
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}