SvnBackupModule.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 OpenSim 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. using System;
  28. using System.Collections.Generic;
  29. using System.IO;
  30. using System.Reflection;
  31. using System.Timers;
  32. using log4net;
  33. using Nini.Config;
  34. using OpenSim.Region.Environment.Interfaces;
  35. using OpenSim.Region.Environment.Modules.World.Serialiser;
  36. using OpenSim.Region.Environment.Modules.World.Terrain;
  37. using OpenSim.Region.Environment.Scenes;
  38. using PumaCode.SvnDotNet.AprSharp;
  39. using PumaCode.SvnDotNet.SubversionSharp;
  40. using Slash=System.IO.Path;
  41. namespace OpenSim.Region.Modules.SvnSerialiser
  42. {
  43. public class SvnBackupModule : IRegionModule
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. private readonly List<Scene> m_scenes = new List<Scene>();
  47. private readonly Timer m_timer = new Timer();
  48. private bool m_enabled = false;
  49. private bool m_installBackupOnLoad = false;
  50. private IRegionSerialiser m_serialiser;
  51. private bool m_svnAutoSave = false;
  52. private SvnClient m_svnClient;
  53. private string m_svndir = "SVNmodule" + Slash.DirectorySeparatorChar + "repo";
  54. private string m_svnpass = "password";
  55. private TimeSpan m_svnperiod = new TimeSpan(0, 0, 15, 0, 0);
  56. private string m_svnurl = "svn://insert.your.svn/here/";
  57. private string m_svnuser = "username";
  58. #region SvnModule Core
  59. /// <summary>
  60. /// Exports a specified scene to the SVN repo directory, then commits.
  61. /// </summary>
  62. /// <param name="scene">The scene to export</param>
  63. public void SaveRegion(Scene scene)
  64. {
  65. List<string> svnfilenames = CreateAndAddExport(scene);
  66. m_svnClient.Commit3(svnfilenames, true, false);
  67. m_log.Info("[SVNBACKUP]: Region backup successful (" + scene.RegionInfo.RegionName + ").");
  68. }
  69. /// <summary>
  70. /// Saves all registered scenes to the SVN repo, then commits.
  71. /// </summary>
  72. public void SaveAllRegions()
  73. {
  74. List<string> svnfilenames = new List<string>();
  75. List<string> regions = new List<string>();
  76. foreach (Scene scene in m_scenes)
  77. {
  78. svnfilenames.AddRange(CreateAndAddExport(scene));
  79. regions.Add("'" + scene.RegionInfo.RegionName + "' ");
  80. }
  81. m_svnClient.Commit3(svnfilenames, true, false);
  82. m_log.Info("[SVNBACKUP]: Server backup successful (" + String.Concat(regions.ToArray()) + ").");
  83. }
  84. private List<string> CreateAndAddExport(Scene scene)
  85. {
  86. m_log.Info("[SVNBACKUP]: Saving a region to SVN with name " + scene.RegionInfo.RegionName);
  87. List<string> filenames = m_serialiser.SerialiseRegion(scene, m_svndir + Slash.DirectorySeparatorChar + scene.RegionInfo.RegionID + Slash.DirectorySeparatorChar);
  88. try
  89. {
  90. m_svnClient.Add3(m_svndir + Slash.DirectorySeparatorChar + scene.RegionInfo.RegionID, true, false, false);
  91. }
  92. catch (SvnException)
  93. {
  94. }
  95. List<string> svnfilenames = new List<string>();
  96. foreach (string filename in filenames)
  97. svnfilenames.Add(m_svndir + Slash.DirectorySeparatorChar + scene.RegionInfo.RegionID + Slash.DirectorySeparatorChar + filename);
  98. svnfilenames.Add(m_svndir + Slash.DirectorySeparatorChar + scene.RegionInfo.RegionID);
  99. return svnfilenames;
  100. }
  101. public void LoadRegion(Scene scene)
  102. {
  103. scene.LoadPrimsFromXml2(m_svndir + Slash.DirectorySeparatorChar + scene.RegionInfo.RegionID +
  104. Slash.DirectorySeparatorChar + "objects.xml");
  105. scene.RequestModuleInterface<ITerrainModule>().LoadFromFile(m_svndir + Slash.DirectorySeparatorChar + scene.RegionInfo.RegionID +
  106. Slash.DirectorySeparatorChar + "heightmap.r32");
  107. m_log.Info("[SVNBACKUP]: Region load successful (" + scene.RegionInfo.RegionName + ").");
  108. }
  109. private void CheckoutSvn()
  110. {
  111. m_svnClient.Checkout2(m_svnurl, m_svndir, Svn.Revision.Head, Svn.Revision.Head, true, false);
  112. }
  113. private void CheckoutSvn(SvnRevision revision)
  114. {
  115. m_svnClient.Checkout2(m_svnurl, m_svndir, revision, revision, true, false);
  116. }
  117. // private void CheckoutSvnPartial(string subdir)
  118. // {
  119. // if (!Directory.Exists(m_svndir + Slash.DirectorySeparatorChar + subdir))
  120. // Directory.CreateDirectory(m_svndir + Slash.DirectorySeparatorChar + subdir);
  121. // m_svnClient.Checkout2(m_svnurl + "/" + subdir, m_svndir, Svn.Revision.Head, Svn.Revision.Head, true, false);
  122. // }
  123. // private void CheckoutSvnPartial(string subdir, SvnRevision revision)
  124. // {
  125. // if (!Directory.Exists(m_svndir + Slash.DirectorySeparatorChar + subdir))
  126. // Directory.CreateDirectory(m_svndir + Slash.DirectorySeparatorChar + subdir);
  127. // m_svnClient.Checkout2(m_svnurl + "/" + subdir, m_svndir, revision, revision, true, false);
  128. // }
  129. #endregion
  130. #region SvnDotNet Callbacks
  131. private SvnError SimpleAuth(out SvnAuthCredSimple svnCredentials, IntPtr baton,
  132. AprString realm, AprString username, bool maySave, AprPool pool)
  133. {
  134. svnCredentials = SvnAuthCredSimple.Alloc(pool);
  135. svnCredentials.Username = new AprString(m_svnuser, pool);
  136. svnCredentials.Password = new AprString(m_svnpass, pool);
  137. svnCredentials.MaySave = false;
  138. return SvnError.NoError;
  139. }
  140. private SvnError GetCommitLogCallback(out AprString logMessage, out SvnPath tmpFile, AprArray commitItems, IntPtr baton, AprPool pool)
  141. {
  142. if (!commitItems.IsNull)
  143. {
  144. foreach (SvnClientCommitItem2 item in commitItems)
  145. {
  146. m_log.Debug("[SVNBACKUP]: ... " + Path.GetFileName(item.Path.ToString()) + " (" + item.Kind.ToString() + ") r" + item.Revision.ToString());
  147. }
  148. }
  149. string msg = "Region Backup (" + System.Environment.MachineName + " at " + DateTime.UtcNow + " UTC)";
  150. m_log.Debug("[SVNBACKUP]: Saved with message: " + msg);
  151. logMessage = new AprString(msg, pool);
  152. tmpFile = new SvnPath(pool);
  153. return (SvnError.NoError);
  154. }
  155. #endregion
  156. #region IRegionModule Members
  157. public void Initialise(Scene scene, IConfigSource source)
  158. {
  159. try
  160. {
  161. if (!source.Configs["SVN"].GetBoolean("Enabled", false))
  162. return;
  163. m_enabled = true;
  164. m_svndir = source.Configs["SVN"].GetString("Directory", m_svndir);
  165. m_svnurl = source.Configs["SVN"].GetString("URL", m_svnurl);
  166. m_svnuser = source.Configs["SVN"].GetString("Username", m_svnuser);
  167. m_svnpass = source.Configs["SVN"].GetString("Password", m_svnpass);
  168. m_installBackupOnLoad = source.Configs["SVN"].GetBoolean("ImportOnStartup", m_installBackupOnLoad);
  169. m_svnAutoSave = source.Configs["SVN"].GetBoolean("Autosave", m_svnAutoSave);
  170. m_svnperiod = new TimeSpan(0, source.Configs["SVN"].GetInt("AutosavePeriod", (int) m_svnperiod.TotalMinutes), 0);
  171. }
  172. catch (Exception)
  173. {
  174. }
  175. lock (m_scenes)
  176. {
  177. m_scenes.Add(scene);
  178. }
  179. scene.EventManager.OnPluginConsole += EventManager_OnPluginConsole;
  180. }
  181. public void PostInitialise()
  182. {
  183. if (m_enabled == false)
  184. return;
  185. if (m_svnAutoSave)
  186. {
  187. m_timer.Interval = m_svnperiod.TotalMilliseconds;
  188. m_timer.Elapsed += m_timer_Elapsed;
  189. m_timer.AutoReset = true;
  190. m_timer.Start();
  191. }
  192. m_log.Info("[SVNBACKUP]: Connecting to SVN server " + m_svnurl + " ...");
  193. SetupSvnProvider();
  194. m_log.Info("[SVNBACKUP]: Creating repository in " + m_svndir + ".");
  195. CreateSvnDirectory();
  196. CheckoutSvn();
  197. SetupSerialiser();
  198. if (m_installBackupOnLoad)
  199. {
  200. m_log.Info("[SVNBACKUP]: Importing latest SVN revision to scenes...");
  201. foreach (Scene scene in m_scenes)
  202. {
  203. LoadRegion(scene);
  204. }
  205. }
  206. }
  207. public void Close()
  208. {
  209. }
  210. public string Name
  211. {
  212. get { return "SvnBackupModule"; }
  213. }
  214. public bool IsSharedModule
  215. {
  216. get { return true; }
  217. }
  218. #endregion
  219. private void EventManager_OnPluginConsole(string[] args)
  220. {
  221. if (args[0] == "svn" && args[1] == "save")
  222. {
  223. SaveAllRegions();
  224. }
  225. if (args.Length == 2)
  226. {
  227. if (args[0] == "svn" && args[1] == "load")
  228. {
  229. LoadAllScenes();
  230. }
  231. }
  232. if (args.Length == 3)
  233. {
  234. if (args[0] == "svn" && args[1] == "load")
  235. {
  236. LoadAllScenes(Int32.Parse(args[2]));
  237. }
  238. }
  239. if (args.Length == 3)
  240. {
  241. if (args[0] == "svn" && args[1] == "load-region")
  242. {
  243. LoadScene(args[2]);
  244. }
  245. }
  246. if (args.Length == 4)
  247. {
  248. if (args[0] == "svn" && args[1] == "load-region")
  249. {
  250. LoadScene(args[2], Int32.Parse(args[3]));
  251. }
  252. }
  253. }
  254. public void LoadScene(string name)
  255. {
  256. CheckoutSvn();
  257. foreach (Scene scene in m_scenes)
  258. {
  259. if (scene.RegionInfo.RegionName.ToLower().Equals(name.ToLower()))
  260. {
  261. LoadRegion(scene);
  262. return;
  263. }
  264. }
  265. m_log.Warn("[SVNBACKUP]: No region loaded - unable to find matching name.");
  266. }
  267. public void LoadScene(string name, int revision)
  268. {
  269. CheckoutSvn(new SvnRevision(revision));
  270. foreach (Scene scene in m_scenes)
  271. {
  272. if (scene.RegionInfo.RegionName.ToLower().Equals(name.ToLower()))
  273. {
  274. LoadRegion(scene);
  275. return;
  276. }
  277. }
  278. m_log.Warn("[SVNBACKUP]: No region loaded - unable to find matching name.");
  279. }
  280. public void LoadAllScenes()
  281. {
  282. CheckoutSvn();
  283. foreach (Scene scene in m_scenes)
  284. {
  285. LoadRegion(scene);
  286. }
  287. }
  288. public void LoadAllScenes(int revision)
  289. {
  290. CheckoutSvn(new SvnRevision(revision));
  291. foreach (Scene scene in m_scenes)
  292. {
  293. LoadRegion(scene);
  294. }
  295. }
  296. private void m_timer_Elapsed(object sender, ElapsedEventArgs e)
  297. {
  298. SaveAllRegions();
  299. }
  300. private void SetupSerialiser()
  301. {
  302. if (m_scenes.Count > 0)
  303. m_serialiser = m_scenes[0].RequestModuleInterface<IRegionSerialiser>();
  304. }
  305. private void SetupSvnProvider()
  306. {
  307. m_svnClient = new SvnClient();
  308. m_svnClient.AddUsernameProvider();
  309. m_svnClient.AddPromptProvider(new SvnAuthProviderObject.SimplePrompt(SimpleAuth), IntPtr.Zero, 2);
  310. m_svnClient.OpenAuth();
  311. m_svnClient.Context.LogMsgFunc2 = new SvnDelegate(new SvnClient.GetCommitLog2(GetCommitLogCallback));
  312. }
  313. private void CreateSvnDirectory()
  314. {
  315. if (!Directory.Exists(m_svndir))
  316. Directory.CreateDirectory(m_svndir);
  317. }
  318. }
  319. }