From 0ad4c805bb1ed638c0323b31deff5dd5871d92db Mon Sep 17 00:00:00 2001 From: "ada.sescu" Date: Tue, 12 Aug 2014 13:17:50 +0000 Subject: [PATCH] Add mssql registry cleanup at app stop --- .../src/control/Program.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/cartridges/openshift-origin-cartridge-mssql/src/control/Program.cs b/cartridges/openshift-origin-cartridge-mssql/src/control/Program.cs index cf01e07..4624312 100644 --- a/cartridges/openshift-origin-cartridge-mssql/src/control/Program.cs +++ b/cartridges/openshift-origin-cartridge-mssql/src/control/Program.cs @@ -164,6 +164,8 @@ private static void StopCartridge() int processId = int.Parse(File.ReadAllText(pidFile)); Process.Start("taskkill", string.Format("/F /T /PID {0}", processId)).WaitForExit(); File.Delete(pidFile); + + RemoveRegistry(); } else { @@ -251,5 +253,22 @@ private static void RunProcess(string processFile, string arguments, string exce throw new Exception(string.Format("{0}: {1}", exception, process.StandardError.ReadToEnd())); } } + + private static void RemoveRegistry() + { + string currentDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); + string registryFile = Path.Combine(currentDir, "sqlserver.reg"); + + string content = File.ReadAllText(registryFile, System.Text.Encoding.ASCII).Replace("[HKEY_LOCAL_MACHINE", "[-HKEY_LOCAL_MACHINE"); + + string instanceName = string.Format("Instance{0}", Environment.GetEnvironmentVariable("OPENSHIFT_MSSQL_DB_PORT")); + string sss = string.Format("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SQL Server\\{0}.{1}]{2}@=\"{1}\"", instanceType, instanceName, Environment.NewLine); + content = content.Replace(string.Format("-{0}", sss), sss); + + File.WriteAllText(registryFile, content, System.Text.Encoding.ASCII); + + //import registry file with "-" before each key to remove keys + RunProcess(@"cmd.exe", "/C reg import " + registryFile + " /reg:64", "Error while importing registry file"); + } } }