コード例 #1
0
 private void ConfigManager_Load(object sender, EventArgs e)
 {
     _workInProgress.Caption = "Connecting to database";
     _workInProgress.Text    = "Now connecting, please wait...";
     _workInProgress.Run();
     try
     {
         try
         {
             tsslSQLServer.Text = Properties.Settings.Default.SQLServerName + " : " + Properties.Settings.Default.DatabaseName;
             UpdateConfigTargetList();
             UpdateConfigSetList();
             UpdateTargetGroupFilterList();
         }
         catch (System.Data.SqlClient.SqlException Ex)
         {
             _workInProgress.Cancel();
             if (MessageBox.Show(string.Format("An unexpected database error occurred : {0} \r\n Do you want to reconfigure SQL connection parameters ? ", Ex.Message), "Database connection error", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
             {
                 (new SqlConnectionEditor(_workInProgress)).ShowDialog();
             }
         }
         catch (Exception Ex)
         {
             _workInProgress.Cancel();
             MessageBox.Show(string.Format("An unexpected database error occurred : {0}", Ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
         this.Text += string.Format(" - V{0}", Assembly.GetExecutingAssembly().GetName().Version.ToString());
     }
     finally
     {
         _workInProgress.Cancel();
     }
 }
コード例 #2
0
        private void SqlConnectionEditor_FormClosed(object sender, FormClosedEventArgs e)
        {
            Settings curSettings = Settings.Default;

            if (DialogResult == System.Windows.Forms.DialogResult.OK)
            {
                _animation.Run();
                try
                {
                    if (curSettings.SQLServerNames == null)
                    {
                        curSettings.SQLServerNames = new System.Collections.Specialized.StringCollection();
                    }
                    if (!curSettings.SQLServerNames.Contains(cbxSQLServerName.Text))
                    {
                        curSettings.SQLServerNames.Add(cbxSQLServerName.Text);
                    }
                    curSettings.SQLServerName = cbxSQLServerName.Text;
                    curSettings.Save();
                    Hide();
                    DatabaseManager.RegisterDatabase(curSettings.SQLServerName, curSettings.DatabaseName, curSettings.SQLIntegratedSecurity, curSettings.SQLServerUsername, curSettings.SQLServerPassword, _animation);
                }
                finally
                {
                    _animation?.Cancel();
                }
            }
        }
コード例 #3
0
        private void SqlConnectionEditor_FormClosed(object sender, FormClosedEventArgs e)
        {
            Settings curSettings = Settings.Default;

            if (DialogResult == System.Windows.Forms.DialogResult.OK)
            {
                _animation.Run();
                try
                {
                    if (curSettings.SQLServerNames == null)
                    {
                        curSettings.SQLServerNames = new System.Collections.Specialized.StringCollection();
                    }
                    if (!curSettings.SQLServerNames.Contains(cbxSQLServerName.Text))
                    {
                        curSettings.SQLServerNames.Add(cbxSQLServerName.Text);
                    }
                    curSettings.SQLServerName = cbxSQLServerName.Text;
                    curSettings.Save();
                    Hide();
                    DatabaseManager.RegisterDatabase(curSettings.SQLServerName, curSettings.DatabaseName, curSettings.SQLIntegratedSecurity, curSettings.SQLServerUsername, curSettings.SQLServerPassword, _animation);
                }
                finally
                {
                    _animation?.Cancel();
                }
                MessageBox.Show("Application needs to be restarted for the updated configuration to take effect.", "Restart required", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
コード例 #4
0
 private void tsmConfigManager_Click(object sender, EventArgs e)
 {
     _workInProgress.Caption = "Please wait";
     _workInProgress.Text    = "Loading module";
     _workInProgress.Run();
     try
     {
         ConfigManager CM = new ConfigManager();
         CM.Show();
         CM.MdiParent = AppMainForm;
     }
     finally
     {
         _workInProgress.Cancel();
     }
 }
コード例 #5
0
        /// <summary>
        /// Checks database and creates it if missing and also creates required DB objects
        /// </summary>
        /// <param name="ServerName">The SQL Server name to connect to</param>
        /// <param name="DatabaseName">The name of the database to check or create</param>
        /// <param name="intSecurity">If integrated security to be used for connection</param>
        /// <param name="sqlUserName">SQL Username for connection</param>
        /// <param name="sqlPassword">SQL Password for connection</param>
        /// <param name="animation">A Backgoundworker to stop before displaying any dialog boxes. Optional</param>
        /// <returns></returns>
        public static bool RegisterDatabase(string ServerName, string DatabaseName, bool intSecurity, string sqlUserName, string sqlPassword, WorkInProgress animation = null)
        {
            bool runResult        = true;
            bool DBExists         = false;
            bool DBCreated        = false;
            bool DBObjectsCreated = false;

            try
            {
                #region Create Database if not exists
                string MasterConnectionString = intSecurity ? string.Format("Data Source={0};Initial Catalog=master;Integrated Security=True", ServerName) :
                                                string.Format("Data Source={0};Initial Catalog=master;Persist Security Info=True;User ID={1};Password={2}", ServerName, sqlUserName, sqlPassword);
                DebugEx.WriteLine(string.Format("Connecting to master db using : {0}", MasterConnectionString), DebugLevel.Informational);
                using (SqlConnection conn = new SqlConnection(MasterConnectionString))
                {
                    try
                    {
                        conn.Open();
                        #region Test database existence
                        try
                        {
                            DebugEx.WriteLine(string.Format("Checking database : {0}", DatabaseName), DebugLevel.Informational);
                            SqlCommand scmd = conn.CreateCommand();
                            scmd.CommandText = string.Format("SELECT name FROM master.sys.databases WHERE name = N'{0}'", DatabaseName);
                            object o = new object();
                            o        = scmd.ExecuteScalar();
                            DBExists = o != null;
                            if (!DBExists)
                            {
                                DebugEx.WriteLine("database doesn't exist", DebugLevel.Informational);
                                animation?.Cancel();
                                runResult = MessageBox.Show(string.Format("Database {0} does not exists. Do you want to create it now?", DatabaseName), "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes;
                                if (runResult)
                                {
                                    animation?.Run();
                                }
                            }
                            else
                            {
                                DebugEx.WriteLine("database exists", DebugLevel.Informational);
                            }
                        }
                        catch (SqlException Ex)
                        {
                            DebugEx.WriteLine("Unexpected error 1");
                            animation?.Cancel();
                            MessageBox.Show(string.Format("Unexpected error while checking database {0} : {1} ", DatabaseName, Ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            runResult = false;
                        }
                        #endregion
                        if (runResult & !DBExists)
                        {
                            #region Create database
                            try
                            {
                                DebugEx.WriteLine(string.Format("Creating database : {0}", DatabaseName), DebugLevel.Informational);
                                SqlCommand scmd = conn.CreateCommand();
                                scmd.CommandText = "CREATE DATABASE " + DatabaseName;
                                scmd.ExecuteNonQuery();
                                scmd.CommandText = string.Format("ALTER DATABASE {0} SET AUTO_SHRINK ON", DatabaseName);
                                scmd.ExecuteNonQuery();
                                DBCreated = true;
                                DebugEx.WriteLine("database created successfully", DebugLevel.Informational);
                            }
                            catch (SqlException Ex)
                            {
                                if (((SqlException)Ex).Number == 1801)
                                {
                                    DebugEx.WriteLine("database already exists", DebugLevel.Informational);
                                }
                                else
                                {
                                    DebugEx.WriteLine("Unexpected error 2");
                                    animation?.Cancel();
                                    MessageBox.Show(string.Format("Unexpected error while creating database {0} : {1} ", DatabaseName, Ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    runResult = false;
                                }
                            }
                            #endregion
                        }
                    }
                    catch (SqlException Ex)
                    {
                        DebugEx.WriteLine("Unexpected error 3");
                        animation?.Cancel();
                        MessageBox.Show(string.Format("Unexpected error while opening connection : {0} ", Ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        runResult = false;
                    }
                }
                #endregion

                if (runResult)
                {
                    #region Connect to database and create database objects if missing

                    string InstallerConnectionString = intSecurity ? string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True", ServerName, DatabaseName) :
                                                       string.Format("Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={1};Password={2}", ServerName, DatabaseName, sqlUserName, sqlPassword);
                    DebugEx.WriteLine(string.Format("Connecting using : {0}", InstallerConnectionString), DebugLevel.Informational);
                    using (SqlConnection conn = new SqlConnection(InstallerConnectionString))
                    {
                        conn.Open();
                        SqlCommand scmd;
                        try
                        {
                            try
                            {
                                #region Check if require objects exist
                                DebugEx.WriteLine("Checking database objects", DebugLevel.Informational);
                                bool RequiredObjectsExist = false;
                                // This is to try whether required database objects exist or not
                                scmd = new SqlCommand("SELECT COUNT (*) FROM CONFIGTARGETS", conn);
                                try
                                {
                                    object o = new object();
                                    o = scmd.ExecuteScalar();
                                    RequiredObjectsExist = true;
                                }
                                catch (SqlException Ex)
                                {
                                    // Error number 208 is normal, meaning that there is no Params table yet
                                    if (((SqlException)Ex).Number == 208)
                                    {
                                        DebugEx.WriteLine("Error 208 : there is no CONFIGTARGETS table yet");
                                    }
                                    else
                                    {
                                        // other exceptions are unexpected and unhandled errors
                                        animation?.Cancel();
                                        MessageBox.Show(string.Format("Unexpected error while creating database {0} : {1} ", DatabaseName, Ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                        runResult = false;
                                        throw Ex;
                                    }
                                }
                                #endregion

                                #region Run DDL script if required objects do not exist
                                if (!RequiredObjectsExist)
                                {
                                    // this is a new empty database
                                    DebugEx.WriteLine("this is a new empty database, running DDL script", DebugLevel.Informational);
                                    try
                                    {
                                        string SqlScript = Resource1.CreateConfigManagerObjects_20151007_v1;
                                        SqlScript = SqlScript.Replace("GO", "^");
                                        string[] SqlCommands = SqlScript.Split("^".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                                        foreach (string command in SqlCommands)
                                        {
                                            DebugEx.WriteLine(scmd.CommandText);
                                            scmd = new SqlCommand(command, conn);
                                            scmd.ExecuteNonQuery();
                                        }
                                        DBObjectsCreated = true;
                                        DebugEx.WriteLine("DB objects created sucessfully", DebugLevel.Informational);
                                    }
                                    catch (Exception Ex)
                                    {
                                        animation?.Cancel();
                                        MessageBox.Show(string.Format("Unexpected error while creating database objects : {0} ", Ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                        runResult = false;
                                    }
                                }
                                #endregion
                            }
                            finally
                            {
                                conn.Close();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Sorry, I am unable to register database. Please view debug logs for more details.", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            runResult = false;
                        }
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                animation?.Cancel();
                MessageBox.Show("Sorry, I am unable to register database. Please view debug logs for more details. Error : " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                runResult = false;
            }
            if (DBCreated | DBObjectsCreated)
            {
                animation?.Cancel();
                string s = DBCreated ? "Database created sucessfully." : "";
                if (DBObjectsCreated)
                {
                    s += " Required database objects created sucessfully.";
                }
                MessageBox.Show(s, "SQL Operation success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            return(runResult);
        }
コード例 #6
0
 private void PullConfig()
 {
     _workInProgressCaption = "Please wait...";
     _workInProgressText    = "Generating script...";
     _workInProgress.Run();
     DisableControls();
     try
     {
         if (cbCreateNewSet.Checked)
         {
             DateTime d = DateTime.Now;
             object   o = qTA.CloneConfigSet(_configurationSetID, string.Format("{0}_{1}{2}{3}{4}{5}{6}", tbSelConfigSetName.Text, d.Year, d.Month, d.Day, d.Hour, d.Minute, d.Second));
             _configurationSetID = Convert.ToInt32(o);
         }
         SNGDataSet.ScriptSettingRow _scriptSettings = Scriptngo.Common.SettingsManager.GetCurrentScriptSettings(this.SNGDataSet);
         string sepChar         = _scriptSettings.CSVSeparator;
         string sExtendedHeader = string.Join(sepChar, Enum.GetNames(typeof(InputFileHeader)));
         sExtendedHeader += "SetTargetID";
         Scriptngo.SNGScriptManager _ScriptManager = ScriptingFormManager.OpenNewScriptingForm();
         if (_ScriptManager != null)
         {
             ScriptManagers.Add(_ScriptManager);
             _ScriptManager.UpdateHeader(sExtendedHeader.Split(sepChar.ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
             _ScriptManager.OnScriptAborted  += _ScriptManager_OnScriptAborted;
             _ScriptManager.OnScriptFinished += _ScriptManager_OnScriptFinished;
             string thisLine = string.Empty;
             _ScriptManager.BeginAddingEntries();
             try
             {
                 int lastConfigTargetID = -1;
                 int targetCount        = clbTargets.CheckedItems.Count;
                 int targetNum          = 1;
                 foreach (var target in configDS.SetTargets)
                 {
                     if (IsTargetSelected(target.ConfigTargetID))
                     {
                         if (lastConfigTargetID == -1)
                         {
                             lastConfigTargetID = target.ConfigTargetID;
                         }
                         // Open a new ScriptinForm if script needs to be generated per configuration target
                         if (target.ConfigTargetID != lastConfigTargetID && cbScriptPerHost.Checked && (targetNum % (numHostPerForm.Value + 1) == 0))
                         {
                             if (_ScriptManager != null)
                             {
                                 _ScriptManager.EndAddingEntries();
                                 _ScriptManager.ExecuteScript(null, false);
                             }
                             _ScriptManager = ScriptingFormManager.OpenNewScriptingForm();
                             if (_ScriptManager == null)
                             {
                                 break;
                             }
                             _ScriptManager.OnScriptAborted  += _ScriptManager_OnScriptAborted;
                             _ScriptManager.OnScriptFinished += _ScriptManager_OnScriptFinished;
                             ScriptManagers.Add(_ScriptManager);
                             _ScriptManager.UpdateHeader(sExtendedHeader.Split(sepChar.ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
                             _ScriptManager.BeginAddingEntries();
                             lastConfigTargetID = target.ConfigTargetID;
                         }
                         int configSetTargetID = cbCreateNewSet.Checked ? (int)qTA.GetConfigSetTargetID(_configurationSetID, target.ConfigTargetID) : target.ConfigSetTargetID;
                         thisLine = "1," + string.Concat(target.IsJumpServerIPNull() ? "" : target.JumpServerIP.Trim()) + "," + target.DeviceVendor + "," + target.TargetIP + string.Concat(target.IsPortNull() ? "," : string.Format(":{0},", target.Port)) + "" + "," + target.Protocol + "," + "sh run" + ",yes,,,"
                                    + ",,," + configSetTargetID;
                         if (!string.IsNullOrEmpty(thisLine))
                         {
                             bool added = _ScriptManager.AddEntry(thisLine, false);
                             if (!added)
                             {
                                 if (MessageBox.Show("There was an error adding a script line. Do you want to continue ?", "Script generation error", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == System.Windows.Forms.DialogResult.No)
                                 {
                                     break;
                                 }
                             }
                         }
                         targetNum++;
                     }
                 }
             }
             finally
             {
                 if (_ScriptManager != null)
                 {
                     _ScriptManager.EndAddingEntries();
                     _ScriptManager.ExecuteScript(null, false);
                 }
             }
         }
     }
     finally
     {
         _workInProgress.Cancel();
         _workInProgressSupportCancellation = false;
         btnPullConfig.Enabled       = ScriptManagers.Count == 0;
         lblOperationInProgress.Text = "Pulling configuration...";
         pbPullProgress.Value        = 0;
         pbPullProgress.Maximum      = ScriptManagers.Count;
         pbPullProgress.Visible      = ScriptManagers.Count > 1;
         btnCancelPull.Visible       = !btnPullConfig.Enabled;
     }
 }
コード例 #7
0
 private void Deploy()
 {
     _workInProgress.Caption = "Please wait...";
     _workInProgress.Text    = "Generating script...";
     _workInProgress.Run();
     try
     {
         SNGDataSet.ScriptSettingRow _scriptSettings = Scriptngo.Common.SettingsManager.GetCurrentScriptSettings(this.SNGDataSet);
         string sepChar         = _scriptSettings.CSVSeparator;
         string sExtendedHeader = string.Join(sepChar, Enum.GetNames(typeof(InputFileHeader)));
         sExtendedHeader += "ConfigLineID";
         Scriptngo.SNGScriptManager _ScriptManager = ScriptingFormManager.OpenNewScriptingForm();
         if (_ScriptManager != null)
         {
             string thisLine = string.Empty;
             _ScriptManager.SetSavePrompt(false);
             _ScriptManager.BeginAddingEntries();
             try
             {
                 int lastConfigTargetID = (configDS.Deploy.Rows[0] as ConfigDS.DeployRow).ConfigTargetID;
                 int targetCount        = clbTargets.CheckedItems.Count;
                 int targetNum          = 0;
                 foreach (var configLine in configDS.Deploy)
                 {
                     if (IsTargetSelected(configLine.ConfigTargetID))
                     {
                         if (configLine.ConfigTargetID != lastConfigTargetID)
                         {
                             targetNum++;
                         }
                         // Open a new ScriptinForm if script needs to be generated per configuration target
                         if (configLine.ConfigTargetID != lastConfigTargetID && cbScriptPerHost.Checked && (targetNum > 0 && targetNum % numHostPerForm.Value == 0))
                         {
                             #region Open a new Scripting Form
                             if (_ScriptManager != null)
                             {
                                 _ScriptManager.EndAddingEntries();
                                 if (cbStartScripts.Checked)
                                 {
                                     _ScriptManager.ExecuteScript(null, false);
                                 }
                             }
                             _ScriptManager = ScriptingFormManager.OpenNewScriptingForm();
                             if (_ScriptManager == null)
                             {
                                 break;
                             }
                             _ScriptManager.SetSavePrompt(false);
                             _ScriptManager.BeginAddingEntries();
                             lastConfigTargetID = configLine.ConfigTargetID;
                             #endregion
                         }
                         thisLine = "1," + string.Concat(configLine.IsJumpServerIPNull() ? "" : configLine.JumpServerIP.Trim()) + "," + configLine.DeviceVendor + "," + configLine.TargetIP + string.Concat(configLine.IsPortNull() ? "," : string.Format(":{0},", configLine.Port)) + "" + "," + configLine.Protocol + "," + configLine.ConfigLine + ",yes,,,"
                                    + ",,,," + configLine.ConfigLineID;
                         if (!string.IsNullOrEmpty(thisLine))
                         {
                             bool added = _ScriptManager.AddEntry(thisLine, false);
                             if (!added)
                             {
                                 if (MessageBox.Show("There was an error adding a script line. Do you want to continue ?", "Script generation error", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == System.Windows.Forms.DialogResult.No)
                                 {
                                     break;
                                 }
                             }
                         }
                     }
                     if (configLine.ConfigTargetID != lastConfigTargetID)
                     {
                         lastConfigTargetID = configLine.ConfigTargetID;
                     }
                 }
             }
             finally
             {
                 if (_ScriptManager != null)
                 {
                     _ScriptManager.EndAddingEntries();
                     if (cbStartScripts.Checked)
                     {
                         _ScriptManager.ExecuteScript(null, false);
                     }
                 }
             }
         }
     }
     finally
     {
         _workInProgress.Cancel();
         _workInProgressSupportCancellation = false;
         DialogResult = DialogResult.OK;
     }
 }