File: lpostcmd.lua

package info (click to toggle)
lsyncd 2.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, trixie
  • size: 484 kB
  • sloc: ansic: 2,640; sh: 81; makefile: 11
file content (118 lines) | stat: -rw-r--r-- 2,809 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
-----
-- User configuration file for lsyncd.
-- This needs lsyncd >= 2.0.3
--
-- This configuration will execute a command on the remote host
-- after every successfullycompleted rsync operation.
-- for example to restart servlets on the target host or so.

local rsyncpostcmd = {

	-- based on default rsync.
	default.rsync,

	checkgauge = {
		default.rsync.checkgauge,
		host = true,
		targetdir = true,
		target = true,
		postcmd = true,
	},

	-- for this config it is important to keep maxProcesses at 1, so
	-- the postcmds will only be spawned after the rsync completed
	maxProcesses = 1,

	-- called whenever something is to be done
	action = function
	(
		inlet
	)
		local event = inlet.getEvent( )
		local config = inlet.getConfig( )
		-- if the event is a blanket event and not the startup,
		-- its there to spawn the webservice restart at the target.
		if event.etype == 'Blanket'
		then
			-- uses rawget to test if "isPostcmd" has been set without
			-- triggering an error if not.
			local isPostcmd = rawget( event, 'isPostcmd' )

			if isPostcmd
			then
				spawn(event, "/usr/bin/ssh",
					config.host, config.postcmd)
				return
			else
				-- this is the startup, forwards it to default routine.
				return default.rsync.action(inlet)
			end
			error( 'this should never be reached' )
		end
		-- for any other event, a blanket event is created that
		-- will stack on the queue and do the postcmd when its finished
		local sync = inlet.createBlanketEvent()
		sync.isPostcmd = true
		-- the original event is simply forwarded to the normal action handler
		return default.rsync.action(inlet)
	end,

	-- called when a process exited.
	-- this can be a rsync command, the startup rsync or the postcmd
	collect = function
	(
		agent,
		exitcode
	)
		-- for the ssh commands 255 is network error -> try again
		local isPostcmd = rawget( agent, 'isPostcmd' )

		if not agent.isList and agent.etype == "Blanket" and isPostcmd
		then
			if exitcode == 255 then return 'again' end

			return
		else
			--- everything else, forward to default collection handler
			return default.collect( agent,exitcode )
		end
		error( 'this should never be reached' )
	end,

	-- called before anything else
	-- builds the target from host and targetdir
	prepare = function
	(
		config,
		level,
		skipTarget
	)
		if not config.host
		then
			error( 'rsyncpostcmd needs "host" configured', 4 )
		end

		if not config.targetdir
		then
			error( 'rsyncpostcmd needs "targetdir" configured', 4)
		end

		if not config.target
		then
			config.target = config.host .. ":" .. config.targetdir
		end

		return default.rsync.prepare(config, level, skipTarget)
	end
}


sync {
	rsyncpostcmd,
	delay = 3,
	source = '/path/to/src',
	host = 'localhost',
	targetdir = '/path/to/trg',
	postcmd = '/usr/local/bin/dopostcmd',
}