This repository was archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 605
Expand file tree
/
Copy pathcache.go
More file actions
238 lines (201 loc) · 6.41 KB
/
Copy pathcache.go
File metadata and controls
238 lines (201 loc) · 6.41 KB
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
package remote
import (
"errors"
"fmt"
"path"
"path/filepath"
"github.com/koding/kite"
"github.com/koding/kite/dnode"
"github.com/koding/logging"
"koding/klient/remote/machine"
"koding/klient/remote/mount"
"koding/klient/remote/req"
"koding/klient/remote/rsync"
)
// CacheFolderHandler implements a prefetching / caching mechanism, currently
// implemented
func (r *Remote) CacheFolderHandler(kreq *kite.Request) (interface{}, error) {
log := logging.NewLogger("remote").New("remote.cacheFolder")
var params struct {
req.Cache
// klient uses vendored version of dnode with path rewrite that's not
// compatible with other apps, hence we embed common fields into req.Cache
// and specify dnode.Function by itself
Progress dnode.Function `json:"progress"`
}
if kreq.Args == nil {
return nil, errors.New("Required arguments were not passed.")
}
if err := kreq.Args.One().Unmarshal(¶ms); err != nil {
err = fmt.Errorf(
"remote.cacheFolder: Error '%s' while unmarshalling request '%s'\n",
err, kreq.Args.One(),
)
r.log.Error(err.Error())
return nil, err
}
if params.Debug {
log.SetLevel(logging.DEBUG)
}
switch {
case params.Name == "":
return nil, errors.New("Missing required argument `name`.")
case params.LocalPath == "":
return nil, errors.New("Missing required argument `localPath`.")
case params.Username == "":
return nil, errors.New("Missing required argument `username`.")
case params.SSHAuthSock == "":
return nil, errors.New("Missing required argument `sshAuthSock`.")
}
log = log.New(
"mountName", params.Name,
"localPath", params.LocalPath,
)
remoteMachine, err := r.GetDialedMachine(params.Name)
if err != nil {
log.Error("Error getting dialed, valid machine. err:%s", err)
return nil, err
}
if params.RemotePath == "" {
home, err := remoteMachine.HomeWithDefault()
if err != nil {
return nil, err
}
params.RemotePath = home
}
if !filepath.IsAbs(params.RemotePath) {
home, err := remoteMachine.HomeWithDefault()
if err != nil {
return nil, err
}
params.RemotePath = path.Join(home, params.RemotePath)
}
if !params.LocalToRemote {
exists, err := remoteMachine.DoesRemotePathExist(params.RemotePath)
if err != nil {
return nil, err
}
if !exists {
return nil, mount.ErrRemotePathDoesNotExist
}
}
var remoteSize int64
if params.LocalToRemote {
remoteSize, err = getSizeOfLocalPath(params.LocalPath)
if err != nil {
return nil, err
}
} else {
remoteSize, err = remoteMachine.GetFolderSize(params.RemotePath)
if err != nil {
return nil, err
} else {
log.Debug("Remote path %q is size: %d", params.RemotePath, remoteSize)
}
}
// If there is an actively running intervaler, run the requested cache
// *between* intervals. Locking to prevent any conflicts between the cache
// implementation.
runBetweenIntervals := remoteMachine.Intervaler != nil && params.Interval == 0
// If there is an interval already running, we may need to stop or pause it.
replaceIntervaler := remoteMachine.Intervaler != nil && params.Interval != 0
if replaceIntervaler {
log.Info("Unsubscribing from existing Sync Intervaler to replace it.")
remoteMachine.Intervaler.Stop()
}
rs := rsync.NewClient(log)
syncOpts := rsync.SyncIntervalOpts{
SyncOpts: rsync.SyncOpts{
Host: remoteMachine.IP,
Username: params.Username,
RemoteDir: params.RemotePath,
LocalDir: params.LocalPath,
SSHAuthSock: params.SSHAuthSock,
SSHPrivateKeyPath: params.SSHPrivateKeyPath,
DirSize: remoteSize,
LocalToRemote: params.LocalToRemote,
IgnoreFile: params.IgnoreFile,
IncludePath: params.IncludePath,
},
Interval: params.Interval,
}
if params.OnlyInterval {
startIntervalerIfNeeded(log, remoteMachine, rs, syncOpts)
return nil, nil
}
log.Info("Caching remote via RSync, with options:%#v", syncOpts)
progCh := rs.Sync(syncOpts.SyncOpts)
// If a valid callback is not provided, this method blocks until the data is done
// transferring.
if !params.Progress.IsValid() {
log.Debug(
"Progress callback is not valid. Running remote.cache in synchronous mode.",
)
// If there is an existing Intervaler, lock it for the duration of this
// synchronous method.
if runBetweenIntervals {
remoteMachine.Intervaler.Lock()
defer remoteMachine.Intervaler.Unlock()
}
// For predictable behavior we log any errors, but do not immediately return on
// them. If we return early, RSync may still be running - by blocking until the
// channel is closed, we ensure that this method, in blocking form, only returns
// after RSync is done.
var err error
for p := range progCh {
if p.Error.Message != "" {
log.Error(
"Error encountered in blocking remote.cache. progress:%d, err:%s",
p.Progress, p.Error.Message,
)
err = errors.New(p.Error.Message)
}
}
// After the progress chan is done, start our SyncInterval
startIntervalerIfNeeded(r.log, remoteMachine, rs, syncOpts)
return nil, err
}
go func() {
log.Debug(
"Progress callback is valid. Running remote.cache in asynchronous mode.",
)
// If there is an existing Intervaler, lock it for the duration of this synchronous
// method.
if runBetweenIntervals {
remoteMachine.Intervaler.Lock()
defer remoteMachine.Intervaler.Unlock()
}
for p := range progCh {
if p.Error.Message != "" {
log.Error(
"Error encountered in nonblocking remote.cache. progress:%d, err:%s",
p.Progress, p.Error.Message,
)
}
params.Progress.Call(p)
}
// After the progress chan is done, start our SyncInterval
startIntervalerIfNeeded(log, remoteMachine, rs, syncOpts)
}()
return nil, nil
}
// startIntervalerIfNeeded starts the given rsync interval, logs any errors, and adds the
// resulting Intervaler to the Mount struct for later Stoppage.
func startIntervalerIfNeeded(log logging.Logger, remoteMachine *machine.Machine, c *rsync.Client, opts rsync.SyncIntervalOpts) {
log = log.New("startIntervalerIfNeeded")
if opts.Interval <= 0 {
// Using debug, because this is not an error - just informative.
log.Debug(
"startIntervalerIfNeeded() called with interval:%d. Cannot start Intervaler",
opts.Interval,
)
return
}
log.Info("Creating and starting RSync SyncInterval")
intervaler, err := c.SyncInterval(opts)
if err != nil {
log.Error("rsync SyncInterval returned an error:%s", err)
return
}
remoteMachine.Intervaler = intervaler
}