/
syncthing.go
1079 lines (927 loc) · 30.1 KB
/
syncthing.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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2023 The Okteto Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package syncthing
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"text/template"
"time"
"github.com/google/uuid"
"github.com/okteto/okteto/pkg/config"
oktetoErrors "github.com/okteto/okteto/pkg/errors"
"github.com/okteto/okteto/pkg/filesystem"
oktetoLog "github.com/okteto/okteto/pkg/log"
"github.com/okteto/okteto/pkg/model"
"github.com/shirou/gopsutil/process"
"github.com/spf13/afero"
"golang.org/x/crypto/bcrypt"
yaml "gopkg.in/yaml.v2"
)
var (
configTemplate = template.Must(template.New("syncthingConfig").Parse(configXML))
isHealthyRetries = 0
cachedStatus = map[bool]string{}
cachedPullErrors = map[bool]int64{}
cachedFolderErrors = map[bool]FolderErrorEvent{}
)
const (
certFile = "cert.pem"
keyFile = "key.pem"
configFile = "config.xml"
// DefaultRemoteDeviceID remote syncthing device ID
DefaultRemoteDeviceID = "ATOPHFJ-VPVLDFY-QVZDCF2-OQQ7IOW-OG4DIXF-OA7RWU3-ZYA4S22-SI4XVAU"
// LocalDeviceID local syncthing device ID
LocalDeviceID = "ABKAVQF-RUO4CYO-FSC2VIP-VRX4QDA-TQQRN2J-MRDXJUC-FXNWP6N-S6ZSAAR"
// DefaultFileWatcherDelay how much to wait before starting a sync after a file change
DefaultFileWatcherDelay = 5
// ClusterPort is the port used by syncthing in the cluster
ClusterPort = 22000
// GUIPort is the port used by syncthing in the cluster for the http endpoint
GUIPort = 8384
maxRetries = 3
maxLogTailLinesToRead = 5
// one line of logs in UTF-8 is between 3-400 bytes
maxLogTailChunkByteSize int64 = 1024
)
var regexErrOpeningDatabase = regexp.MustCompile("Error opening database: mkdir .*: no space left on device")
var regexInsufficientSpace = regexp.MustCompile("insufficient space on disk for database")
// Syncthing represents the local syncthing process.
type Syncthing struct {
Fs afero.Fs `yaml:"-"`
Client *http.Client `yaml:"-"`
cmd *exec.Cmd `yaml:"-"`
RescanInterval string `yaml:"-"`
Home string `yaml:"-"`
RemoteGUIAddress string `yaml:"remote"`
GUIPassword string `yaml:"password"`
GUIPasswordHash string `yaml:"-"`
binPath string `yaml:"-"`
GUIAddress string `yaml:"local"`
Type string `yaml:"-"`
LogPath string `yaml:"-"`
ListenAddress string `yaml:"-"`
RemoteAddress string `yaml:"-"`
APIKey string `yaml:"apikey"`
Compression string `yaml:"-"`
RemoteDeviceID string `yaml:"-"`
Folders []*Folder `yaml:"folders"`
FileWatcherDelay int `yaml:"-"`
RemoteGUIPort int `yaml:"-"`
RemotePort int `yaml:"-"`
LocalGUIPort int `yaml:"-"`
LocalPort int `yaml:"-"`
pid int `yaml:"-"`
timeout time.Duration `yaml:"-"`
ForceSendOnly bool `yaml:"-"`
ResetDatabase bool `yaml:"-"`
IgnoreDelete bool `yaml:"-"`
Verbose bool `yaml:"-"`
}
// Folder represents a sync folder
type Folder struct {
Name string `yaml:"name"`
LocalPath string `yaml:"localPath"`
RemotePath string `yaml:"remotePath"`
Overwritten bool `yaml:"-"`
}
// Status represents the status of a syncthing folder.
type Status struct {
State string `json:"state"`
PullErrors int64 `json:"pullErrors"`
}
// StateChangedEvent represents state changed in syncthing.
type StateChangedEvent struct {
Type string `json:"type"`
Data DataStateChangedEvent `json:"data"`
}
// DataStateChangedEvent represents data state changed in syncthing.
type DataStateChangedEvent struct {
State string `json:"to"`
}
// FolderSummaryEvent represents folder summary in syncthing.
type FolderSummaryEvent struct {
Type string `json:"type"`
Data DataFolderSummaryEvent `json:"data"`
}
// DataFolderSummaryEvent represents data folder summary in syncthing.
type DataFolderSummaryEvent struct {
Summary Status `json:"summary"`
}
// FolderErrorEvent represents folder errors in syncthing.
type FolderErrorEvent struct {
Type string `json:"type"`
Data DataFolderErrorEvent `json:"data"`
}
// DataFolderErrorEvent represents data folder errors in syncthing.
type DataFolderErrorEvent struct {
Errors []FolderError `json:"errors"`
}
// FolderError represents a folder error in syncthing.
type FolderError struct {
Error string `json:"error"`
Path string `json:"path"`
}
// ItemEvent represents an item event of any type in syncthing.
type ItemEvent struct {
Time time.Time `json:"time"`
Data map[string]map[string]DownloadProgressData `json:"data"`
Id int `json:"id"`
GlobalId int `json:"globalID"`
}
// SystemError represents a system error in syncthing.
type SystemError struct {
Message string `json:"message"`
}
// SystemErrors represents a list of system errors
type SystemErrors struct {
Errors []SystemError `json:"errors"`
}
// Connections represents syncthing connections.
type Connections struct {
Connections map[string]Connection `json:"connections"`
}
// Connection represents syncthing connection.
type Connection struct {
Connected bool `json:"connected"`
}
// DownloadProgressData represents the information about a DownloadProgress event
type DownloadProgressData struct {
BytesTotal int64 `json:"bytesTotal"`
}
// New constructs a new Syncthing.
func New(dev *model.Dev, fs afero.Fs) (*Syncthing, error) {
fullPath := getInstallPath()
remotePort, err := model.GetAvailablePort(dev.Interface)
if err != nil {
return nil, err
}
remoteGUIPort, err := model.GetAvailablePort(dev.Interface)
if err != nil {
return nil, err
}
guiPort, err := model.GetAvailablePort(dev.Interface)
if err != nil {
return nil, err
}
listenPort, err := model.GetAvailablePort(dev.Interface)
if err != nil {
return nil, err
}
pwd := uuid.New().String()
hash, err := bcrypt.GenerateFromPassword([]byte(pwd), 10)
if err != nil {
oktetoLog.Infof("couldn't hash the password %s", err)
hash = []byte("")
}
compression := "metadata"
if dev.Sync.Compression {
compression = "always"
}
s := &Syncthing{
APIKey: "cnd",
GUIPassword: pwd,
GUIPasswordHash: string(hash),
binPath: fullPath,
Client: NewAPIClient(),
FileWatcherDelay: DefaultFileWatcherDelay,
GUIAddress: net.JoinHostPort(dev.Interface, strconv.Itoa(guiPort)),
Home: config.GetAppHome(dev.Namespace, dev.Name),
LogPath: GetLogFile(dev.Namespace, dev.Name),
ListenAddress: net.JoinHostPort(dev.Interface, strconv.Itoa(listenPort)),
RemoteAddress: fmt.Sprintf("tcp://%s:%d", dev.Interface, remotePort),
RemoteDeviceID: DefaultRemoteDeviceID,
RemoteGUIAddress: net.JoinHostPort(dev.Interface, strconv.Itoa(remoteGUIPort)),
LocalGUIPort: guiPort,
LocalPort: listenPort,
RemoteGUIPort: remoteGUIPort,
RemotePort: remotePort,
Type: "sendonly",
IgnoreDelete: true,
Verbose: dev.Sync.Verbose,
Folders: []*Folder{},
RescanInterval: strconv.Itoa(dev.Sync.RescanInterval),
Compression: compression,
timeout: dev.Timeout.Default,
Fs: fs,
}
index := 1
for _, sync := range dev.Sync.Folders {
result, err := dev.IsSubPathFolder(sync.LocalPath)
if err != nil {
return nil, err
}
if !result {
s.Folders = append(
s.Folders,
&Folder{
Name: strconv.Itoa(index),
LocalPath: sync.LocalPath,
RemotePath: sync.RemotePath,
},
)
index++
}
}
return s, nil
}
func (s *Syncthing) initConfig() error {
if err := os.MkdirAll(s.Home, 0700); err != nil {
return fmt.Errorf("failed to create %s: %w", s.Home, err)
}
if err := s.UpdateConfig(); err != nil {
return err
}
if err := os.WriteFile(filepath.Join(s.Home, certFile), cert, 0600); err != nil {
return fmt.Errorf("failed to write syncthing certificate: %w", err)
}
if err := os.WriteFile(filepath.Join(s.Home, keyFile), key, 0600); err != nil {
return fmt.Errorf("failed to write syncthing key: %w", err)
}
return nil
}
// UpdateConfig updates the syncthing config file
func (s *Syncthing) UpdateConfig() error {
buf := new(bytes.Buffer)
if err := configTemplate.Execute(buf, s); err != nil {
return fmt.Errorf("failed to write syncthing configuration template: %w", err)
}
if err := os.WriteFile(filepath.Join(s.Home, configFile), buf.Bytes(), 0600); err != nil {
return fmt.Errorf("failed to write syncthing configuration file: %w", err)
}
return nil
}
// Run starts up a local syncthing process to serve files from.
func (s *Syncthing) Run() error {
if err := s.initConfig(); err != nil {
return err
}
if s.ResetDatabase {
cmd := exec.Command(s.binPath, "-home", s.Home, "-reset-database")
output, err := cmd.CombinedOutput()
if err != nil {
oktetoLog.Errorf("error resetting syncthing database: %s\n%s", err.Error(), output)
}
}
cmdArgs := []string{
"-home", s.Home,
"-no-browser",
"-logfile", s.LogPath,
"-log-max-old-files=0",
}
if s.Verbose {
cmdArgs = append(cmdArgs, "-verbose")
}
s.cmd = exec.Command(s.binPath, cmdArgs...) // nolint: gas, gosec
s.cmd.Env = append(os.Environ(), "STNOUPGRADE=1")
if err := s.cmd.Start(); err != nil {
return fmt.Errorf("failed to start syncthing: %w", err)
}
if s.cmd.Process == nil {
return nil
}
s.pid = s.cmd.Process.Pid
return nil
}
// WaitForPing waits for syncthing to be ready
func (s *Syncthing) WaitForPing(ctx context.Context, local bool) error {
ticker := time.NewTicker(300 * time.Millisecond)
to := time.Now().Add(s.timeout)
oktetoLog.Infof("waiting for syncthing local=%t to be ready", local)
for retries := 0; ; retries++ {
select {
case <-ticker.C:
if s.Ping(ctx, local) {
oktetoLog.Infof("syncthing local=%t is ready", local)
return nil
}
if retries%5 == 0 {
oktetoLog.Infof("syncthing local=%t is not ready yet", local)
}
if time.Now().After(to) && retries > 10 {
// before returning a generic error, we try detecting why syncthing is not ready and return a more accurate error
errDetected := s.IdentifyReadinessIssue()
if errDetected != nil {
return errDetected
}
return fmt.Errorf("syncthing local=%t didn't respond after %s", local, s.timeout.String())
}
case <-ctx.Done():
oktetoLog.Infof("syncthing.WaitForPing cancelled local=%t", local)
return ctx.Err()
}
}
}
// IdentifyReadinessIssue attempts to identify the issue that is preventing syncthing from being ready
func (s *Syncthing) IdentifyReadinessIssue() error {
if s.RegexMatchesLogs(regexErrOpeningDatabase) {
return oktetoErrors.ErrInsufficientSpaceOnUserDisk
}
if s.RegexMatchesLogs(regexInsufficientSpace) {
return oktetoErrors.ErrInsufficientSpaceOnUserDisk
}
return nil
}
// RegexMatchesLogs checks if a regex matches in the syncthing logs
func (s *Syncthing) RegexMatchesLogs(regx *regexp.Regexp) bool {
lines, err := filesystem.GetLastNLines(s.Fs, s.LogPath, maxLogTailLinesToRead, maxLogTailChunkByteSize)
if err != nil {
oktetoLog.Infof("error reading syncthing log: %s", err)
return false
}
for _, log := range lines {
if regx.MatchString(log) {
return true
}
}
return false
}
// Ping checks if syncthing is available
func (s *Syncthing) Ping(ctx context.Context, local bool) bool {
_, err := s.APICall(ctx, "rest/system/ping", "GET", http.StatusOK, nil, local, nil, false, 0)
if err == nil {
return true
}
if strings.Contains(err.Error(), "Client.Timeout") {
return true
}
oktetoLog.Infof("error pinging syncthing: %s", err.Error())
return false
}
// Overwrite overwrites local changes to the remote syncthing
func (s *Syncthing) Overwrite(ctx context.Context) error {
for _, folder := range s.Folders {
oktetoLog.Infof("overriding local changes to the remote syncthing path=%s", folder.LocalPath)
params := getFolderParameter(folder)
_, err := s.APICall(ctx, "rest/db/override", "POST", http.StatusOK, params, true, nil, false, maxRetries)
if err != nil {
oktetoLog.Infof("error posting 'rest/db/override' syncthing API: %s", err)
if strings.Contains(err.Error(), "Client.Timeout") {
return oktetoErrors.ErrBusySyncthing
}
return oktetoErrors.ErrLostSyncthing
}
folder.Overwritten = true
}
return nil
}
// IsAllOverwritten checks if all overwrite operations has been completed
func (s *Syncthing) IsAllOverwritten() bool {
for _, folder := range s.Folders {
if !folder.Overwritten {
return false
}
}
return true
}
// WaitForConnected waits for local and remote syncthing to be connected
func (s *Syncthing) WaitForConnected(ctx context.Context) error {
ticker := time.NewTicker(100 * time.Millisecond)
oktetoLog.Info("waiting for remote device to be connected")
to := time.Now().Add(s.timeout)
for retries := 0; ; retries++ {
connections := &Connections{}
body, err := s.APICall(ctx, "rest/system/connections", "GET", http.StatusOK, nil, true, nil, true, maxRetries)
if err != nil {
oktetoLog.Infof("error getting connections: %s", err.Error())
if strings.Contains(err.Error(), "Client.Timeout") {
return oktetoErrors.ErrBusySyncthing
}
return oktetoErrors.ErrLostSyncthing
}
err = json.Unmarshal(body, connections)
if err != nil {
oktetoLog.Infof("error unmarshalling connections: %s", err.Error())
return oktetoErrors.ErrLostSyncthing
}
if connection, ok := connections.Connections[DefaultRemoteDeviceID]; ok {
if connection.Connected {
return nil
}
}
if time.Now().After(to) && retries > 10 {
oktetoLog.Infof("remote syncthing connection not completed after %s, please try again", s.timeout.String())
return oktetoErrors.ErrLostSyncthing
}
select {
case <-ticker.C:
continue
case <-ctx.Done():
oktetoLog.Info("call to syncthing.WaitForConnected canceled")
return ctx.Err()
}
}
}
// WaitForScanning waits for syncthing to finish initial scanning
func (s *Syncthing) WaitForScanning(ctx context.Context, local bool) error {
for _, folder := range s.Folders {
if err := s.waitForFolderScanning(ctx, folder, local); err != nil {
return err
}
}
return nil
}
func (s *Syncthing) waitForFolderScanning(ctx context.Context, folder *Folder, local bool) error {
ticker := time.NewTicker(100 * time.Millisecond)
oktetoLog.Infof("waiting for initial scan to complete path=%s local=%t", folder.LocalPath, local)
timeoutDuration := s.timeout * 10
to := time.Now().Add(timeoutDuration) // 5 minutes
for retries := 0; ; retries++ {
status, err := s.GetSyncthingStatus(ctx, folder, local)
if err != nil && err != oktetoErrors.ErrBusySyncthing {
return err
}
if status != "" {
if retries%100 == 0 {
// one log every 10 seconds
oktetoLog.Infof("syncthing folder local=%t is '%s'", local, status)
}
if status != "scanning" && status != "scan-waiting" {
oktetoLog.Infof("syncthing folder local=%t finished scanning: '%s'", local, status)
return nil
}
}
if time.Now().After(to) && retries > 10 {
return fmt.Errorf("initial file scan not completed after %s, please try again", s.timeout.String())
}
select {
case <-ticker.C:
continue
case <-ctx.Done():
oktetoLog.Info("call to syncthing.waitForFolderScanning canceled")
return ctx.Err()
}
}
}
// GetCompletion returns the syncthing completion
func (s *Syncthing) GetCompletion(ctx context.Context, local bool, device string) (*Completion, error) {
params := map[string]string{"device": device}
completion := &Completion{}
body, err := s.APICall(ctx, "rest/db/completion", "GET", http.StatusOK, params, local, nil, true, maxRetries)
if err != nil {
oktetoLog.Infof("error calling 'rest/db/completion' local=%t syncthing API: %s", local, err)
if strings.Contains(err.Error(), "Client.Timeout") {
return nil, oktetoErrors.ErrBusySyncthing
}
return nil, oktetoErrors.ErrLostSyncthing
}
err = json.Unmarshal(body, completion)
if err != nil {
oktetoLog.Infof("error unmarshalling 'rest/db/completion' local=%t syncthing API: %s", local, err)
return nil, oktetoErrors.ErrLostSyncthing
}
return completion, nil
}
// IsHealthy returns the syncthing error or nil
func (s *Syncthing) IsHealthy(ctx context.Context, local bool, max int) error {
pullErrors, err := s.GetPullErrors(ctx, local)
if err != nil {
if err == oktetoErrors.ErrBusySyncthing {
return nil
}
return err
}
if pullErrors == 0 {
isHealthyRetries = 0
return nil
}
isHealthyRetries++
err = s.GetFolderErrors(ctx, local)
if err != nil {
oktetoLog.Infof("syncthing error local=%t retry %d: %s", local, isHealthyRetries, err.Error())
}
if err == oktetoErrors.ErrInsufficientSpace {
return err
}
if isHealthyRetries <= max {
return nil
}
if err == nil || err == oktetoErrors.ErrBusySyncthing {
return oktetoErrors.ErrUnknownSyncError
}
return err
}
// GetSyncthingStatus returns the syncthing status
func (s *Syncthing) GetSyncthingStatus(ctx context.Context, folder *Folder, local bool) (string, error) {
params := map[string]string{
"folder": GetFolderName(folder),
"since": "0",
"limit": "1",
"timeout": "0",
"events": "StateChanged",
}
scList := []StateChangedEvent{}
body, err := s.APICall(ctx, "rest/events", "GET", http.StatusOK, params, local, nil, true, maxRetries)
if err != nil {
oktetoLog.Infof("error getting events: %s", err.Error())
if strings.Contains(err.Error(), "Client.Timeout") {
return "", oktetoErrors.ErrBusySyncthing
}
return "", oktetoErrors.ErrLostSyncthing
}
err = json.Unmarshal(body, &scList)
if err != nil {
oktetoLog.Infof("error unmarshalling events: %s", err.Error())
return "", oktetoErrors.ErrLostSyncthing
}
if len(scList) != 0 {
return scList[0].Data.State, nil
}
if v, ok := cachedStatus[local]; ok {
return v, nil
}
delete(params, "events")
delete(params, "limit")
body, err = s.APICall(ctx, "rest/events", "GET", http.StatusOK, params, local, nil, true, maxRetries)
if err != nil {
oktetoLog.Infof("error getting events: %s", err.Error())
if strings.Contains(err.Error(), "Client.Timeout") {
return "", oktetoErrors.ErrBusySyncthing
}
return "", oktetoErrors.ErrLostSyncthing
}
err = json.Unmarshal(body, &scList)
if err != nil {
oktetoLog.Infof("error unmarshalling events: %s", err.Error())
return "", oktetoErrors.ErrLostSyncthing
}
for i := len(scList) - 1; i >= 0; i-- {
if scList[i].Type == "StateChanged" {
cachedStatus[local] = scList[i].Data.State
return cachedStatus[local], nil
}
}
cachedStatus[local] = "scanning"
return cachedStatus[local], nil
}
// GetPullErrors returns the syncthing current pull errors
func (s *Syncthing) GetPullErrors(ctx context.Context, local bool) (int64, error) {
params := map[string]string{
"since": "0",
"limit": "1",
"timeout": "0",
"events": "FolderSummary",
}
fsList := []FolderSummaryEvent{}
body, err := s.APICall(ctx, "rest/events", "GET", http.StatusOK, params, local, nil, true, maxRetries)
if err != nil {
oktetoLog.Infof("error getting events: %s", err.Error())
if strings.Contains(err.Error(), "Client.Timeout") {
return 0, oktetoErrors.ErrBusySyncthing
}
return 0, oktetoErrors.ErrLostSyncthing
}
err = json.Unmarshal(body, &fsList)
if err != nil {
oktetoLog.Infof("error unmarshalling events: %s", err.Error())
return 0, oktetoErrors.ErrLostSyncthing
}
if len(fsList) != 0 {
return fsList[0].Data.Summary.PullErrors, nil
}
if v, ok := cachedPullErrors[local]; ok {
return v, nil
}
delete(params, "events")
delete(params, "limit")
body, err = s.APICall(ctx, "rest/events", "GET", http.StatusOK, params, local, nil, true, maxRetries)
if err != nil {
oktetoLog.Infof("error getting events: %s", err.Error())
if strings.Contains(err.Error(), "Client.Timeout") {
return 0, oktetoErrors.ErrBusySyncthing
}
return 0, oktetoErrors.ErrLostSyncthing
}
err = json.Unmarshal(body, &fsList)
if err != nil {
oktetoLog.Infof("error unmarshalling events: %s", err.Error())
return 0, oktetoErrors.ErrLostSyncthing
}
for i := len(fsList) - 1; i >= 0; i-- {
if fsList[i].Type == "FolderSummary" {
cachedPullErrors[local] = fsList[i].Data.Summary.PullErrors
return cachedPullErrors[local], nil
}
}
cachedPullErrors[local] = 0
return cachedPullErrors[local], nil
}
// GetFolderErrors returns the last folder errors
func (s *Syncthing) GetFolderErrors(ctx context.Context, local bool) error {
params := map[string]string{
"since": "0",
"limit": "1",
"timeout": "0",
"events": "FolderErrors",
}
folderErrorsList := []FolderErrorEvent{}
body, err := s.APICall(ctx, "rest/events", "GET", http.StatusOK, params, local, nil, true, maxRetries)
if err != nil {
oktetoLog.Infof("error getting events: %s", err.Error())
if strings.Contains(err.Error(), "Client.Timeout") {
return oktetoErrors.ErrBusySyncthing
}
return oktetoErrors.ErrLostSyncthing
}
err = json.Unmarshal(body, &folderErrorsList)
if err != nil {
oktetoLog.Infof("error unmarshalling events: %s", err.Error())
return oktetoErrors.ErrLostSyncthing
}
folderErrors := FolderErrorEvent{
Data: DataFolderErrorEvent{},
}
if len(folderErrorsList) != 0 {
folderErrors = folderErrorsList[0]
} else {
if v, ok := cachedFolderErrors[local]; ok {
folderErrors = v
} else {
delete(params, "events")
delete(params, "limit")
body, err = s.APICall(ctx, "rest/events", "GET", http.StatusOK, params, local, nil, true, maxRetries)
if err != nil {
oktetoLog.Infof("error getting events: %s", err.Error())
if strings.Contains(err.Error(), "Client.Timeout") {
return oktetoErrors.ErrBusySyncthing
}
return oktetoErrors.ErrLostSyncthing
}
err = json.Unmarshal(body, &folderErrorsList)
if err != nil {
oktetoLog.Infof("error unmarshalling events: %s", err.Error())
return oktetoErrors.ErrLostSyncthing
}
cachedFolderErrors[local] = folderErrors
for i := len(folderErrorsList) - 1; i >= 0; i-- {
if folderErrorsList[i].Type == "FolderErrors" {
cachedFolderErrors[local] = folderErrorsList[i]
folderErrors = cachedFolderErrors[local]
}
}
}
}
if len(folderErrors.Data.Errors) == 0 {
return nil
}
errMsg := folderErrors.Data.Errors[0].Error
if strings.Contains(errMsg, "insufficient space") {
oktetoLog.Infof("syncthing insufficient space local=%t: %s", local, errMsg)
return oktetoErrors.ErrInsufficientSpace
}
oktetoLog.Infof("syncthing pull error local=%t: %s", local, errMsg)
return fmt.Errorf("%s: %s", folderErrors.Data.Errors[0].Path, errMsg)
}
// GetSystemErrors returns the system errors identified by syncthing
func (s *Syncthing) GetSystemErrors(ctx context.Context, local bool) (*SystemErrors, error) {
var sysErrs *SystemErrors
body, err := s.APICall(ctx, "rest/system/error", "GET", http.StatusOK, nil, local, nil, true, maxRetries)
if err != nil {
oktetoLog.Infof("error getting system errors: %s", err.Error())
if strings.Contains(err.Error(), "Client.Timeout") {
return nil, oktetoErrors.ErrBusySyncthing
}
return nil, oktetoErrors.ErrLostSyncthing
}
err = json.Unmarshal(body, &sysErrs)
if err != nil {
oktetoLog.Infof("error unmarshalling system errors: %s", err.Error())
return nil, oktetoErrors.ErrLostSyncthing
}
return sysErrs, nil
}
func (s *Syncthing) IsLocalRunningOutOfSpace(ctx context.Context) bool {
sysErrs, err := s.GetSystemErrors(ctx, true)
if err != nil {
oktetoLog.Infof("error getting system errors: %s", err.Error())
return false
}
for _, sysErr := range sysErrs.Errors {
if strings.Contains(sysErr.Message, "insufficient space") {
return true
}
}
return false
}
// GetInSynchronizationFile the files syncthing
func (s *Syncthing) GetInSynchronizationFile(ctx context.Context) string {
events := []ItemEvent{}
params := map[string]string{
"device": DefaultRemoteDeviceID,
"since": "0",
"limit": "1",
"timeout": "0",
"events": "DownloadProgress",
}
body, err := s.APICall(ctx, "rest/events", "GET", http.StatusOK, params, false, nil, true, 0)
if err != nil {
oktetoLog.Infof("error getting GetInSynchronizationItem: %s", err.Error())
return ""
}
if err := json.Unmarshal(body, &events); err != nil {
oktetoLog.Infof("error unmarshalling events: %s", err.Error())
return ""
}
if len(events) == 0 {
return ""
}
return getInSynchronizationLargestFile(events[len(events)-1])
}
func getInSynchronizationLargestFile(e ItemEvent) string {
result := ""
var largerFileSize int64
for _, folderStatus := range e.Data {
for fileName := range folderStatus {
fileSize := folderStatus[fileName].BytesTotal
if fileSize > largerFileSize {
result = fileName
largerFileSize = fileSize
}
}
}
return result
}
// Restart restarts the syncthing process
func (s *Syncthing) Restart(ctx context.Context) error {
_, err := s.APICall(ctx, "rest/system/restart", "POST", http.StatusOK, nil, true, nil, false, maxRetries)
return err
}
// HardTerminate halts the background process, waits for 1s and kills the process if it is still running
func (s *Syncthing) HardTerminate() error {
oktetoLog.Info("termitating previous syncthing process")
pList, err := process.Processes()
if err != nil {
return err
}
for _, p := range pList {
if p.Pid == 0 {
continue
}
name, err := p.Name()
if err != nil {
// it's expected go get EOF if the process no longer exists at this point.
if err != io.EOF {
oktetoLog.Infof("error getting name for process %d: %s", p.Pid, err.Error())
}
continue
}
if name == "" {
oktetoLog.Infof("ignoring pid %d with no name: %v", p.Pid, p)
continue
}
if !strings.Contains(name, "syncthing") {
continue
}
cmdline, err := p.Cmdline()
if err != nil {
return err
}
oktetoLog.Infof("checking syncthing home '%s' with command '%s'", s.Home, cmdline)
if !strings.Contains(cmdline, fmt.Sprintf("-home %s", s.Home)) {
continue
}
oktetoLog.Infof("terminating syncthing %d with wait: %s", p.Pid, s.Home)
if err := terminate(p, true); err != nil {
oktetoLog.Infof("error terminating syncthing %d with wait: %s", p.Pid, err.Error())
}
oktetoLog.Infof("terminated syncthing %d with wait: %s", p.Pid, s.Home)
}
return nil
}
// SoftTerminate halts the background process
func (s *Syncthing) SoftTerminate() error {
if s.pid == 0 {
return nil
}
p, err := process.NewProcess(int32(s.pid))
if err != nil {
return fmt.Errorf("error getting syncthing process %d: %w", s.pid, err)
}
oktetoLog.Infof("terminating syncthing %d without wait", s.pid)
if err := terminate(p, false); err != nil {
return fmt.Errorf("error terminating syncthing %d without wait: %w", p.Pid, err)
}
oktetoLog.Infof("terminated syncthing %d without wait", s.pid)
return nil
}
// SaveConfig saves the syncthing object in the dev home folder
func (s *Syncthing) SaveConfig(dev *model.Dev) error {
marshalled, err := yaml.Marshal(s)
if err != nil {
return err
}
syncthingInfoFile := getInfoFile(dev.Namespace, dev.Name)
if err := os.WriteFile(syncthingInfoFile, marshalled, 0600); err != nil {
return fmt.Errorf("failed to write syncthing info file: %w", err)
}
return nil
}
// Load loads the syncthing object from the dev home folder
func Load(dev *model.Dev) (*Syncthing, error) {
syncthingInfoFile := getInfoFile(dev.Namespace, dev.Name)
b, err := os.ReadFile(syncthingInfoFile)
if err != nil {
return nil, err
}
s := &Syncthing{
Client: NewAPIClient(),
}
if err := yaml.Unmarshal(b, s); err != nil {
return nil, err
}
return s, nil
}
// RemoveFolder deletes all the files created by the syncthing instance
func RemoveFolder(dev *model.Dev, fs afero.Fs) error {
s, err := New(dev, fs)
if err != nil {
return fmt.Errorf("failed to create syncthing instance")
}
if s.Home == "" {
oktetoLog.Info("the home directory is not set when deleting")
return nil