-
Notifications
You must be signed in to change notification settings - Fork 3
/
mount.go
423 lines (330 loc) · 10.2 KB
/
mount.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package core
import (
"bufio"
"bytes"
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/DODAS-TS/sts-wire/pkg/rclone"
"github.com/rs/zerolog/log"
)
const (
numCheckAttempts = 10
checkExeWait = 1 * time.Second
waitFileBusy = 1 * time.Second
closeChannelWait = 2 * time.Second
)
func CacheDir() (string, error) {
cacheDir, errCacheDir := os.UserCacheDir()
if errCacheDir != nil {
log.Err(errCacheDir).Msg("Caching dir not available")
return "", fmt.Errorf("CacheDir %w", errCacheDir)
}
return filepath.Join(cacheDir, "sts-wire"), nil
}
func ExePath() (string, error) {
cacheDir, errCacheDir := CacheDir()
if errCacheDir != nil {
return "", errCacheDir
}
return filepath.Join(cacheDir, rcloneExeString), nil
}
func CheckExeFile(rcloneFile string, originalData []byte) error {
var rcloneExeFile bytes.Buffer
rcloneFileReader, errOpen := os.Open(rcloneFile)
if errOpen != nil {
log.Err(errOpen).Msg("Cannot open rclone executable in cache dir")
return fmt.Errorf("prepare rclone %w", errOpen)
}
defer rcloneFileReader.Close()
// Check if it has executable permissions
if runtime.GOOS != "windows" {
rcloneInfo, err := rcloneFileReader.Stat()
if err != nil {
return fmt.Errorf("cannot have rclone file stat")
}
if rcloneInfo.Mode() != os.FileMode(exeFileMode) {
return fmt.Errorf("rclone is not an executable")
}
}
// Verify checksum
_, errRead := rcloneExeFile.ReadFrom(rcloneFileReader)
if errRead != nil {
log.Err(errRead).Msg("Cannot read rclone executable in cache dir")
return fmt.Errorf("prepare rclone %w", errRead)
}
curExe := md5.New() //nolint:gosec
_, errWriteCurExe := io.Copy(curExe, bytes.NewReader(rcloneExeFile.Bytes()))
if errWriteCurExe != nil {
log.Err(errWriteCurExe).Msg("Cannot calculate md5 for rclone executable in cache dir")
return fmt.Errorf("prepare rclone %w", errWriteCurExe)
}
rcloneExe := md5.New() //nolint:gosec
_, errWriteRcloneExe := io.Copy(rcloneExe, bytes.NewReader(originalData))
if errWriteRcloneExe != nil {
log.Err(errWriteRcloneExe).Msg("Cannot calculate md5 for original rclone")
return fmt.Errorf("prepare rclone %w", errWriteRcloneExe)
}
rcloneMd5 := curExe.Sum(nil)
originalMd5 := rcloneExe.Sum(nil)
log.Debug().Str("rcloneMd5",
hex.EncodeToString(rcloneMd5)).Str("originalMd5",
hex.EncodeToString(originalMd5)).Msg("rclone - executble checksum")
if !bytes.Equal(rcloneMd5, originalMd5) {
log.Err(nil).Msg("checksum not equal")
return fmt.Errorf("checksum not equal")
}
return nil
}
func PrepareRclone() error { // nolint: funlen,gocognit
baseDir, errCacheDir := CacheDir()
if errCacheDir != nil {
return errCacheDir
}
log.Debug().Str("basedir", baseDir).Msg("rclone")
rcloneFile, errExePath := ExePath()
if errExePath != nil {
return errExePath
}
log.Debug().Str("rcloneFile", rcloneFile).Msg("rclone")
log.Debug().Msg("rclone - get asset data")
data := rclone.Executable
log.Debug().Int("assetLen", len(data)).Msg("rclone")
log.Debug().Msg("rclone - create executable base directories")
errMkdir := os.MkdirAll(baseDir, os.ModePerm)
if errMkdir != nil && !os.IsExist(errMkdir) {
log.Err(errMkdir).Msg("Cannot create rclone cache dir")
return fmt.Errorf("prepare rclone %w", errMkdir)
}
_, errStat := os.Stat(rcloneFile)
log.Debug().Bool("missingExe", os.IsNotExist(errStat)).Msg("rclone")
if errStat != nil && os.IsNotExist(errStat) { //nolint:nestif
log.Debug().Msg("rclone - creating executable")
rcloneExeFile, errCreate := os.OpenFile(rcloneFile, os.O_RDWR|os.O_CREATE|os.O_SYNC, fileMode)
if errCreate != nil {
log.Err(errCreate).Msg("Cannot open with write permission the rclone executable in cache dir")
// file busy
if strings.Contains(errCreate.Error(), "file busy") {
var errCheck error
for attempt := 0; attempt < numCheckAttempts; attempt++ {
log.Debug().Int("attempt", attempt).Msg("rclone - verify executable")
errCheck = CheckExeFile(rcloneFile, data)
if errCheck != nil {
log.Err(errCheck).Int("attempt", attempt).Msg("Cannot verify rclone executable in cache dir when file is busy")
} else {
break
}
time.Sleep(waitFileBusy)
}
if errCheck != nil {
return errCheck
}
return nil
}
log.Err(errCreate).Msg("Cannot create rclone executable in cache dir")
return fmt.Errorf("prepare rclone %w", errCreate)
}
buff := bytes.NewReader(data)
writtenData, errWrite := io.Copy(rcloneExeFile, buff)
log.Debug().Int64("writtenData", writtenData).Msg("rclone")
if errWrite != nil {
log.Err(errWrite).Msg("Cannot write rclone executable in cache dir")
return fmt.Errorf("prepare rclone %w", errWrite)
}
rcloneExeFile.Close()
log.Debug().Msg("rclone - change executable mod")
errChmod := os.Chmod(rcloneFile, os.FileMode(exeFileMode))
if errChmod != nil {
log.Err(errChmod).Msg("Cannot make rclone an executable in cache dir")
return fmt.Errorf("prepare rclone %w", errChmod)
}
}
var errCheck error
for attempt := 0; attempt < numCheckAttempts; attempt++ {
log.Debug().Int("attempt", attempt).Msg("rclone - verify executable")
errCheck = CheckExeFile(rcloneFile, data)
if errCheck != nil {
log.Err(errCheck).Int("attempt", attempt).Msg("Cannot verify rclone executable in cache dir")
} else {
break
}
time.Sleep(checkExeWait)
}
if errCheck != nil {
return fmt.Errorf("prepare rclone %w", errCheck)
}
return nil
}
func MountVolume(instance string, remotePath string, localPath string, configPath string) (*exec.Cmd, chan error, string, error) { // nolint: funlen,gocognit,lll
log.Debug().Str("action", "prepare rclone").Msg("rclone - mount")
errPrepare := PrepareRclone()
if errPrepare != nil {
log.Err(errPrepare).Msg("rclone - mount")
return nil, nil, "", errPrepare
}
log.Debug().Str("action", "get file path").Msg("rclone - mount")
rcloneFile, errExePath := ExePath()
if errExePath != nil {
return nil, nil, "", errExePath
}
log.Debug().Str("action", "make local dir").Msg("rclone - mount")
if runtime.GOOS != "windows" {
_, errLocalPath := os.Stat(localPath)
if os.IsNotExist(errLocalPath) {
errMkdir := os.MkdirAll(localPath, os.ModePerm)
if errMkdir != nil {
panic(errMkdir)
}
}
}
conf := fmt.Sprintf("%s:%s", instance, remotePath)
log.Debug().Str("action", "prepare mounting points").Msg("rclone - mount")
configPathAbs, errConfigPath := filepath.Abs(filepath.Join(configPath, "/rclone.conf"))
if errConfigPath != nil {
log.Err(errConfigPath).Msg("server")
return nil, nil, "", fmt.Errorf("rclone config abs: %w", errConfigPath)
}
logPath, errLogPath := filepath.Abs(filepath.Join(configPath, "/rclone.log"))
if errLogPath != nil {
log.Err(errLogPath).Msg("server")
return nil, nil, "", fmt.Errorf("rclone log abs: %w", errLogPath)
}
localPathAbs, errLocalPath := filepath.Abs(localPath)
if errLocalPath != nil {
log.Err(errLocalPath).Msg("server")
return nil, nil, "", fmt.Errorf("local path abs: %w", errLocalPath)
}
log.Debug().Str("command", strings.Join([]string{
rcloneFile,
"--config",
configPathAbs,
"--no-check-certificate",
"mount",
// "--daemon",
"--log-file",
logPath,
"--log-level=DEBUG",
"--vfs-cache-mode",
"full",
"--no-modtime",
conf,
localPathAbs,
}, " ")).Msg("rclone - mount")
rcloneCmd := exec.Command(
rcloneFile,
"--config",
configPathAbs,
"--no-check-certificate",
"mount",
// "--daemon",
"--log-file",
logPath,
"--log-level=DEBUG",
"--vfs-cache-mode",
"full",
"--no-modtime",
conf,
localPathAbs,
)
log.Debug().Str("action", "start rclone").Msg("rclone - mount")
errStart := rcloneCmd.Start()
if errStart != nil {
panic(errStart)
}
cmdErr := make(chan error)
cmdErrorCheck := func() {
procState, errWait := rcloneCmd.Process.Wait()
// Wait for main server to close channel if User pressed Ctrl+C
time.Sleep(closeChannelWait)
openChannel := true
// Check if channel still is open
select {
case _, ok := <-cmdErr:
if !ok {
openChannel = false
}
default:
}
if procState != nil {
// ref: https://rclone.org/docs/#exit-code
switch exitCode := procState.ExitCode(); exitCode {
case 0:
log.Debug().Int("exitCode",
exitCode).Msg("success")
case 1:
log.Debug().Int("exitCode",
exitCode).Msg("Syntax or usage error")
case 2:
log.Debug().Int("exitCode",
exitCode).Msg("Error not otherwise categorised")
case 3:
log.Debug().Int("exitCode",
exitCode).Msg("Directory not found")
case 4:
log.Debug().Int("exitCode",
exitCode).Msg("File not found")
case 5:
log.Debug().Int("exitCode",
exitCode).Msg("Temporary error (one that more retries might fix) (Retry errors)")
case 6:
log.Debug().Int("exitCode",
exitCode).Msg("Less serious errors (like 461 errors from dropbox) (NoRetry errors)")
case 7:
log.Debug().Int("exitCode",
exitCode).Msg("Fatal error (one that more retries won't fix, like account suspended) (Fatal errors)")
case 8:
log.Debug().Int("exitCode",
exitCode).Msg("Transfer exceeded - limit set by --max-transfer reached")
case 9:
log.Debug().Int("exitCode",
exitCode).Msg("Operation successful, but no files transferred")
}
}
if openChannel { //nolint:nestif
// rclone exited with errors
if errWait != nil {
cmdErr <- errWait
}
defer close(cmdErr)
} else {
if errWait == nil {
// rclone not exited after user pressed Ctrl+C
if !procState.Exited() {
panic("rclone termination error")
}
} else if errWait.Error() != "wait: no child processes" {
// rclone exited for unknown reason
panic(errWait)
}
}
}
go cmdErrorCheck()
return rcloneCmd, cmdErr, logPath, nil
}
func RcloneLogErrors(logPath string) chan string {
outErrors := make(chan string)
go func() {
defer close(outErrors)
readFile, err := os.Open(logPath)
if err != nil {
log.Err(err).Str("logFile", logFile).Msg("failed to open log file")
}
defer readFile.Close()
fileScanner := bufio.NewScanner(readFile)
fileScanner.Split(bufio.ScanLines)
for fileScanner.Scan() {
curLine := fileScanner.Text()
if strings.Contains(curLine, "error") {
outErrors <- fileScanner.Text()
}
}
}()
return outErrors
}