From 75ccdbb916d4505b2155e3ee5ffaee479d5186bd Mon Sep 17 00:00:00 2001 From: "a.skorina" Date: Thu, 18 Dec 2014 17:50:06 +0300 Subject: [PATCH] scheduler installer fix --- .../Setup.SchedulerService/Product.wxs | 19 ++- .../CustomAction.cs | 119 +++++++++++++----- .../Common/SecurityUtils.cs | 19 +++ 3 files changed, 121 insertions(+), 36 deletions(-) diff --git a/WebsitePanel.Installer/Sources/Setup.SchedulerService/Product.wxs b/WebsitePanel.Installer/Sources/Setup.SchedulerService/Product.wxs index 59fc493e..42e79b27 100644 --- a/WebsitePanel.Installer/Sources/Setup.SchedulerService/Product.wxs +++ b/WebsitePanel.Installer/Sources/Setup.SchedulerService/Product.wxs @@ -82,7 +82,9 @@ - 1 + 1 + SKIPCONNECTIONSTRINGSTEP = "0" + SKIPCONNECTIONSTRINGSTEP = "1" 1 @@ -101,13 +103,15 @@ LicenseAccepted = "1" - 1 + SKIPCONNECTIONSTRINGSTEP = "0" + SKIPCONNECTIONSTRINGSTEP = "1" + (NOT UPGRADINGPRODUCTCODE) AND (REMOVE="ALL") - NOT Installed or REINSTALL + NOT Installed or REINSTALL @@ -115,9 +119,16 @@ - + + + + + + + + diff --git a/WebsitePanel.Installer/Sources/WebsitePanel.SchedulerServiceInstaller/CustomAction.cs b/WebsitePanel.Installer/Sources/WebsitePanel.SchedulerServiceInstaller/CustomAction.cs index dbfaac3c..4fe768ea 100644 --- a/WebsitePanel.Installer/Sources/WebsitePanel.SchedulerServiceInstaller/CustomAction.cs +++ b/WebsitePanel.Installer/Sources/WebsitePanel.SchedulerServiceInstaller/CustomAction.cs @@ -27,6 +27,7 @@ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. using System; +using System.Collections.Generic; using System.Configuration.Install; using System.Data; using System.Data.SqlClient; @@ -35,6 +36,9 @@ using System.Linq; using System.ServiceProcess; using System.Text.RegularExpressions; using System.Threading; +using System.Windows.Forms; +using System.Windows.Forms.VisualStyles; +using System.Xml; using Microsoft.Deployment.WindowsInstaller; using WebsitePanel.Setup; @@ -42,6 +46,8 @@ namespace WebsitePanel.SchedulerServiceInstaller { public class CustomActions { + public const string CustomDataDelimiter = "-=del=-"; + [CustomAction] public static ActionResult CheckConnection(Session session) { @@ -63,9 +69,23 @@ namespace WebsitePanel.SchedulerServiceInstaller [CustomAction] public static ActionResult FinalizeInstall(Session session) { - ChangeConfigString("installer.connectionstring", session["CONNECTIONSTRING"], session["INSTALLFOLDER"]); - ChangeCryptoKey(session["INSTALLFOLDER"]); - InstallService(session["INSTALLFOLDER"]); + var connectionString = GetCustomActionProperty(session, "ConnectionString").Replace(CustomDataDelimiter, ";"); + var serviceFolder = GetCustomActionProperty(session, "ServiceFolder"); + var previousConnectionString = GetCustomActionProperty(session, "PreviousConnectionString").Replace(CustomDataDelimiter, ";"); + var previousCryptoKey = GetCustomActionProperty(session, "PreviousCryptoKey"); + + if (string.IsNullOrEmpty(serviceFolder)) + { + return ActionResult.Success; + } + + connectionString = string.IsNullOrEmpty(previousConnectionString) + ? connectionString + : previousConnectionString; + + ChangeConfigString("/configuration/connectionStrings/add[@name='EnterpriseServer']", "connectionString", connectionString, serviceFolder); + ChangeConfigString("/configuration/appSettings/add[@key='WebsitePanel.CryptoKey']", "value", previousCryptoKey, serviceFolder); + InstallService(serviceFolder); return ActionResult.Success; } @@ -78,6 +98,46 @@ namespace WebsitePanel.SchedulerServiceInstaller return ActionResult.Success; } + [CustomAction] + public static ActionResult PreInstallationAction(Session session) + { + session["SKIPCONNECTIONSTRINGSTEP"] = "0"; + + session["SERVICEFOLDER"] = session["INSTALLFOLDER"]; + + var servicePath = SecurityUtils.GetServicePath("WebsitePanel Scheduler"); + + if (!string.IsNullOrEmpty(servicePath)) + { + string path = Path.Combine(servicePath, "WebsitePanel.SchedulerService.exe.config"); + + if (File.Exists(path)) + { + using (var reader = new StreamReader(path)) + { + string content = reader.ReadToEnd(); + var pattern = new Regex(@"(?<=)"); + Match match = pattern.Match(content); + session["PREVIOUSCRYPTOKEY"] = match.Value; + + var connectionStringPattern = new Regex(@"(?<=)"); + match = connectionStringPattern.Match(content); + session["PREVIOUSCONNECTIONSTRING"] = match.Value.Replace(";", CustomDataDelimiter); + } + + session["SKIPCONNECTIONSTRINGSTEP"] = "1"; + + if (string.IsNullOrEmpty(session["SERVICEFOLDER"])) + { + session["SERVICEFOLDER"] = servicePath; + } + } + + } + + return ActionResult.Success; + } + private static void InstallService(string installFolder) { try @@ -122,44 +182,29 @@ namespace WebsitePanel.SchedulerServiceInstaller } } - private static void ChangeCryptoKey(string installFolder) + private static void ChangeConfigString(string nodePath, string attrToChange, string value, string installFolder) { - string path = Path.Combine(installFolder.Replace("SchedulerService", "Enterprise Server"), "web.config"); - string cryptoKey = "0123456789"; - - if (File.Exists(path)) - { - using (var reader = new StreamReader(path)) - { - string content = reader.ReadToEnd(); - var pattern = new Regex(@"(?<=)"); - Match match = pattern.Match(content); - cryptoKey = match.Value; - } - } - - ChangeConfigString("installer.cryptokey", cryptoKey, installFolder); - } - - private static void ChangeConfigString(string searchString, string replaceValue, string installFolder) - { - string content; string path = Path.Combine(installFolder, "WebsitePanel.SchedulerService.exe.config"); - using (var reader = new StreamReader(path)) + if (!File.Exists(path)) { - content = reader.ReadToEnd(); + return; } - var re = new Regex("\\$\\{" + searchString + "\\}+", RegexOptions.IgnoreCase); - content = re.Replace(content, replaceValue); + XmlDocument xmldoc = new XmlDocument(); + xmldoc.Load(path); - using (var writer = new StreamWriter(path)) + XmlElement node = xmldoc.SelectSingleNode(nodePath) as XmlElement; + + if (node != null) { - writer.Write(content); + node.SetAttribute(attrToChange, value); + + xmldoc.Save(path); } } + private static void StopService(string serviceName) { var sc = new ServiceController(serviceName); @@ -184,12 +229,12 @@ namespace WebsitePanel.SchedulerServiceInstaller private static string GetConnectionString(string serverName, string databaseName) { - return string.Format("Server={0};database={1};Trusted_Connection=true;", serverName, databaseName); + return string.Format("Server={0};database={1};Trusted_Connection=true;", serverName, databaseName).Replace(";", CustomDataDelimiter); } private static string GetConnectionString(string serverName, string databaseName, string login, string password) { - return string.Format("Server={0};database={1};uid={2};password={3};", serverName, databaseName, login, password); + return string.Format("Server={0};database={1};uid={2};password={3};", serverName, databaseName, login, password).Replace(";", CustomDataDelimiter); } private static bool CheckConnection(string connectionString) @@ -215,5 +260,15 @@ namespace WebsitePanel.SchedulerServiceInstaller return result; } + + private static string GetCustomActionProperty(Session session, string key) + { + if (session.CustomActionData.ContainsKey(key)) + { + return session.CustomActionData[key].Replace("-=-", ";"); + } + + return string.Empty; + } } } \ No newline at end of file diff --git a/WebsitePanel.Installer/Sources/WebsitePanel.Setup/Common/SecurityUtils.cs b/WebsitePanel.Installer/Sources/WebsitePanel.Setup/Common/SecurityUtils.cs index 668e7875..5e71edae 100644 --- a/WebsitePanel.Installer/Sources/WebsitePanel.Setup/Common/SecurityUtils.cs +++ b/WebsitePanel.Installer/Sources/WebsitePanel.Setup/Common/SecurityUtils.cs @@ -1110,6 +1110,25 @@ namespace WebsitePanel.Setup wmiService.Delete(); } + public static string GetServicePath(string serviceName) + { + var mc = new ManagementClass("Win32_Service"); + + foreach (var mo in mc.GetInstances()) + { + if (mo.GetPropertyValue("Name").ToString() == serviceName) + { + var path = mo.GetPropertyValue("PathName").ToString().Trim('"'); + + var directory = Path.GetDirectoryName(path); + + return directory; + } + } + + return string.Empty; + } + #endregion }