From 848cbd4bb1d68b93cf37e9579daf91046d38dccd Mon Sep 17 00:00:00 2001 From: McMak Date: Thu, 21 May 2015 15:49:02 +0300 Subject: [PATCH] Installer: old to new update #6 - add scheduler service registration. --- .../Common/InstallAction.cs | 3 +- .../Common/SetupVariables.cs | 6 +- .../WebsitePanel.Setup/Internal/Adapter.cs | 148 +++++++++++++----- 3 files changed, 119 insertions(+), 38 deletions(-) diff --git a/WebsitePanel.Installer/Sources/WebsitePanel.Setup/Common/InstallAction.cs b/WebsitePanel.Installer/Sources/WebsitePanel.Setup/Common/InstallAction.cs index 6cfa5953..832de5ff 100644 --- a/WebsitePanel.Installer/Sources/WebsitePanel.Setup/Common/InstallAction.cs +++ b/WebsitePanel.Installer/Sources/WebsitePanel.Setup/Common/InstallAction.cs @@ -89,7 +89,8 @@ namespace WebsitePanel.Setup SwitchEntServer2AspNet40, SwitchWebPortal2AspNet40, ConfigureSecureSessionModuleInWebConfig, - RestoreConfig + RestoreConfig, + UpdateXml } public class InstallAction diff --git a/WebsitePanel.Installer/Sources/WebsitePanel.Setup/Common/SetupVariables.cs b/WebsitePanel.Installer/Sources/WebsitePanel.Setup/Common/SetupVariables.cs index 15cbc217..4f5fd3fb 100644 --- a/WebsitePanel.Installer/Sources/WebsitePanel.Setup/Common/SetupVariables.cs +++ b/WebsitePanel.Installer/Sources/WebsitePanel.Setup/Common/SetupVariables.cs @@ -251,7 +251,8 @@ namespace WebsitePanel.Setup public string SpecialBaseDirectory { get; set; } public IDictionary FileNameMap { get; set; } - + public IDictionary SessionVariables { get; set; } + public IDictionary XmlData { get; set; } // XPath, Value. public bool ComponentExists { get; set; } public string UpdateVersion { get; set; } @@ -283,7 +284,8 @@ namespace WebsitePanel.Setup public string ServiceName { get; set; } public string ConfigurationFile { get; set; } - + private bool m_UseUserCredentials = true; + public bool UseUserCredentials { get { return m_UseUserCredentials; } set { m_UseUserCredentials = value; } } public string ServiceFile { get; set; } public string LicenseKey { get; set; } diff --git a/WebsitePanel.Installer/Sources/WebsitePanel.Setup/Internal/Adapter.cs b/WebsitePanel.Installer/Sources/WebsitePanel.Setup/Internal/Adapter.cs index 8a3dca10..1a8d8469 100644 --- a/WebsitePanel.Installer/Sources/WebsitePanel.Setup/Internal/Adapter.cs +++ b/WebsitePanel.Installer/Sources/WebsitePanel.Setup/Internal/Adapter.cs @@ -7,8 +7,10 @@ using System.Configuration.Install; using System.Data; using System.Diagnostics; using System.IO; +using System.Linq; using System.Reflection; using System.Runtime.Serialization; +using System.ServiceProcess; using System.Text; using System.Text.RegularExpressions; using System.Xml; @@ -165,6 +167,9 @@ namespace WebsitePanel.Setup.Internal Dst.BaseDirectory = Utils.GetStringSetupParameter(Hash, Global.Parameters.BaseDirectory); Dst.ComponentId = Utils.GetStringSetupParameter(Hash, Global.Parameters.ComponentId); Dst.ComponentExists = string.IsNullOrWhiteSpace(Dst.ComponentId) ? false : true; + + Dst.UpdateVersion = Utils.GetStringSetupParameter(Hash, "Version"); + Dst.SessionVariables = Src; } public static string GetFullConfigPath(SetupVariables Ctx) { @@ -406,6 +411,9 @@ namespace WebsitePanel.Setup.Internal case ActionTypes.RestoreConfig: RestoreXmlConfigs(Execute.SetupVariables); break; + case ActionTypes.UpdateXml: + UpdateXml(Execute.Path, Execute.SetupVariables.XmlData); + break; } } catch (Exception ex) @@ -2167,39 +2175,54 @@ namespace WebsitePanel.Setup.Internal try { string componentId = Context.ComponentId; - string path = Path.Combine(Context.InstallationFolder, Context.ServiceFile); + string path = Context.ServiceFile; // FullFileName. string service = Context.ServiceName; + + Log.WriteStart(string.Format("Registering \"{0}\" windows service", service)); + if (!File.Exists(path)) { Log.WriteError(string.Format("File {0} not found", path), null); return; } - - Log.WriteStart(string.Format("Registering \"{0}\" windows service", service)); - string domain = Context.UserDomain; - if (string.IsNullOrEmpty(domain)) - domain = "."; - string arguments = string.Format("/i /LogFile=\"\" /user={0}\\{1} /password={2}", - domain, Context.UserAccount, Context.UserPassword); - int exitCode = Utils.RunProcess(path, arguments); - if (exitCode == 0) + if (ServiceController.GetServices().Any(s => s.DisplayName.Equals(service, StringComparison.CurrentCultureIgnoreCase))) { + var Msg = string.Format("Service \"{0}\" already installed.", service); + Log.WriteEnd(Msg); + InstallLog.AppendLine(Msg); + } + try + { + string domain = Context.UserDomain; + if (string.IsNullOrEmpty(domain)) + domain = "."; + + string arguments = string.Empty; + if (Context.UseUserCredentials) + arguments = string.Format("/i /LogFile=\"\" /user={0}\\{1} /password={2}", domain, Context.UserAccount, Context.UserPassword); + else + arguments = "/i /LogFile= ''"; + + ManagedInstallerClass.InstallHelper(new[] { arguments, path }); //add rollback action RollBack.RegisterWindowsService(path, service); - + var Msg = string.Format("Registered \"{0}\" Windows service ", service); //update log - Log.WriteEnd("Registered windows service"); + Log.WriteEnd(Msg); //update install log - InstallLog.AppendLine(string.Format("- Registered \"{0}\" Windows service ", service)); + InstallLog.AppendLine(Msg); + + // update config setings + AppConfig.EnsureComponentConfig(componentId); + AppConfig.SetComponentSettingStringValue(componentId, "ServiceName", service); + AppConfig.SetComponentSettingStringValue(componentId, "ServiceFile", path); + AppConfig.SaveConfiguration(); } - else + catch (Exception ex) { - Log.WriteError(string.Format("Unable to register \"{0}\" Windows service. Error code: {1}", service, exitCode), null); + Log.WriteError(string.Format("Unable to register \"{0}\" Windows service.", service), null); InstallLog.AppendLine(string.Format("- Failed to register \"{0}\" windows service ", service)); - } - // update config setings - AppConfig.SetComponentSettingStringValue(componentId, "ServiceName", Context.ServiceName); - AppConfig.SetComponentSettingStringValue(componentId, "ServiceFile", Context.ServiceFile); + } } catch (Exception ex) { @@ -3793,9 +3816,9 @@ namespace WebsitePanel.Setup.Internal { //update settings AppConfig.SetComponentSettingStringValue(componentId, "Release", Context.UpdateVersion); - AppConfig.SetComponentSettingStringValue(componentId, "Installer", Context.Installer); - AppConfig.SetComponentSettingStringValue(componentId, "InstallerType", Context.InstallerType); - AppConfig.SetComponentSettingStringValue(componentId, "InstallerPath", Context.InstallerPath); + AppConfig.SetComponentSettingStringValue(componentId, "Installer", Context.SessionVariables["Installer"]); + AppConfig.SetComponentSettingStringValue(componentId, "InstallerType", Context.SessionVariables["InstallerType"]); + AppConfig.SetComponentSettingStringValue(componentId, "InstallerPath", Context.SessionVariables["InstallerPath"]); } Log.WriteInfo("Saving system configuration"); @@ -3989,6 +4012,52 @@ namespace WebsitePanel.Setup.Internal throw; } } + private void UpdateXml(string FullFileName, IDictionary Data) + { + var Msg = "Update xml files."; + try + { + Log.WriteStart(Msg); + if (!File.Exists(FullFileName)) + throw new FileNotFoundException(FullFileName); + var Doc = new XmlDocument(); + Doc.Load(FullFileName); + foreach(var Key in Data.Keys) + { + var Node = Doc.SelectSingleNode(Key) as XmlElement; + if (Node == null) + { + Log.WriteInfo(string.Format("XPath \"{0}\" not found.", Key)); + } + else + { + var Value = Data[Key]; + switch (Value.Length) + { + case 1: + Node.Value = Value[0]; + break; + case 2: + Node.SetAttribute(Value[0], Value[1]); + break; + default: + Log.WriteError(string.Format("Bad xml value for \"{0}\".", Key)); + break; + } + } + } + Doc.Save(FullFileName); + } + catch (Exception ex) + { + Log.WriteError(ex.ToString()); + throw; + } + finally + { + Log.WriteEnd(Msg); + } + } } public class UninstallScript : SetupScript // UninstallPage { @@ -4000,7 +4069,6 @@ namespace WebsitePanel.Setup.Internal { var list = base.GetActions(componentId); InstallAction action = null; - //windows service string serviceName = AppConfig.GetComponentSettingStringValue(componentId, "ServiceName"); string serviceFile = AppConfig.GetComponentSettingStringValue(componentId, "ServiceFile"); @@ -4008,16 +4076,18 @@ namespace WebsitePanel.Setup.Internal if (!string.IsNullOrEmpty(serviceName) && !string.IsNullOrEmpty(serviceFile)) { action = new InstallAction(ActionTypes.UnregisterWindowsService); - action.Path = Path.Combine(installFolder, serviceFile); + action.Path = serviceFile; // FullFileName. action.Name = serviceName; action.Description = "Removing Windows service..."; action.Log = string.Format("- Remove {0} Windows service", serviceName); list.Add(action); } - //database bool deleteDatabase = AppConfig.GetComponentSettingBooleanValue(componentId, "NewDatabase"); - if (deleteDatabase) + bool allowDelete = true; + if (Context.InstallerType.ToLowerInvariant().Equals("msi")) // DB handled by MSI (WiX) by default. + allowDelete = false; + if (deleteDatabase && allowDelete) { string connectionString = AppConfig.GetComponentSettingStringValue(componentId, "InstallConnectionString"); string database = AppConfig.GetComponentSettingStringValue(componentId, "Database"); @@ -4053,7 +4123,6 @@ namespace WebsitePanel.Setup.Internal action.Log = string.Format("- Delete {0} database login", loginName); list.Add(action); } - //virtual directory bool deleteVirtualDirectory = AppConfig.GetComponentSettingBooleanValue(componentId, "NewVirtualDirectory"); if (deleteVirtualDirectory) @@ -4067,7 +4136,6 @@ namespace WebsitePanel.Setup.Internal action.Log = string.Format("- Delete {0} virtual directory...", virtualDirectory); list.Add(action); } - //web site bool deleteWebSite = AppConfig.GetComponentSettingBooleanValue(componentId, "NewWebSite"); if (deleteWebSite) @@ -4079,7 +4147,6 @@ namespace WebsitePanel.Setup.Internal action.Log = string.Format("- Delete {0} web site", siteId); list.Add(action); } - //application pool bool deleteAppPool = AppConfig.GetComponentSettingBooleanValue(componentId, "NewApplicationPool"); if (deleteAppPool) @@ -4093,7 +4160,6 @@ namespace WebsitePanel.Setup.Internal action.Log = string.Format("- Delete {0} application pool", appPoolName); list.Add(action); } - //user account bool deleteUserAccount = AppConfig.GetComponentSettingBooleanValue(componentId, "NewUserAccount"); if (deleteUserAccount) @@ -4111,7 +4177,6 @@ namespace WebsitePanel.Setup.Internal action.Log = string.Format("- Remove {0} user account membership", username); list.Add(action); } - action = new InstallAction(ActionTypes.DeleteUserAccount); action.Name = username; action.Domain = domain; @@ -4253,7 +4318,6 @@ namespace WebsitePanel.Setup.Internal else if (ModeExtension == ModeExtension.Restore) { Context.ComponentId = GetComponentID(Context); - Context.UpdateVersion = Context.Release; AppConfig.LoadComponentSettings(Context); new RestoreScript(Context).Run(); } @@ -4331,9 +4395,24 @@ namespace WebsitePanel.Setup.Internal else if (ModeExtension == ModeExtension.Restore) { Context.ComponentId = GetComponentID(Context); - Context.UpdateVersion = Context.Release; + Context.UseUserCredentials = false; AppConfig.LoadComponentSettings(Context); - new RestoreScript(Context).Run(); + if (string.IsNullOrWhiteSpace(Context.ServiceName) || string.IsNullOrWhiteSpace(Context.ServiceFile)) + { + Context.ServiceName = Global.Parameters.SchedulerServiceName; + Context.ServiceFile = Path.Combine(Context.InstallationFolder, "bin", Global.Parameters.SchedulerServiceFileName); + } + SetupScript Script = new RestoreScript(Context); + Script.Run(); + Script = new ExpressScript(Context); + var XmlUp = new Dictionary(); + XmlUp.Add("configuration/connectionStrings/add[@name='EnterpriseServer']", new string[] {"connectionString", Context.ConnectionString}); + XmlUp.Add("configuration/appSettings/add[@key='WebsitePanel.CryptoKey']", new string[] {"value", Context.CryptoKey }); + Context.XmlData = XmlUp; + Script.Actions.Add(new InstallAction(ActionTypes.UpdateXml) { SetupVariables = Context, Path = string.Format("{0}.config", Context.ServiceFile) }); + Script.Actions.Add(new InstallAction(ActionTypes.RegisterWindowsService)); + Script.Actions.Add(new InstallAction(ActionTypes.StartWindowsService)); + Script.Run(); } else throw new NotImplementedException("Install " + ModeExtension.ToString()); @@ -4409,7 +4488,6 @@ namespace WebsitePanel.Setup.Internal else if (ModeExtension == ModeExtension.Restore) { Context.ComponentId = GetComponentID(Context); - Context.UpdateVersion = Context.Release; AppConfig.LoadComponentSettings(Context); new RestoreScript(Context).Run(); }