1-----
2-- User configuration file for lsyncd.
3-- This needs lsyncd >= 2.0.3
4--
5-- This configuration will execute a command on the remote host
6-- after every successfullycompleted rsync operation.
7-- for example to restart servlets on the target host or so.
8
9local rsyncpostcmd = {
10
11	-- based on default rsync.
12	default.rsync,
13
14	-- for this config it is important to keep maxProcesses at 1, so
15	-- the postcmds will only be spawned after the rsync completed
16	maxProcesses = 1,
17
18	-- called whenever something is to be done
19	action = function(inlet)
20		local event = inlet.getEvent()
21		local config = inlet.getConfig()
22		-- if the event is a blanket event and not the startup,
23		-- its there to spawn the webservice restart at the target.
24		if event.etype == "Blanket" then
25			-- uses rawget to test if "isPostcmd" has been set without
26			-- triggering an error if not.
27			local isPostcmd = rawget(event, "isPostcmd")
28			if event.isPostcmd then
29				spawn(event, "/usr/bin/ssh",
30					config.host, config.postcmd)
31        		return
32			else
33            	-- this is the startup, forwards it to default routine.
34            	return default.rsync.action(inlet)
35        	end
36			error("this should never be reached")
37		end
38		-- for any other event, a blanket event is created that
39		-- will stack on the queue and do the postcmd when its finished
40		local sync = inlet.createBlanketEvent()
41		sync.isPostcmd = true
42		-- the original event is simply forwarded to the normal action handler
43		return default.rsync.action(inlet)
44	end,
45
46	-- called when a process exited.
47	-- this can be a rsync command, the startup rsync or the postcmd
48	collect = function(agent, exitcode)
49		-- for the ssh commands 255 is network error -> try again
50		local isPostcmd = rawget(agent, "isPostcmd")
51		if not agent.isList and agent.etype == "Blanket" and isPostcmd then
52			if exitcode == 255 then
53				return "again"
54			end
55			return
56		else
57			--- everything else, forward to default collection handler
58			return default.collect(agent,exitcode)
59		end
60		error("this should never be reached")
61	end
62
63	-- called before anything else
64	-- builds the target from host and targetdir
65	prepare = function(config)
66		if not config.host then
67			error("rsyncpostcmd neets 'host' configured", 4)
68		end
69		if not config.targetdir then
70			error("rsyncpostcmd needs 'targetdir' configured", 4)
71		end
72		if not config.target then
73			config.target = config.host .. ":" .. config.targetdir
74		end
75		return default.rsync.prepare(config)
76	end
77}
78
79
80sync {
81	rsyncpostcmd,
82	source = "src",
83	host = "beetle",
84	targetdir = "/path/to/trg",
85	postcmd = "/usr/local/bin/restart-servelt.sh",
86}
87
88