lsyncd/default/direct.lua

213 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
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2018-03-13 15:09:41 +00:00
if not default then error( 'default not loaded' ) end
2012-10-02 20:06:05 +00:00
2018-03-13 15:09:41 +00:00
if not default.rsync then error( 'default-direct needs default.rsync loaded' ) end
2012-10-02 20:06:05 +00:00
2018-03-13 15:09:41 +00:00
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
--
2018-03-13 15:09:41 +00:00
direct.checkgauge =
{
2012-10-07 17:49:33 +00:00
--
-- 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
--
2018-03-13 15:09:41 +00:00
direct.action =
function
(
inlet
)
2012-10-06 12:22:08 +00:00
-- gets all events ready for syncing
local event, event2 = inlet.getEvent()
local config = inlet.getConfig()
2018-03-13 15:09:41 +00:00
if event.etype == 'Create'
then
if event.isdir
then
spawn( event, '/bin/mkdir', '--', event.targetPath )
2012-10-06 12:22:08 +00:00
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
2018-03-13 15:09:41 +00:00
elseif event.etype == 'Modify'
then
if event.isdir
then
error( 'Do not know how to handle "Modify" on dirs' )
2012-10-06 12:22:08 +00:00
end
2018-03-13 15:09:41 +00:00
spawn(event, '/bin/cp', '-p', '--', 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
2018-03-13 15:09:41 +00:00
if tp == '' or tp == '/' or not tp
then
error( 'Refusing to erase your harddisk!' )
2012-10-06 12:22:08 +00:00
end
spawn(event, '/bin/rm', '-rf', '--', tp)
2018-03-13 15:09:41 +00:00
elseif event.etype == 'Move'
then
2012-10-06 12:22:08 +00:00
local tp = event.targetPath
2012-10-06 12:22:08 +00:00
-- extra security check
2018-03-13 15:09:41 +00:00
if tp == '' or tp == '/' or not tp
then
error( 'Refusing to erase your harddisk!' )
2012-10-06 12:22:08 +00:00
end
local command = '/bin/mv -- "$1" "$2" || /bin/rm -rf -- "$1"'
2018-03-13 15:09:41 +00:00
if config.delete ~= true
and config.delete ~= 'running'
then
command = '/bin/mv -- "$1" "$2"'
end
2018-03-13 15:09:41 +00:00
spawnShell( event, command, event.targetPath, event2.targetPath )
2012-10-06 12:22:08 +00:00
else
2018-03-13 15:09:41 +00:00
log( 'Warn', 'ignored an event of type "',event.etype, '"' )
2012-10-06 12:22:08 +00:00
inlet.discardEvent(event)
end
end
--
-- Called when collecting a finished child process
--
2018-03-13 15:09:41 +00:00
direct.collect = function
(
agent,
exitcode
)
2012-10-06 12:22:08 +00:00
local config = agent.config
2018-03-13 15:09:41 +00:00
if not agent.isList and agent.etype == 'Init'
then
2012-10-06 12:22:08 +00:00
local rc = config.rsyncExitCodes[exitcode]
2018-03-13 15:09:41 +00:00
if rc == 'ok'
then
log( 'Normal', 'Startup of "',agent.source,'" finished: ', exitcode )
elseif rc == 'again'
then
if settings( 'insist' )
then
2018-03-13 15:09:41 +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.');
2018-03-13 15:09:41 +00:00
terminate( -1 )
end
2018-03-13 15:09:41 +00:00
elseif rc == 'die'
then
log( 'Error', 'Failure on startup of "',agent.source,'": ', exitcode )
2012-10-06 12:22:08 +00:00
else
2018-03-13 15:09:41 +00:00
log( 'Error', 'Unknown exitcode on startup of "', agent.source,': "',exitcode )
2012-10-06 12:22:08 +00:00
rc = 'die'
end
2018-03-13 15:09:41 +00:00
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.
--
2018-03-13 15:09:41 +00:00
direct.prepare = function
(
config,
level
)
2012-10-06 12:22:08 +00:00
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
2018-03-13 15:09:41 +00:00