lsyncd/default-direct.lua

215 lines
4.3 KiB
Lua
Raw Normal View History

2012-02-15 19:10:50 +00:00
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- default-direct.lua
--
-- Keeps two directories with /bin/cp, /bin/rm and /bin/mv in sync.
-- Startup still uses rsync tough.
--
-- A (Layer 1) configuration.
--
-- Note:
2012-02-15 19:10:50 +00:00
-- this is infact just a configuration using Layer 1 configuration
-- like any other. It only gets compiled into the binary by default.
2012-10-02 20:06:05 +00:00
--
-- You can simply use a modified one, by copying everything into a
2012-02-15 19:10:50 +00:00
-- config file of yours and name it differently.
--
-- License: GPLv2 (see COPYING) or any later version
-- Authors: Axel Kittenberger <axkibe@gmail.com>
--
2012-02-15 19:10:50 +00:00
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2012-10-02 20:06:05 +00:00
if not default then
error('default not loaded')
end
if not default.rsync then
error('default-direct (currently) needs default.rsync loaded')
end
if default.direct then
error('default-direct already loaded')
end
2012-10-06 12:22:08 +00:00
local direct = { }
default.direct = direct
2012-10-07 17:49:33 +00:00
--
-- known configuration parameters
--
direct.checkgauge = {
--
-- inherits rsync config params
--
default.rsync.checkgauge,
rsyncExitCodes = true,
onMove = true,
}
2012-10-06 12:22:08 +00:00
--
-- Spawns rsync for a list of events
--
direct.action = function(inlet)
-- gets all events ready for syncing
local event, event2 = inlet.getEvent()
local config = inlet.getConfig()
if event.etype == 'Create' then
if event.isdir then
spawn(
event,
'/bin/mkdir',
'--',
2012-10-06 12:22:08 +00:00
event.targetPath
)
else
-- 'cp -t', not supported on OSX
spawn(
event,
'/bin/cp',
'-p',
'--',
2012-03-15 02:07:42 +00:00
event.sourcePath,
event.targetPathdir
)
2012-10-06 12:22:08 +00:00
end
elseif event.etype == 'Modify' then
if event.isdir then
error("Do not know how to handle 'Modify' on dirs")
end
spawn(event,
'/bin/cp',
'-p',
'--',
2012-10-06 12:22:08 +00:00
event.sourcePath,
event.targetPathdir
)
elseif event.etype == 'Delete' then
if
config.delete ~= true and
config.delete ~= 'running'
then
inlet.discardEvent(event)
return
end
2012-10-06 12:22:08 +00:00
local tp = event.targetPath
2012-10-06 12:22:08 +00:00
-- extra security check
if tp == '' or tp == '/' or not tp then
error('Refusing to erase your harddisk!')
end
spawn(event, '/bin/rm', '-rf', '--', tp)
2012-10-06 12:22:08 +00:00
elseif event.etype == 'Move' then
local tp = event.targetPath
2012-10-06 12:22:08 +00:00
-- extra security check
if tp == '' or tp == '/' or not tp then
error('Refusing to erase your harddisk!')
end
local command = '/bin/mv -- "$1" "$2" || /bin/rm -rf -- "$1"'
if
config.delete ~= true and
config.delete ~= 'running'
then
command = '/bin/mv -- "$1" "$2"'
end
2012-10-06 12:22:08 +00:00
spawnShell(
event,
command,
event.targetPath,
event2.targetPath
)
2012-10-06 12:22:08 +00:00
else
log('Warn', 'ignored an event of type "',event.etype, '"')
inlet.discardEvent(event)
end
end
--
-- Called when collecting a finished child process
--
direct.collect = function(agent, exitcode)
2012-10-06 12:22:08 +00:00
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
2012-10-06 12:22:08 +00:00
log('Normal', 'Retrying startup of "',agent.source,'": ', exitcode)
else
2012-10-06 12:22:08 +00:00
log('Error', 'Temporary or permanent failure on startup of "',
agent.source, '". Terminating since "insist" is not set.');
terminate(-1) -- ERRNO
end
2012-10-06 12:22:08 +00:00
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
2012-10-06 12:22:08 +00:00
return rc
end
2012-10-06 12:22:08 +00:00
-- everything else is just as it is,
-- there is no network to retry something.
return
end
2012-10-06 12:22:08 +00:00
--
-- Spawns the recursive startup sync
-- (currently) identical to default rsync.
--
direct.init = default.rsync.init
--
-- Checks the configuration.
--
direct.prepare = function( config, level )
default.rsync.prepare( config, level + 1 )
end
--
-- Default delay is very short.
--
direct.delay = 1
--
-- Let the core not split move events.
--
direct.onMove = true
2012-10-07 17:49:33 +00:00
--
-- Rsync configuration for startup.
--
direct.rsync = default.rsync.rsync
direct.rsyncExitCodes = default.rsyncExitCodes
2012-10-06 12:22:08 +00:00
--
-- By default do deletes.
--
direct.delete = true
--
-- On many system multiple disk operations just rather slow down
-- than speed up.
direct.maxProcesses = 1