/[AnywhereTS-MSSQL]/trunk/TSAdminTool/ATSAmdin.Installer.cs
ViewVC logotype

Contents of /trunk/TSAdminTool/ATSAmdin.Installer.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 177 - (show annotations) (download)
Mon Jul 16 12:12:41 2012 UTC (8 years, 9 months ago) by william
File size: 14248 byte(s)
+ use registry search to get DBServer and Instance at install time

1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.ComponentModel;
5 using System.Configuration.Install;
6 using System.Linq;
7 using System.Reflection;
8 using System.Threading;
9 using System.Windows.Forms;
10 using System.Drawing;
11 using AnywhereTS.DBSupport;
12 using System.Data.SqlClient;
13
14
15 namespace AnywhereTS
16 {
17 [RunInstaller(true)]
18 public partial class ATSAmdin : System.Configuration.Install.Installer
19 {
20 string DBServer;
21 string DBInstance;
22 public ATSAmdin()
23 {
24 InitializeComponent();
25 }
26
27 protected override void OnBeforeInstall(IDictionary savedState)
28 {
29 base.OnBeforeInstall(savedState);
30 }
31
32 #region Install Members
33 DatabaseInstallerWaitDialog dlg;
34 public void InstallDatabaseWaitMessage()
35 {
36 dlg = new DatabaseInstallerWaitDialog("", string.Format(@"Please Wait... Installing database: {2} to {0}\{1}", DBServer, DBInstance, ATSGlobals.strDatabaseName));
37 dlg.ForeColor = Color.Black;
38 dlg.ShowDialog();
39 }
40 private void InstallDatabase()
41 {
42 // install the database
43 Thread db_installer_thread = new Thread(new ThreadStart(InstallDatabaseWaitMessage));
44 db_installer_thread.IsBackground = true;
45 db_installer_thread.Start();
46 Logging.ATSAdminInstallerLog.DebugFormat(@"Installing database: {2} to {0}\{1}", DBServer, DBInstance, ATSGlobals.strDatabaseName);
47 AnywhereTS.DatabaseSupport dbsup = new DatabaseSupport();
48 dbsup.SetupDatabase();
49 dlg.Message = string.Format(@"Successfully installed the database: {2} to {0}\{1}", DBServer, DBInstance, ATSGlobals.strDatabaseName);
50 dlg.ForeColor = Color.Green;
51 Thread.Sleep(new TimeSpan(0, 0, 15));
52 dlg.Close();
53 Logging.ATSAdminInstallerLog.DebugFormat(@"Successfully Installed database: {2} to {0}\{1}", DBServer, DBInstance, ATSGlobals.strDatabaseName);
54 }
55
56 private void ConfigureATS()
57 {
58 try
59 {
60 Logging.ATSAdminInstallerLog.Debug("Configuring AnywhereTS");
61 frmAdmin atsadmin = new frmAdmin();
62 if (!atsadmin.ConfigureATS())
63 {
64 throw new Exception("AnywhereTS cannot be installed without configuring it first.");
65 }
66 Logging.ATSAdminInstallerLog.Debug("Successfully Configured AnywhereTS");
67 }
68 catch (Exception ex)
69 {
70 // database install failed
71 using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString())))
72 {
73 Logging.ATSAdminInstallerLog.Error("ConfigureATS() failed.");
74 }
75 throw ex;
76 }
77 }
78
79 public override void Install(IDictionary stateSaver)
80 {
81 base.Install(stateSaver);
82 try
83 {
84
85 string path = this.Context.Parameters["targetdir"];
86 Logging.UpdateLogPath(string.Format(@"{0}\logs", path));
87 using (log4net.NDC.Push("Logged from ATSAdmin.Installer"))
88 {
89 // setup database
90 DBServer = this.Context.Parameters["DBSERVER"];
91 DBInstance = this.Context.Parameters["DBINSTANCE"];
92 try
93 {
94 string server = string.Empty;
95 string instance = string.Empty;
96
97 try { server = ATSGlobals.GetATSRegValueString(ProSupport.strRegDatabaseServer); }
98 catch { }
99 try { instance = ATSGlobals.GetATSRegValueString(ProSupport.strRegDatabaseInstance); }
100 catch { }
101 Logging.ATSAdminInstallerLog.DebugFormat("RegServer={0} RegInstance={1}", server, instance);
102
103 if (!string.IsNullOrEmpty(server))
104 DBServer = server;
105 if (!string.IsNullOrEmpty(instance))
106 DBInstance = instance;
107 }
108 catch
109 {
110 DBServer = this.Context.Parameters["DBSERVER"];
111 DBInstance = this.Context.Parameters["DBINSTANCE"];
112 }
113
114
115 // Log the entered data
116 Logging.ATSAdminInstallerLog.DebugFormat("Server={0} Instance={1}", DBServer, DBInstance);
117 CreateRegistryConfigKeys();
118 ATSGlobals.SetATSRegValue(ProSupport.strRegDatabaseServer, DBServer);
119 ATSGlobals.SetATSRegValue(ProSupport.strRegDatabaseInstance, DBInstance);
120 ProSupport.strDatabaseServer = ATSGlobals.GetATSRegValueString(ProSupport.strRegDatabaseServer);
121 ProSupport.strDatabaseInstance = ATSGlobals.GetATSRegValueString(ProSupport.strRegDatabaseInstance);
122
123 InstallDatabase();
124 ConfigureATS();
125 }
126 }
127 catch (Exception ex)
128 {
129 Logging.ATSAdminInstallerLog.DebugFormat(@"Failed to Install database: {2} to {0}\{1}", DBServer, DBInstance, ATSGlobals.strDatabaseName);
130 dlg.Message = string.Format(@"Failed to install the database: {2} to {0}\{1}", DBServer, DBInstance, ATSGlobals.strDatabaseName);
131 dlg.ForeColor = Color.Red;
132 Thread.Sleep(new TimeSpan(0, 0, 15));
133 dlg.Close();
134 // database install failed
135 using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString())))
136 {
137 Logging.ATSAdminInstallerLog.Error("Install() failed.");
138 }
139 throw ex;
140 }
141 }
142
143 private void CreateRegistryConfigKeys()
144 {
145 try
146 {
147 Microsoft.Win32.RegistryKey key = null;
148 if (IntPtr.Size == 8)
149 { // 64 bit OS
150 //string t = @"SOFTWARE\Wow6432Node\" + ATSGlobals.ApplicationName + @"\ts-config";
151 key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\", true);
152 key.CreateSubKey("TFTPD32").CreateSubKey("DHCP");
153 key = key.CreateSubKey(ATSGlobals.ApplicationName).CreateSubKey("ts-config");
154 }
155 else
156 { // 32 bit OS
157 //strATSregRoot = @"SOFTWARE\" + ATSGlobals.ApplicationName + @"\ts-config";
158 key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software", true);
159 key.CreateSubKey("TFTPD32").CreateSubKey("DHCP");
160 key = key.CreateSubKey(ATSGlobals.ApplicationName).CreateSubKey("ts-config");
161 }
162
163 // AdminVersion = 2
164 key.SetValue("AdminVersion", 2);
165 // RunFirstTime = 1
166 key.SetValue("RunFirstTime", 1);
167 ATSGlobals.CreateRegistryValues();
168 }
169 catch (Exception ex)
170 {
171 using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString())))
172 {
173 Logging.ATSAdminInstallerLog.Error("CreateRegistryConfigKeys() failed.");
174 }
175 }
176 }
177 #endregion
178
179 #region uninstall
180 public override void Uninstall(IDictionary savedState)
181 {
182 using (log4net.NDC.Push("Logged from ATSAdmin.Installer"))
183 {
184 try
185 {
186 string path = this.Context.Parameters["targetdir"];
187 Logging.ATSAdminInstallerLog.DebugFormat("Install Dir: {0}", path);
188 Logging.UpdateLogPath(string.Format(@"{0}\logs", path));
189 Logging.ATSAdminInstallerLog.DebugFormat("Showing uninstall dialog");
190 UninstallDialog udlg = new UninstallDialog();
191 DialogResult result = udlg.ShowDialog();
192 if (result == DialogResult.Cancel)
193 {
194 Logging.ATSAdminInstallerLog.DebugFormat("user aborted uninstall");
195 MessageBox.Show("Uninstall canceled ... nothing was uninstalled", "Uinstall was canceled by user", MessageBoxButtons.OK, MessageBoxIcon.Warning);
196 return;
197 }
198 base.Uninstall(savedState);
199 using (log4net.NDC.Push(string.Format("KeepDatabase={0} KeepApplicationSettings={1} KeepClientImages={2}", udlg.KeepDatabase, udlg.KeepApplicationSettings, udlg.KeepClientImages)))
200 {
201 Logging.ATSAdminInstallerLog.DebugFormat("Unistall options");
202 }
203 string server = ATSGlobals.GetATSRegValueString(ProSupport.strRegDatabaseServer);
204 string instance = ATSGlobals.GetATSRegValueString(ProSupport.strRegDatabaseInstance);
205 string database = ATSGlobals.strDatabaseName;
206 if (!udlg.KeepDatabase)
207 {
208 Exception ErrorInfo = null;
209 // delete database
210 using (MsSqlConnector conn = new MsSqlConnector(server, instance, "master"))
211 {
212 try
213 {
214 conn.CreateConnection(out ErrorInfo);
215 conn.OpenConnection(out ErrorInfo);
216 SqlConnection sqlCon;
217 conn.GetConnectionClone(out sqlCon, out ErrorInfo);
218 SqlCommand sqlCmd = new SqlCommand();
219 sqlCmd.Connection = sqlCon;
220 Logging.ATSAdminInstallerLog.DebugFormat("Removing database: {0}", database);
221 sqlCmd.CommandText = string.Format("DROP DATABASE {0};",database);
222 sqlCmd.ExecuteNonQuery();
223 Logging.ATSAdminInstallerLog.DebugFormat("Successfully removed database: {0}", database);
224 }
225 catch (Exception ex)
226 {
227 Logging.ATSAdminInstallerLog.DebugFormat("Failed to remove database: {0}", database);
228 ErrorInfo = ex;
229 }
230 }
231 if (ErrorInfo != null)
232 throw ErrorInfo;
233 }
234 if (!udlg.KeepApplicationSettings)
235 {
236 // delete registry settings
237 try
238 {
239 Logging.ATSAdminInstallerLog.DebugFormat("Removing AnywhereTS Registry settings");
240 Microsoft.Win32.RegistryKey key = null;
241 if (IntPtr.Size == 8)
242 { // 64 bit OS
243 //string t = @"SOFTWARE\Wow6432Node\" + ATSGlobals.ApplicationName + @"\ts-config";
244 key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\", true);
245 key.DeleteSubKeyTree("TFTPD32", false);
246 key.DeleteSubKeyTree(ATSGlobals.ApplicationName, false);
247 }
248 else
249 { // 32 bit OS
250 //strATSregRoot = @"SOFTWARE\" + ATSGlobals.ApplicationName + @"\ts-config";
251 key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software", true);
252 key.DeleteSubKeyTree("TFTPD32", false);
253 key.DeleteSubKeyTree(ATSGlobals.ApplicationName, false);
254 }
255 Logging.ATSAdminInstallerLog.DebugFormat("Successfully removed AnywhereTS Registry settings");
256 }
257 catch (Exception ex)
258 {
259 Logging.ATSAdminInstallerLog.DebugFormat("Failed to remove AnywhereTS Registry settings");
260 throw ex;
261 }
262 }
263 if (!udlg.KeepClientImages)
264 {
265 // delete client images
266 try
267 {
268 Logging.ATSAdminInstallerLog.Warn("Deleteing Client Images has not been implemented yet.");
269 }
270 catch (Exception ex)
271 {
272 throw ex;
273 }
274 }
275 }
276 catch (Exception ex)
277 {
278 // database install failed
279 using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString())))
280 {
281 Logging.ATSAdminInstallerLog.Error("Install() failed.");
282 }
283 throw ex;
284 }
285 }
286 }
287 #endregion
288 }
289 }

  ViewVC Help
Powered by ViewVC 1.1.22