/[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 160 - (show annotations) (download)
Sun Jul 15 15:35:36 2012 UTC (7 years, 7 months ago) by william
File size: 13010 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22