-
Notifications
You must be signed in to change notification settings - Fork 53
/
service.go
667 lines (566 loc) · 14.3 KB
/
service.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
package fs
import (
"fmt"
"github.com/cenkalti/backoff/v4"
config2 "github.com/crawlab-team/crawlab-core/config"
"github.com/crawlab-team/crawlab-core/constants"
"github.com/crawlab-team/crawlab-core/entity"
"github.com/crawlab-team/crawlab-core/errors"
"github.com/crawlab-team/crawlab-core/interfaces"
"github.com/crawlab-team/crawlab-core/node/config"
cfs "github.com/crawlab-team/crawlab-fs"
vcs "github.com/crawlab-team/crawlab-vcs"
"github.com/crawlab-team/go-trace"
"github.com/spf13/viper"
"github.com/ztrue/tracerr"
"go.uber.org/dig"
"os"
"sort"
"strings"
"time"
)
type Service struct {
// settings
cfgPath string
fsPath string
workspacePath string
repoPath string
// dependencies
nodeCfgSvc interfaces.NodeConfigService
fs cfs.Manager
// internals
gitClient *vcs.GitClient // workspace git client (only for master)
}
func (svc *Service) List(path string, opts ...interfaces.ServiceCrudOption) (files []interfaces.FsFileInfo, err error) {
// forbidden if not master
if !svc.nodeCfgSvc.IsMaster() {
return files, errors.ErrorFsForbidden
}
// apply options
o := svc.newCrudOptions()
for _, opt := range opts {
opt(o)
}
// remote path
remotePath := svc.getRemotePath(path, o)
// list items of directory
items, err := svc.fs.ListDir(remotePath, false)
if err != nil {
return files, err
}
// sort items
itemsSlice := FilerFileInfoSlice(items)
sort.Sort(itemsSlice)
items = itemsSlice
// iterate items and list items recursively
for _, item := range items {
// skip keep file
if item.Name == constants.FsKeepFileName {
continue
}
itemPath := strings.Replace(item.FullPath, svc.fsPath, "", 1)
if o.IsAbsolute {
itemPath = item.FullPath
}
f := &entity.FsFileInfo{
Name: item.Name,
Path: itemPath,
FullPath: item.FullPath,
Extension: item.Extension,
Md5: item.Md5,
IsDir: item.IsDir,
FileSize: item.FileSize,
}
if item.IsDir {
relativePath := strings.Replace(item.FullPath, svc.fsPath, "", 1)
if o.IsAbsolute {
relativePath = item.FullPath
}
f.Children, err = svc.List(relativePath, opts...)
if err != nil {
return files, err
}
}
files = append(files, f)
}
return files, nil
}
func (svc *Service) GetFile(path string, opts ...interfaces.ServiceCrudOption) (data []byte, err error) {
// forbidden if not master
if !svc.nodeCfgSvc.IsMaster() {
return data, trace.TraceError(errors.ErrorFsForbidden)
}
// apply options
o := svc.newCrudOptions()
for _, opt := range opts {
opt(o)
}
// normalize remote path
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
remotePath := fmt.Sprintf("%s%s", svc.fsPath, path)
if o.IsAbsolute {
remotePath = path
}
return svc.fs.GetFile(remotePath)
}
func (svc *Service) GetFileInfo(path string, opts ...interfaces.ServiceCrudOption) (file interfaces.FsFileInfo, err error) {
// forbidden if not master
if !svc.nodeCfgSvc.IsMaster() {
return file, trace.TraceError(errors.ErrorFsForbidden)
}
// apply options
o := svc.newCrudOptions()
for _, opt := range opts {
opt(o)
}
// normalize remote path
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
remotePath := fmt.Sprintf("%s%s", svc.fsPath, path)
if o.IsAbsolute {
remotePath = path
}
f, err := svc.fs.GetFileInfo(remotePath)
if err != nil {
return nil, err
}
itemPath := strings.Replace(f.FullPath, svc.fsPath, "", 1)
if o.IsAbsolute {
itemPath = f.FullPath
}
file = &entity.FsFileInfo{
Name: f.Name,
Path: itemPath,
FullPath: f.FullPath,
Extension: f.Extension,
Md5: f.Md5,
IsDir: f.IsDir,
FileSize: f.FileSize,
}
return file, nil
}
func (svc *Service) Save(path string, data []byte, opts ...interfaces.ServiceCrudOption) (err error) {
// forbidden if not master
if !svc.nodeCfgSvc.IsMaster() {
return trace.TraceError(errors.ErrorFsForbidden)
}
// apply options
o := svc.newCrudOptions()
for _, opt := range opts {
opt(o)
}
// save fs
if err := svc.saveFs(path, data, opts...); err != nil {
return err
}
// skip if NotSyncToWorkspace is set to true
if o.NotSyncToWorkspace {
return nil
}
// sync to workspace
return svc.SyncToWorkspace()
}
func (svc *Service) Rename(path, newPath string, opts ...interfaces.ServiceCrudOption) (err error) {
// TODO: implement rename directory
// forbidden if not master
if !svc.nodeCfgSvc.IsMaster() {
return trace.TraceError(errors.ErrorFsForbidden)
}
// apply options
o := svc.newCrudOptions()
for _, opt := range opts {
opt(o)
}
// rename fs
if err := svc.renameFs(path, newPath, opts...); err != nil {
return err
}
// skip if NotSyncToWorkspace is set to true
if o.NotSyncToWorkspace {
return nil
}
// sync to workspace
return svc.SyncToWorkspace()
}
func (svc *Service) Delete(path string, opts ...interfaces.ServiceCrudOption) (err error) {
// forbidden if not master
if !svc.nodeCfgSvc.IsMaster() {
return trace.TraceError(errors.ErrorFsForbidden)
}
// apply options
o := svc.newCrudOptions()
for _, opt := range opts {
opt(o)
}
// delete fs
if err := svc.deleteFs(path, opts...); err != nil {
return err
}
// skip if NotSyncToWorkspace is set to true
if o.NotSyncToWorkspace {
return nil
}
// sync to workspace
return svc.SyncToWorkspace()
}
func (svc *Service) Copy(path, newPath string, opts ...interfaces.ServiceCrudOption) (err error) {
// forbidden if not master
if !svc.nodeCfgSvc.IsMaster() {
return tracerr.Wrap(errors.ErrorFsForbidden)
}
// apply options
o := svc.newCrudOptions()
for _, opt := range opts {
opt(o)
}
// remote path
remotePath := svc.getRemotePath(path, o)
// info
info, err := svc.fs.GetFileInfo(remotePath)
if err != nil {
return err
}
if info.IsDir {
err = svc.copyFsDir(path, newPath, opts...)
} else {
err = svc.copyFsFile(path, newPath, opts...)
}
// skip if NotSyncToWorkspace is set to true
if o.NotSyncToWorkspace {
return nil
}
// sync to workspace
return svc.SyncToWorkspace()
}
func (svc *Service) Commit(msg string) (err error) {
// forbidden if not master
if !svc.nodeCfgSvc.IsMaster() {
return trace.TraceError(errors.ErrorFsForbidden)
}
// TODO: check whether need to sync remote fs to local (based on md5?)
//if err := svc.fs.SyncRemoteToLocal(svc.fsPath, c.GetPath()); err != nil {
// return err
//}
// commit
if err := svc.gitClient.CommitAll(msg); err != nil {
return trace.TraceError(err)
}
// push to repo
if err := svc.gitClient.Push(vcs.WithRemoteNamePush(vcs.GitDefaultRemoteName)); err != nil {
return trace.TraceError(err)
}
return nil
}
// SyncToFs sync from repo/workspace to fs
func (svc *Service) SyncToFs(opts ...interfaces.ServiceCrudOption) (err error) {
// forbidden if not master
if !svc.nodeCfgSvc.IsMaster() {
return trace.TraceError(errors.ErrorFsForbidden)
}
// apply options
o := svc.newCrudOptions()
for _, opt := range opts {
opt(o)
}
// sync from workspace to fs
if err := svc.fs.SyncLocalToRemote(svc.GetWorkspacePath(), svc.fsPath); err != nil {
return err
}
return nil
}
// SyncToWorkspace sync from fs to workspace
func (svc *Service) SyncToWorkspace() (err error) {
// validate workspace path
if svc.workspacePath == "" {
return trace.TraceError(errors.ErrorFsEmptyWorkspacePath)
}
// create workspace directory if not exists
if _, err := os.Stat(svc.workspacePath); err != nil {
if err := os.MkdirAll(svc.workspacePath, os.ModePerm); err != nil {
return trace.TraceError(err)
}
}
// sync to local workspace from remote fs
if err := svc.fs.SyncRemoteToLocal(svc.fsPath, svc.workspacePath); err != nil {
return err
}
return nil
}
func (svc *Service) GetFsPath() (path string) {
return svc.fsPath
}
func (svc *Service) SetFsPath(path string) {
svc.fsPath = path
}
func (svc *Service) GetWorkspacePath() (path string) {
return svc.workspacePath
}
func (svc *Service) SetWorkspacePath(path string) {
svc.workspacePath = path
}
func (svc *Service) GetRepoPath() (path string) {
return svc.repoPath
}
func (svc *Service) SetRepoPath(path string) {
svc.repoPath = path
}
func (svc *Service) GetConfigPath() string {
return svc.cfgPath
}
func (svc *Service) SetConfigPath(path string) {
svc.cfgPath = path
}
func (svc *Service) GetFs() (fs cfs.Manager) {
return svc.fs
}
func (svc *Service) GetGitClient() (c *vcs.GitClient) {
return svc.gitClient
}
func (svc *Service) saveFs(path string, data []byte, opts ...interfaces.ServiceCrudOption) (err error) {
// apply options
o := svc.newCrudOptions()
for _, opt := range opts {
opt(o)
}
// remote path
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
remotePath := fmt.Sprintf("%s%s", svc.fsPath, path)
if o.IsAbsolute {
remotePath = path
}
return svc.fs.UpdateFile(remotePath, data)
}
func (svc *Service) renameFs(path, newPath string, opts ...interfaces.ServiceCrudOption) (err error) {
// apply options
o := svc.newCrudOptions()
for _, opt := range opts {
opt(o)
}
// remote paths
remotePath := svc.getRemotePath(path, o)
newRemotePath := svc.getRemotePath(newPath, o)
// error if new remote path exists
ok, err := svc.fs.Exists(newRemotePath)
if err != nil {
return err
}
if ok {
return trace.TraceError(errors.ErrorFsAlreadyExists)
}
// get original file data
data, err := svc.fs.GetFile(remotePath)
if err != nil {
return err
}
// save data to new file
if err := svc.fs.UpdateFile(newRemotePath, data); err != nil {
return err
}
// delete original file
if err := svc.fs.DeleteFile(remotePath); err != nil {
return err
}
return nil
}
func (svc *Service) deleteFs(path string, opts ...interfaces.ServiceCrudOption) (err error) {
// apply options
o := svc.newCrudOptions()
for _, opt := range opts {
opt(o)
}
// remote path
remotePath := svc.getRemotePath(path, o)
// info
info, err := svc.fs.GetFileInfo(remotePath)
if err != nil {
return err
}
// delete remote file
if info.IsDir {
err = svc.fs.DeleteDir(remotePath)
} else {
err = svc.fs.DeleteFile(remotePath)
}
if err != nil {
return err
}
return nil
}
func (svc *Service) copyFsFile(path, newPath string, opts ...interfaces.ServiceCrudOption) (err error) {
// apply options
o := svc.newCrudOptions()
for _, opt := range opts {
opt(o)
}
// normalize paths
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
if !strings.HasPrefix(newPath, "/") {
newPath = "/" + newPath
}
// error if new remote path exists
ok, err := svc.fs.Exists(svc.getRemotePath(newPath, o))
if err != nil {
return err
}
if ok {
return trace.TraceError(errors.ErrorFsAlreadyExists)
}
// original file data
data, err := svc.GetFile(path)
if err != nil {
return err
}
// write to new path
if err := svc.Save(newPath, data); err != nil {
return err
}
return nil
}
func (svc *Service) copyFsDir(path, newPath string, opts ...interfaces.ServiceCrudOption) (err error) {
// apply options
o := svc.newCrudOptions()
for _, opt := range opts {
opt(o)
}
// normalize paths
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
if !strings.HasPrefix(newPath, "/") {
newPath = "/" + newPath
}
// iterate all files
files, err := svc.List(path, opts...)
if err != nil {
return trace.TraceError(err)
}
for _, f := range files {
if f.GetIsDir() {
// directory
dirPathNew := fmt.Sprintf("%s/%s", newPath, f.GetName())
dirPath := f.GetPath()
if o.IsAbsolute {
dirPath = f.GetFullPath()
}
if err := svc.copyFsDir(dirPath, dirPathNew, opts...); err != nil {
return err
}
} else {
// file
filePath := f.GetPath()
if o.IsAbsolute {
filePath = f.GetFullPath()
}
data, err := svc.GetFile(filePath, opts...)
if err != nil {
return err
}
filePathNew := fmt.Sprintf("%s/%s", newPath, f.GetName())
if err := svc.Save(filePathNew, data, opts...); err != nil {
return err
}
}
}
return nil
}
func (svc *Service) getRemotePath(path string, o *interfaces.ServiceCrudOptions) (remotePath string) {
// normalize path
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
if o.IsAbsolute {
// absolute path
return path
} else {
// relative path
return fmt.Sprintf("%s%s", svc.fsPath, path)
}
}
func (svc *Service) newCrudOptions() (o *interfaces.ServiceCrudOptions) {
return &interfaces.ServiceCrudOptions{
IsAbsolute: false,
OnlyFromWorkspace: false,
NotSyncToWorkspace: true,
}
}
func NewFsService(opts ...Option) (svc2 interfaces.FsService, err error) {
// service
svc := &Service{
cfgPath: config2.DefaultConfigPath,
fsPath: "",
//workspacePath: DefaultWorkspacePath,
//repoPath: DefaultRepoPath,
}
// apply options
for _, opt := range opts {
opt(svc)
}
// normalize fs base path
if !strings.HasPrefix(svc.fsPath, "/") {
svc.fsPath = "/" + svc.fsPath
}
// dependency injection
c := dig.New()
if err := c.Provide(config.ProvideConfigService(svc.cfgPath)); err != nil {
return nil, trace.TraceError(err)
}
if err := c.Invoke(func(nodeCfgSvc interfaces.NodeConfigService) {
svc.nodeCfgSvc = nodeCfgSvc
}); err != nil {
return nil, trace.TraceError(err)
}
// fs manager
var fsOpts []cfs.Option
if viper.GetString("fs.filer.url") != "" {
fsOpts = append(fsOpts, cfs.WithFilerUrl(viper.GetString("fs.filer.url")))
}
if viper.GetString("fs.filer.authKey") != "" {
fsOpts = append(fsOpts, cfs.WithFilerAuthKey(viper.GetString("fs.filer.authKey")))
}
svc.fs, err = cfs.NewSeaweedFsManager(fsOpts...)
if err != nil {
return nil, trace.TraceError(err)
}
// workspace directory
if svc.GetWorkspacePath() == "" {
svc.SetWorkspacePath(DefaultWorkspacePath)
}
if _, err := os.Stat(svc.GetWorkspacePath()); err != nil {
if err := os.MkdirAll(svc.GetWorkspacePath(), os.FileMode(0766)); err != nil {
return nil, trace.TraceError(err)
}
}
// remote repo and local workspace git client
// TODO: external repo
if svc.repoPath != "" {
// remote repo
if err := backoff.Retry(func() error {
if !vcs.IsGitRepoExists(svc.GetRepoPath()) {
if err := vcs.CreateBareGitRepo(svc.GetRepoPath()); err != nil {
return err
}
}
return nil
}, backoff.WithMaxRetries(backoff.NewConstantBackOff(100*time.Millisecond), 10)); err != nil {
return nil, trace.TraceError(err)
}
// local workspace git client
// TODO: auth
gitOpts := []vcs.GitOption{
vcs.WithPath(svc.GetWorkspacePath()),
vcs.WithRemoteUrl(svc.GetRepoPath()),
}
svc.gitClient, err = vcs.NewGitClient(gitOpts...)
if err != nil {
return nil, trace.TraceError(err)
}
}
return svc, nil
}