Installer: old to new update #6 - add scheduler service registration.

This commit is contained in:
McMak 2015-05-21 15:49:02 +03:00
parent a0ceaf800a
commit 848cbd4bb1
3 changed files with 119 additions and 38 deletions

View file

@ -89,7 +89,8 @@ namespace WebsitePanel.Setup
SwitchEntServer2AspNet40, SwitchEntServer2AspNet40,
SwitchWebPortal2AspNet40, SwitchWebPortal2AspNet40,
ConfigureSecureSessionModuleInWebConfig, ConfigureSecureSessionModuleInWebConfig,
RestoreConfig RestoreConfig,
UpdateXml
} }
public class InstallAction public class InstallAction

View file

@ -251,7 +251,8 @@ namespace WebsitePanel.Setup
public string SpecialBaseDirectory { get; set; } public string SpecialBaseDirectory { get; set; }
public IDictionary<string, string> FileNameMap { get; set; } public IDictionary<string, string> FileNameMap { get; set; }
public IDictionary<string, string> SessionVariables { get; set; }
public IDictionary<string, string[]> XmlData { get; set; } // XPath, Value.
public bool ComponentExists { get; set; } public bool ComponentExists { get; set; }
public string UpdateVersion { get; set; } public string UpdateVersion { get; set; }
@ -283,7 +284,8 @@ namespace WebsitePanel.Setup
public string ServiceName { get; set; } public string ServiceName { get; set; }
public string ConfigurationFile { 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 ServiceFile { get; set; }
public string LicenseKey { get; set; } public string LicenseKey { get; set; }

View file

@ -7,8 +7,10 @@ using System.Configuration.Install;
using System.Data; using System.Data;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq;
using System.Reflection; using System.Reflection;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using System.ServiceProcess;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Xml; using System.Xml;
@ -165,6 +167,9 @@ namespace WebsitePanel.Setup.Internal
Dst.BaseDirectory = Utils.GetStringSetupParameter(Hash, Global.Parameters.BaseDirectory); Dst.BaseDirectory = Utils.GetStringSetupParameter(Hash, Global.Parameters.BaseDirectory);
Dst.ComponentId = Utils.GetStringSetupParameter(Hash, Global.Parameters.ComponentId); Dst.ComponentId = Utils.GetStringSetupParameter(Hash, Global.Parameters.ComponentId);
Dst.ComponentExists = string.IsNullOrWhiteSpace(Dst.ComponentId) ? false : true; Dst.ComponentExists = string.IsNullOrWhiteSpace(Dst.ComponentId) ? false : true;
Dst.UpdateVersion = Utils.GetStringSetupParameter(Hash, "Version");
Dst.SessionVariables = Src;
} }
public static string GetFullConfigPath(SetupVariables Ctx) public static string GetFullConfigPath(SetupVariables Ctx)
{ {
@ -406,6 +411,9 @@ namespace WebsitePanel.Setup.Internal
case ActionTypes.RestoreConfig: case ActionTypes.RestoreConfig:
RestoreXmlConfigs(Execute.SetupVariables); RestoreXmlConfigs(Execute.SetupVariables);
break; break;
case ActionTypes.UpdateXml:
UpdateXml(Execute.Path, Execute.SetupVariables.XmlData);
break;
} }
} }
catch (Exception ex) catch (Exception ex)
@ -2167,39 +2175,54 @@ namespace WebsitePanel.Setup.Internal
try try
{ {
string componentId = Context.ComponentId; string componentId = Context.ComponentId;
string path = Path.Combine(Context.InstallationFolder, Context.ServiceFile); string path = Context.ServiceFile; // FullFileName.
string service = Context.ServiceName; string service = Context.ServiceName;
Log.WriteStart(string.Format("Registering \"{0}\" windows service", service));
if (!File.Exists(path)) if (!File.Exists(path))
{ {
Log.WriteError(string.Format("File {0} not found", path), null); Log.WriteError(string.Format("File {0} not found", path), null);
return; return;
} }
if (ServiceController.GetServices().Any(s => s.DisplayName.Equals(service, StringComparison.CurrentCultureIgnoreCase)))
Log.WriteStart(string.Format("Registering \"{0}\" windows service", service)); {
var Msg = string.Format("Service \"{0}\" already installed.", service);
Log.WriteEnd(Msg);
InstallLog.AppendLine(Msg);
}
try
{
string domain = Context.UserDomain; string domain = Context.UserDomain;
if (string.IsNullOrEmpty(domain)) if (string.IsNullOrEmpty(domain))
domain = "."; domain = ".";
string arguments = string.Format("/i /LogFile=\"\" /user={0}\\{1} /password={2}",
domain, Context.UserAccount, Context.UserPassword); string arguments = string.Empty;
int exitCode = Utils.RunProcess(path, arguments); if (Context.UseUserCredentials)
if (exitCode == 0) 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 //add rollback action
RollBack.RegisterWindowsService(path, service); RollBack.RegisterWindowsService(path, service);
var Msg = string.Format("Registered \"{0}\" Windows service ", service);
//update log //update log
Log.WriteEnd("Registered windows service"); Log.WriteEnd(Msg);
//update install log //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)); 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) catch (Exception ex)
{ {
@ -3793,9 +3816,9 @@ namespace WebsitePanel.Setup.Internal
{ {
//update settings //update settings
AppConfig.SetComponentSettingStringValue(componentId, "Release", Context.UpdateVersion); AppConfig.SetComponentSettingStringValue(componentId, "Release", Context.UpdateVersion);
AppConfig.SetComponentSettingStringValue(componentId, "Installer", Context.Installer); AppConfig.SetComponentSettingStringValue(componentId, "Installer", Context.SessionVariables["Installer"]);
AppConfig.SetComponentSettingStringValue(componentId, "InstallerType", Context.InstallerType); AppConfig.SetComponentSettingStringValue(componentId, "InstallerType", Context.SessionVariables["InstallerType"]);
AppConfig.SetComponentSettingStringValue(componentId, "InstallerPath", Context.InstallerPath); AppConfig.SetComponentSettingStringValue(componentId, "InstallerPath", Context.SessionVariables["InstallerPath"]);
} }
Log.WriteInfo("Saving system configuration"); Log.WriteInfo("Saving system configuration");
@ -3989,6 +4012,52 @@ namespace WebsitePanel.Setup.Internal
throw; throw;
} }
} }
private void UpdateXml(string FullFileName, IDictionary<string, string[]> 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 public class UninstallScript : SetupScript // UninstallPage
{ {
@ -4000,7 +4069,6 @@ namespace WebsitePanel.Setup.Internal
{ {
var list = base.GetActions(componentId); var list = base.GetActions(componentId);
InstallAction action = null; InstallAction action = null;
//windows service //windows service
string serviceName = AppConfig.GetComponentSettingStringValue(componentId, "ServiceName"); string serviceName = AppConfig.GetComponentSettingStringValue(componentId, "ServiceName");
string serviceFile = AppConfig.GetComponentSettingStringValue(componentId, "ServiceFile"); string serviceFile = AppConfig.GetComponentSettingStringValue(componentId, "ServiceFile");
@ -4008,16 +4076,18 @@ namespace WebsitePanel.Setup.Internal
if (!string.IsNullOrEmpty(serviceName) && !string.IsNullOrEmpty(serviceFile)) if (!string.IsNullOrEmpty(serviceName) && !string.IsNullOrEmpty(serviceFile))
{ {
action = new InstallAction(ActionTypes.UnregisterWindowsService); action = new InstallAction(ActionTypes.UnregisterWindowsService);
action.Path = Path.Combine(installFolder, serviceFile); action.Path = serviceFile; // FullFileName.
action.Name = serviceName; action.Name = serviceName;
action.Description = "Removing Windows service..."; action.Description = "Removing Windows service...";
action.Log = string.Format("- Remove {0} Windows service", serviceName); action.Log = string.Format("- Remove {0} Windows service", serviceName);
list.Add(action); list.Add(action);
} }
//database //database
bool deleteDatabase = AppConfig.GetComponentSettingBooleanValue(componentId, "NewDatabase"); 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 connectionString = AppConfig.GetComponentSettingStringValue(componentId, "InstallConnectionString");
string database = AppConfig.GetComponentSettingStringValue(componentId, "Database"); string database = AppConfig.GetComponentSettingStringValue(componentId, "Database");
@ -4053,7 +4123,6 @@ namespace WebsitePanel.Setup.Internal
action.Log = string.Format("- Delete {0} database login", loginName); action.Log = string.Format("- Delete {0} database login", loginName);
list.Add(action); list.Add(action);
} }
//virtual directory //virtual directory
bool deleteVirtualDirectory = AppConfig.GetComponentSettingBooleanValue(componentId, "NewVirtualDirectory"); bool deleteVirtualDirectory = AppConfig.GetComponentSettingBooleanValue(componentId, "NewVirtualDirectory");
if (deleteVirtualDirectory) if (deleteVirtualDirectory)
@ -4067,7 +4136,6 @@ namespace WebsitePanel.Setup.Internal
action.Log = string.Format("- Delete {0} virtual directory...", virtualDirectory); action.Log = string.Format("- Delete {0} virtual directory...", virtualDirectory);
list.Add(action); list.Add(action);
} }
//web site //web site
bool deleteWebSite = AppConfig.GetComponentSettingBooleanValue(componentId, "NewWebSite"); bool deleteWebSite = AppConfig.GetComponentSettingBooleanValue(componentId, "NewWebSite");
if (deleteWebSite) if (deleteWebSite)
@ -4079,7 +4147,6 @@ namespace WebsitePanel.Setup.Internal
action.Log = string.Format("- Delete {0} web site", siteId); action.Log = string.Format("- Delete {0} web site", siteId);
list.Add(action); list.Add(action);
} }
//application pool //application pool
bool deleteAppPool = AppConfig.GetComponentSettingBooleanValue(componentId, "NewApplicationPool"); bool deleteAppPool = AppConfig.GetComponentSettingBooleanValue(componentId, "NewApplicationPool");
if (deleteAppPool) if (deleteAppPool)
@ -4093,7 +4160,6 @@ namespace WebsitePanel.Setup.Internal
action.Log = string.Format("- Delete {0} application pool", appPoolName); action.Log = string.Format("- Delete {0} application pool", appPoolName);
list.Add(action); list.Add(action);
} }
//user account //user account
bool deleteUserAccount = AppConfig.GetComponentSettingBooleanValue(componentId, "NewUserAccount"); bool deleteUserAccount = AppConfig.GetComponentSettingBooleanValue(componentId, "NewUserAccount");
if (deleteUserAccount) if (deleteUserAccount)
@ -4111,7 +4177,6 @@ namespace WebsitePanel.Setup.Internal
action.Log = string.Format("- Remove {0} user account membership", username); action.Log = string.Format("- Remove {0} user account membership", username);
list.Add(action); list.Add(action);
} }
action = new InstallAction(ActionTypes.DeleteUserAccount); action = new InstallAction(ActionTypes.DeleteUserAccount);
action.Name = username; action.Name = username;
action.Domain = domain; action.Domain = domain;
@ -4253,7 +4318,6 @@ namespace WebsitePanel.Setup.Internal
else if (ModeExtension == ModeExtension.Restore) else if (ModeExtension == ModeExtension.Restore)
{ {
Context.ComponentId = GetComponentID(Context); Context.ComponentId = GetComponentID(Context);
Context.UpdateVersion = Context.Release;
AppConfig.LoadComponentSettings(Context); AppConfig.LoadComponentSettings(Context);
new RestoreScript(Context).Run(); new RestoreScript(Context).Run();
} }
@ -4331,9 +4395,24 @@ namespace WebsitePanel.Setup.Internal
else if (ModeExtension == ModeExtension.Restore) else if (ModeExtension == ModeExtension.Restore)
{ {
Context.ComponentId = GetComponentID(Context); Context.ComponentId = GetComponentID(Context);
Context.UpdateVersion = Context.Release; Context.UseUserCredentials = false;
AppConfig.LoadComponentSettings(Context); 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<string, string[]>();
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 else
throw new NotImplementedException("Install " + ModeExtension.ToString()); throw new NotImplementedException("Install " + ModeExtension.ToString());
@ -4409,7 +4488,6 @@ namespace WebsitePanel.Setup.Internal
else if (ModeExtension == ModeExtension.Restore) else if (ModeExtension == ModeExtension.Restore)
{ {
Context.ComponentId = GetComponentID(Context); Context.ComponentId = GetComponentID(Context);
Context.UpdateVersion = Context.Release;
AppConfig.LoadComponentSettings(Context); AppConfig.LoadComponentSettings(Context);
new RestoreScript(Context).Run(); new RestoreScript(Context).Run();
} }