Ejemplo n.º 1
0
        /// <summary>
        /// Responsible for All The Initial Tasks in the Application
        /// </summary>
        public void Initiate()
        {
            try
            {
                string ReplaceFile_Enabled      = _readAppConfigData.ReadValueByKey("ReplaceFile_Enabled");
                string ReplaceFolder_Enabled    = _readAppConfigData.ReadValueByKey("ReplaceFolder_Enabled");
                string SendNotification_Enabled = _readAppConfigData.ReadValueByKey("SendNotification_Enabled");

                if (ReplaceFile_Enabled != null && Convert.ToBoolean(ReplaceFile_Enabled))
                {
                    Console.WriteLine("IISStopped: {0}", _helper.IISResetStop() ? "Yes" : "No");
                    IsFileReplaced = _replaceFile.Initiate();
                    Console.WriteLine("IISRunning: {0}", _helper.IISResetStart() ? "Yes" : "No");
                }
                if (ReplaceFolder_Enabled != null && Convert.ToBoolean(ReplaceFolder_Enabled))
                {
                    IsFolderReplaced = _replaceFolder.Initiate();
                }
                if (SendNotification_Enabled != null && Convert.ToBoolean(SendNotification_Enabled) && IsFileReplaced && IsFolderReplaced)
                {
                    IsMailSent = _mailNotification.Initiate();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("***************************** ERROR *****************************");
                Console.WriteLine("SOME ERROR OCCURRED!!");
                Console.WriteLine(ex.ToString());
                Console.WriteLine("***************************** ERROR *****************************");
            }
            finally
            {
                Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
                Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
                Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
                Console.WriteLine(string.Format("\n IsFileReplaced: {0} \n IsFolderReplaced: {1} \n IsMailSent: {0}", IsFileReplaced ? "Yes" : "No", IsFolderReplaced ? "Yes" : "No", IsMailSent ? "Yes" : "No"));
                Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
                Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
                Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");

                Console.WriteLine("=================================================================");
                Console.WriteLine("TASK COMPLETED!!");
                Console.WriteLine("================================================================= \n");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initiate ES Manipulation
        /// </summary>
        public bool Initiate()
        {
            string            ESServiceName = _readAppConfigData.ReadValueByKey("ESServiceName");
            ServiceController sc            = new ServiceController(ESServiceName);

            Console.WriteLine("=================================================================");
            Console.WriteLine("The ES service status is currently set to {0}", sc.Status.ToString());
            Console.WriteLine("================================================================= \n");

            bool isESStopped = _helper.StopESService(sc);

            Console.WriteLine("ESStopped: {0}", isESStopped ? "Yes" : "No");

            if (isESStopped)
            {
                IsOperationSucceed = ReplaceElasticSearch();
            }

            bool isESRunning = _helper.StartESService(sc);

            Console.WriteLine("ESRunning: {0}", isESRunning ? "Yes" : "No");
            return(IsOperationSucceed);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Replace The Web.config File
        /// </summary>
        public bool ReplaceWebConfig()
        {
            Regex reg = new Regex("[*'\",/:_&#^@]");

            #region Read Cofiguration Values and Validate

            string SourceFilePath       = _readAppConfigData.ReadValueByKey("SourceFilePath");
            string TargetFilePath       = _readAppConfigData.ReadValueByKey("TargetFilePath");
            string DateTimeCustomFormat = _readAppConfigData.ReadValueByKey("DateTimeCustomFormat") != null?_readAppConfigData.ReadValueByKey("DateTimeCustomFormat") : DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss");

            string BackUpFilePathDirectory = _readAppConfigData.ReadValueByKey("BackUpFilePathDirectory") != null?_readAppConfigData.ReadValueByKey("BackUpFilePathDirectory") + @"\" + reg.Replace(DateTime.Now.ToString(DateTimeCustomFormat), " ") : null;

            string FileNameToReplace  = _readAppConfigData.ReadValueByKey("FileNameToReplace");
            string FileIsReplacedFlag = _readAppConfigData.ReadValueByKey("FileIsReplacedFlag");

            bool isValidationSucess = InitialValidations(SourceFilePath, TargetFilePath, FileIsReplacedFlag, BackUpFilePathDirectory, FileNameToReplace);
            if (!isValidationSucess)
            {
                return(false);
            }
            else
            {
                Console.WriteLine("=================================================================");
                Console.WriteLine("Initial Validation for Web.Config is Successful.");
                Console.WriteLine("================================================================= \n");
            }

            #endregion

            Console.WriteLine("=================================================================");
            Console.WriteLine("WebConfig Replace and Backup Operation Started.");
            Console.WriteLine("================================================================= \n");

            try
            {
                // Ensure that the target does exist.
                if (File.Exists(TargetFilePath))
                {
                    Console.WriteLine("=================================================================");
                    Console.WriteLine("The deployment is completed. The webconfig file exists. Hence, Backup/Replacement Operation initiated.");
                    Console.WriteLine("================================================================= \n");

                    #region Backup the existing WebConfig
                    Console.WriteLine("=================================================================");
                    Console.WriteLine("Backup started");
                    Console.WriteLine("================================================================= \n");

                    // Determine whether the backup directory exists.
                    if (Directory.Exists(BackUpFilePathDirectory))
                    {
                        Console.WriteLine("-- The backup directory exists. No need to Craete one.");
                    }
                    else
                    {
                        Console.WriteLine("-- That backup directory does not exist. Create one new backup directory.");

                        DirectoryInfo di = Directory.CreateDirectory(BackUpFilePathDirectory);
                        Console.WriteLine("-- The backup directory was created successfully at {0}.", Directory.GetCreationTime(BackUpFilePathDirectory));
                    }

                    string backupSourcePath = TargetFilePath;
                    string backupTargetPath = string.Concat(BackUpFilePathDirectory, string.Concat(@"\" + FileNameToReplace));

                    // Check if the Web Config backup target file exist, Delete it
                    if (File.Exists(backupTargetPath))
                    {
                        Console.WriteLine("-- The backup target file already exists, so deleting it.");
                        File.Delete(backupTargetPath);
                    }
                    File.Copy(backupSourcePath, backupTargetPath);
                    Console.WriteLine("-- The backup is made under path: " + backupTargetPath + "\n");

                    Console.WriteLine("=================================================================");
                    Console.WriteLine("Backup completed.");
                    Console.WriteLine("================================================================= \n");
                    #endregion

                    #region Replace WebConfig
                    Console.WriteLine("=================================================================");
                    Console.WriteLine("Replecement started.");
                    Console.WriteLine("================================================================= \n");
                    File.Delete(TargetFilePath);
                    // Copy the file from source to target.
                    File.Copy(SourceFilePath, TargetFilePath);
                    Console.WriteLine("{0} was moved to {1}.", SourceFilePath, TargetFilePath);
                    Console.WriteLine("Webconfig Replacement is Completed.");

                    // See if the webconfig exists now.
                    if (File.Exists(TargetFilePath))
                    {
                        Console.WriteLine("Replaced WebConfig Exists. (after replace For confirmation) \n");
                        Console.WriteLine("=================================================================");
                        Console.WriteLine("Replecement completed.");
                        Console.WriteLine("================================================================= \n");
                    }
                    else
                    {
                        Console.WriteLine("***************************** ERROR *****************************");
                        Console.WriteLine("WebConfig does not exist. Something Wrong. ERROR Alert!!");
                        Console.WriteLine("***************************** ERROR *****************************");
                    }

                    #endregion

                    #region Create FileIsReplacedFlag

                    if (!File.Exists(FileIsReplacedFlag))
                    {
                        // This statement ensures that the webConfigIsReplaced file is created,
                        // but the handle is not kept.
                        using (FileStream fs = File.Create(FileIsReplacedFlag)) { }
                    }
                    #endregion

                    Console.WriteLine("=================================================================");
                    Console.WriteLine("WebConfig Replace and Backup Operation completed.");
                    Console.WriteLine("================================================================= \n");
                }
                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine("***************************** ERROR *****************************");
                Console.WriteLine("The Replace WebConfig process failed: {0}", e.ToString());
                Console.WriteLine("***************************** ERROR *****************************");
                return(false);
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Constructor of the class
 /// </summary>
 public Helper()
 {
     _readAppConfigData = new ReadAppConfigData();
     _scW3SVC           = new ServiceController(_readAppConfigData.ReadValueByKey("W3SVCServiceName"));
 }
        /// <summary>
        /// Send Email
        /// </summary>
        public bool SendMail()
        {
            #region Read SMTP Cofiguration Values

            string EmailServer     = _readAppConfigData.ReadValueByKey("EmailServer");
            string EmailServerPort = _readAppConfigData.ReadValueByKey("EmailServerPort");
            string EmailUserName   = _readAppConfigData.ReadValueByKey("EmailUserName");
            string EmailPassword   = _readAppConfigData.ReadValueByKey("EmailPassword");
            string EmailTo         = _readAppConfigData.ReadValueByKey("EmailTo");
            string EmailCC         = _readAppConfigData.ReadValueByKey("EmailCC");
            string EmailSubject    = _readAppConfigData.ReadValueByKey("EmailSubject");
            string EmailBody       = _readAppConfigData.ReadValueByKey("EmailBody");
            string EmailSignature  = _readAppConfigData.ReadValueByKey("EmailSignature");

            #endregion

            #region Instatiate SMTP Client

            SmtpClient smtp = new SmtpClient
            {
                Host                  = EmailServer,
                Port                  = Convert.ToInt32(EmailServerPort),
                EnableSsl             = true,
                DeliveryMethod        = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = true,
                Credentials           = new NetworkCredential(EmailUserName, EmailPassword)
            };

            //using (MailMessage mail = new MailMessage(new MailAddress(EmailUserName, EmailUserName), new MailAddress(EmailTo, EmailTo))
            //{
            //    Subject = EmailSubject + " On " + DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm:ss"),
            //    Body = "<b>" + EmailSubject + " </b> " + "<br> You can do chill now.<b></b>.",
            //    BodyEncoding = System.Text.Encoding.UTF8,
            //    IsBodyHtml = true
            //})

            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(EmailUserName, EmailUserName);
            foreach (var to in EmailTo.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
            {
                mail.To.Add(to);
            }
            mail.Subject      = EmailSubject + " On " + DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm:ss");
            mail.Body         = "<b>" + EmailSubject + " </b> " + "<br>" + EmailBody + "<br><br><br><br>With Regards,<br>" + EmailSignature + "<b></b>";
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            mail.IsBodyHtml   = true;

            #endregion

            try
            {
                Console.WriteLine("=================================================================");
                Console.WriteLine("Sending Mail Started.");
                Console.WriteLine("================================================================= \n");

                smtp.Send(mail);

                Console.WriteLine("=================================================================");
                Console.WriteLine("Mail is Sent.");
                Console.WriteLine("================================================================= \n");
                return(true);
            }
            catch (SmtpException ex)
            {
                Console.WriteLine("***************************** ERROR *****************************");
                throw new ApplicationException("SmtpException has occured: " + ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("***************************** ERROR *****************************");
                Console.WriteLine("Exception caught in SendMail {0}", ex.ToString());
                Console.WriteLine("***************************** ERROR *****************************");
                return(true);
            }
        }