Ejemplo n.º 1
0
            public void Init()
            {
                ascProcess ascInit = new ascProcess();
                string     dbMessage, backup_status, copy_status, source, error;
                bool       checkFile = false,
                           checkPath = false;

                tmp = Console.Out;
                Console.WriteLine("\nAsctool version 1.0 ");
                Console.WriteLine("Apsmith (c) 2014 \n");
                ascInit.threadWait();
                try
                {
                    if (ascInit.CheckConnection())
                    {
                        try
                        {
                            dbMessage = "Database connection has been established.";
                            Console.WriteLine(dbMessage);
                        }
                        catch (Exception ex)
                        {
                            Console.SetOut(sw);
                            Console.WriteLine(ex.Message);
                            sw.Close();
                        }

                        ascInit.threadWait();

                        ascInit.dbExecute();

                        dbMessage = "SQL query execution complete.";
                        Console.WriteLine("\n");
                        Console.WriteLine(dbMessage);
                        ascInit.threadWait();

                        #region try IIS STOP
                        try
                        {
                            ascInit.ExecuteCommandSync("iisreset /stop");
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("\nAn Error Occured.\nPlease enter destination path for logs: \n");
                            error = Console.ReadLine();

                            #region errorPath
                            while (!checkPath)
                            {
                                if ((File.Exists(error) || Directory.Exists(error)) && checkPath == false)
                                {
                                    fs  = new FileStream(@"" + error + "(" + DateTime.Now.ToString("MM-dd-yy_HHmm") + ")" + ".txt", FileMode.Create);
                                    sw  = new StreamWriter(fs);
                                    tmp = Console.Out;
                                    Console.SetOut(sw);
                                    Console.WriteLine(ex.Message);
                                    sw.Close();
                                    Console.SetOut(tmp);
                                    Console.WriteLine(ex.Message);
                                    break;
                                }
                                Console.WriteLine("{0} Sorry your input is invalid. \n", error);
                                Console.WriteLine("\nAn Error Occured.\nPlease enter destination path for logs: \n");
                                error = Console.ReadLine();
                                continue;
                            }
                            #endregion
                        }
                        #endregion


                        #region while source backUp
                        Console.WriteLine("\nAsctool version 1.0 ");
                        Console.WriteLine("Apsmith (c) 2014 \n");
                        Console.WriteLine("Please enter source path: " + "(e.g. " + @"C:\foldername\.." + ") \n");
                        source = Console.ReadLine();
                        while (!checkFile)
                        {
                            if ((Directory.Exists(source)) && checkFile == false && ascInit.IsDirectoryEmpty(source) == false)
                            {
                                string sourceDirName = @"" + source;
                                Console.WriteLine("\nPlease enter destination path: " + "(e.g. " + @"C:\foldername\.." + ") \n");
                                string dirSource = Console.ReadLine();
                                Console.WriteLine("\n");
                                string destDirName = @"" + dirSource + "(" + File.GetLastWriteTime(sourceDirName).ToString("MM-dd-yy") + ")";
                                if (Directory.Exists(sourceDirName))
                                {
                                    DirectoryInfo sourceInfo = new DirectoryInfo(sourceDirName);
                                    DirectoryInfo destInfo   = new DirectoryInfo(destDirName);
                                    ascInit.BackCopy(sourceInfo, destInfo);
                                    backup_status = "Backup successful.";
                                    Console.WriteLine(backup_status);
                                }
                                else
                                {
                                    backup_status = "File doesn't exists.";
                                    Console.WriteLine(backup_status);
                                }
                                break;
                            }

                            Console.WriteLine("\nAn Error Occured.\nPlease enter destination path for logs: \n");
                            error = Console.ReadLine();

                            #region errorPath
                            while (!checkPath)
                            {
                                if ((File.Exists(error) || Directory.Exists(error)) && checkPath == false)
                                {
                                    fs  = new FileStream(@"" + error + "(" + DateTime.Now.ToString("MM-dd-yy_HHmm") + ")" + ".txt", FileMode.Create);
                                    sw  = new StreamWriter(fs);
                                    tmp = Console.Out;
                                    Console.SetOut(sw);
                                    Console.WriteLine("Sorry {0} is invalid or folder/s empty. \n", source);
                                    sw.Close();
                                    Console.SetOut(tmp);
                                    Console.WriteLine("Sorry {0} is invalid or folder/s empty. \n", source);
                                    Console.WriteLine("Please enter source path: " + "(e.g. " + @"C:\foldername\.." + ") \n");
                                    source = Console.ReadLine();
                                    break;
                                }
                                Console.WriteLine("{0} Sorry your input is invalid. \n", error);
                                Console.WriteLine("\nAn Error Occured.\nPlease enter destination path for logs: \n");
                                error = Console.ReadLine();
                                continue;
                            }
                            #endregion
                            continue;
                        }
                        #endregion

                        ascInit.threadWait();

                        #region while CopyNew
                        Console.WriteLine("\nAsctool version 1.0 ");
                        Console.WriteLine("Apsmith (c) 2014 \n");
                        Console.WriteLine("Please enter source path: " + "(e.g. " + @"C:\foldername\.." + ") \n");
                        source = Console.ReadLine();
                        while (!checkFile)
                        {
                            if ((Directory.Exists(source)) && checkFile == false && ascInit.IsDirectoryEmpty(source) == false)
                            {
                                string sourceDirName2 = @"" + source;
                                Console.WriteLine("\nPlease enter destination path: " + "(e.g. " + @"C:\foldername\.." + ") \n");
                                string dirSource = Console.ReadLine();
                                Console.WriteLine("\n");
                                string destDirName2 = @"" + dirSource;
                                if (Directory.Exists(sourceDirName2))
                                {
                                    DirectoryInfo sourceInfo2 = new DirectoryInfo(sourceDirName2);
                                    DirectoryInfo destInfo2   = new DirectoryInfo(destDirName2);
                                    ascInit.BackCopy(sourceInfo2, destInfo2);
                                    copy_status = "Copy successful.";
                                    Console.WriteLine(copy_status);
                                }
                                else
                                {
                                    copy_status = "File doesn't exists.";
                                    Console.WriteLine(copy_status);
                                }
                                break;
                            }
                            Console.WriteLine("\nAn Error Occured.\nPlease enter destination path for logs: \n");
                            error = Console.ReadLine();

                            #region errorPath
                            while (!checkPath)
                            {
                                if ((File.Exists(error) || Directory.Exists(error)) && checkPath == false)
                                {
                                    fs  = new FileStream(@"" + error + "(" + DateTime.Now.ToString("MM-dd-yy_HHmm") + ")" + ".txt", FileMode.Create);
                                    sw  = new StreamWriter(fs);
                                    tmp = Console.Out;
                                    Console.SetOut(sw);
                                    Console.WriteLine("Sorry {0} is invalid or folder/s empty. \n", source);
                                    sw.Close();
                                    Console.SetOut(tmp);
                                    Console.WriteLine("Sorry {0} is invalid or folder/s empty. \n", source);
                                    Console.WriteLine("Please enter source path: " + "(e.g. " + @"C:\foldername\.." + ") \n");
                                    source = Console.ReadLine();
                                    break;
                                }
                                Console.WriteLine("{0} Sorry your input is invalid. \n", error);
                                Console.WriteLine("\nAn Error Occured.\nPlease enter destination path for logs: \n");
                                error = Console.ReadLine();
                                continue;
                            }
                            #endregion

                            continue;
                        }
                        #endregion

                        ascInit.threadWait();

                        Console.WriteLine("\n");

                        #region try IIS Start
                        try
                        {
                            ascInit.ExecuteCommandSync("iisreset /start");
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("\nAn Error Occured.\nPlease enter destination path for logs: \n");
                            error = Console.ReadLine();

                            #region errorPath
                            while (!checkPath)
                            {
                                if ((File.Exists(error) || Directory.Exists(error)) && checkPath == false)
                                {
                                    fs  = new FileStream(@"" + error + "(" + DateTime.Now.ToString("MM-dd-yy_HHmm") + ")" + ".txt", FileMode.Create);
                                    sw  = new StreamWriter(fs);
                                    tmp = Console.Out;
                                    Console.SetOut(sw);
                                    Console.WriteLine(ex.Message);
                                    sw.Close();
                                    Console.SetOut(tmp);
                                    Console.WriteLine(ex.Message);
                                    break;
                                }
                                Console.WriteLine("{0} Sorry your input is invalid. \n", error);
                                Console.WriteLine("\nAn Error Occured.\nPlease enter destination path for logs: \n");
                                error = Console.ReadLine();
                                continue;
                            }
                            #endregion
                        }
                        #endregion


                        Console.WriteLine("Deployment process complete. " + DateTime.Now.ToString("MM-dd-yy HH:mm"));
                        Environment.Exit(0);
                    }
                    else
                    {
                        dbMessage = "SQL query execution incomplete.";
                        Console.WriteLine(dbMessage);
                    }
                }
                catch (Exception ex)
                {
                    #region errorLogs
                    Console.WriteLine("\nAn Error Occured.\nPlease enter destination path for logs: \n");
                    error = Console.ReadLine();
                    while (!checkPath)
                    {
                        if ((File.Exists(error) || Directory.Exists(error)) && checkPath == false)
                        {
                            fs = new FileStream(@"" + error + "(" + DateTime.Now.ToString("MM-dd-yy_HHmm") + ")" + ".txt", FileMode.Create);
                            sw = new StreamWriter(fs);
                            Console.SetOut(sw);
                            Console.WriteLine(ex.Message);
                            Console.SetOut(tmp);
                            Console.WriteLine(ex.Message);
                            sw.Close();
                            break;
                        }
                        Console.WriteLine("{0} Sorry your input is invalid. \n", error);
                        Console.WriteLine("\nAn Error Occured.\nPlease enter destination path for logs: \n");
                        error = Console.ReadLine();
                        continue;
                    }
                    #endregion
                }
            }
