/// <summary> /// Updates XML configuration based on the installation result. /// </summary> /// <param name="type">The installation type.</param> public void UpdateXmlConfig(InstallType type) { if (type == InstallType.Api) { return; } List<string> configFiles = this.FindXmlConfigFiles(); XmlUpdater updater = new XmlUpdater(); Dictionary<string, string> xmlNamespaces = new Dictionary<string, string> { { Constants.Unity_XmlNamespacePrefix, Constants.Unity_XmlNamespaceUri } }; List<XmlReplacement> replacements = new List<XmlReplacement>(); List<XmlCreate> creates = new List<XmlCreate>(); List<XmlRemoval> removals = new List<XmlRemoval>(); if (!string.IsNullOrEmpty(this.installVariables.OrgId)) { // Update service accounts foreach (KeyValuePair<User, string> kvp in this.installVariables.ServiceAccounts) { replacements.Create(string.Format(Constants.XPath_ServiceUserFormat, kvp.Key.DisplayName), Constants.XmlAttr_Value, currentValue => kvp.Key.Username); replacements.Create(string.Format(Constants.XPath_ServicePasswordFormat, kvp.Key.DisplayName), Constants.XmlAttr_Value, currentValue => kvp.Value); } } // Update service endpoints if (!string.IsNullOrEmpty(this.installOptions.WebsiteName)) { Regex regex = new Regex(@"(?<=http://).*(?=/iSolutions)", RegexOptions.IgnoreCase); var replacementSiteName = this.installOptions.RootSite != null ? string.Format("{0}/{1}", this.installOptions.HostName, this.installOptions.WebsiteName) : this.installOptions.WebsiteName; replacements.Create(Constants.XPath_Endpoint, Constants.XmlAttr_Address, currentValue => regex.Replace(currentValue, replacementSiteName)); replacements.Create(Constants.XPath_EntLibSource, Constants.XmlAttr_Source, currentValue => this.installOptions.EnterpriseLibraryLoggingSource); } // Update SMTP if (!string.IsNullOrEmpty(this.installOptions.SmtpHost)) { if (string.IsNullOrEmpty(this.installOptions.SmtpUsername)) { removals.Add(new XmlRemoval { NodeXPath = Constants.XPath_SmtpCredsDependency }); replacements.Create(Constants.XPath_SmtpCredsUsername, Constants.XmlAttr_Value, currentValue => "Username"); replacements.Create(Constants.XPath_SmtpCredsPassword, Constants.XmlAttr_Value, currentValue => "Password"); replacements.Create(Constants.XPath_SmtpHost, Constants.XmlAttr_Value, currentValue => this.installOptions.SmtpHost); } else { creates.Add(this.CreateNode(Constants.XPath_SmtpCredsDependency)); replacements.Create(Constants.XPath_SmtpCredsDependency, Constants.XmlAttr_DependencyName, currentValue => Constants.XmlVal_SmtpCreds); replacements.Create(Constants.XPath_SmtpCredsUsername, Constants.XmlAttr_Value, currentValue => this.installOptions.SmtpUsername); replacements.Create(Constants.XPath_SmtpCredsPassword, Constants.XmlAttr_Value, currentValue => this.installOptions.SmtpPassword); replacements.Create(Constants.XPath_SmtpHost, Constants.XmlAttr_Value, currentValue => this.installOptions.SmtpHost); } } updater.Update(configFiles, xmlNamespaces, replacements, creates, removals); }
/// <summary> /// Installs the windows scheduled tasks. /// </summary> public void InstallScheduledTasks() { using (TaskService taskService = new TaskService()) { TaskFolder taskFolder = taskService.RootFolder.SubFolders.FirstOrDefault(f => f.Name == FOLDER_NAME) ?? taskService.RootFolder.CreateFolder(FOLDER_NAME); string taskXml = AssemblyResourceReader.ReadAsString(TEMPLATE_FILE); Dictionary<string, string> namespaces = new Dictionary<string, string> { { Constants.Task_XmlNamespacePrefix, Constants.Task_XmlNamespaceUri } }; List<XmlReplacement> replacements = new List<XmlReplacement>(); string startBoundaryVal = DateTime.Now.ToString("s"); replacements.Create(Constants.XPath_StartBoundary, null, currentValue => startBoundaryVal); XmlReplacement commandReplacement = replacements.Create(Constants.XPath_Command, null, null); string json = AssemblyResourceReader.ReadAsString(TASK_JSON_FILE) .Replace("{websiteName}", HttpUtility.JavaScriptStringEncode(this.installOptions.WebsiteName)); XmlUpdater updater = new XmlUpdater(); JArray tasks = JArray.Parse(json); foreach (JToken task in tasks) { string taskName = task.Value<string>(TASK_NAME_JSON_KEY); this.installLogger.Log(string.Format(Messages.TASK_CreateScheduledTask, taskName)); XmlDocument doc = new XmlDocument(); doc.LoadXml(taskXml); string executablePath = Path.Combine(this.installVariables.BasePath, task.Value<string>(COMMAND_PATH_JSON_KEY)); commandReplacement.ReplacementValue = currentValue => executablePath; updater.Update(doc, namespaces, replacements); doc.Save(this.tempFile); TaskDefinition taskDefinition = taskService.NewTaskFromFile(this.tempFile); float interval = task.Value<float>(INTERVAL_MINUTES_JSON_KEY); this.AddSchedule(taskDefinition, interval); taskFolder.RegisterTaskDefinition(taskName, taskDefinition, TaskCreation.CreateOrUpdate, @"NT AUTHORITY\SYSTEM", null, TaskLogonType.ServiceAccount); this.AddFolderPermission(Directory.GetParent(executablePath).FullName); this.installLogger.LogSuccess(Messages.MAIN_StepComplete); } File.Delete(this.tempFile); } }
/// <summary> /// Update the endpoint addresses found in <paramref name="files"/>. /// </summary> /// <param name="files">The absolute file paths.</param> private static void UpdateEndpointAddresses(List<string> files) { XmlUpdater updater = new XmlUpdater(); Dictionary<string, string> xmlNamespaces = new Dictionary<string, string>(); List<XmlReplacement> replacements = new List<XmlReplacement>(); Regex regex = new Regex("(?<=http://)[^/]*", RegexOptions.IgnoreCase); replacements.Create(@"./configuration/system.serviceModel/client/endpoint", "address", currentValue => regex.Replace(currentValue, CurrentSiteAddress)); //// TODO: What to do with this? ////replacements.Create(Constants.XPath_EntLibSource, Constants.XmlAttr_Source, (currentValue) => this.installOptions.EnterpriseLibraryLoggingSource); TraceHelper.TraceInformation(string.Format("Updating the following files: {0}", string.Join("\t" + Environment.NewLine + ";", files))); updater.Update(files, xmlNamespaces, replacements); }