Skip to content

Instantly share code, notes, and snippets.

@Snawoot
Created March 18, 2015 02:31
Show Gist options
  • Save Snawoot/fbb03dd5123720a48da0 to your computer and use it in GitHub Desktop.
Save Snawoot/fbb03dd5123720a48da0 to your computer and use it in GitHub Desktop.
rsyncpostcmd = {
-- based on default rsync.
default.rsyncssh,
-- 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, config.postcmd,
config.host)
return
else
-- this is the startup, forwards it to default routine.
return default.rsyncssh.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.rsyncssh.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,
checkgauge = {
default.rsyncssh.checkgauge,
host = true,
targetdir = true,
target = true,
postcmd = true,
},
-- called before anything else
-- builds the target from host and targetdir
prepare = function(config, level, skipTarget)
if not config.host then
error("rsyncpostcmd neets '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.rsyncssh.prepare(config, level, skipTarget)
end
}
sync {
default.rsyncssh,
source = "/var/www",
host = "root@50.7.202.29",
targetdir = "/var/www",
rsync = {
archive = true,
compress = false,
whole_file = false
}
}
--sync {
-- default.rsyncssh,
-- source = "/etc/nginx",
-- host = "root@50.7.202.29",
-- targetdir = "/etc/nginx",
-- rsync = {
-- archive = true,
-- compress = false,
-- whole_file = false
-- }
--}
sync {
rsyncpostcmd,
source = "/etc/nginx",
host = "root@50.7.202.29",
targetdir = "/etc/nginx",
postcmd = "/etc/lsyncd/reload-nginx.sh",
rsync = {
archive = true,
compress = false,
whole_file = false
}
}
sync {
rsyncpostcmd,
source = "/etc/php5",
host = "root@50.7.202.29",
targetdir = "/etc/php5",
postcmd = "/etc/lsyncd/reload-php5.sh",
rsync = {
archive = true,
compress = false,
whole_file = false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment