File: default-rsyncssh.lua

package info (click to toggle)
lsyncd 2.1.5-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 732 kB
  • ctags: 363
  • sloc: ansic: 2,549; sh: 1,134; makefile: 48
file content (401 lines) | stat: -rw-r--r-- 7,914 bytes parent folder | download
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
--
-- 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 <axkibe@gmail.com>
--
--

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,
		port        =  true,
		_extra      =  true
	},

	-- xargs settings
	xargs = {
		binary      =  true,
		delimiter   =  true,
		_extra      =  true
	}
}

--
-- Spawns rsync for a list of events
--
rsyncssh.action = function( inlet )

	local event, event2 = inlet.getEvent()
	local config = inlet.getConfig()

	-- 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

	-- uses ssh to delete files on remote host
	-- instead of constructing rsync filters

	if event.etype == 'Delete' then

		if
			config.delete ~= true and
			config.delete ~= 'running'
		then
			inlet.discardEvent(event)
			return
		end

		-- gets all other deletes ready to be
		-- executed
		local elist = inlet.getEvents(
			function( e )
				return e.etype == 'Delete'
			end
		)

		-- returns the paths of the delete list
		local paths = elist.getPaths(
			function( etype, path1, path2 )
				if path2 then
					return config.targetdir..path1, config.targetdir..path2
				else
					return config.targetdir..path1
				end
			end
		)

		-- ensures none of the paths is '/'
		for _, v in pairs( paths ) do
			if string.match(v, '^%s*/+%s*$') then
				log('Error', 'refusing to `rm -rf /` the target!')
				terminate(-1) -- ERRNO
			end
		end

		log(
			'Normal',
			'Deleting list\n',
			table.concat( paths, '\n' )
		)

		local params = { }

		spawn(
			elist,
			config.ssh.binary,
			'<', table.concat(paths, config.xargs.delimiter),
			params,
			config.ssh._computed,
			config.host,
			config.xargs.binary,
			config.xargs._extra
		)

		return
	end

	--
	-- for everything else a rsync is spawned
	--
	local elist = inlet.getEvents(
		function(e)
			-- TODO use a table
			return e.etype ~= 'Move' and
			       e.etype ~= 'Delete' and
				   e.etype ~= 'Init' and
				   e.etype ~= 'Blanket'
		end
	)

	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

	local sPaths = table.concat(paths, '\n')
	local zPaths = table.concat(paths, '\000')

	log('Normal', 'Rsyncing list\n', sPaths)

	spawn(
		elist,
		config.rsync.binary,
		'<', zPaths,
		config.rsync._computed,
		'--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

	local cssh = config.ssh;
	cssh._computed = { }
	local computed = cssh._computed
	local computedN = 1

	if cssh._extra then
		for k, v in ipairs( cssh._extra ) do
			computed[ computedN ] = v
			computedN = computedN  + 1
		end
	end

	if cssh.port then
		computed[ computedN     ] = '-p'
		computed[ computedN + 1 ] = cssh.port
		computedN = computedN + 2

		local rsyncc = config.rsync._computed
		rsyncc[ #rsyncc + 1 ] = '--rsh=ssh -p ' .. cssh.port
	end

	-- appends a slash to the targetdir if missing
	if 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

--
-- xargs calls configuration
--
-- xargs is used to delete multiple remote files, when ssh access is
-- available this is simpler than to build filters for rsync for this.
--
rsyncssh.xargs = {

	--
	-- the binary called (on target host)
	binary = '/usr/bin/xargs',

	--
	-- delimiter, uses null by default, you might want to override this for older
	-- by for example '\n'
	delimiter = '\000',

	--
	-- extra parameters
	_extra = { '-0', 'rm -rf' }
}

--
-- 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 connect to this port
	--
	port = nil,

	--
	-- extra parameters
	--
	_extra = { }
}