Ejemplo n.º 2
0
        public static void Main(string[] args)
        {
            ascProcess ascProcess = new ascProcess();
            bool       show_help  = false,
                       connectDB  = false,
                       iisreset   = false,
                       iisstart   = false,
                       backup     = false,
                       copy       = false,
                       initialize = false,
                       checkFile  = false,
                       checkPath  = false;
            StreamWriter sw;
            FileStream   fs;
            TextWriter   tmp;
            string       dbMessage, copy_status, backup_status, error;


            #region OptionSet
            OptionSet p = new OptionSet()
            {
                { "d|database", "update database.",
                  v => connectDB = v != null },
                { "k|iis stop", "stop IIS.",
                  v => iisreset = v != null },
                { "s|iis start", "start IIS.",
                  v => iisstart = v != null },
                { "b|backup", "backup website file.",
                  v => backup = v != null },
                { "c|copy", "copy new website file.",
                  v => copy = v != null },
                { "i|start deployment", "deployment process.",
                  v => initialize = v != null },
                { "h|help", "show this message and exit.",
                  v => show_help = v != null }
            };
            #endregion

            #region extra Parse
            List <string> extra = new List <string>();
            try
            {
                if (args == null || args.Length == 0)
                {
                    show_help = true;
                }
                else
                {
                    extra = p.Parse(args);
                    if (extra.Any())
                    {
                        foreach (var item in extra)
                        {
                            Console.WriteLine("\nunrecognized option: {0}", item);
                        }
                        show_help = true;
                    }
                }
            }
            catch (OptionException e)
            {
                //Console.Write("greet: ");
                Console.WriteLine(e.Message);
                //Console.WriteLine("Try `greet --help' for more information.");
                //Console.ReadLine();
            }
            #endregion

            if (show_help)
            {
                //show_help = Console.ReadLine();
                //show_help = true;
                ascProcess.ShowHelp(p);
                //Console.WriteLine("help: {0}", show_help);
            }

            #region name
            //string message;
            //if (extra.Count > 0)
            //{
            //    message = string.Join(" ", extra.ToArray());
            //    //Debug("using new message: {0}", message);
            //}
            //else
            //{
            //    message = "Hello {0}!";
            //    //Debug("using default message: {0}", message);
            //}

            //foreach (string name in names)
            //{
            //    for (int i = 0; i < repeat; ++i)
            //        Console.WriteLine(message, name);
            //}
            #endregion

            #region database
            if (connectDB)
            {
                Console.WriteLine("\nAsctool version 1.0 ");
                Console.WriteLine("Apsmith (c) 2014 \n");
                if (ascProcess.CheckConnection())
                {
                    dbMessage = "\nDatabase connection has been established.\n";
                    Console.WriteLine(dbMessage);
                    ascProcess.dbExecute();
                    dbMessage = "\nSQL query execution complete.";
                    Console.WriteLine(dbMessage);
                    Environment.Exit(0);
                }
            }
            #endregion

            #region iis start or stop
            if (iisreset)
            {
                try
                {
                    Console.WriteLine("\nAsctool version 1.0 ");
                    Console.WriteLine("Apsmith (c) 2014 \n");
                    ascProcess.ExecuteCommandSync("iisreset /stop");
                    Environment.Exit(0);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("\nAn Error Occured.\nPlease enter destination path for logs: \n");
                    error = Console.ReadLine();

                    #region errorPath
                    while (!checkPath)
                    {
                        if ((File.Exists(error) || Directory.Exists(error)) && checkPath == false)
                        {
                            fs  = new FileStream(@"" + error + "(" + DateTime.Now.ToString("MM-dd-yy_HHmm") + ")" + ".txt", FileMode.Create);
                            sw  = new StreamWriter(fs);
                            tmp = Console.Out;
                            Console.SetOut(sw);
                            Console.WriteLine(ex.Message);
                            sw.Close();
                            Console.SetOut(tmp);
                            Console.WriteLine(ex.Message);
                            break;
                        }
                        Console.WriteLine("{0} Sorry your input is invalid. \n", error);
                        Console.WriteLine("\nAn Error Occured.\nPlease enter destination path for logs: \n");
                        error = Console.ReadLine();
                        continue;
                    }
                    #endregion
                }
            }


            if (iisstart)
            {
                try
                {
                    Console.WriteLine("\nAsctool version 1.0 ");
                    Console.WriteLine("Apsmith (c) 2014 \n");
                    ascProcess.ExecuteCommandSync("iisreset /start");
                    Environment.Exit(0);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("\nAn Error Occured.\nPlease enter destination path for logs: \n");
                    error = Console.ReadLine();

                    #region errorPath
                    while (!checkPath)
                    {
                        if ((File.Exists(error) || Directory.Exists(error)) && checkPath == false)
                        {
                            fs  = new FileStream(@"" + error + "(" + DateTime.Now.ToString("MM-dd-yy_HHmm") + ")" + ".txt", FileMode.Create);
                            sw  = new StreamWriter(fs);
                            tmp = Console.Out;
                            Console.SetOut(sw);
                            Console.WriteLine(ex.Message);
                            sw.Close();
                            Console.SetOut(tmp);
                            Console.WriteLine(ex.Message);
                            break;
                        }
                        Console.WriteLine("{0} Sorry your input is invalid. \n", error);
                        Console.WriteLine("\nAn Error Occured.\nPlease enter destination path for logs: \n");
                        error = Console.ReadLine();
                        continue;
                    }
                    #endregion
                }
            }
            #endregion

            #region backup

            if (backup)
            {
                Console.WriteLine("\nAsctool version 1.0 ");
                Console.WriteLine("Apsmith (c) 2014 \n");
                Console.WriteLine("Please enter source path: " + "(e.g. " + @"C:\foldername\.." + ") \n");
                string source = Console.ReadLine();
                while (!checkFile)
                {
                    if ((Directory.Exists(source)) && checkFile == false && ascProcess.IsDirectoryEmpty(source) == false)
                    {
                        string sourceDirName = @"" + source;
                        Console.WriteLine("\nPlease enter destination path: " + "(e.g. " + @"C:\foldername\.." + ") \n");
                        string dirSource = Console.ReadLine();
                        Console.WriteLine("\n");
                        string destDirName = @"" + dirSource + "(" + File.GetLastWriteTime(sourceDirName).ToString("MM-dd-yy") + ")";
                        if (Directory.Exists(sourceDirName))
                        {
                            DirectoryInfo sourceInfo = new DirectoryInfo(sourceDirName);
                            DirectoryInfo destInfo   = new DirectoryInfo(destDirName);
                            ascProcess.BackCopy(sourceInfo, destInfo);
                            backup_status = "Backup successful.";
                            Console.WriteLine(backup_status);
                            Environment.Exit(0);
                        }
                        else
                        {
                            backup_status = "File doesn't exists.";
                            Console.WriteLine(backup_status);
                        }
                        break;
                    }

                    Console.WriteLine("\nAn Error Occured.\nPlease enter destination path for logs: \n");
                    error = Console.ReadLine();

                    #region errorPath
                    while (!checkPath)
                    {
                        if ((File.Exists(error) || Directory.Exists(error)) && checkPath == false)
                        {
                            fs  = new FileStream(@"" + error + "(" + DateTime.Now.ToString("MM-dd-yy_HHmm") + ")" + ".txt", FileMode.Create);
                            sw  = new StreamWriter(fs);
                            tmp = Console.Out;
                            Console.SetOut(sw);
                            Console.WriteLine("Sorry {0} is invalid or folder/s empty. \n", source);
                            sw.Close();
                            Console.SetOut(tmp);
                            Console.WriteLine("Sorry {0} is invalid or folder/s empty. \n", source);
                            Console.WriteLine("Please enter source path: " + "(e.g. " + @"C:\foldername\.." + ") \n");
                            source = Console.ReadLine();
                            break;
                        }
                        Console.WriteLine("{0} Sorry your input is invalid. \n", error);
                        Console.WriteLine("\nAn Error Occured.\nPlease enter destination path for logs: \n");
                        error = Console.ReadLine();
                        continue;
                    }
                    #endregion
                    continue;
                }
            }
            #endregion

            #region copy

            if (copy)
            {
                Console.WriteLine("\nAsctool version 1.0 ");
                Console.WriteLine("Apsmith (c) 2014 \n");
                Console.WriteLine("Please enter source path: " + "(e.g. " + @"C:\foldername\.." + ") \n");
                string source = Console.ReadLine();
                while (!checkFile)
                {
                    if ((Directory.Exists(source)) && checkFile == false && ascProcess.IsDirectoryEmpty(source) == false)
                    {
                        string sourceDirName2 = @"" + source;
                        Console.WriteLine("\nPlease enter destination path: " + "(e.g. " + @"C:\foldername\.." + ") \n");
                        string dirSource = Console.ReadLine();
                        Console.WriteLine("\n");
                        string destDirName2 = @"" + dirSource;
                        if (Directory.Exists(sourceDirName2))
                        {
                            DirectoryInfo sourceInfo2 = new DirectoryInfo(sourceDirName2);
                            DirectoryInfo destInfo2   = new DirectoryInfo(destDirName2);
                            ascProcess.BackCopy(sourceInfo2, destInfo2);
                            copy_status = "Copy successful.";
                            Console.WriteLine(copy_status);
                            Environment.Exit(0);
                        }
                        else
                        {
                            copy_status = "File doesn't exists.";
                            Console.WriteLine(copy_status);
                        }
                        break;
                    }
                    Console.WriteLine("\nAn Error Occured.\nPlease enter destination path for logs: \n");
                    error = Console.ReadLine();

                    #region errorPath
                    while (!checkPath)
                    {
                        if ((File.Exists(error) || Directory.Exists(error)) && checkPath == false)
                        {
                            fs  = new FileStream(@"" + error + "(" + DateTime.Now.ToString("MM-dd-yy_HHmm") + ")" + ".txt", FileMode.Create);
                            sw  = new StreamWriter(fs);
                            tmp = Console.Out;
                            Console.SetOut(sw);
                            Console.WriteLine("Sorry {0} is invalid or folder/s empty. \n", source);
                            sw.Close();
                            Console.SetOut(tmp);
                            Console.WriteLine("Sorry {0} is invalid or folder/s empty. \n", source);
                            Console.WriteLine("Please enter source path: " + "(e.g. " + @"C:\foldername\.." + ") \n");
                            source = Console.ReadLine();
                            break;
                        }
                        Console.WriteLine("{0} Sorry your input is invalid. \n", error);
                        Console.WriteLine("\nAn Error Occured.\nPlease enter destination path for logs: \n");
                        error = Console.ReadLine();
                        continue;
                    }
                    #endregion
                    continue;
                }
            }
            #endregion

            #region initialize
            if (initialize)
            {
                ascProcess.Init();
            }
            Console.Read();
            #endregion
        }