From 70f880384b20552d683498ae6e89d73241e923cd Mon Sep 17 00:00:00 2001 From: Axel Kittenberger Date: Tue, 8 Feb 2011 14:14:07 +0000 Subject: [PATCH] adding direct default configuration --- lsyncd.lua | 98 ++++++++++++++++++++++++++++++++++++++++ tests/churn-direct.lua | 42 +++++++++++++++++ tests/churn-rsync.lua | 1 - tests/churn-rsyncssh.lua | 1 - 4 files changed, 140 insertions(+), 2 deletions(-) create mode 100755 tests/churn-direct.lua diff --git a/lsyncd.lua b/lsyncd.lua index e38866d..7422ab6 100644 --- a/lsyncd.lua +++ b/lsyncd.lua @@ -2558,6 +2558,9 @@ USAGE: default rsync with mv's through ssh: lsyncd [OPTIONS] -rsyncssh [SOURCE] [HOST] [TARGETDIR] + + default local copying mechanisms (cp|mv|rm): + lsyncd [OPTIONS] -direct [SOURCE] [TARGETDIR] OPTIONS: -delay SECS Overrides default delay times @@ -2649,6 +2652,11 @@ function runner.configure(args, monitors) clSettings.syncs = clSettings.syncs or {} table.insert(clSettings.syncs, {"rsyncssh", src, host, tdir}) end}, + direct = + {2, function(src, trg) + clSettings.syncs = clSettings.syncs or {} + table.insert(clSettings.syncs, {"direct", src, trg}) + end}, version = {0, function() io.stdout:write("Version: ", lsyncd_version,"\n") @@ -2747,6 +2755,8 @@ function runner.initialize() sync{default.rsync, source=s[2], target=s[3]} elseif s[1] == "rsyncssh" then sync{default.rsyncssh, source=s[2], host=s[3], targetdir=s[4]} + elseif s[1] == "direct" then + sync{default.direct, source=s[2], target=s[3]} end end end @@ -3409,6 +3419,89 @@ local default_rsyncssh = { xargs = {delimiter = '\000', binary = "xargs", xparams = {"-0", "rm -rf"}} } +----- +-- Keeps two directories with /bin/cp, /bin/rm and /bin/mv in sync. +-- Startup still uses rsync tough. +-- +local default_direct = { + ----- + -- Spawns rsync for a list of events + -- + action = function(inlet) + -- gets all events ready for syncing + local event, event2 = inlet.getEvent() + + if event.etype == "Create" or event.etpye == "Modifiy" then + spawn(event, "/bin/cp", "-r", event.sourcePath, event.targetPathdir) + elseif event.etype == "Delete" then + local tp = event.targetPath + -- extra security check + if tp == "" or tp == "/" or not tp then + error("Refusing to erase your harddisk") + end + spawn(event, "/bin/rm", "-rf", tp) + elseif event.etype == "Move" then + spawn(event, "/bin/mv", event.targetPath, event2.targetPath) + else + error("Do not know how to handle unknown event") + end + end, + + ----- + -- Called when collecting a finished child process + -- + collect = function(agent, exitcode) + local config = agent.config + + if not agent.isList and agent.etype == "Blanket" then + if exitcode == 0 then + log("Normal", "Startup of '",agent.source,"' finished.") + elseif rsync_exitcodes and + rsync_exitcodes[exitcode] == "again" + then + log("Normal", + "Retrying startup of '",agent.source,"'.") + return "again" + else + log("Error", "Failure on startup of '",agent.source,"'.") + terminate(-1) -- ERRNO + end + return + end + + -- everything else just is as is, + -- there is no network to retry something. + return nil + end, + + ----- + -- Spawns the recursive startup sync + -- identical to default rsync. + -- + init = default_rsync.init, + + ----- + -- Checks the configuration. + -- + prepare = function(config) + if not config.target then + error("default.direct needs 'target' configured", 4) + end + end, + + ----- + -- Default delay is very short. + -- + delay = 1, + + ----- + -- On many system multiple disk operations just rather slow down + -- than speed up. + + maxProcesses = 1, +} + + ----- -- The default table for the user to accesss. -- Provides all the default layer 1 functions. @@ -3524,6 +3617,11 @@ default = { -- a default rsync configuration with ssh'd move and rm actions -- rsyncssh = default_rsyncssh, + + ----- + -- a default configuration using /bin/cp|rm|mv. + -- + direct = default_direct, ----- -- Minimum seconds between two writes of a status file. diff --git a/tests/churn-direct.lua b/tests/churn-direct.lua new file mode 100755 index 0000000..031726e --- /dev/null +++ b/tests/churn-direct.lua @@ -0,0 +1,42 @@ +#!/usr/bin/lua +-- a heavy duty test. +-- makes thousends of random changes to the source tree +require("posix") +dofile("tests/testlib.lua") + +cwriteln("****************************************************************") +cwriteln(" Testing default.direct with random data activity ") +cwriteln("****************************************************************") + +local tdir, srcdir, trgdir = mktemps() + +-- makes some startup data +churn(srcdir, 10) + +local logs = {} +--logs = {"-log", "Delay", "-log", "Fsevents" } +local pid = spawn("./lsyncd", "-nodaemon", "-delay", "5", + "-direct", srcdir, trgdir, unpack(logs)) + +cwriteln("waiting for Lsyncd to startup") +posix.sleep(1) + +churn(srcdir, 500) + +cwriteln("waiting for Lsyncd to finish its jobs.") +posix.sleep(10) + +cwriteln("killing the Lsyncd daemon") +posix.kill(pid) +local _, exitmsg, lexitcode = posix.wait(lpid) +cwriteln("Exitcode of Lsyncd = ", exitmsg, " ", lexitcode) + +exitcode = os.execute("diff -r "..srcdir.." "..trgdir) +cwriteln("Exitcode of diff = '", exitcode, "'") +if exitcode ~= 0 then + os.exit(1) +else + os.exit(0) +end + + diff --git a/tests/churn-rsync.lua b/tests/churn-rsync.lua index 8f2ec4c..3b7b8ed 100755 --- a/tests/churn-rsync.lua +++ b/tests/churn-rsync.lua @@ -1,7 +1,6 @@ #!/usr/bin/lua -- a heavy duty test. -- makes thousends of random changes to the source tree --- checks every X changes if lsyncd managed to keep target tree in sync. require("posix") dofile("tests/testlib.lua") diff --git a/tests/churn-rsyncssh.lua b/tests/churn-rsyncssh.lua index 4d06cc5..30dc3ef 100755 --- a/tests/churn-rsyncssh.lua +++ b/tests/churn-rsyncssh.lua @@ -1,7 +1,6 @@ #!/usr/bin/lua -- a heavy duty test. -- makes thousends of random changes to the source tree --- checks every X changes if lsyncd managed to keep target tree in sync. require("posix") dofile("tests/testlib.lua")