using System; using System.IO; using Microsoft.Deployment.WindowsInstaller; using System.ServiceProcess; namespace RedisMsi.CustomActions { /// /// Defines actions to take during the MSI install that don't /// come standard with WiX. /// public class CustomActions { /// /// Overwrites settings in the Redis config file using values from the installer. /// /// The install session context /// Returns Success when the method completes. Exceptions will bubble up and /// cause the installer to roll back. [CustomAction] public static ActionResult UpdateRedisConfig(Session session) { string configFilePath = session.CustomActionData["CONFIG_PATH"]; if (File.Exists(configFilePath)) { string originalContent = File.ReadAllText(configFilePath); string port = session.CustomActionData["PORT"]; string updatedContent = originalContent.Replace("port 6379", "port " + port); string useMaxMemoryLimit = session.CustomActionData["USE_MEMORY_LIMITS"]; if (useMaxMemoryLimit == "1") { string strMaxMemoryMB = session.CustomActionData["MAXMEMORY"]; try { int intMaxMemory = Int32.Parse(strMaxMemoryMB); if (intMaxMemory <= 0) { strMaxMemoryMB = "100"; } } catch { strMaxMemoryMB = "100"; } updatedContent = updatedContent.Replace("# maxmemory ", "maxmemory " + strMaxMemoryMB + "mb"); } File.WriteAllText(configFilePath, updatedContent); } else { throw new ApplicationException("UpdateRedisConfig: Config file not found. Could not update its settings."); } return ActionResult.Success; } /// /// Sets a WiX property to indicate whether the Windows Firewall service is stopped. /// If the firewall service is stopped, the install will not succeed if it attempts /// to add a firewall exception. Note that just setting the state of the /// firewall to off does not pose a problem. /// /// /// [CustomAction] public static ActionResult CheckIfFirewallServiceRunning(Session session) { ServiceController sc = new ServiceController("MpsSvc"); // Windows Firewall service bool isStopped = sc.Status.Equals(ServiceControllerStatus.Stopped) || sc.Status.Equals(ServiceControllerStatus.StopPending); if (isStopped) { session["FIREWALL_SERVICE_STOPPED"] = "1"; } return ActionResult.Success; } } }