ssh port options, making ssh binary configurable

This commit is contained in:
Axel Kittenberger 2012-08-25 13:45:50 +02:00
parent 7fbf97cd21
commit f50be8e51b
1 changed files with 124 additions and 44 deletions

View File

@ -1,4 +1,4 @@
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --
-- default-rsyncssh.lua -- default-rsyncssh.lua
-- --
-- Improved rsync - sync with rsync, but moves and deletes executed over ssh. -- Improved rsync - sync with rsync, but moves and deletes executed over ssh.
@ -13,16 +13,23 @@
-- License: GPLv2 (see COPYING) or any later version -- License: GPLv2 (see COPYING) or any later version
-- Authors: Axel Kittenberger <axkibe@gmail.com> -- Authors: Axel Kittenberger <axkibe@gmail.com>
-- --
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --
if not default then error('default not loaded'); end if not default then
if default.rsyncssh then error('default-rsyncssh already loaded'); end error('default not loaded');
end
if default.rsyncssh then
error('default-rsyncssh already loaded');
end
default.rsyncssh = { default.rsyncssh = {
-----
--
-- Spawns rsync for a list of events -- Spawns rsync for a list of events
-- --
action = function(inlet) action = function(inlet)
local event, event2 = inlet.getEvent() local event, event2 = inlet.getEvent()
local config = inlet.getConfig() local config = inlet.getConfig()
@ -42,7 +49,9 @@ default.rsyncssh = {
-- uses ssh to delete files on remote host -- uses ssh to delete files on remote host
-- instead of constructing rsync filters -- instead of constructing rsync filters
if event.etype == 'Delete' then if event.etype == 'Delete' then
if not config.delete then if not config.delete then
inlet.discardEvent(event) inlet.discardEvent(event)
return return
@ -51,7 +60,8 @@ default.rsyncssh = {
local elist = inlet.getEvents( local elist = inlet.getEvents(
function(e) function(e)
return e.etype == 'Delete' return e.etype == 'Delete'
end) end
)
local paths = elist.getPaths( local paths = elist.getPaths(
function(etype, path1, path2) function(etype, path1, path2)
@ -60,7 +70,8 @@ default.rsyncssh = {
else else
return config.targetdir..path1 return config.targetdir..path1
end end
end) end
)
for _, v in pairs(paths) do for _, v in pairs(paths) do
if string.match(v, '^%s*/+%s*$') then if string.match(v, '^%s*/+%s*$') then
@ -69,13 +80,26 @@ default.rsyncssh = {
end end
end end
local sPaths = table.concat(paths, '\n') log('Normal', 'Deleting list\n', table.concat(paths, '\n'))
local zPaths = table.concat(paths, config.xargs.delimiter)
log('Normal', 'Deleting list\n', sPaths) local params = {}
spawn(elist, '/usr/bin/ssh',
'<', zPaths, if config.port then
params[#params + 1] = 'p'
params[#params + 1] = config.port
end
spawn(
elist,
config.ssh.binary,
'<', table.concat(paths, config.xargs.delimiter),
params,
config.ssh._xparams
config.host, config.host,
config.xargs.binary, config.xargs.xparams) config.xargs.binary,
config.xargs._xparams
)
return return
end end
@ -87,7 +111,9 @@ default.rsyncssh = {
e.etype ~= 'Delete' and e.etype ~= 'Delete' and
e.etype ~= 'Init' and e.etype ~= 'Init' and
e.etype ~= 'Blanket' e.etype ~= 'Blanket'
end) end
)
local paths = elist.getPaths() local paths = elist.getPaths()
-- removes trailing slashes from dirs. -- removes trailing slashes from dirs.
@ -96,11 +122,14 @@ default.rsyncssh = {
paths[k] = string.sub(v, 1, -2) paths[k] = string.sub(v, 1, -2)
end end
end end
local sPaths = table.concat(paths, '\n') local sPaths = table.concat(paths, '\n')
local zPaths = table.concat(paths, '\000') local zPaths = table.concat(paths, '\000')
log('Normal', 'Rsyncing list\n', sPaths) log('Normal', 'Rsyncing list\n', sPaths)
spawn( spawn(
elist, config.rsyncBinary, elist,
config.rsyncBinary,
'<', zPaths, '<', zPaths,
config.rsyncOpts, config.rsyncOpts,
'--from0', '--from0',
@ -114,10 +143,13 @@ default.rsyncssh = {
-- Called when collecting a finished child process -- Called when collecting a finished child process
-- --
collect = function(agent, exitcode) collect = function(agent, exitcode)
local config = agent.config local config = agent.config
if not agent.isList and agent.etype == 'Init' then if not agent.isList and agent.etype == 'Init' then
local rc = config.rsyncExitCodes[exitcode] local rc = config.rsyncExitCodes[exitcode]
if rc == 'ok' then if rc == 'ok' then
log('Normal', 'Startup of "',agent.source,'" finished: ', exitcode) log('Normal', 'Startup of "',agent.source,'" finished: ', exitcode)
elseif rc == 'again' then elseif rc == 'again' then
@ -134,21 +166,32 @@ default.rsyncssh = {
log('Error', 'Unknown exitcode on startup of "', agent.source,': "',exitcode) log('Error', 'Unknown exitcode on startup of "', agent.source,': "',exitcode)
rc = 'die' rc = 'die'
end end
return rc return rc
end end
if agent.isList then if agent.isList then
local rc = config.rsyncExitCodes[exitcode] local rc = config.rsyncExitCodes[exitcode]
if rc == 'ok' then log('Normal', 'Finished (list): ',exitcode)
elseif rc == 'again' then log('Normal', 'Retrying (list): ',exitcode) if rc == 'ok' then
elseif rc == 'die' then log('Error', 'Failure (list): ', exitcode) log('Normal', 'Finished (list): ',exitcode)
elseif rc == 'again' then
log('Normal', 'Retrying (list): ',exitcode)
elseif rc == 'die' then
log('Error', 'Failure (list): ', exitcode)
else else
log('Error', 'Unknown exitcode (list): ',exitcode) log('Error', 'Unknown exitcode (list): ',exitcode)
rc = 'die' rc = 'die'
end end
return rc return rc
else else
local rc = config.sshExitCodes[exitcode] local rc = config.sshExitCodes[exitcode]
if rc == 'ok' then if rc == 'ok' then
log('Normal', 'Finished ',agent.etype,' ',agent.sourcePath,': ',exitcode) log('Normal', 'Finished ',agent.etype,' ',agent.sourcePath,': ',exitcode)
elseif rc == 'again' then elseif rc == 'again' then
@ -159,20 +202,27 @@ default.rsyncssh = {
log('Error', 'Unknown exitcode ',agent.etype,' ',agent.sourcePath,': ',exitcode) log('Error', 'Unknown exitcode ',agent.etype,' ',agent.sourcePath,': ',exitcode)
rc = 'die' rc = 'die'
end end
return rc return rc
end end
end, end,
----- --
-- Spawns the recursive startup sync -- spawns the recursive startup sync
-- --
init = function(event) init = function(event)
local config = event.config
local inlet = event.inlet local config = event.config
local inlet = event.inlet
local excludes = inlet.getExcludes() local excludes = inlet.getExcludes()
local target = config.host .. ':' .. config.targetdir local target = config.host .. ':' .. config.targetdir
local delete = nil local delete = nil
if config.delete then delete = { '--delete', '--ignore-errors' }; end
if config.delete then
delete = { '--delete', '--ignore-errors' };
end
if #excludes == 0 then if #excludes == 0 then
log('Normal', 'Recursive startup rsync: ',config.source,' -> ',target) log('Normal', 'Recursive startup rsync: ',config.source,' -> ',target)
@ -201,8 +251,8 @@ default.rsyncssh = {
end end
end, end,
----- --
-- Checks the configuration. -- checks the configuration.
-- --
prepare = function(config) prepare = function(config)
if not config.host then error('default.rsyncssh needs "host" configured', 4) end if not config.host then error('default.rsyncssh needs "host" configured', 4) end
@ -218,56 +268,86 @@ default.rsyncssh = {
end end
end, end,
----- --
-- The rsync binary called. -- the rsync binary called
-- --
rsyncBinary = '/usr/bin/rsync', rsyncBinary = '/usr/bin/rsync',
----- --
-- Calls rsync with this default short opts. -- calls rsync with this default short opts
-- --
rsyncOpts = '-lts', rsyncOpts = '-lts',
----- --
-- allow processes -- allow processes
-- --
maxProcesses = 1, maxProcesses = 1,
------ --
-- Let the core not split move events. -- The core should not split move events
-- --
onMove = true, onMove = true,
----- --
-- Default delay. -- default delay
-- --
delay = 15, delay = 15,
----- --
-- By default do deletes. -- by default do deletes
-- --
delete = true, delete = true,
----- --
-- rsync exit codes -- rsync exit codes
-- --
rsyncExitCodes = default.rsyncExitCodes, rsyncExitCodes = default.rsyncExitCodes,
----- --
-- ssh exit codes -- ssh exit codes
-- --
sshExitCodes = default.sshExitCodes, sshExitCodes = default.sshExitCodes,
----- --
-- Delimiter, the binary and the paramters passed to xargs -- xargs calls configuration
--
-- xargs is used to delete multiple remote files, when ssh access is -- xargs is used to delete multiple remote files, when ssh access is
-- available this is simpler than to build filters for rsync for this. -- available this is simpler than to build filters for rsync for this.
-- Default uses '0' as limiter, you might override this for old systems.
-- --
xargs = { xargs = {
--
-- the binary called (on target host)
binary = '/usr/bin/xargs', binary = '/usr/bin/xargs',
--
-- delimiter, uses null by default, you might want to override this for older
-- by for example '\n'
delimiter = '\000', delimiter = '\000',
xparams = {'-0', 'rm -rf'}
--
-- extra parameters
_xparams = {'-0', 'rm -rf'}
},
--
-- ssh calls configuration
--
-- ssh is used to move and delete files on the target host
--
ssh = {
--
-- the binary called
binary = '/usr/bin/ssh',
--
-- if set connect to this port
port = nil
--
-- extra parameters
_xparams = {}
} }
} }