monotone

monotone Mtn Source Tree

Root/contrib/monotone-cluster-push.lua

1-- Copyright (c) 2007 by Richard Levitte <richard@levitte.org>
2-- All rights reserved.
3--
4-- Redistribution and use in source and binary forms, with or without
5-- modification, are permitted provided that the following conditions
6-- are met:
7--
8-- 1. Redistributions of source code must retain the above copyright
9-- notice, this list of conditions and the following disclaimer.
10--
11-- 2. Redistributions in binary form must reproduce the above copyright
12-- notice, this list of conditions and the following disclaimer in the
13-- documentation and/or other materials provided with the distribution.
14--
15-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16-- ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19-- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21-- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22-- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23-- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25-- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27-------------------------------------------------------------------------------
28-- Usage:
29--
30-- NOTE: THIS SOFTWARE IS ONLY MEANT FOR SERVER PROCESSES!
31-- Anything else will fail miserably!
32--
33-- in your server's monotonerc, add the following include:
34--
35--include("/PATH/TO/monotone-cluster-push.lua")
36--
37-- You may want to change the following variables:
38--
39--MCP_rcfile
40--The absolute path to the configuration file used
41--by this script. It contains basio stanza with the
42--following keys:
43--
44--patternBranch pattern, as in
45--read-permissions. MUST
46--come first in each stanza.
47--serverServer address or address:port
48--to which this pattern should
49--be pushed. There may be many
50--of these.
51--
52--The default is cluster-push.rc located in the server's
53--configuration directory.
54-------------------------------------------------------------------------------
55
56-------------------------------------------------------------------------------
57-- Variables
58-------------------------------------------------------------------------------
59MCP_default_rcfile = get_confdir() .. "/cluster-push.rc"
60
61
62if not MCP_rcfile then MCP_rcfile = MCP_default_rcfile end
63
64-------------------------------------------------------------------------------
65-- Local hack of the note_netsync_* functions
66-------------------------------------------------------------------------------
67do
68 local debug = false
69
70 local branches = {}
71
72 local saved_note_netsync_start = note_netsync_start
73 function note_netsync_start(nonce, ...)
74 if saved_note_netsync_start then
75 saved_note_netsync_start(nonce,
76 unpack(arg))
77 end
78 if debug then
79 io.stderr:write("note_netsync_start: initialise branches\n")
80 end
81 branches[nonce] = {}
82 end
83
84 local saved_note_netsync_cert_received = note_netsync_cert_received
85 function note_netsync_cert_received(rev_id, key, name, value, nonce, ...)
86 if saved_note_netsync_cert_received then
87 saved_note_netsync_cert_received(rev_id, key, name, value, nonce,
88 unpack(arg))
89 end
90 if debug then
91 io.stderr:write("note_netsync_cert_received: cert ", name,
92 " with value ", value, " received\n")
93 end
94 if name == "branch" then
95 if debug then
96 io.stderr:write("note_netsync_cert_received: branch ", value,
97 " identified\n")
98 end
99 branches[nonce][value] = true
100 end
101 end
102
103 local saved_note_netsync_revision_received = note_netsync_revision_received
104 function note_netsync_revision_received(new_id, revision, certs, nonce, ...)
105 if saved_note_netsync_revision_received then
106 saved_note_netsync_revision_received(new_id, revision, certs, nonce,
107 unpack(arg))
108 end
109 for _, item in pairs(certs)
110 do
111 if debug then
112 io.stderr:write("note_netsync_revision_received: cert ", item.name,
113 " with value ", item.value, " received\n")
114 end
115 if item.name == "branch" then
116 if debug then
117 io.stderr:write("note_netsync_revision_received: branch ",
118 item.value, " identified\n")
119 end
120 branches[nonce][item.value] = true
121 end
122 end
123 end
124
125 local saved_note_netsync_end = note_netsync_end
126 function note_netsync_end(nonce, status,
127 bytes_in, bytes_out,
128 certs_in, certs_out,
129 revs_in, revs_out,
130 keys_in, keys_out,
131 ...)
132 if saved_note_netsync_end then
133 saved_note_netsync_end(nonce, status,
134bytes_in, bytes_out,
135certs_in, certs_out,
136revs_in, revs_out,
137keys_in, keys_out,
138unpack(arg))
139 end
140 if debug then
141 io.stderr:write("note_netsync_end: ",
142 string.format("%d certs, %d revs, %d keys",
143 certs_in, revs_in, keys_in),
144 "\n")
145 end
146 if certs_in > 0 or revs_in > 0 or keys_in > 0 then
147 if debug then
148 io.stderr:write("note_netsync_end: reading ", MCP_rcfile, "\n")
149 end
150 local rcfile = io.open(MCP_rcfile, "r")
151 if (rcfile == nil) then
152 io.stderr:write("file ", MCP_rcfile, " cannot be opened\n")
153 return false
154 end
155 local dat = rcfile:read("*a")
156 io.close(rcfile)
157 if debug then
158 io.stderr:write("note_netsync_end: got this:\n", dat, "\n")
159 end
160 local res = parse_basic_io(dat)
161 if res == nil then
162 io.stderr:write("file ", MCP_rcfile, " cannot be parsed\n")
163 return false
164 end
165
166 local matches = false
167 local patterns = {}
168 local previous_name = ""
169 for i, item in pairs(res) do
170 if item.name == "pattern" then
171 if debug then
172 io.stderr:write("note_netsync_end: found ", item.name,
173 " = \"", item.values[1], "\"\n")
174 end
175 if previous_name ~= "pattern" then
176 if debug then
177 io.stderr:write("note_netsync_end: clearing matches and patterns because previous_name = \"", previous_name, "\"\n")
178 end
179 matches = false
180 patterns = {}
181 end
182 local pattern = item.values[1]
183 for branch, b in pairs(branches[nonce]) do
184 if debug then
185 io.stderr:write("note_netsync_end: trying to match branch ",
186 branch, "\n")
187 end
188 if globish_match(pattern, branch) then
189 if debug then
190io.stderr:write("note_netsync_end: it matches branch ",
191branch, "\n")
192 end
193 matches = true
194 patterns[pattern] = true
195 end
196 end
197 elseif matches then
198 if item.name == "server" then
199 if debug then
200 io.stderr:write("note_netsync_end: found ", item.name,
201 " = \"", item.values[1], "\"\n")
202 end
203 local server = item.values[1]
204 for pattern, b in pairs(patterns) do
205 io.stderr:write("pushing pattern \"", pattern,
206 "\" to server ", server, "\n")
207 server_request_sync("push", server, pattern, "")
208 end
209 end
210 end
211 previous_name = item.name
212 end
213 end
214 end
215
216 local saved_note_mtn_startup = note_mtn_startup
217 function note_mtn_startup(...)
218 if saved_note_mtn_startup then
219 saved_note_mtn_startup(unpack(arg))
220 end
221
222 if debug then
223 io.stderr:write("note_mtn_startup: reading ", MCP_rcfile,
224 "\n")
225 end
226 local rcfile = io.open(MCP_rcfile, "r")
227 if (rcfile == nil) then
228 io.stderr:write("file ", MCP_rcfile, " cannot be opened\n")
229 return false
230 end
231 local dat = rcfile:read("*a")
232 io.close(rcfile)
233 if debug then
234 io.stderr:write("note_mtn_startup: got this:\n", dat, "\n")
235 end
236 local res = parse_basic_io(dat)
237 if res == nil then
238 io.stderr:write("file ", MCP_rcfile, " cannot be parsed\n")
239 return false
240 end
241
242 local patterns = {}
243 local previous_name = ""
244 for i, item in pairs(res) do
245 if item.name == "pattern" then
246 if debug then
247 io.stderr:write("note_mtn_startup: found ", item.name, " = \"",
248 item.values[1], "\"\n")
249 end
250 if previous_name ~= "pattern" then
251 if debug then
252 io.stderr:write("note_mtn_startup: clearing patterns because previous_name = \"", previous_name, "\"\n")
253 end
254 patterns = {}
255 end
256 patterns[item.values[1]] = true
257 elseif item.name == "server" then
258 if debug then
259 io.stderr:write("note_mtn_startup: found ", item.name, " = \"",
260 item.values[1], "\"\n")
261 end
262 local server = item.values[1]
263 for pattern, b in pairs(patterns) do
264 io.stderr:write("pushing pattern \"", pattern, "\" to server ",
265 server, "\n")
266 server_request_sync("push", server, pattern, "")
267 end
268 end
269 previous_name = item.name
270 end
271 return nil
272 end
273end

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status