forked from mattermost/mattermost
/
upgrader_linux.go
338 lines (297 loc) · 10.2 KB
/
upgrader_linux.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
336
337
338
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package upgrader
import (
"archive/tar"
"bytes"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/user"
"path"
"runtime"
"strconv"
"strings"
"sync/atomic"
"syscall"
"github.com/pkg/errors"
"golang.org/x/crypto/openpgp"
"github.com/adacta-ru/mattermost-server/v5/mlog"
"github.com/adacta-ru/mattermost-server/v5/model"
)
const mattermostBuildPublicKey = `-----BEGIN PGP PUBLIC KEY BLOCK-----
mQENBFjZQxwBCAC6kNn3zDlq/aY83M9V7MHVPoK2jnZ3BfH7sA+ibQXsijCkPSR4
5bCUJ9qVA4XKGK+cpO9vkolSNs10igCaaemaUZNB6ksu3gT737/SZcCAfRO+cLX7
Q2la+jwTvu1YeT/M5xDZ1KHTFxsGskeIenz2rZHeuZwBl9qep34QszWtRX40eRts
fl6WltLrepiExTp6NMZ50k+Em4JGM6CWBMo22ucy0jYjZXO5hEGb3o6NGiG+Dx2z
b2J78LksCKGsSrn0F1rLJeA933bFL4g9ozv9asBlzmpgG77ESg6YE1N/Rh7WDzVA
prIR0MuB5JjElASw5LDVxDV6RZsxEVQr7ETLABEBAAG0KU1hdHRlcm1vc3QgQnVp
bGQgPGRldi1vcHNAbWF0dGVybW9zdC5jb20+iQFUBBMBCAA+AhsDBQsJCAcCBhUI
CQoLAgQWAgMBAh4BAheAFiEEobMdRvDzoQsCzy1E+PLDF0R3SygFAl6HYr0FCQlw
hqEACgkQ+PLDF0R3SyheNQgAnkiT2vFMCtU5FmC16HVYXzDpYMtdCQPh/gmeEkiI
80rFRg/cn6f0BNnaTfDu6r6cepmhLNpDAowjQ7uBnv8fL2dzCydIGFv2r7FfmcOJ
zhEQ3zXPwP6mYlxPCCgxAozsLv9Yv41KGCHIlzYwkAazc0BhpAW/h8L3VGkE+b+g
x6lKVoufm4rKnT49Dgly6fVOxuR/BqZo87B5jksV3izLTHt5hiY8Pc5GW8WwO/tr
pNAw+6HRXq1Dr/JRz5PIOr5KP5tVLBed4IteZ1xaTRd4++07ZbiZjhXY8WKpVp3y
iN7Om24jQpxbJI9+KKJ3+yhcwhr8/PJ8ZVuhJo3BNv1PcQ==
=9Qk8
-----END PGP PUBLIC KEY BLOCK-----`
var upgradePercentage int64
var upgradeError error
var upgrading int32
type writeCounter struct {
total int64
readed int64
}
func (wc *writeCounter) Write(p []byte) (int, error) {
n := len(p)
wc.readed += int64(n)
percentage := (wc.readed * 100) / wc.total
if percentage == 0 {
upgradePercentage = 1
} else if percentage == 100 {
upgradePercentage = 99
} else {
upgradePercentage = percentage
}
return n, nil
}
func getCurrentVersionTgzUrl() string {
version := model.CurrentVersion
if strings.HasPrefix(model.BuildNumber, version+"-rc") {
version = model.BuildNumber
}
return "https://releases.mattermost.com/" + version + "/mattermost-" + version + "-linux-amd64.tar.gz"
}
func verifySignature(filename string, sigfilename string, publicKey string) error {
keyring, err := openpgp.ReadArmoredKeyRing(bytes.NewReader([]byte(publicKey)))
if err != nil {
mlog.Error("Unable to load the public key to verify the file signature", mlog.Err(err))
return NewInvalidSignature()
}
mattermost_tar, err := os.Open(filename)
if err != nil {
mlog.Error("Unable to open the Mattermost .tar file to verify the file signature", mlog.Err(err))
return NewInvalidSignature()
}
signature, err := os.Open(sigfilename)
if err != nil {
mlog.Error("Unable to open the Mattermost .sig file verify the file signature", mlog.Err(err))
return NewInvalidSignature()
}
_, err = openpgp.CheckDetachedSignature(keyring, mattermost_tar, signature)
if err != nil {
mlog.Error("Unable to verify the Mattermost file signature", mlog.Err(err))
return NewInvalidSignature()
}
return nil
}
func canIWriteTheExecutable() error {
executablePath, err := os.Executable()
if err != nil {
return errors.New("error getting the path of the executable")
}
executableInfo, err := os.Stat(path.Dir(executablePath))
if err != nil {
return errors.New("error getting the executable info")
}
stat, ok := executableInfo.Sys().(*syscall.Stat_t)
if !ok {
return errors.New("error getting the executable info")
}
fileUID := int(stat.Uid)
fileUser, err := user.LookupId(strconv.Itoa(fileUID))
if err != nil {
return errors.New("error getting the executable info")
}
mattermostUID := os.Getuid()
mattermostUser, err := user.LookupId(strconv.Itoa(mattermostUID))
if err != nil {
return errors.New("error getting the executable info")
}
mode := executableInfo.Mode()
if fileUID != mattermostUID && mode&(1<<1) == 0 && mode&(1<<7) == 0 {
return NewInvalidPermissions("invalid-user-and-permission", path.Dir(executablePath), mattermostUser.Username, fileUser.Username)
}
if fileUID != mattermostUID && mode&(1<<1) == 0 && mode&(1<<7) != 0 {
return NewInvalidPermissions("invalid-user", path.Dir(executablePath), mattermostUser.Username, fileUser.Username)
}
if fileUID == mattermostUID && mode&(1<<7) == 0 {
return NewInvalidPermissions("invalid-permission", path.Dir(executablePath), mattermostUser.Username, fileUser.Username)
}
return nil
}
func canIUpgrade() error {
if runtime.GOARCH != "amd64" {
return NewInvalidArch()
}
if runtime.GOOS != "linux" {
return NewInvalidArch()
}
return canIWriteTheExecutable()
}
func CanIUpgradeToE0() error {
if err := canIUpgrade(); err != nil {
mlog.Error("Unable to upgrade from TE to E0", mlog.Err(err))
return err
}
if model.BuildEnterpriseReady == "true" {
mlog.Warn("Unable to upgrade from TE to E0. The server is already running E0.")
return errors.New("you cannot upgrade your server from TE to E0 because you are already running Mattermost Enterprise Edition")
}
return nil
}
func UpgradeToE0() error {
if !atomic.CompareAndSwapInt32(&upgrading, 0, 1) {
mlog.Warn("Trying to upgrade while another upgrade is running")
return errors.New("another upgrade is already running")
}
defer atomic.CompareAndSwapInt32(&upgrading, 1, 0)
upgradePercentage = 1
upgradeError = nil
executablePath, err := os.Executable()
if err != nil {
upgradePercentage = 0
upgradeError = errors.New("error getting the executable path")
mlog.Error("Unable to get the path of the Mattermost executable", mlog.Err(err))
return err
}
filename, err := download(getCurrentVersionTgzUrl(), 1024*1024*300)
if err != nil {
if filename != "" {
os.Remove(filename)
}
upgradeError = fmt.Errorf("error downloading the new Mattermost server binary file (percentage: %d)", upgradePercentage)
mlog.Error("Unable to download the Mattermost server binary file", mlog.Int64("percentage", upgradePercentage), mlog.String("url", getCurrentVersionTgzUrl()), mlog.Err(err))
upgradePercentage = 0
return err
}
defer os.Remove(filename)
sigfilename, err := download(getCurrentVersionTgzUrl()+".sig", 1024)
if err != nil {
if sigfilename != "" {
os.Remove(sigfilename)
}
upgradeError = errors.New("error downloading the signature file of the new server")
mlog.Error("Unable to download the signature file of the new Mattermost server", mlog.String("url", getCurrentVersionTgzUrl()+".sig"), mlog.Err(err))
upgradePercentage = 0
return err
}
defer os.Remove(sigfilename)
err = verifySignature(filename, sigfilename, mattermostBuildPublicKey)
if err != nil {
upgradePercentage = 0
upgradeError = errors.New("unable to verify the signature of the downloaded file")
mlog.Error("Unable to verify the signature of the downloaded file", mlog.Err(err))
return err
}
err = extractBinary(executablePath, filename)
if err != nil {
upgradePercentage = 0
upgradeError = err
mlog.Error("Unable to extract the binary from the downloaded file", mlog.Err(err))
return err
}
upgradePercentage = 100
return nil
}
func UpgradeToE0Status() (int64, error) {
return upgradePercentage, upgradeError
}
func download(url string, limit int64) (string, error) {
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
out, err := ioutil.TempFile("", "*_mattermost.tar.gz")
if err != nil {
return "", err
}
defer out.Close()
counter := &writeCounter{total: resp.ContentLength}
_, err = io.Copy(out, io.TeeReader(&io.LimitedReader{R: resp.Body, N: limit}, counter))
return out.Name(), err
}
func getFilePermissionsOrDefault(filename string, def os.FileMode) os.FileMode {
file, err := os.Open(filename)
if err != nil {
mlog.Error("Unable to get the file permissions", mlog.String("filename", filename), mlog.Err(err))
return def
}
fileStats, err := file.Stat()
if err != nil {
mlog.Error("Unable to get the file permissions", mlog.String("filename", filename), mlog.Err(err))
return def
}
return fileStats.Mode()
}
func extractBinary(executablePath string, filename string) error {
gzipStream, err := os.Open(filename)
if err != nil {
return err
}
uncompressedStream, err := gzip.NewReader(gzipStream)
if err != nil {
return err
}
tarReader := tar.NewReader(uncompressedStream)
for {
header, err := tarReader.Next()
if err == io.EOF {
return errors.New("unable to find the Mattermost binary in the downloaded version")
}
if err != nil {
return err
}
if header.Typeflag == tar.TypeReg && header.Name == "mattermost/bin/mattermost" {
permissions := getFilePermissionsOrDefault(executablePath, 0755)
tmpFile, err := ioutil.TempFile(path.Dir(executablePath), "*")
if err != nil {
return err
}
tmpFileName := tmpFile.Name()
os.Remove(tmpFileName)
err = os.Rename(executablePath, tmpFileName)
if err != nil {
return err
}
outFile, err := os.Create(executablePath)
if err != nil {
err2 := os.Rename(tmpFileName, executablePath)
if err2 != nil {
mlog.Critical("Unable to restore the backup of the executable file. Restore the executable file manually.")
return errors.Wrap(err2, "critical error: unable to upgrade the binary or restore the old binary version. Please restore it manually")
}
return err
}
defer outFile.Close()
if _, err = io.Copy(outFile, tarReader); err != nil {
err2 := os.Remove(executablePath)
if err2 != nil {
mlog.Critical("Unable to restore the backup of the executable file. Restore the executable file manually.")
return errors.Wrap(err2, "critical error: unable to upgrade the binary or restore the old binary version. Please restore it manually")
}
err2 = os.Rename(tmpFileName, executablePath)
if err2 != nil {
mlog.Critical("Unable to restore the backup of the executable file. Restore the executable file manually.")
return errors.Wrap(err2, "critical error: unable to upgrade the binary or restore the old binary version. Please restore it manually")
}
return err
}
err = os.Remove(tmpFileName)
if err != nil {
mlog.Warn("Unable to clean up the binary backup file.", mlog.Err(err))
}
err = os.Chmod(executablePath, permissions)
if err != nil {
mlog.Warn("Unable to set the correct permissions for the file.", mlog.Err(err))
}
break
}
}
return nil
}