aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/OptionalModules/SvnSerialiser/SvnBackupModule.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/OptionalModules/SvnSerialiser/SvnBackupModule.cs')
-rw-r--r--OpenSim/Region/OptionalModules/SvnSerialiser/SvnBackupModule.cs399
1 files changed, 0 insertions, 399 deletions
diff --git a/OpenSim/Region/OptionalModules/SvnSerialiser/SvnBackupModule.cs b/OpenSim/Region/OptionalModules/SvnSerialiser/SvnBackupModule.cs
deleted file mode 100644
index ccdea14..0000000
--- a/OpenSim/Region/OptionalModules/SvnSerialiser/SvnBackupModule.cs
+++ /dev/null
@@ -1,399 +0,0 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSimulator Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28using System;
29using System.Collections.Generic;
30using System.IO;
31using System.Reflection;
32using System.Timers;
33using log4net;
34using Nini.Config;
35using OpenSim.Region.Framework.Interfaces;
36using OpenSim.Region.CoreModules.World.Serialiser;
37using OpenSim.Region.CoreModules.World.Terrain;
38using OpenSim.Region.Framework.Scenes;
39using PumaCode.SvnDotNet.AprSharp;
40using PumaCode.SvnDotNet.SubversionSharp;
41using Slash = System.IO.Path;
42
43namespace OpenSim.Region.Modules.SvnSerialiser
44{
45 public class SvnBackupModule : IRegionModule
46 {
47 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
48
49 private List<Scene> m_scenes;
50 private Timer m_timer;
51 private bool m_enabled;
52 private bool m_installBackupOnLoad;
53 private IRegionSerialiserModule m_serialiser;
54 private bool m_svnAutoSave;
55 private SvnClient m_svnClient;
56 private string m_svndir = "SVNmodule" + Slash.DirectorySeparatorChar + "repo";
57 private string m_svnpass = "password";
58
59 private TimeSpan m_svnperiod = new TimeSpan(0, 0, 15, 0, 0);
60 private string m_svnurl = "svn://insert.Your.svn/here/";
61 private string m_svnuser = "username";
62
63 #region SvnModule Core
64
65 /// <summary>
66 /// Exports a specified scene to the SVN repo directory, then commits.
67 /// </summary>
68 /// <param name="scene">The scene to export</param>
69 public void SaveRegion(Scene scene)
70 {
71 List<string> svnfilenames = CreateAndAddExport(scene);
72
73 m_svnClient.Commit3(svnfilenames, true, false);
74 m_log.Info("[SVNBACKUP]: Region backup successful (" + scene.RegionInfo.RegionName + ").");
75 }
76
77 /// <summary>
78 /// Saves all registered scenes to the SVN repo, then commits.
79 /// </summary>
80 public void SaveAllRegions()
81 {
82 List<string> svnfilenames = new List<string>();
83 List<string> regions = new List<string>();
84
85 foreach (Scene scene in m_scenes)
86 {
87 svnfilenames.AddRange(CreateAndAddExport(scene));
88 regions.Add("'" + scene.RegionInfo.RegionName + "' ");
89 }
90
91 m_svnClient.Commit3(svnfilenames, true, false);
92 m_log.Info("[SVNBACKUP]: Server backup successful (" + String.Concat(regions.ToArray()) + ").");
93 }
94
95 private List<string> CreateAndAddExport(Scene scene)
96 {
97 m_log.Info("[SVNBACKUP]: Saving a region to SVN with name " + scene.RegionInfo.RegionName);
98
99 List<string> filenames = m_serialiser.SerialiseRegion(scene, m_svndir + Slash.DirectorySeparatorChar + scene.RegionInfo.RegionID + Slash.DirectorySeparatorChar);
100
101 try
102 {
103 m_svnClient.Add3(m_svndir + Slash.DirectorySeparatorChar + scene.RegionInfo.RegionID, true, false, false);
104 }
105 catch (SvnException)
106 {
107 }
108
109 List<string> svnfilenames = new List<string>();
110 foreach (string filename in filenames)
111 svnfilenames.Add(m_svndir + Slash.DirectorySeparatorChar + scene.RegionInfo.RegionID + Slash.DirectorySeparatorChar + filename);
112 svnfilenames.Add(m_svndir + Slash.DirectorySeparatorChar + scene.RegionInfo.RegionID);
113
114 return svnfilenames;
115 }
116
117 public void LoadRegion(Scene scene)
118 {
119 IRegionSerialiserModule serialiser = scene.RequestModuleInterface<IRegionSerialiserModule>();
120 if (serialiser != null)
121 {
122 serialiser.LoadPrimsFromXml2(
123 scene,
124 m_svndir + Slash.DirectorySeparatorChar + scene.RegionInfo.RegionID
125 + Slash.DirectorySeparatorChar + "objects.xml");
126
127 scene.RequestModuleInterface<ITerrainModule>().LoadFromFile(
128 m_svndir + Slash.DirectorySeparatorChar + scene.RegionInfo.RegionID
129 + Slash.DirectorySeparatorChar + "heightmap.r32");
130
131 m_log.Info("[SVNBACKUP]: Region load successful (" + scene.RegionInfo.RegionName + ").");
132 }
133 else
134 {
135 m_log.ErrorFormat(
136 "[SVNBACKUP]: Region load of {0} failed - no serialisation module available",
137 scene.RegionInfo.RegionName);
138 }
139 }
140
141 private void CheckoutSvn()
142 {
143 m_svnClient.Checkout2(m_svnurl, m_svndir, Svn.Revision.Head, Svn.Revision.Head, true, false);
144 }
145
146 private void CheckoutSvn(SvnRevision revision)
147 {
148 m_svnClient.Checkout2(m_svnurl, m_svndir, revision, revision, true, false);
149 }
150
151 // private void CheckoutSvnPartial(string subdir)
152 // {
153 // if (!Directory.Exists(m_svndir + Slash.DirectorySeparatorChar + subdir))
154 // Directory.CreateDirectory(m_svndir + Slash.DirectorySeparatorChar + subdir);
155
156 // m_svnClient.Checkout2(m_svnurl + "/" + subdir, m_svndir, Svn.Revision.Head, Svn.Revision.Head, true, false);
157 // }
158
159 // private void CheckoutSvnPartial(string subdir, SvnRevision revision)
160 // {
161 // if (!Directory.Exists(m_svndir + Slash.DirectorySeparatorChar + subdir))
162 // Directory.CreateDirectory(m_svndir + Slash.DirectorySeparatorChar + subdir);
163
164 // m_svnClient.Checkout2(m_svnurl + "/" + subdir, m_svndir, revision, revision, true, false);
165 // }
166
167 #endregion
168
169 #region SvnDotNet Callbacks
170
171 private SvnError SimpleAuth(out SvnAuthCredSimple svnCredentials, IntPtr baton,
172 AprString realm, AprString username, bool maySave, AprPool pool)
173 {
174 svnCredentials = SvnAuthCredSimple.Alloc(pool);
175 svnCredentials.Username = new AprString(m_svnuser, pool);
176 svnCredentials.Password = new AprString(m_svnpass, pool);
177 svnCredentials.MaySave = false;
178 return SvnError.NoError;
179 }
180
181 private SvnError GetCommitLogCallback(out AprString logMessage, out SvnPath tmpFile, AprArray commitItems, IntPtr baton, AprPool pool)
182 {
183 if (!commitItems.IsNull)
184 {
185 foreach (SvnClientCommitItem2 item in commitItems)
186 {
187 m_log.Debug("[SVNBACKUP]: ... " + Path.GetFileName(item.Path.ToString()) + " (" + item.Kind.ToString() + ") r" + item.Revision.ToString());
188 }
189 }
190
191 string msg = "Region Backup (" + System.Environment.MachineName + " at " + DateTime.UtcNow + " UTC)";
192
193 m_log.Debug("[SVNBACKUP]: Saved with message: " + msg);
194
195 logMessage = new AprString(msg, pool);
196 tmpFile = new SvnPath(pool);
197
198 return (SvnError.NoError);
199 }
200
201 #endregion
202
203 #region IRegionModule Members
204
205 public void Initialise(Scene scene, IConfigSource source)
206 {
207 m_scenes = new List<Scene>();
208 m_timer = new Timer();
209
210 try
211 {
212 if (!source.Configs["SVN"].GetBoolean("Enabled", false))
213 return;
214
215 m_enabled = true;
216
217 m_svndir = source.Configs["SVN"].GetString("Directory", m_svndir);
218 m_svnurl = source.Configs["SVN"].GetString("URL", m_svnurl);
219 m_svnuser = source.Configs["SVN"].GetString("Username", m_svnuser);
220 m_svnpass = source.Configs["SVN"].GetString("Password", m_svnpass);
221 m_installBackupOnLoad = source.Configs["SVN"].GetBoolean("ImportOnStartup", m_installBackupOnLoad);
222 m_svnAutoSave = source.Configs["SVN"].GetBoolean("Autosave", m_svnAutoSave);
223 m_svnperiod = new TimeSpan(0, source.Configs["SVN"].GetInt("AutosavePeriod", (int) m_svnperiod.TotalMinutes), 0);
224 }
225 catch (Exception)
226 {
227 }
228
229 lock (m_scenes)
230 {
231 m_scenes.Add(scene);
232 }
233 //Only register it once, to prevent command being executed x*region times
234 if (m_scenes.Count == 1)
235 {
236 scene.EventManager.OnPluginConsole += EventManager_OnPluginConsole;
237 }
238 }
239
240 public void PostInitialise()
241 {
242 if (m_enabled == false)
243 return;
244
245 if (m_svnAutoSave)
246 {
247 m_timer.Interval = m_svnperiod.TotalMilliseconds;
248 m_timer.Elapsed += m_timer_Elapsed;
249 m_timer.AutoReset = true;
250 m_timer.Start();
251 }
252
253 m_log.Info("[SVNBACKUP]: Connecting to SVN server " + m_svnurl + " ...");
254 SetupSvnProvider();
255
256 m_log.Info("[SVNBACKUP]: Creating repository in " + m_svndir + ".");
257 CreateSvnDirectory();
258 CheckoutSvn();
259 SetupSerialiser();
260
261 if (m_installBackupOnLoad)
262 {
263 m_log.Info("[SVNBACKUP]: Importing latest SVN revision to scenes...");
264 foreach (Scene scene in m_scenes)
265 {
266 LoadRegion(scene);
267 }
268 }
269 }
270
271 public void Close()
272 {
273 }
274
275 public string Name
276 {
277 get { return "SvnBackupModule"; }
278 }
279
280 public bool IsSharedModule
281 {
282 get { return true; }
283 }
284
285 #endregion
286
287 private void EventManager_OnPluginConsole(string[] args)
288 {
289 if (args[0] == "svn" && args[1] == "save")
290 {
291 SaveAllRegions();
292 }
293 if (args.Length == 2)
294 {
295 if (args[0] == "svn" && args[1] == "load")
296 {
297 LoadAllScenes();
298 }
299 }
300 if (args.Length == 3)
301 {
302 if (args[0] == "svn" && args[1] == "load")
303 {
304 LoadAllScenes(Int32.Parse(args[2]));
305 }
306 }
307 if (args.Length == 3)
308 {
309 if (args[0] == "svn" && args[1] == "load-region")
310 {
311 LoadScene(args[2]);
312 }
313 }
314 if (args.Length == 4)
315 {
316 if (args[0] == "svn" && args[1] == "load-region")
317 {
318 LoadScene(args[2], Int32.Parse(args[3]));
319 }
320 }
321 }
322
323 public void LoadScene(string name)
324 {
325 CheckoutSvn();
326
327 foreach (Scene scene in m_scenes)
328 {
329 if (scene.RegionInfo.RegionName.ToLower().Equals(name.ToLower()))
330 {
331 LoadRegion(scene);
332 return;
333 }
334 }
335 m_log.Warn("[SVNBACKUP]: No region loaded - unable to find matching name.");
336 }
337
338 public void LoadScene(string name, int revision)
339 {
340 CheckoutSvn(new SvnRevision(revision));
341
342 foreach (Scene scene in m_scenes)
343 {
344 if (scene.RegionInfo.RegionName.ToLower().Equals(name.ToLower()))
345 {
346 LoadRegion(scene);
347 return;
348 }
349 }
350 m_log.Warn("[SVNBACKUP]: No region loaded - unable to find matching name.");
351 }
352
353 public void LoadAllScenes()
354 {
355 CheckoutSvn();
356
357 foreach (Scene scene in m_scenes)
358 {
359 LoadRegion(scene);
360 }
361 }
362
363 public void LoadAllScenes(int revision)
364 {
365 CheckoutSvn(new SvnRevision(revision));
366
367 foreach (Scene scene in m_scenes)
368 {
369 LoadRegion(scene);
370 }
371 }
372
373 private void m_timer_Elapsed(object sender, ElapsedEventArgs e)
374 {
375 SaveAllRegions();
376 }
377
378 private void SetupSerialiser()
379 {
380 if (m_scenes.Count > 0)
381 m_serialiser = m_scenes[0].RequestModuleInterface<IRegionSerialiserModule>();
382 }
383
384 private void SetupSvnProvider()
385 {
386 m_svnClient = new SvnClient();
387 m_svnClient.AddUsernameProvider();
388 m_svnClient.AddPromptProvider(new SvnAuthProviderObject.SimplePrompt(SimpleAuth), IntPtr.Zero, 2);
389 m_svnClient.OpenAuth();
390 m_svnClient.Context.LogMsgFunc2 = new SvnDelegate(new SvnClient.GetCommitLog2(GetCommitLogCallback));
391 }
392
393 private void CreateSvnDirectory()
394 {
395 if (!Directory.Exists(m_svndir))
396 Directory.CreateDirectory(m_svndir);
397 }
398 }
399}