forked from lsyncd/lsyncd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
default-rsyncssh.lua
633 lines (527 loc) · 11.2 KB
/
default-rsyncssh.lua
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
--
-- default-rsyncssh.lua
--
-- Improved rsync - sync with rsync, but moves and deletes executed over ssh.
-- A (Layer 1) configuration.
--
-- Note:
-- this is infact just a configuration using Layer 1 configuration
-- like any other. It only gets compiled into the binary by default.
-- You can simply use a modified one, by copying everything into a
-- config file of yours and name it differently.
--
-- License: GPLv2 (see COPYING) or any later version
-- Authors: Axel Kittenberger <[email protected]>
--
--
if not default
then
error( 'default not loaded' );
end
if not default.rsync
then
error( 'default.rsync not loaded' );
end
if default.rsyncssh
then
error( 'default-rsyncssh already loaded' );
end
--
-- rsyncssh extends default.rsync
--
local rsyncssh = { default.rsync }
default.rsyncssh = rsyncssh
--
-- used to ensure there aren't typos in the keys
--
rsyncssh.checkgauge = {
-- unsets the inherited value of from default.rsync
target = false,
onMove = true,
-- rsyncssh users host and targetdir
host = true,
targetdir = true,
sshExitCodes = true,
rsyncExitCodes = true,
-- ssh settings
ssh = {
binary = true,
identityFile = true,
options = true,
port = true,
_extra = true
},
}
--
-- Returns true for non Init, Blanket and Move events.
--
local eventNotInitBlankMove =
function
(
event
)
-- TODO use a table
if event.etype == 'Move'
or event.etype == 'Init'
or event.etype == 'Blanket'
then
return 'break'
else
return true
end
end
--
-- Replaces what rsync would consider filter rules by literals.
--
local replaceRsyncFilter =
function
(
path
)
if not path then return end
return(
path
:gsub( '%?', '\\?' )
:gsub( '%*', '\\*' )
:gsub( '%[', '\\[' )
)
end
--
-- Spawns rsync for a list of events
--
rsyncssh.action = function
(
inlet
)
local config = inlet.getConfig( )
local event, event2 = inlet.getEvent( )
-- makes move local on target host
-- if the move fails, it deletes the source
if event.etype == 'Move'
then
local path1 = config.targetdir .. event.path
local path2 = config.targetdir .. event2.path
path1 = "'" .. path1:gsub ('\'', '\'"\'"\'') .. "'"
path2 = "'" .. path2:gsub ('\'', '\'"\'"\'') .. "'"
log(
'Normal',
'Moving ',
event.path,
' -> ',
event2.path
)
spawn(
event,
config.ssh.binary,
config.ssh._computed,
config.host,
'mv',
path1,
path2,
'||', 'rm', '-rf',
path1
)
return
end
-- otherwise a rsync is spawned
local elist = inlet.getEvents( eventNotInitBlankMove )
-- gets the list of paths for the event list
-- deletes create multi match patterns
local paths = elist.getPaths( )
--
-- Replaces what rsync would consider filter rules by literals
--
local function sub( p )
if not p then return end
return p:
gsub( '%?', '\\?' ):
gsub( '%*', '\\*' ):
gsub( '%[', '\\[' ):
gsub( '%]', '\\]' )
end
--
-- Gets the list of paths for the event list
--
-- Deletes create multi match patterns
--
local paths = elist.getPaths(
function( etype, path1, path2 )
if string.byte( path1, -1 ) == 47 and etype == 'Delete' then
return sub( path1 )..'***', sub( path2 )
else
return sub( path1 ), sub( path2 )
end
end
)
-- stores all filters by integer index
local filterI = { }
-- stores all filters with path index
local filterP = { }
-- adds one path to the filter
local function addToFilter( path )
if filterP[ path ] then return end
filterP[ path ] = true
table.insert( filterI, path )
end
-- adds a path to the filter.
--
-- rsync needs to have entries for all steps in the path,
-- so the file for example d1/d2/d3/f1 needs following filters:
-- 'd1/', 'd1/d2/', 'd1/d2/d3/' and 'd1/d2/d3/f1'
for _, path in ipairs( paths )
do
if path and path ~= ''
then
addToFilter(path)
local pp = string.match( path, '^(.*/)[^/]+/?' )
while pp
do
addToFilter(pp)
pp = string.match( pp, '^(.*/)[^/]+/?' )
end
end
end
log(
'Normal',
'Calling rsync with filter-list of new/modified files/dirs\n',
table.concat( filterI, '\n' )
)
local config = inlet.getConfig( )
local delete = nil
if config.delete == true or config.delete == 'running'
then
delete = { '--delete', '--ignore-errors' }
end
spawn(
elist,
config.rsync.binary,
'<', table.concat( filterI, '\000' ),
config.rsync._computed,
'-r',
delete,
'--force',
'--from0',
'--include-from=-',
'--exclude=*',
config.source,
config.host .. ':' .. config.targetdir
)
end
----
---- NOTE: This optimized version can be used once
---- https://bugzilla.samba.org/show_bug.cgi?id=12569
---- is fixed.
----
--
-- Spawns rsync for a list of events
--
--rsyncssh.action = function
--(
-- inlet
--)
-- local config = inlet.getConfig( )
--
-- local event, event2 = inlet.getEvent( )
--
-- -- makes move local on target host
-- -- if the move fails, it deletes the source
-- if event.etype == 'Move'
-- then
-- local path1 = config.targetdir .. event.path
--
-- local path2 = config.targetdir .. event2.path
--
-- path1 = "'" .. path1:gsub ('\'', '\'"\'"\'') .. "'"
-- path2 = "'" .. path2:gsub ('\'', '\'"\'"\'') .. "'"
--
-- log(
-- 'Normal',
-- 'Moving ',
-- event.path,
-- ' -> ',
-- event2.path
-- )
--
-- spawn(
-- event,
-- config.ssh.binary,
-- config.ssh._computed,
-- config.host,
-- 'mv',
-- path1,
-- path2,
-- '||', 'rm', '-rf',
-- path1
-- )
--
-- return
-- end
--
-- -- otherwise a rsync is spawned
-- local elist = inlet.getEvents( eventNotInitBlankMove )
--
-- -- gets the list of paths for the event list
-- -- deletes create multi match patterns
-- local paths = elist.getPaths( )
--
-- -- removes trailing slashes from dirs.
-- for k, v in ipairs( paths )
-- do
-- if string.byte( v, -1 ) == 47
-- then
-- paths[ k ] = string.sub( v, 1, -2 )
-- end
-- end
--
-- log(
-- 'Normal',
-- 'Rsyncing list\n',
-- table.concat( paths, '\n' )
-- )
--
-- local delete = nil
--
-- if config.delete == true
-- or config.delete == 'running'
-- then
-- delete = { '--delete-missing-args', '--ignore-errors' }
-- end
--
-- spawn(
-- elist,
-- config.rsync.binary,
-- '<', table.concat( paths, '\000' ),
-- config.rsync._computed,
-- delete,
-- '--force',
-- '--from0',
-- '--files-from=-',
-- config.source,
-- config.host .. ':' .. config.targetdir
-- )
--end
--
-- Called when collecting a finished child process
--
rsyncssh.collect = function
(
agent,
exitcode
)
local config = agent.config
if not agent.isList and agent.etype == 'Init'
then
local rc = config.rsyncExitCodes[exitcode]
if rc == 'ok'
then
log( 'Normal', 'Startup of "', agent.source, '" finished: ', exitcode )
elseif rc == 'again'
then
if settings('insist')
then
log( 'Normal', 'Retrying startup of "', agent.source, '": ', exitcode )
else
log(
'Error',
'Temporary or permanent failure on startup of "',
agent.source, '". Terminating since "insist" is not set.'
)
terminate( -1 ) -- ERRNO
end
elseif rc == 'die'
then
log( 'Error', 'Failure on startup of "', agent.source, '": ', exitcode )
else
log( 'Error', 'Unknown exitcode on startup of "', agent.source, ': "', exitcode )
rc = 'die'
end
return rc
end
if agent.isList
then
local rc = config.rsyncExitCodes[ exitcode ]
if rc == 'ok'
then
log( 'Normal', 'Finished (list): ', exitcode )
elseif rc == 'again'
then
log( 'Normal', 'Retrying (list): ', exitcode )
elseif rc == 'die'
then
log( 'Error', 'Failure (list): ', exitcode )
else
log( 'Error', 'Unknown exitcode (list): ', exitcode )
rc = 'die'
end
return rc
else
local rc = config.sshExitCodes[exitcode]
if rc == 'ok'
then
log( 'Normal', 'Finished ', agent.etype,' ', agent.sourcePath, ': ', exitcode )
elseif rc == 'again'
then
log( 'Normal', 'Retrying ', agent.etype, ' ', agent.sourcePath, ': ', exitcode )
elseif rc == 'die'
then
log( 'Normal', 'Failure ', agent.etype, ' ', agent.sourcePath, ': ', exitcode )
else
log( 'Error', 'Unknown exitcode ', agent.etype,' ', agent.sourcePath,': ', exitcode )
rc = 'die'
end
return rc
end
end
--
-- checks the configuration.
--
rsyncssh.prepare = function
(
config,
level
)
default.rsync.prepare( config, level + 1, true )
if not config.host
then
error( 'default.rsyncssh needs "host" configured', level )
end
if not config.targetdir
then
error( 'default.rsyncssh needs "targetdir" configured', level )
end
--
-- computes the ssh options
--
if config.ssh._computed
then
error( 'please do not use the internal rsync._computed parameter', level )
end
if config.maxProcesses ~= 1
then
error( 'default.rsyncssh must have maxProcesses set to 1.', level )
end
local cssh = config.ssh;
cssh._computed = { }
local computed = cssh._computed
local computedN = 1
local rsyncc = config.rsync._computed
if cssh.identityFile
then
computed[ computedN ] = '-i'
computed[ computedN + 1 ] = cssh.identityFile
computedN = computedN + 2
if not config.rsync._rshIndex
then
config.rsync._rshIndex = #rsyncc + 1
rsyncc[ config.rsync._rshIndex ] = '--rsh=ssh'
end
rsyncc[ config.rsync._rshIndex ] =
rsyncc[ config.rsync._rshIndex ] ..
' -i ' ..
cssh.identityFile
end
if cssh.options
then
for k, v in pairs( cssh.options )
do
computed[ computedN ] = '-o'
computed[ computedN + 1 ] = k .. '=' .. v
computedN = computedN + 2
if not config.rsync._rshIndex
then
config.rsync._rshIndex = #rsyncc + 1
rsyncc[ config.rsync._rshIndex ] = '--rsh=ssh'
end
rsyncc[ config.rsync._rshIndex ] =
table.concat(
{
rsyncc[ config.rsync._rshIndex ],
' -o ',
k,
'=',
v
},
''
)
end
end
if cssh.port
then
computed[ computedN ] = '-p'
computed[ computedN + 1 ] = cssh.port
computedN = computedN + 2
if not config.rsync._rshIndex
then
config.rsync._rshIndex = #rsyncc + 1
rsyncc[ config.rsync._rshIndex ] = '--rsh=ssh'
end
rsyncc[ config.rsync._rshIndex ] =
rsyncc[ config.rsync._rshIndex ] .. ' -p ' .. cssh.port
end
if cssh._extra
then
for k, v in ipairs( cssh._extra )
do
computed[ computedN ] = v
computedN = computedN + 1
end
end
-- appends a slash to the targetdir if missing
-- and is not ':' for home dir
if string.sub( config.targetdir, -1 ) ~= '/'
and string.sub( config.targetdir, -1 ) ~= ':'
then
config.targetdir = config.targetdir .. '/'
end
end
--
-- allow processes
--
rsyncssh.maxProcesses = 1
--
-- The core should not split move events
--
rsyncssh.onMove = true
--
-- default delay
--
rsyncssh.delay = 15
--
-- no default exit codes
--
rsyncssh.exitcodes = false
--
-- rsync exit codes
--
rsyncssh.rsyncExitCodes = default.rsyncExitCodes
--
-- ssh exit codes
--
rsyncssh.sshExitCodes = default.sshExitCodes
--
-- ssh calls configuration
--
-- ssh is used to move and delete files on the target host
--
rsyncssh.ssh = {
--
-- the binary called
--
binary = '/usr/bin/ssh',
--
-- if set adds this key to ssh
--
identityFile = nil,
--
-- if set adds this special options to ssh
--
options = nil,
--
-- if set connect to this port
--
port = nil,
--
-- extra parameters
--
_extra = { }
}