/vmrunGUI/Wrappers/vmrunGUIXmlWrapper.cs

# · C# · 290 lines · 248 code · 42 blank · 0 comment · 11 complexity · 64715e37e995cd95657a41eada616edc MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Xml;
  5. using System.IO;
  6. using System.Collections;
  7. namespace Ellanet
  8. {
  9. class vmrunGUIXmlWrapper
  10. {
  11. private const string encryptionKey = "Ch4rlt0n";
  12. private XmlDocument vmrunGUIXmlDoc;
  13. private string _xmlPath = null;
  14. public enum GuestStatus
  15. {
  16. PoweredOn,
  17. PoweredOff,
  18. Unknown
  19. }
  20. public struct VMwareGuest
  21. {
  22. public string vmxPath;
  23. public string host;
  24. public GuestStatus status;
  25. }
  26. public vmrunGUIXmlWrapper(string xmlPath)
  27. {
  28. _xmlPath = xmlPath;
  29. vmrunGUIXmlDoc = new XmlDocument();
  30. if (File.Exists(_xmlPath))
  31. {
  32. vmrunGUIXmlDoc.Load(_xmlPath);
  33. }
  34. else
  35. {
  36. vmrunGUIXmlDoc.LoadXml("<vmrun_gui><vmrun><path /></vmrun><hosts /><guests /><credentials_cache /></vmrun_gui>");
  37. SaveXml();
  38. }
  39. }
  40. public void SaveXml()
  41. {
  42. vmrunGUIXmlDoc.Save(_xmlPath);
  43. }
  44. public string GetvmrunPath()
  45. {
  46. return vmrunGUIXmlDoc.SelectSingleNode("vmrun_gui/vmrun/path").InnerText;
  47. }
  48. public void SetvmrunPath(string vmrunPath)
  49. {
  50. vmrunGUIXmlDoc.SelectSingleNode("vmrun_gui/vmrun/path").InnerText = vmrunPath;
  51. }
  52. public void AddHost(string name, string type, string host, string port, string arguments, string localhost, string overrideArguments, string credentials)
  53. {
  54. XmlNode hostNode = vmrunGUIXmlDoc.CreateElement("host");
  55. hostNode.InnerXml = "<name /><type /><host /><port /><arguments /><localhost /><override /><credentials />";
  56. hostNode.SelectSingleNode("name").InnerText = name;
  57. hostNode.SelectSingleNode("type").InnerText = type;
  58. hostNode.SelectSingleNode("host").InnerText = host;
  59. hostNode.SelectSingleNode("port").InnerText = port;
  60. hostNode.SelectSingleNode("arguments").InnerText = arguments;
  61. hostNode.SelectSingleNode("localhost").InnerText = localhost;
  62. hostNode.SelectSingleNode("override").InnerText = overrideArguments;
  63. hostNode.SelectSingleNode("credentials").InnerText = credentials;
  64. vmrunGUIXmlDoc.SelectSingleNode("vmrun_gui/hosts").AppendChild(hostNode);
  65. }
  66. public bool HostExists(string hostName)
  67. {
  68. return Convert.ToBoolean(vmrunGUIXmlDoc.SelectNodes(String.Format("vmrun_gui/hosts/host[name=\"{0}\"]", hostName)).Count > 0);
  69. }
  70. public ArrayList GetHostNames()
  71. {
  72. ArrayList hosts = new ArrayList();
  73. foreach (XmlNode hostNode in vmrunGUIXmlDoc.SelectNodes("vmrun_gui/hosts/host"))
  74. {
  75. hosts.Add(hostNode.SelectSingleNode("name").InnerText);
  76. }
  77. hosts.Sort();
  78. return hosts;
  79. }
  80. public string GetHostType(string hostName)
  81. {
  82. if (vmrunGUIXmlDoc.SelectNodes(String.Format("vmrun_gui/hosts/host[name=\"{0}\"]", hostName)).Count > 0)
  83. {
  84. return vmrunGUIXmlDoc.SelectSingleNode(String.Format("vmrun_gui/hosts/host[name=\"{0}\"]/type", hostName)).InnerText;
  85. }
  86. else
  87. {
  88. return null;
  89. }
  90. }
  91. public void RemoveHost(string hostName)
  92. {
  93. if (vmrunGUIXmlDoc.SelectNodes(String.Format("vmrun_gui/hosts/host[name=\"{0}\"]", hostName)).Count > 0)
  94. {
  95. XmlNode hostNode = vmrunGUIXmlDoc.SelectSingleNode(String.Format("vmrun_gui/hosts/host[name=\"{0}\"]", hostName));
  96. hostNode.ParentNode.RemoveChild(hostNode);
  97. }
  98. }
  99. public string GetHostChildNodeInnerText(string hostName, string nodeName)
  100. {
  101. if (vmrunGUIXmlDoc.SelectNodes(String.Format("vmrun_gui/hosts/host[name=\"{0}\" and {1}]", hostName, nodeName)).Count > 0)
  102. {
  103. return vmrunGUIXmlDoc.SelectSingleNode(String.Format("vmrun_gui/hosts/host[name=\"{0}\"]/{1}", hostName, nodeName)).InnerText;
  104. }
  105. else
  106. {
  107. return null;
  108. }
  109. }
  110. public ArrayList GetHostsByCredentials(string credentials)
  111. {
  112. ArrayList hosts = new ArrayList();
  113. foreach (XmlNode hostNode in vmrunGUIXmlDoc.SelectNodes(String.Format("vmrun_gui/hosts/host[credentials=\"{0}\"]", credentials)))
  114. {
  115. hosts.Add(hostNode.SelectSingleNode("name").InnerText);
  116. }
  117. hosts.Sort();
  118. return hosts;
  119. }
  120. public void AddGuest(string name, string host, string vmxPath, string description, string credentials)
  121. {
  122. XmlNode guestNode = vmrunGUIXmlDoc.CreateElement("guest");
  123. guestNode.InnerXml = "<name /><host /><vmx_path /><description /><credentials />";
  124. guestNode.SelectSingleNode("name").InnerText = name;
  125. guestNode.SelectSingleNode("host").InnerText = host;
  126. guestNode.SelectSingleNode("vmx_path").InnerText = vmxPath;
  127. guestNode.SelectSingleNode("description").InnerText = description;
  128. guestNode.SelectSingleNode("credentials").InnerText = credentials;
  129. vmrunGUIXmlDoc.SelectSingleNode("vmrun_gui/guests").AppendChild(guestNode);
  130. }
  131. public ArrayList GetGuestsFromHost(string hostName)
  132. {
  133. ArrayList guests = new ArrayList();
  134. foreach (XmlNode guestNode in vmrunGUIXmlDoc.SelectNodes(String.Format("vmrun_gui/guests/guest[host=\"{0}\"]", hostName)))
  135. {
  136. guests.Add(guestNode.SelectSingleNode("vmx_path").InnerText);
  137. }
  138. guests.Sort();
  139. return guests;
  140. }
  141. public string GetGuestChildNodeInnerText(string vmxPath, string hostName, string nodeName)
  142. {
  143. if (vmrunGUIXmlDoc.SelectNodes(String.Format("vmrun_gui/guests/guest[vmx_path=\"{0}\" and host=\"{1}\" and {2}]", vmxPath, hostName, nodeName)).Count > 0)
  144. {
  145. return vmrunGUIXmlDoc.SelectSingleNode(String.Format("vmrun_gui/guests/guest[vmx_path=\"{0}\" and host=\"{1}\"]/{2}", vmxPath, hostName, nodeName)).InnerText;
  146. }
  147. else
  148. {
  149. return null;
  150. }
  151. }
  152. public void SetGuestChildNodeInnerText(string vmxPath, string hostName, string nodeName, string value)
  153. {
  154. if (vmrunGUIXmlDoc.SelectNodes(String.Format("vmrun_gui/guests/guest[vmx_path=\"{0}\" and host=\"{1}\" and {2}]", vmxPath, hostName, nodeName)).Count > 0)
  155. {
  156. vmrunGUIXmlDoc.SelectSingleNode(String.Format("vmrun_gui/guests/guest[vmx_path=\"{0}\" and host=\"{1}\"]/{2}", vmxPath, hostName, nodeName)).InnerText = value;
  157. }
  158. }
  159. public void RemoveGuest(string vmxPath, string hostName)
  160. {
  161. if (vmrunGUIXmlDoc.SelectNodes(String.Format("vmrun_gui/guests/guest[vmx_path=\"{0}\" and host=\"{1}\"]", vmxPath, hostName)).Count > 0)
  162. {
  163. XmlNode guestNode = vmrunGUIXmlDoc.SelectSingleNode(String.Format("vmrun_gui/guests/guest[vmx_path=\"{0}\" and host=\"{1}\"]", vmxPath, hostName));
  164. guestNode.ParentNode.RemoveChild(guestNode);
  165. }
  166. }
  167. public bool GuestExists(string vmxPath, string hostName)
  168. {
  169. return Convert.ToBoolean(vmrunGUIXmlDoc.SelectNodes(String.Format("vmrun_gui/guests/guest[vmx_path=\"{0}\" and host=\"{1}\"]", vmxPath, hostName)).Count > 0);
  170. }
  171. public VMwareGuest[] GetGuestsByCredentials(string credentials)
  172. {
  173. ArrayList guests = new ArrayList();
  174. foreach (XmlNode guestNode in vmrunGUIXmlDoc.SelectNodes(String.Format("vmrun_gui/guests/guest[credentials=\"{0}\"]", credentials)))
  175. {
  176. VMwareGuest guest = new VMwareGuest();
  177. guest.host = guestNode.SelectSingleNode("host").InnerText;
  178. guest.vmxPath = guestNode.SelectSingleNode("vmx_path").InnerText;
  179. guests.Add(guest);
  180. }
  181. return (VMwareGuest[])guests.ToArray(typeof(VMwareGuest));
  182. }
  183. public void GetCredentials(string name, out string username, out string password)
  184. {
  185. username = null;
  186. password = null;
  187. if (vmrunGUIXmlDoc.SelectNodes(String.Format("vmrun_gui/credentials_cache/credentials[name=\"{0}\"]", name)).Count > 0)
  188. {
  189. XmlNode crdentialsNode = vmrunGUIXmlDoc.SelectSingleNode(String.Format("vmrun_gui/credentials_cache/credentials[name=\"{0}\"]", name));
  190. username = crdentialsNode.SelectSingleNode("username").InnerText;
  191. password = new StringEncryption().Decrypt(crdentialsNode.SelectSingleNode("password").InnerText, encryptionKey);
  192. }
  193. }
  194. public ArrayList GetCredentialsNames()
  195. {
  196. ArrayList credentials = new ArrayList();
  197. foreach (XmlNode hostNode in vmrunGUIXmlDoc.SelectNodes("vmrun_gui/credentials_cache/credentials"))
  198. {
  199. credentials.Add(hostNode.SelectSingleNode("name").InnerText);
  200. }
  201. credentials.Sort();
  202. return credentials;
  203. }
  204. public void AddCredentials(string name, string username, string password)
  205. {
  206. XmlNode hostNode = vmrunGUIXmlDoc.CreateElement("credentials");
  207. hostNode.InnerXml = "<name /><username /><password />";
  208. hostNode.SelectSingleNode("name").InnerText = name;
  209. hostNode.SelectSingleNode("username").InnerText = username;
  210. hostNode.SelectSingleNode("password").InnerText = new StringEncryption().Encrypt(password, encryptionKey);
  211. vmrunGUIXmlDoc.SelectSingleNode("vmrun_gui/credentials_cache").AppendChild(hostNode);
  212. }
  213. public void RemoveCredentials(string name)
  214. {
  215. if (vmrunGUIXmlDoc.SelectNodes(String.Format("vmrun_gui/credentials_cache/credentials[name=\"{0}\"]", name)).Count > 0)
  216. {
  217. XmlNode crdentialsNode = vmrunGUIXmlDoc.SelectSingleNode(String.Format("vmrun_gui/credentials_cache/credentials[name=\"{0}\"]", name));
  218. crdentialsNode.ParentNode.RemoveChild(crdentialsNode);
  219. }
  220. }
  221. public bool CredentialsExists(string name)
  222. {
  223. return Convert.ToBoolean(vmrunGUIXmlDoc.SelectNodes(String.Format("vmrun_gui/credentials_cache/credentials[name=\"{0}\"]", name)).Count > 0);
  224. }
  225. public void UpdateCredentialsOnHostsAndGuests(string previousName, string newName)
  226. {
  227. foreach (XmlNode guestNode in vmrunGUIXmlDoc.SelectNodes(String.Format("vmrun_gui/guests/guest[credentials=\"{0}\"]", previousName)))
  228. {
  229. guestNode.SelectSingleNode("credentials").InnerText = newName;
  230. }
  231. foreach (XmlNode hostNode in vmrunGUIXmlDoc.SelectNodes(String.Format("vmrun_gui/hosts/host[credentials=\"{0}\"]", previousName)))
  232. {
  233. hostNode.SelectSingleNode("credentials").InnerText = newName;
  234. }
  235. }
  236. public bool CredentialsInUse(string name)
  237. {
  238. if ((vmrunGUIXmlDoc.SelectNodes(String.Format("vmrun_gui/guests/guest[credentials=\"{0}\"]", name)).Count > 0) || (vmrunGUIXmlDoc.SelectNodes(String.Format("vmrun_gui/hosts/host[credentials=\"{0}\"]", name)).Count > 0))
  239. {
  240. return true;
  241. }
  242. else
  243. {
  244. return false;
  245. }
  246. }
  247. }
  248. }