2012-02-15 19:10:50 +00:00
|
|
|
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
-- default-direct.lua
|
2012-02-15 19:00:28 +00:00
|
|
|
--
|
|
|
|
-- 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
|
2012-02-15 19:00:28 +00:00
|
|
|
-- like any other. It only gets compiled into the binary by default.
|
2012-10-02 20:06:05 +00:00
|
|
|
--
|
2012-02-15 19:00:28 +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.
|
2012-02-15 19:00:28 +00:00
|
|
|
--
|
|
|
|
-- License: GPLv2 (see COPYING) or any later version
|
|
|
|
-- Authors: Axel Kittenberger <axkibe@gmail.com>
|
|
|
|
--
|
2012-02-15 19:10:50 +00:00
|
|
|
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2012-02-15 19:00:28 +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-02-15 19:00:28 +00:00
|
|
|
|
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',
|
2016-11-25 15:23:19 +00:00
|
|
|
'--',
|
2012-10-06 12:22:08 +00:00
|
|
|
event.targetPath
|
|
|
|
)
|
|
|
|
else
|
|
|
|
-- 'cp -t', not supported on OSX
|
|
|
|
spawn(
|
|
|
|
event,
|
2012-02-15 19:00:28 +00:00
|
|
|
'/bin/cp',
|
2016-12-02 15:24:07 +00:00
|
|
|
'-p',
|
2016-11-25 15:23:19 +00:00
|
|
|
'--',
|
2012-03-15 02:07:42 +00:00
|
|
|
event.sourcePath,
|
|
|
|
event.targetPathdir
|
2012-02-15 19:00:28 +00:00
|
|
|
)
|
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',
|
2016-12-02 15:24:07 +00:00
|
|
|
'-p',
|
2016-11-25 15:23:19 +00:00
|
|
|
'--',
|
2012-10-06 12:22:08 +00:00
|
|
|
event.sourcePath,
|
|
|
|
event.targetPathdir
|
|
|
|
)
|
|
|
|
elseif event.etype == 'Delete' then
|
2012-10-09 15:47:39 +00:00
|
|
|
|
|
|
|
if
|
|
|
|
config.delete ~= true and
|
|
|
|
config.delete ~= 'running'
|
|
|
|
then
|
2012-02-15 19:00:28 +00:00
|
|
|
inlet.discardEvent(event)
|
2012-10-09 15:47:39 +00:00
|
|
|
return
|
2012-02-15 19:00:28 +00:00
|
|
|
end
|
2012-10-06 12:22:08 +00:00
|
|
|
|
|
|
|
local tp = event.targetPath
|
2012-10-09 15:47:39 +00:00
|
|
|
|
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
|
2012-10-09 15:47:39 +00:00
|
|
|
|
2016-11-25 15:23:19 +00:00
|
|
|
spawn(event, '/bin/rm', '-rf', '--', tp)
|
2012-10-09 15:47:39 +00:00
|
|
|
|
2012-10-06 12:22:08 +00:00
|
|
|
elseif event.etype == 'Move' then
|
|
|
|
local tp = event.targetPath
|
2012-10-09 15:47:39 +00:00
|
|
|
|
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
|
2012-10-09 15:47:39 +00:00
|
|
|
|
2016-11-25 15:23:19 +00:00
|
|
|
local command = '/bin/mv -- "$1" "$2" || /bin/rm -rf -- "$1"'
|
2012-10-09 15:47:39 +00:00
|
|
|
|
|
|
|
if
|
|
|
|
config.delete ~= true and
|
|
|
|
config.delete ~= 'running'
|
|
|
|
then
|
2016-11-25 15:23:19 +00:00
|
|
|
command = '/bin/mv -- "$1" "$2"'
|
2012-10-09 15:47:39 +00:00
|
|
|
end
|
|
|
|
|
2012-10-06 12:22:08 +00:00
|
|
|
spawnShell(
|
|
|
|
event,
|
|
|
|
command,
|
|
|
|
event.targetPath,
|
2012-10-09 15:47:39 +00:00
|
|
|
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-09 15:47:39 +00:00
|
|
|
|
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)
|
2016-12-02 15:24:07 +00:00
|
|
|
elseif rc == 'again'
|
|
|
|
then
|
|
|
|
if settings( 'insist' )
|
|
|
|
then
|
2012-10-06 12:22:08 +00:00
|
|
|
log('Normal', 'Retrying startup of "',agent.source,'": ', exitcode)
|
2012-02-15 19:00:28 +00:00
|
|
|
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
|
2012-02-15 19:00:28 +00:00
|
|
|
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'
|
2012-02-15 19:00:28 +00:00
|
|
|
end
|
2012-10-06 12:22:08 +00:00
|
|
|
return rc
|
|
|
|
end
|
2012-02-15 19:00:28 +00:00
|
|
|
|
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-02-15 19:00:28 +00:00
|
|
|
|
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
|