PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/Resources/Companion/AdminRole/VMManagerService/MySQLBasedDBInstaller.cs

https://bitbucket.org/zgramana/azure-accelerators-project
C# | 276 lines | 209 code | 38 blank | 29 comment | 9 complexity | 529ece1f197622c299fa541b01771dcc MD5 | raw file
Possible License(s): LGPL-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ServiceModel.Syndication;
  6. using System.Diagnostics;
  7. using System.IO;
  8. using Microsoft.WindowsAzure.ServiceRuntime;
  9. using System.Threading;
  10. using System.Collections.Specialized;
  11. namespace WindowsAzureCompanion.VMManagerService
  12. {
  13. class MySQLBasedDBInstaller : IInstaller
  14. {
  15. private static string mySqlBasedDBName = null;
  16. private static Process mySqlBasedDBProcess = null;
  17. private SyndicationItem product = null;
  18. private string downloadFolder = null;
  19. private string downloadUrl = null;
  20. private string downloadFileName = null;
  21. private string installationFolder = null;
  22. private string applicationPath = null;
  23. private NameValueCollection productProperties = null;
  24. public MySQLBasedDBInstaller(string installationFolder,
  25. string downloadFolder,
  26. SyndicationItem product,
  27. string productVersion,
  28. NameValueCollection productProperties)
  29. {
  30. this.product = product;
  31. this.downloadFolder = downloadFolder;
  32. this.downloadUrl = WindowsAzureVMManager.GetDownloadUrlFromProductVersion(product, productVersion);
  33. this.downloadFileName = WindowsAzureVMManager.GetAttributeValueFromProductVersion(product, productVersion, "downloadFileName");
  34. this.installationFolder = installationFolder;
  35. this.applicationPath = WindowsAzureVMManager.GetAttributeValueFromProductVersion(product, productVersion, "applicationPath");
  36. this.productProperties = productProperties;
  37. }
  38. public void Install()
  39. {
  40. try
  41. {
  42. WindowsAzureVMManager.DownloadAndExtractWebArchive(downloadUrl,
  43. downloadFileName,
  44. downloadFolder,
  45. installationFolder,
  46. applicationPath);
  47. // Get internal port number for MySQL based database
  48. string portNumber = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["MySQLBasedDBPort"].IPEndpoint.Port.ToString();
  49. // Create thread for launching MySQL based database
  50. ThreadStart starter = delegate { StartMySQLBasedDB(portNumber); };
  51. Thread thread = new Thread(starter);
  52. thread.Start();
  53. Trace.TraceInformation("Successfully installed {0}", product.Title.Text);
  54. }
  55. catch (Exception ex)
  56. {
  57. Trace.TraceError("Unable to install {0}: {1}", product.Title.Text, downloadUrl);
  58. throw ex;
  59. }
  60. }
  61. // Stop MySQL based database
  62. public static bool StopMySQLBasedDB()
  63. {
  64. if (MySQLBasedDBInstaller.mySqlBasedDBProcess == null)
  65. {
  66. Trace.TraceError("{0} is not running.", MySQLBasedDBInstaller.mySqlBasedDBName);
  67. return false;
  68. }
  69. try
  70. {
  71. if (MySQLBasedDBInstaller.mySqlBasedDBProcess.HasExited)
  72. {
  73. Trace.TraceError("{0} already exited.", MySQLBasedDBInstaller.mySqlBasedDBName);
  74. return false;
  75. }
  76. else
  77. {
  78. Trace.TraceInformation("Stopping {0}..", MySQLBasedDBInstaller.mySqlBasedDBName);
  79. MySQLBasedDBInstaller.mySqlBasedDBProcess.Kill();
  80. MySQLBasedDBInstaller.mySqlBasedDBProcess.Close();
  81. MySQLBasedDBInstaller.mySqlBasedDBProcess = null;
  82. Trace.TraceInformation("{0} stopped.", MySQLBasedDBInstaller.mySqlBasedDBName);
  83. }
  84. return true;
  85. }
  86. catch (Exception ex)
  87. {
  88. Trace.TraceError("Failed to stop {0}. Error: {1}", MySQLBasedDBInstaller.mySqlBasedDBName, ex.Message);
  89. return false;
  90. }
  91. }
  92. // Start already configured MySQL
  93. public static bool StartConfiguredMySQL(string installationFolder, string iniFileName, string mySqlBasedDBName)
  94. {
  95. if (MySQLBasedDBInstaller.mySqlBasedDBProcess != null)
  96. {
  97. Trace.TraceError("{0} already running.", MySQLBasedDBInstaller.mySqlBasedDBName);
  98. return false;
  99. }
  100. try
  101. {
  102. // Get internal port number for MySQL based database
  103. string portNumber = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["MySQLBasedDBPort"].IPEndpoint.Port.ToString();
  104. // Create process and set required properties
  105. MySQLBasedDBInstaller.mySqlBasedDBProcess = new Process();
  106. MySQLBasedDBInstaller.mySqlBasedDBProcess.StartInfo.UseShellExecute = false;
  107. MySQLBasedDBInstaller.mySqlBasedDBProcess.StartInfo.RedirectStandardInput = true;
  108. MySQLBasedDBInstaller.mySqlBasedDBProcess.StartInfo.RedirectStandardOutput = true;
  109. // Output data received handler
  110. MySQLBasedDBInstaller.mySqlBasedDBProcess.OutputDataReceived +=
  111. new DataReceivedEventHandler(OutputDataReceivedHandler);
  112. // setting the process file name and arguments
  113. MySQLBasedDBInstaller.mySqlBasedDBProcess.StartInfo.WorkingDirectory = installationFolder;
  114. MySQLBasedDBInstaller.mySqlBasedDBProcess.StartInfo.FileName = Path.Combine(installationFolder, @"bin\mysqld.exe");
  115. MySQLBasedDBInstaller.mySqlBasedDBProcess.StartInfo.Arguments =
  116. "--defaults-file=" + iniFileName
  117. + " --port " + portNumber
  118. + " --console --standalone";
  119. // Start MySQL based DB process
  120. Trace.TraceInformation("Staring {0} ({1}) with arguments: ",
  121. MySQLBasedDBInstaller.mySqlBasedDBName,
  122. RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["MySQLBasedDBPort"].IPEndpoint.Address.ToString(),
  123. MySQLBasedDBInstaller.mySqlBasedDBProcess.StartInfo.Arguments);
  124. MySQLBasedDBInstaller.mySqlBasedDBProcess.Start();
  125. // Start the asynchronous read of the output stream.
  126. MySQLBasedDBInstaller.mySqlBasedDBProcess.BeginOutputReadLine();
  127. // Set MySQL based database name
  128. MySQLBasedDBInstaller.mySqlBasedDBName = mySqlBasedDBName;
  129. return true;
  130. }
  131. catch (Exception ex)
  132. {
  133. Trace.TraceError(ex.Message);
  134. return false;
  135. }
  136. }
  137. private bool StartMySQLBasedDB(string portNumber)
  138. {
  139. if (MySQLBasedDBInstaller.mySqlBasedDBProcess != null)
  140. {
  141. Trace.TraceError("{0} already running.", product.Title.Text);
  142. return false;
  143. }
  144. try
  145. {
  146. // Get ini file name
  147. string iniFileName = productProperties["iniFileName"];
  148. // Create process and set required properties
  149. MySQLBasedDBInstaller.mySqlBasedDBProcess = new Process();
  150. MySQLBasedDBInstaller.mySqlBasedDBProcess.StartInfo.UseShellExecute = false;
  151. MySQLBasedDBInstaller.mySqlBasedDBProcess.StartInfo.RedirectStandardInput = true;
  152. MySQLBasedDBInstaller.mySqlBasedDBProcess.StartInfo.RedirectStandardOutput = true;
  153. // Output data received handler
  154. MySQLBasedDBInstaller.mySqlBasedDBProcess.OutputDataReceived +=
  155. new DataReceivedEventHandler(OutputDataReceivedHandler);
  156. // setting the process file name and arguments
  157. MySQLBasedDBInstaller.mySqlBasedDBProcess.StartInfo.WorkingDirectory = installationFolder;
  158. MySQLBasedDBInstaller.mySqlBasedDBProcess.StartInfo.FileName = Path.Combine(installationFolder, @"bin\mysqld.exe");
  159. MySQLBasedDBInstaller.mySqlBasedDBProcess.StartInfo.Arguments =
  160. "--defaults-file=" + iniFileName
  161. + " --port " + portNumber
  162. + " --console --standalone";
  163. // Start MySQL based DB process
  164. Trace.TraceInformation("Staring {0} ({1}) with arguments: ",
  165. product.Title.Text,
  166. RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["MySQLBasedDBPort"].IPEndpoint.Address.ToString(),
  167. MySQLBasedDBInstaller.mySqlBasedDBProcess.StartInfo.Arguments);
  168. MySQLBasedDBInstaller.mySqlBasedDBProcess.Start();
  169. // Start the asynchronous read of the output stream.
  170. MySQLBasedDBInstaller.mySqlBasedDBProcess.BeginOutputReadLine();
  171. // Wait 10s and update the password
  172. Thread.Sleep(10000);
  173. Trace.TraceInformation("Updating {0} Server root password...", product.Title.Text);
  174. Process adminToolProcess = new Process();
  175. adminToolProcess.StartInfo.UseShellExecute = false;
  176. adminToolProcess.StartInfo.RedirectStandardInput = true;
  177. adminToolProcess.StartInfo.RedirectStandardOutput = true;
  178. // Output data received handler
  179. adminToolProcess.OutputDataReceived +=
  180. new DataReceivedEventHandler(OutputDataReceivedHandler);
  181. // Setting the process file name and arguments
  182. adminToolProcess.StartInfo.WorkingDirectory = installationFolder;
  183. adminToolProcess.StartInfo.FileName = Path.Combine(installationFolder, @"bin\mysqladmin.exe");
  184. adminToolProcess.StartInfo.Arguments =
  185. " -h 127.0.0.1 "
  186. + " -P " + portNumber
  187. + " -u root password "
  188. + productProperties["rootPassword"];
  189. // Start MySQL based database process
  190. adminToolProcess.Start();
  191. // Start the asynchronous read of the output stream.
  192. adminToolProcess.BeginOutputReadLine();
  193. adminToolProcess.WaitForExit();
  194. // Set MySQL based database name
  195. MySQLBasedDBInstaller.mySqlBasedDBName = product.Title.Text;
  196. return true;
  197. }
  198. catch (Exception ex)
  199. {
  200. Trace.TraceError(ex.Message);
  201. return false;
  202. }
  203. }
  204. // Get MySQL based database name
  205. public static string GetMySQLBasedDBName()
  206. {
  207. return MySQLCommunityServerInstaller.mySqlBasedDBName;
  208. }
  209. // Get MySQL based database server port number
  210. public static string GetMySQLBasedDBServerPortNumber()
  211. {
  212. return RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["MySQLBasedDBPort"].IPEndpoint.Port.ToString();
  213. }
  214. // Get MySQL based database server IP address
  215. public static string GetMySQLBasedDBServerIPAddress()
  216. {
  217. // Return loopback address
  218. return "127.0.0.1";
  219. }
  220. // Has MySQL Server started?
  221. public static bool IsMySQLServerStarted()
  222. {
  223. if (MySQLBasedDBInstaller.mySqlBasedDBProcess != null)
  224. {
  225. return true;
  226. }
  227. else
  228. {
  229. return false;
  230. }
  231. }
  232. // Output data received handler for invoked processes
  233. static void OutputDataReceivedHandler(object sender, System.Diagnostics.DataReceivedEventArgs e)
  234. {
  235. Trace.TraceInformation(e.Data);
  236. }
  237. }
  238. }