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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/TSAdminTool/ATSAmdin.Installer.cs	2012/07/15 08:34:18	145
+++ trunk/TSAdminTool/ATSAmdin.Installer.cs	2012/07/15 15:18:55	157
@@ -4,6 +4,12 @@
 using System.ComponentModel;
 using System.Configuration.Install;
 using System.Linq;
+using System.Reflection;
+using System.Threading;
+using System.Windows.Forms;
+using System.Drawing;
+using AnywhereTS.DBSupport;
+using System.Data.SqlClient;
 
 
 namespace AnywhereTS
@@ -11,9 +17,240 @@
     [RunInstaller(true)]
     public partial class ATSAmdin : System.Configuration.Install.Installer
     {
+        string DBServer;
+        string DBInstance;
         public ATSAmdin()
         {
             InitializeComponent();
         }
+        #region Install Members
+        DatabaseInstallerWaitDialog dlg;
+        public void InstallDatabaseWaitMessage()
+        {
+            dlg = new DatabaseInstallerWaitDialog("", string.Format(@"Please Wait... Installing database: {2} to {0}\{1}", DBServer, DBInstance, ATSGlobals.strDatabaseName));
+            dlg.ForeColor = Color.Black;
+            dlg.ShowDialog();            
+        }        
+        private void InstallDatabase()
+        {
+            // install the database 
+            Thread db_installer_thread = new Thread(new ThreadStart(InstallDatabaseWaitMessage));
+            db_installer_thread.IsBackground = true;
+            db_installer_thread.Start();
+            Logging.ATSAdminInstallerLog.DebugFormat(@"Installing database: {2} to {0}\{1}", DBServer, DBInstance, ATSGlobals.strDatabaseName);
+            AnywhereTS.DatabaseSupport dbsup = new DatabaseSupport();
+            dbsup.SetupDatabase();
+            dlg.Message = string.Format(@"Successfully installed the database: {2} to {0}\{1}", DBServer, DBInstance, ATSGlobals.strDatabaseName);
+            dlg.ForeColor = Color.Green;
+            Thread.Sleep(new TimeSpan(0, 0, 15));
+            dlg.Close();
+            Logging.ATSAdminInstallerLog.DebugFormat(@"Successfully Installed database: {2} to {0}\{1}", DBServer, DBInstance, ATSGlobals.strDatabaseName);
+        }
+
+        private void ConfigureATS()
+        {
+            try
+            {
+                Logging.ATSAdminInstallerLog.Debug("Configuring AnywhereTS");
+                frmAdmin atsadmin = new frmAdmin();
+                if (!atsadmin.ConfigureATS())
+                {
+                    throw new Exception("AnywhereTS cannot be installed without configuring it first.");
+                }
+                Logging.ATSAdminInstallerLog.Debug("Successfully Configured AnywhereTS");
+            }
+            catch (Exception ex)
+            {
+                // database install failed
+                using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString())))
+                {
+                    Logging.ATSAdminInstallerLog.Error("ConfigureATS() failed.");
+                }
+                throw ex;
+            }
+        }
+
+        public override void Install(IDictionary stateSaver)
+        {
+            base.Install(stateSaver);
+            try
+            {
+                
+                string path = this.Context.Parameters["targetdir"];
+                Logging.UpdateLogPath(string.Format(@"{0}\logs", path));
+                using (log4net.NDC.Push("Logged from ATSAdmin.Installer"))
+                {
+                    // setup database
+                    DBServer = this.Context.Parameters["DBSERVER"];
+                    DBInstance = this.Context.Parameters["DBINSTANCE"];
+                    // Log the entered data
+                    Logging.ATSAdminInstallerLog.DebugFormat("Server={0} Instance={1}", DBServer, DBInstance);
+                    CreateRegistryConfigKeys();
+                    ATSGlobals.SetATSRegValue(ProSupport.strRegDatabaseServer, DBServer);
+                    ATSGlobals.SetATSRegValue(ProSupport.strRegDatabaseInstance, DBInstance);
+                    ProSupport.strDatabaseServer = ATSGlobals.GetATSRegValueString(ProSupport.strRegDatabaseServer);
+                    ProSupport.strDatabaseInstance = ATSGlobals.GetATSRegValueString(ProSupport.strRegDatabaseInstance);
+
+                    InstallDatabase();
+                    ConfigureATS();
+                }
+            }
+            catch (Exception ex)
+            {
+                Logging.ATSAdminInstallerLog.DebugFormat(@"Failed to Install database: {2} to {0}\{1}", DBServer, DBInstance, ATSGlobals.strDatabaseName);
+                dlg.Message = string.Format(@"Failed to install the database: {2} to {0}\{1}", DBServer, DBInstance, ATSGlobals.strDatabaseName);
+                dlg.ForeColor = Color.Red;
+                Thread.Sleep(new TimeSpan(0, 0, 15));
+                dlg.Close();
+                // database install failed
+                using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString())))
+                {
+                    Logging.ATSAdminInstallerLog.Error("Install() failed.");
+                }
+                throw ex;
+            }
+        }
+
+        private void CreateRegistryConfigKeys()
+        {
+            try
+            {
+                Microsoft.Win32.RegistryKey key = null;
+                if (IntPtr.Size == 8)
+                {   // 64 bit OS
+                    //string t = @"SOFTWARE\Wow6432Node\" + ATSGlobals.ApplicationName + @"\ts-config";
+                    key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\", true);
+                    key.CreateSubKey("TFTPD32").CreateSubKey("DHCP");
+                    key = key.CreateSubKey(ATSGlobals.ApplicationName).CreateSubKey("ts-config");
+                }
+                else
+                {   // 32 bit OS
+                    //strATSregRoot = @"SOFTWARE\" + ATSGlobals.ApplicationName + @"\ts-config";
+                    key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software", true);
+                    key.CreateSubKey("TFTPD32").CreateSubKey("DHCP");
+                    key = key.CreateSubKey(ATSGlobals.ApplicationName).CreateSubKey("ts-config");
+                }
+
+                // AdminVersion = 2
+                key.SetValue("AdminVersion", 2);
+                // RunFirstTime = 1
+                key.SetValue("RunFirstTime", 1);
+                ATSGlobals.CreateRegistryValues();
+            }
+            catch (Exception ex)
+            {
+                using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString())))
+                {
+                    Logging.ATSAdminInstallerLog.Error("CreateRegistryConfigKeys() failed.");
+                }
+            }
+        }
+        #endregion
+
+        public override void Uninstall(IDictionary savedState)
+        {
+            using (log4net.NDC.Push("Logged from ATSAdmin.Installer"))
+            {
+                try
+                {
+                    string path = this.Context.Parameters["targetdir"];
+                    Logging.ATSAdminInstallerLog.DebugFormat("Install Dir: {0}", path);
+                    Logging.UpdateLogPath(string.Format(@"{0}\logs", path));
+                    Logging.ATSAdminInstallerLog.DebugFormat("Showing uninstall dialog");
+                    UninstallDialog udlg = new UninstallDialog();
+                    DialogResult result = udlg.ShowDialog();
+                    if (result == DialogResult.Cancel)
+                    {
+                        Logging.ATSAdminInstallerLog.DebugFormat("user aborted uninstall");
+                        MessageBox.Show("Uninstall canceled ... nothing was uninstalled", "Uinstall was canceled by user", MessageBoxButtons.OK, MessageBoxIcon.Warning);
+                        return;
+                    }
+                    base.Uninstall(savedState);
+                    using (log4net.NDC.Push(string.Format("KeepDatabase={0} KeepApplicationSettings={1} KeepClientImages={2}", udlg.KeepDatabase, udlg.KeepApplicationSettings, udlg.KeepClientImages)))
+                    {
+                        Logging.ATSAdminInstallerLog.DebugFormat("Unistall options");
+                    }
+                    string server = ATSGlobals.GetATSRegValueString(ProSupport.strRegDatabaseServer);
+                    string instance = ATSGlobals.GetATSRegValueString(ProSupport.strRegDatabaseInstance);
+                    string database = ATSGlobals.strDatabaseName;
+                    if (!udlg.KeepDatabase)
+                    {
+                        Exception ErrorInfo = null;
+                        // delete database
+                        using (MsSqlConnector conn = new MsSqlConnector(server, instance, "master"))
+                        {
+                            try
+                            {
+                                conn.CreateConnection(out ErrorInfo);
+                                conn.OpenConnection(out ErrorInfo);
+                                SqlConnection sqlCon;
+                                conn.GetConnectionClone(out sqlCon, out ErrorInfo);
+                                SqlCommand sqlCmd = new SqlCommand();
+                                sqlCmd.Connection = sqlCon;
+                                Logging.ATSAdminInstallerLog.DebugFormat("Removing database: {0}", database);
+                                sqlCmd.CommandText = string.Format("DROP DATABASE {0};",database);
+                                sqlCmd.ExecuteNonQuery();
+                                Logging.ATSAdminInstallerLog.DebugFormat("Successfully removed database: {0}", database);
+                            }
+                            catch (Exception ex)
+                            {
+                                Logging.ATSAdminInstallerLog.DebugFormat("Failed to remove database: {0}", database);
+                                ErrorInfo = ex;
+                            }
+                        }
+                        if (ErrorInfo != null)
+                            throw ErrorInfo;
+                    }
+                    if (!udlg.KeepApplicationSettings)
+                    {
+                        // delete registry settings
+                        try
+                        {
+                            Logging.ATSAdminInstallerLog.DebugFormat("Removing AnywhereTS Registry settings");
+                            Microsoft.Win32.RegistryKey key = null;
+                            if (IntPtr.Size == 8)
+                            {   // 64 bit OS
+                                //string t = @"SOFTWARE\Wow6432Node\" + ATSGlobals.ApplicationName + @"\ts-config";
+                                key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\", true);
+                                key.DeleteSubKey("TFTPD32", false);
+                                key.DeleteSubKey(ATSGlobals.ApplicationName, false);
+                            }
+                            else
+                            {   // 32 bit OS
+                                //strATSregRoot = @"SOFTWARE\" + ATSGlobals.ApplicationName + @"\ts-config";
+                                key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software", true);
+                                key.DeleteSubKey("TFTPD32", false);
+                                key.DeleteSubKey(ATSGlobals.ApplicationName, false);
+                            }
+                            Logging.ATSAdminInstallerLog.DebugFormat("Successfully removed AnywhereTS Registry settings");
+                        }
+                        catch (Exception ex)
+                        {
+                            Logging.ATSAdminInstallerLog.DebugFormat("Failed to remove AnywhereTS Registry settings");
+                            throw ex;
+                        }
+                    }
+                    if (!udlg.KeepClientImages)
+                    {
+                        // delete client images
+                        try
+                        {
+                        }
+                        catch (Exception ex)
+                        {
+                        }
+                    }
+                }
+                catch (Exception ex)
+                {
+                    // database install failed
+                    using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString())))
+                    {
+                        Logging.ATSAdminInstallerLog.Error("Install() failed.");
+                    }
+                    throw ex;
+                }
+            }
+        }
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22