예제 #1
0
        public void Relocate(CustomTraceLog log, List <string> allAlowedProjects, List <string> allowedSvnRootUrlsToRelocate, ref string batchContent, bool executeImmediately)
        {
            using (log.LogScope("Processing " + this.folder + "..."))
            {
                if (!allAlowedProjects.Exists(line => line.StartsWith(this.folder.ToLower() + " | ")))
                {
                    log.AddLine("project skipped.", false);
                }
                else if (!allowedSvnRootUrlsToRelocate.Exists(url => url == this.svnRootUrl))
                {
                    log.AddLine("svn root url " + this.svnRootUrl + " skipped.", false);
                }
                else
                {
                    using (log.LogScope(executeImmediately? "Relocating ":"Adding script for " + this.svnUrl + " ==> " + this.svnNewUrl + " ..."))
                    {
                        if (executeImmediately)
                        {
                            int exitCode = BuildingBlocks.UI.Console.ExecuteCommand(svnExePath, "relocate " + this.svnNewUrl, 10000, this.folder, log);
                            if (exitCode != 0)
                            {
                                log.AddLine("[Failed.]");
                            }
                        }

                        batchContent += "\r\nCD \"" + this.folder + "\"";
                        batchContent += "\r\n\"" + this.svnExePath + "\" relocate " + this.svnNewUrl;
                        batchContent += "\r\n";
                    }
                }
            }
        }
예제 #2
0
        public FormMain(CustomTraceLog log)
        {
            this._appLog = log;
            InitializeComponent();

            EnableEvents();
            this.tbLog.Text = log.ToString();
            RefreshControls(true, true, false, true, true, true);
        }
예제 #3
0
        private void btnReloacteAll_Click(object sender, EventArgs e)
        {
            this.tbOutput.Clear();
            if (!File.Exists(this.tbSvnExePath.Text))
            {
                MessageBox.Show("You must select existing svn.exe under step 0.");
            }
            else if (this.tbNewSvnRootUrl.Text.IsNullOrWhiteSpace())
            {
                MessageBox.Show("You must specify new svn root url under step 5.");
            }
            else
            {
                this._allItems.ForEach(item => item.svnExePath    = this.tbSvnExePath.Text);
                this._allItems.ForEach(item => item.newSvnRootUrl = this.tbNewSvnRootUrl.Text);
            }


            var           log = new CustomTraceLog("Relocating to " + this.tbNewSvnRootUrl + " ...", true, false, (traceLog, line, newLine, level, ending, forNewLine) => { this.tbOutput.AppendText(line + "\r\n"); this.tbOutput.ScrollToCaret(); });
            List <string> allAllowedProjects = this.tbAllProjects.Text.ToLower().ToLines(false, false, new List <string>());
            List <string> svnRootUrlsFound   = this.tbSvnRootUrlsFound.Text.ToLower().ToLines(false, false, new List <string>());

            string batchContent = string.Empty;

            foreach (var item in this._allItems)
            {
                item.Relocate(log, allAllowedProjects, svnRootUrlsFound, ref batchContent, this.cbExecuteImmediately.Checked);
            }

            if (this.cbWriteToBatFile.Checked)
            {
                string batchFilePath = BuildingBlocks.Common.Misc.ApplicationPhysicalExeFilePathWithoutExtension + ".bat";
                using (log.LogScope("Writing to " + batchFilePath + "..."))
                {
                    if (File.Exists(batchFilePath))
                    {
                        using (log.LogScope("Deleting old file ..."))
                        {
                            File.Delete(batchFilePath);
                        }
                    }
                    File.WriteAllText(batchFilePath, batchContent);
                }
            }

            if (log.ToString().Contains("[Failed.]"))
            {
                log.AddLine("DONE WITH ERROR(S).");
            }
            else
            {
                log.AddLine("DONE WITH SUCCESS.");
            }
        }
예제 #4
0
 public static void LogException(Exception e, CustomTraceLog log)
 {
     //if (Settings.Current.logErrors)
     //{
     //CraftSynth.BuildingBlocks.Logging.Misc.AddTimestampedExceptionInfoToApplicationWideLog(e);
     //}
     string[] lines = CraftSynth.BuildingBlocks.Logging.Misc.GetExceptionDescription(e, true, false).Split('\n');
     foreach (string line in lines)
     {
         log.AddLine(line.Trim('\r'));
     }
 }
예제 #5
0
        public static void SendReportIfNecessary(bool allSuccess, CustomTraceLog log)
        {
            string iniFilePath = CraftSynth.BuildingBlocks.Common.Misc.ApplicationPhysicalExeFilePathWithoutExtension + ".ini";

            bool   SendMailOnSuccess    = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <bool>("SendMailOnSuccess", iniFilePath, true, false, true, false, false, false);
            string MailSubjectOnSuccess = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <string>("MailSubjectOnSuccess", iniFilePath, SendMailOnSuccess, null, SendMailOnSuccess, null, false, null);
            bool   SendMailOnError      = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <bool>("SendMailOnError", iniFilePath, true, false, true, false, false, false);
            string MailSubjectOnError   = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <string>("MailSubjectOnError", iniFilePath, SendMailOnSuccess, null, SendMailOnSuccess, null, false, null);

            bool   SmtpIgnoreCertErrors = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <bool>("SmtpIgnoreCertErrors", iniFilePath, false, false, true, false, false, false);
            bool   SmtpViaOffice365     = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <bool>("SmtpViaOffice365", iniFilePath, true, false, true, false, false, false);
            string SmtpAddress          = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <string>("SmtpAddress", iniFilePath, true, null, true, null, false, null);
            int    SmtpPort             = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <int>("SmtpPort", iniFilePath, false, 25, true, 25, false, -1);
            string SmtpFrom             = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <string>("SmtpFrom", iniFilePath, true, null, true, null, false, null);
            string SmtpToCsv            = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <string>("SmtpToCsv", iniFilePath, true, null, true, null, false, null);
            string SmtpUsername         = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <string>("SmtpUsername", iniFilePath, true, null, true, null, false, null);
            string SmtpPassword         = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <string>("SmtpPassword", iniFilePath, true, null, true, null, false, null);

            MailMessage m = new MailMessage();

            m.From = new MailAddress(SmtpFrom);
            List <string> smtpToList = SmtpToCsv.ParseCSV();

            foreach (string s in smtpToList)
            {
                m.To.Add(new MailAddress(s));
            }
            m.Body       = log.ToString();
            m.IsBodyHtml = false;

            if (SmtpIgnoreCertErrors)
            {
                ServicePointManager.ServerCertificateValidationCallback =
                    delegate(object s, X509Certificate certificate,
                             X509Chain chain, SslPolicyErrors sslPolicyErrors)
                { return(true); };
            }

            if (allSuccess && SendMailOnSuccess)
            {
                log.AddLine("Sending mails...");
                m.Subject = MailSubjectOnSuccess;
                CraftSynth.BuildingBlocks.IO.EMail.SendMail(SmtpViaOffice365, SmtpAddress, SmtpPort, SmtpUsername, SmtpPassword, m);
                log.AddLine("Sending mails done.");
            }
            else if (!allSuccess && SendMailOnError)
            {
                log.AddLine("Sending mails...");
                m.Subject = MailSubjectOnError;
                CraftSynth.BuildingBlocks.IO.EMail.SendMail(SmtpViaOffice365, SmtpAddress, SmtpPort, SmtpUsername, SmtpPassword, m);
                log.AddLine("Sending mails done.");
            }
        }
예제 #6
0
        public static ClipboardData Get(IntPtr foregroundWindow)
        {
            ClipboardData dataFromScreen;

            using (WheelOfHistoryController.DisableClipsGrabbingInThisBlock())
            {
                CustomTraceLog log = new CustomTraceLog("Grabb...", false, false, CustomTraceLogAddLinePostProcessingEvent);

                var originalClipboardData = CraftSynth.Win32Clipboard.ClipboardProxy.GrabClipboardDataStripped(false);

                Application.DoEvents();
                CraftSynth.Win32Clipboard.ClipboardProxy.ClearClipboard(log);
                Application.DoEvents();

                if (IntPtr.Zero != foregroundWindow)
                {
                    CraftSynth.BuildingBlocks.WindowsNT.Misc.SetForegroundWindow(foregroundWindow);
                    log.AddLine("foregr ok" + foregroundWindow.ToString());
                }
                else
                {
                    log.AddLine("forgr empty");
                }

                try
                {
                    SendKeys.SendWait("^c");
                }
                catch (Exception ex)
                {
                    Logging.AddExceptionLog(ex);
                    SendKeys.SendWait("^c");
                }
                Application.DoEvents();

                dataFromScreen = CraftSynth.Win32Clipboard.ClipboardProxy.GrabClipboardData(false, log);
                Application.DoEvents();
                Logging.AddActionLog("grab:" + dataFromScreen == null ? "null" : dataFromScreen.Formats.Count.ToString());
                //if (dataFromScreen != null && dataFromScreen.Formats.Count == 0)
                //{
                //	Thread.Sleep(500);
                //	 dataFromScreen = CraftSynth.Win32Clipboard.ClipboardProxy.GrabClipboardData(false, log);
                //	Logging.AddActionLog("grab2:"+dataFromScreen==null?"null":dataFromScreen.Formats.Count.ToString());
                //}
                CraftSynth.Win32Clipboard.ClipboardProxy.RestoreClipboardData(originalClipboardData, false);

                Application.DoEvents();
            }

            return(dataFromScreen);
        }
예제 #7
0
        static int Main()
        {
            int r = 0;
            //string logFilePath = CraftSynth.BuildingBlocks.Common.Misc.ApplicationPhysicalExeFilePathWithoutExtension + ".log";
            string         logFileContent = string.Empty;    //File.ReadAllText(logFilePath)
            CustomTraceLog log            = new CustomTraceLog(logFileContent, true, false, CustomTraceLogAddLinePostProcessingEvent, CustomTraceLogAddLinePreProcessingEvent);

            log.AddLine("--------------------------------------------------------------------------------------------");
            //try
            //{
            log.AddLineAndIncreaseIdent("Starting...");

            //AppDomain.CurrentDomain.UnhandledException += delegate(object sender, UnhandledExceptionEventArgs args1)
            //											  {
            //												  var ex = (Exception) args1.ExceptionObject;
            //												  AzCopyBatch.HandlerForLoging.LogException(ex, log);
            //												  //Environment.Exit(1);
            //												  throw ex;
            //											  };


            List <Process> sameNameProcesses = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).ToList();

            //sameNameProcesses.AddRange(Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName+".vshost").ToList());
            if (sameNameProcesses.Count() > 1)
            {
                log.AddLine("Another instance is allready running. Closing this one...");
            }
            else
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                _formMain = new FormMain(log);

                Application.Run(_formMain);

                log.AddLine("Done...");

                log.DecreaseIdent();
            }
            //}
            //catch (Exception exception)
            //{
            //	HandlerForLoging.LogException(exception, log);
            //	r = -1;
            //	throw;
            //}

            return(r);
        }
예제 #8
0
 private static void CustomTraceLogAddLinePostProcessingEvent(CustomTraceLog sender, string line, bool inNewLine, int level, string lineVersionSuitableForLineEnding, string lineVersionSuitableForNewLine)
 {
     HandlerForLoging.LogAction(line, inNewLine);
     if (_formMain != null && _formMain.Visible)
     {
         if (inNewLine)
         {
             _formMain.AppendTextToTbLog(line);
         }
         else
         {
             _formMain.AppendTextToTbLog(line.Substring(line.IndexOf(')') + 1));
         }
     }
 }
예제 #9
0
        private static void CheckIniFileParameters(CustomTraceLog log)
        {
            log.AddLineAndIncreaseIdent("Checking .ini file...");

            string iniFilePath = CraftSynth.BuildingBlocks.Common.Misc.ApplicationPhysicalExeFilePathWithoutExtension + ".ini";

            log.AddLine("Ini file path: " + iniFilePath);

            log.AddLineAndIncreaseIdent("Content:");
            var           lines = File.ReadLines(iniFilePath);
            List <string> tasks = null;
            string        currentLine;

            foreach (string line in lines)
            {
                currentLine = line;
                if (currentLine.Trim().StartsWith("[Tasks]"))
                {
                    tasks = new List <string>();
                }
                else if (tasks != null && currentLine.Trim().Length > 0 && !currentLine.StartsWith("--"))
                {
                    tasks.Add(currentLine);
                }

                log.AddLine(currentLine);
            }
            log.DecreaseIdent();

            bool   ExecuteNextTaskAfterError    = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <bool>("ExecuteNextTaskAfterError", iniFilePath, true, false, true, false, false, false);
            bool   SendMailOnSuccess            = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <bool>("SendMailOnSuccess", iniFilePath, true, false, true, false, false, false);
            string MailSubjectOnSuccess         = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <string>("MailSubjectOnSuccess", iniFilePath, SendMailOnSuccess, null, SendMailOnSuccess, null, false, null);
            bool   SendMailOnError              = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <bool>("SendMailOnError", iniFilePath, true, false, true, false, false, false);
            string MailSubjectOnError           = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <string>("MailSubjectOnError", iniFilePath, SendMailOnSuccess, null, SendMailOnSuccess, null, false, null);
            int    TimeoutForEveryTaskInMinutes = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <int>("TimeoutForEveryTaskInMinutes", iniFilePath, true, -1, true, -1, false, -1);

            bool   SmtpViaOffice365 = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <bool>("SmtpViaOffice365", iniFilePath, true, false, true, false, false, false);
            string SmtpAddress      = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <string>("SmtpAddress", iniFilePath, true, null, true, null, false, null);
            int    SmtpPort         = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <int>("SmtpPort", iniFilePath, false, 25, true, 25, false, -1);
            string SmtpFrom         = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <string>("SmtpFrom", iniFilePath, true, null, true, null, false, null);
            string SmtpToCsv        = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <string>("SmtpToCsv", iniFilePath, true, null, true, null, false, null);
            string SmtpUsername     = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <string>("SmtpUsername", iniFilePath, true, null, true, null, false, null);
            string SmtpPassword     = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <string>("SmtpPassword", iniFilePath, true, null, true, null, false, null);

            log.AddLine("Tasks count:" + tasks.Count);

            log.AddLineAndDecreaseIdent("Checking .ini file done.");
        }
예제 #10
0
        static int Main(string[] args)
        {
            int r = 0;

            CustomTraceLog log = new CustomTraceLog("--------------------------------------------------------------------------------------------", true, false, CustomTraceLogAddLinePostProcessingEvent, CustomTraceLogAddLinePreProcessingEvent);

            try
            {
                log.AddLineAndIncreaseIdent("Starting...");

                AppDomain.CurrentDomain.UnhandledException += delegate(object sender, UnhandledExceptionEventArgs args1)
                {
                    var ex = (Exception)args1.ExceptionObject;
                    HandlerForLoging.LogException(ex, log);
                    Environment.Exit(1);
                };


                List <Process> sameNameProcesses = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).ToList();
                //sameNameProcesses.AddRange(Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName+".vshost").ToList());
                if (sameNameProcesses.Count() > 1)
                {
                    log.AddLine("Another instance is allready running. Closing this one...");
                }
                else
                {
                    CheckIniFileParameters(log);

                    bool allSuccess = true;
                    allSuccess = allSuccess && HandlerForTasks.Execute(log);

                    log.AddLine(allSuccess?"Success!":"Failure!");
                    HandlerForMails.SendReportIfNecessary(allSuccess, log);


                    log.DecreaseIdent();
                    log.AddLine("Finished with " + (allSuccess ? "success!" : "failure!"));
                }
            }
            catch (Exception exception)
            {
                HandlerForLoging.LogException(exception, log);
                r = -1;
            }

            return(r);
        }
예제 #11
0
        public static int RemoveDummyFilesFromFolders(string folderPath, CustomTraceLog log)
        {
            log.AddLine("Detecting dummy files...");
            var filesPaths = CraftSynth.BuildingBlocks.IO.FileSystem.GetFilePaths(folderPath, true, "EmptyFolderIndicator.txt");

            log.AddLine(filesPaths.Count + " found.", false);
            log.AddLine("Deleting dummy files...");
            int i = 0;

            foreach (string filePath in filesPaths)
            {
                File.Delete(filePath);
                i++;
            }
            log.AddLine(i + " deleted.", false);
            return(i);
        }
예제 #12
0
        public static int CreateDummyFilesInEmptyFolders(string folderPath, CustomTraceLog log)
        {
            log.AddLine("Detecting empty folders...");
            var emptyFoldersPaths = CraftSynth.BuildingBlocks.IO.FileSystem.GetEmptyFoldersPathsRecursively(folderPath);

            log.AddLine(emptyFoldersPaths.Count + " found.", false);
            log.AddLine("Creating dummy files inside empty folders...");
            int i = 0;

            foreach (string emptyFoldersPath in emptyFoldersPaths)
            {
                File.WriteAllText(Path.Combine(emptyFoldersPath, "EmptyFolderIndicator.txt"), string.Empty);
                i++;
            }
            log.AddLine(i + " created.", false);
            return(i);
        }
예제 #13
0
        public Item(string svnExePath, string svnFolder, string newSvnRootUrl)
        {
            this.svnExePath    = svnExePath;
            this.folder        = Path.GetDirectoryName(svnFolder);
            this.newSvnRootUrl = newSvnRootUrl;
            var log      = new CustomTraceLog();
            int exitCode = BuildingBlocks.UI.Console.ExecuteCommand(svnExePath, "info --show-item url", 3000, this.folder, log);

            if (exitCode != 0)
            {
                MessageBox.Show(log.ToString());
            }
            else
            {
                // Files\TortoiseSVN\bin\svn.exe info --show-item url
                //2016.12.29 21:58:10(local)   Command executing...
                //2016.12.29 21:58:10(local) >> https://craftsynth.cloudapp.net:8443/svn/F4CIO
                //2016.12.29 21:58:10(local)   ExitCode: 0}
                this.svnUrl = log.ToString().Replace("\n", "|").GetSubstring(">> ", "\r");
            }
        }
예제 #14
0
        public static CommandDelete Parse(string line, bool handleCommentsAsNormalCommands, CustomTraceLog log)
        {
            CommandDelete r = null;

            try
            {
                r = new CommandDelete();

                line = line.Trim();

                if (handleCommentsAsNormalCommands)
                {
                    line = line.TrimStart('-').Trim();
                }

                if (line.ToLower().Split(' ')[0] != "delete")
                {
                    throw new Exception("This is not delete command.");
                }

                r.line        = line;
                r.CommandName = "delete";
                var parameters = line.GetParameters(false, false, '"');
                if (parameters.Count < 1)
                {
                    throw new Exception("Invalid number of parameters.");
                }

                r.DestinationLocation = parameters[0];

                if (line.GetParameterPresence("/destKey", false, false, '/', ':'))
                {
                    r.DestinationKey = line.GetParameterValue <string>("/destKey", true, null, true, null, false, null, '/', ':');
                }

                r.DeleteOnlyContent = line.GetParameterPresence("/DeleteOnlyContent", false, false, '/', null);
            }
            catch (Exception e)
            {
                HandlerForLoging.LogException(e, log);
                r = null;
            }
            return(r);
        }
예제 #15
0
        public static int FixEmptyFoldersIfNecessary(ref string task, bool azCopyWasExecuted, int createdDummyFiles, ref bool allSuccess, CustomTraceLog log)
        {
            int affectedDummyFiles = 0;

            var    parameters  = task.GetParameters(false, false, '"');
            string source      = parameters[0];
            string destination = parameters[1];

            bool sourceKeyPresent       = task.GetParameterPresence("/sourceKey", false, false, '/', null);
            bool destKeyPresent         = task.GetParameterPresence("/destKey", false, false, '/', null);
            bool skipFixingEmptyFolders = task.GetParameterPresence("/skipFixingEmptyFolders", false, false, '/', null);

            if (!skipFixingEmptyFolders)
            {
                if (!sourceKeyPresent && destKeyPresent && !azCopyWasExecuted)
                {                //files are about to be uploaded to azure storage -create dummy files in empty folders to trigger their creation at azure storage
                    affectedDummyFiles = CreateDummyFilesInEmptyFolders(source, log);
                }
                else if (!sourceKeyPresent && destKeyPresent && azCopyWasExecuted)
                {                //files were uploaded to azure -remove local temporary dummy files
                    affectedDummyFiles = RemoveDummyFilesFromFolders(source, log);
                    allSuccess         = allSuccess && (affectedDummyFiles == createdDummyFiles);
                    if (affectedDummyFiles != createdDummyFiles)
                    {
                        log.AddLine("Error: Number of dummy files created does not match the number of dummy files deleted.");
                    }
                }
                else if (sourceKeyPresent && !destKeyPresent && azCopyWasExecuted)
                {                //files were just downloaded from azure storage to local - remove all dummy files
                    affectedDummyFiles = RemoveDummyFilesFromFolders(destination, log);
                }
            }

            return(affectedDummyFiles);
        }
예제 #16
0
        public static bool Execute(CustomTraceLog log, int taskIndex, string task, string timestamp, int TimeoutForEveryTaskInMinutes)
        {
            bool allSuccess = true;

            log.AddLineAndIncreaseIdent("Executing task: [t" + taskIndex + "] " + task);            //task.Split(' ')[1].Trim() + " ---> " +task.Split(' ')[2].Trim());

            int createdDummyFiles = FixEmptyFoldersIfNecessary(ref task, false, 0, ref allSuccess, log);

            string azCopySubfolderPath = Path.Combine(Path.Combine(CraftSynth.BuildingBlocks.Common.Misc.ApplicationRootFolderPath, "AzCopy"));
            string taskLogFileName     = Path.GetFileName(Misc.ApplicationPhysicalExeFilePathWithoutExtension) + "_" + timestamp + "_t" + taskIndex + ".log";
            string taskLogFilePath     = Path.Combine(azCopySubfolderPath, taskLogFileName);

            log.AddLine("Log file path:" + taskLogFilePath);

            string taskWithoutCustomParameters = RemoveCustomParameters(task);

            string command    = taskWithoutCustomParameters.Split(' ')[0].Trim();
            string parameters = taskWithoutCustomParameters.Substring(command.Length + 1).Trim() + " /V:" + taskLogFileName;

            allSuccess = Console.ExecuteCommand(command, parameters, TimeoutForEveryTaskInMinutes * 60000, azCopySubfolderPath, log, false) == 0 && allSuccess;
            //2014.06.15 20:22:55 (local)     >>
            //2014.06.15 20:22:57 (local)     >> Transfer summary:
            //2014.06.15 20:22:58 (local)     >> -----------------
            //2014.06.15 20:22:59 (local)     >> Total files transferred: 99
            //2014.06.15 20:23:00 (local)     >> Transfer successfully:   99
            //2014.06.15 20:23:01 (local)     >> Transfer failed:         0
            //2014.06.15 20:23:02 (local)     Done.
            List <string> logLines = log.ToString().Split('\n').ToList();
            int           totalFilesTransferred = -1;
            int           transferSuccessfully  = -1;
            int           transferFailed        = -1;

            for (int i = logLines.Count - 1; i >= 0 && i >= logLines.Count - 10; i--)
            {
                if (logLines[i].Contains("Transfer failed:"))
                {
                    transferFailed = Int32.Parse(logLines[i].GetSubstringAfterLastOccurence("Transfer failed:").Trim());
                }
                else if (logLines[i].Contains("Transfer successfully:"))
                {
                    transferSuccessfully = Int32.Parse(logLines[i].GetSubstringAfterLastOccurence("Transfer successfully:").Trim());
                }
                else if (logLines[i].Contains("Total files transferred:"))
                {
                    totalFilesTransferred = Int32.Parse(logLines[i].GetSubstringAfterLastOccurence("Total files transferred:").Trim());
                }

                if (totalFilesTransferred > -1 && transferSuccessfully > -1 && transferFailed > -1)
                {
                    break;
                }
            }

            if (totalFilesTransferred == -1 || transferSuccessfully == -1 || transferFailed == -1)
            {
                throw new Exception("Can not parse AzCopy result. Did it execute?");
            }
            else
            {
                allSuccess = allSuccess && (totalFilesTransferred == transferSuccessfully && transferFailed == 0);
            }

            FixEmptyFoldersIfNecessary(ref task, true, createdDummyFiles, ref allSuccess, log);

            return(allSuccess);
        }
예제 #17
0
        static void Main(string[] args)
        {
            log = new CustomTraceLog("Starting..............................................", true, false, CustomTraceLogAddLinePostProcessingEvent, null);

            bool retryOnError = false;
            bool pauseAtEnd   = false;

            while (true)
            {
                try
                {
                    Console.Clear();
                    log.AddLine("Attempt:" + attempt);
                    log.AddLineAndIncreaseIdent("Reading .ini file...");

                    string accountKey = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <string>("accountKey", null, true, null, true, null, false, null, '=');
                    log.AddLine("accountKey:" + accountKey.FirstXChars(20, "...hidden"));

                    string blobUrl = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <string>("blobUrl", null, true, null, true, null, false, null, '=');
                    log.AddLine("blobUrl:" + blobUrl);

                    string blobIsBlockOrPage = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <string>("blobIsBlockOrPage", null, true, null, true, null, false, null, '=');
                    log.AddLine("blobIsBlockOrPage:" + blobIsBlockOrPage);

                    string localFilePath = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <string>("localFilePath", null, true, null, true, null, false, null, '=');
                    log.AddLine("localFilePath:" + localFilePath);

                    retryOnError = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <bool>("retryOnError", null, true, false, true, false, false, false, '=');
                    log.AddLine("retryOnError:" + retryOnError);

                    pauseAtEnd = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <bool>("pauseAtEnd", null, true, false, true, false, false, false, '=');
                    log.AddLine("pauseAtEnd:" + pauseAtEnd);

                    int delayBeetweenChunksInSeconds = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <int>("delayBeetweenChunksInSeconds", null, true, 0, false, 0, false, 0, '=');
                    log.AddLine("delayBeetweenChunksInSeconds:" + delayBeetweenChunksInSeconds);

                    log.AddLineAndDecreaseIdent("Done.");

                    if (blobIsBlockOrPage != "Page")
                    {
                        throw new Exception("blobIsBlockOrPage can be only 'Page'. Other case is not implemented yet.");
                    }

                    log.AddLineAndIncreaseIdent("Parsing blob url...");
                    CraftSynth.BuildingBlocks.IO.AzureStorage.BlobUrl blobUrlClass = new BlobUrl(blobUrl);
                    string accountName = blobUrlClass.StorageName;
                    log.AddLine("accountName:" + accountName);

                    var containerName = blobUrlClass.ContainerName;
                    log.AddLine("containerName:" + containerName);

                    var blobName = blobUrlClass.BlobName;
                    log.AddLine("blobName:" + blobName);

                    log.AddLineAndDecreaseIdent("Done.");

                    int segmentSize = 1 * 1024 * 1024;                 //1 MB chunk

                    log.AddLineAndIncreaseIdent("Downloading...");
                    Download(accountName, accountKey, containerName, blobName, localFilePath, segmentSize, delayBeetweenChunksInSeconds);
                    log.AddLine("Downloading done.");


                    break;
                }
                catch (Exception e)
                {
                    Exception de = CraftSynth.BuildingBlocks.Common.Misc.GetDeepestException(e);
                    log.AddLine(de.Message);

                    if (retryOnError)
                    {
                        log.AddLine("Retrying in 10 seconds...");
                        Thread.Sleep(10000);
                        attempt++;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (pauseAtEnd)
            {
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();
            }
        }
예제 #18
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="uri">pass null to use value from .ini file</param>
        /// <returns></returns>
        public static string DoJob(string uri, MailAlertShouldBeSent mailAlertShouldBeSent, bool?mailServerIsOffice365, string mailServerAddress, int?mailServerPort, string mailServerUsername, string mailServerPassword, string mailServerTo, string mailSubject)
        {
            string response = string.Empty;
            string error    = null;

            uri = uri ?? DataAccess.GetUri();
            int retryAfterXMinutes = DataAccess.GetRetryAfterXMinutes();

            mailServerIsOffice365 = mailServerIsOffice365 ?? DataAccess.GetMailServerIsOffice365();
            mailServerAddress     = mailServerAddress ?? DataAccess.GetMailServerAddress();
            mailServerPort        = mailServerPort ?? DataAccess.GetMailServerPort();
            mailServerUsername    = mailServerUsername ?? DataAccess.GetMailServerUsername();
            mailServerPassword    = mailServerPassword ?? DataAccess.GetMailServerPassword();
            mailServerTo          = mailServerTo ?? DataAccess.GetMailServerTo();
            mailSubject           = mailSubject ?? DataAccess.GetMailSubject();

            bool ok = IsHealthy(uri, ref response, ref error);

            if (!ok && retryAfterXMinutes > 0)
            {
                DataAccess.WriteLog("Waiting " + retryAfterXMinutes + " minutes before retry...");
                Thread.Sleep(retryAfterXMinutes * 60 * 1000);
                ok = IsHealthy(uri, ref response, ref error);
            }

            bool sendMailAlert;

            switch (mailAlertShouldBeSent)
            {
            case MailAlertShouldBeSent.InAnyCase:
                sendMailAlert = true;
                break;

            case MailAlertShouldBeSent.Never:
                sendMailAlert = false;
                break;

            case MailAlertShouldBeSent.OnlyIfUrlFailed:
                sendMailAlert = !ok;
                break;

            default:
                throw new ArgumentOutOfRangeException("mailAlertShouldBeSent");
            }

            if (sendMailAlert)
            {
                CustomTraceLog customTraceLog = new CustomTraceLog("Sending Mail...");
                try
                {
                    MailMessage mail = new MailMessage(mailServerUsername, mailServerTo, mailSubject, "Log:");
                    if (response.IsNOTNullOrWhiteSpace())
                    {
                        mail.Body       = response;
                        mail.IsBodyHtml = true;
                    }
                    else
                    {
                        mail.Body       = error.ToNonNullString();
                        mail.IsBodyHtml = false;
                    }
                    CraftSynth.BuildingBlocks.IO.EMail.SendMail(mailServerIsOffice365.Value, mailServerAddress, mailServerPort, mailServerUsername, mailServerPassword, mail, customTraceLog);
                }
                catch (Exception e)
                {
                    customTraceLog.AddLine("Error occured while sending mail. Error details:" + e.Message);
                    DataAccess.WriteLog(customTraceLog.ToString());
                }
            }

            return(response);
        }
예제 #19
0
        static int Main(string[] args)
        {
            int?r = null;

            BuildingBlocks.Logging.CustomTraceLog log = new CustomTraceLog("Starting...----------------------------------------------------------------------------------------------------------", true, false, CustomTraceLogAddLinePostProcessingEvent);
            try
            {
                DateTime now                   = DateTime.Now;
                string   nowUniqueString       = now.ToDateAndTimeInSortableFormatForFileSystem() + "-" + now.Millisecond.ToString().PadLeft(3, '0');
                string   sendersEMailAddress   = "Unknown";
                string   receiversEMailAddress = "Unknown";
                string   subject               = string.Empty;

                string        filePath = args[0];
                string        destinationFolderForClonedEmlFiles             = null;
                bool          shouldDeleteClonedEmlFileAfterProcessing       = true;
                bool          shouldDeleteClonedEmlFileFolderAfterProcessing = false;
                bool          shouldDeleteExtractedEmlContentAfterProcessing = false;
                long          maxSizeOfEmlFileThatWillBeProcessedInKBytes    = -1;
                long          virusIsNeverLargerThanXKBytes = -1;
                List <string> virusFileNameExtensions       = null;
                List <string> virusPhrases = null;
                int           virusNeverHasMoreThanXFilesPackedInZip = -1;
                List <string> virusFileNameExtensionsInZip           = null;
                bool          shouldCopyVirusToDestinationFolder     = false;
                string        destinationFolderForDetectedVirus      = null;
                int           whichNumberToReturnIfIsVirus           = -1;
                int           whichNumberToReturnIfIsNotVirus        = -1;
                List <string> trustedEMails = null;


                using (log.LogScope("Processing '" + filePath.ToNonNullString("null") + "' ..."))
                {
                    using (log.LogScope("Reading and parsing parameters... "))
                    {
                        destinationFolderForClonedEmlFiles = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <string>("destinationFolderForClonedEmlFiles", null, true, null, false, string.Empty, false, null, '=');
                        log.AddLine("destinationFolderForClonedEmlFiles=" + destinationFolderForClonedEmlFiles);

                        shouldDeleteClonedEmlFileAfterProcessing = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <bool>("shouldDeleteClonedEmlFileAfterProcessing", null, true, false, true, false, false, false, '=');
                        log.AddLine("shouldDeleteClonedEmlFileAfterProcessing=" + shouldDeleteClonedEmlFileAfterProcessing);

                        shouldDeleteExtractedEmlContentAfterProcessing = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <bool>("shouldDeleteExtractedEmlContentAfterProcessing", null, true, false, true, false, false, false, '=');
                        log.AddLine("shouldDeleteExtractedEmlContentAfterProcessing=" + shouldDeleteExtractedEmlContentAfterProcessing);

                        maxSizeOfEmlFileThatWillBeProcessedInKBytes = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <long>("maxSizeOfEmlFileThatWillBeProcessed", null, true, -1, false, long.MaxValue, false, -1, '=');
                        log.AddLine("maxSizeOfEmlFileThatWillBeProcessedInKBytes=" + maxSizeOfEmlFileThatWillBeProcessedInKBytes);


                        virusIsNeverLargerThanXKBytes = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <long>("virusIsNeverLargerThanXKBytes", null, true, -1, false, long.MaxValue, false, -1, '=');
                        log.AddLine("virusIsNeverLargerThanXKBytes=" + virusIsNeverLargerThanXKBytes);

                        string virusFileNameExtensionsCsv = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <string>("virusFileNameExtensionsCsv", null, true, null, false, string.Empty, false, null, '=');
                        log.AddLine("virusFileNameExtensionsCsv=" + virusFileNameExtensionsCsv);
                        virusFileNameExtensions = virusFileNameExtensionsCsv.IsNullOrWhiteSpace() ? new List <string>() : virusFileNameExtensionsCsv.ParseCSV();

                        string virusPhrasesCsv = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <string>("virusPhrasesCsv", null, true, null, false, string.Empty, false, null, '=');
                        log.AddLine("virusPhrasesCsv=" + virusPhrasesCsv);
                        virusPhrases = virusPhrasesCsv.IsNullOrWhiteSpace() ? new List <string>() : virusPhrasesCsv.ParseCSV(new [] { '|' });

                        virusNeverHasMoreThanXFilesPackedInZip = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <int>("virusNeverHasMoreThanXFilesPackedInZip", null, true, -1, true, -1, false, -1, '=');
                        log.AddLine("virusNeverHasMoreThanXFilesPackedInZip=" + virusNeverHasMoreThanXFilesPackedInZip);

                        string virusFileNameExtensionsInZipCsv = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <string>("virusFileNameExtensionsInZipCsv", null, true, null, false, string.Empty, false, null, '=');
                        log.AddLine("virusFileNameExtensionsInZipCsv=" + virusFileNameExtensionsInZipCsv);
                        virusFileNameExtensionsInZip = virusFileNameExtensionsInZipCsv.IsNullOrWhiteSpace() ? new List <string>() : virusFileNameExtensionsInZipCsv.ParseCSV();

                        shouldCopyVirusToDestinationFolder = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <bool>("shouldCopyVirusToDestinationFolder", null, true, false, true, false, false, false, '=');
                        log.AddLine("shouldCopyVirusToDestinationFolder=" + shouldCopyVirusToDestinationFolder);

                        destinationFolderForDetectedVirus = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <string>("destinationFolderForDetectedVirus", null, true, null, false, string.Empty, false, null, '=');
                        log.AddLine("destinationFolderForDetectedVirus=" + destinationFolderForDetectedVirus);

                        whichNumberToReturnIfIsVirus = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <int>("whichNumberToReturnIfIsVirus", null, true, -1, true, -1, false, -1, '=');
                        log.AddLine("whichNumberToReturnIfIsVirus=" + whichNumberToReturnIfIsVirus);

                        whichNumberToReturnIfIsNotVirus = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <int>("whichNumberToReturnIfIsNotVirus", null, true, -1, true, -1, false, -1, '=');
                        log.AddLine("whichNumberToReturnIfIsNotVirus=" + whichNumberToReturnIfIsNotVirus);


                        string trustedEMailsCsv = CraftSynth.BuildingBlocks.IO.FileSystem.GetSettingFromIniFile <string>("trustedEMailsCsv", null, true, null, false, string.Empty, false, null, '=');
                        log.AddLine("trustedEMailsCsv=" + trustedEMailsCsv);
                        trustedEMails = trustedEMailsCsv.IsNullOrWhiteSpace() ? new List <string>() : trustedEMailsCsv.ParseCSV();
                    }


                    using (log.LogScope("Processing mail '" + filePath.ToNonNullString() + "'..."))
                    {
                        if (filePath.IsNullOrWhiteSpace())
                        {
                            throw new Exception("filePath can not be null or empty.");
                        }
                        if (string.Compare(Path.GetExtension(filePath).Trim('.'), "eml", StringComparison.OrdinalIgnoreCase) != 0 && string.Compare(Path.GetExtension(filePath).Trim('.'), "tmp", StringComparison.OrdinalIgnoreCase) != 0)
                        {
                            throw new Exception("Only .eml file type is supported however file extension can be .eml or .tmp.");
                        }
                        if (!File.Exists(filePath))
                        {
                            throw new Exception("File not found:" + filePath.ToNonNullString());
                        }

                        #region clone .eml file and set filePath to that new file

                        using (log.LogScope("Cloning .eml to destinationFolderForClonedEmlFiles..."))
                        {
                            //we need to clone hServerMail .tmp file in order to change it extension to .eml -only that extension is accepted by MsgReader.
                            if (destinationFolderForClonedEmlFiles.StartsWith(@"\"))
                            {
                                destinationFolderForClonedEmlFiles = Path.Combine(BuildingBlocks.Common.Misc.ApplicationRootFolderPath, destinationFolderForClonedEmlFiles.TrimStart('\\'));
                            }

                            shouldDeleteClonedEmlFileFolderAfterProcessing = shouldDeleteClonedEmlFileAfterProcessing && destinationFolderForClonedEmlFiles.Contains("{0}");
                            destinationFolderForClonedEmlFiles             = destinationFolderForClonedEmlFiles.Replace("{0}", nowUniqueString);
                            string destinationFilePath = Path.Combine(destinationFolderForClonedEmlFiles, nowUniqueString + ".eml");                             //Path.GetFileName(filePath));
                            log.AddLine("destinationFilePath=" + destinationFilePath);
                            if (!Directory.Exists(Path.GetDirectoryName(destinationFilePath)))
                            {
                                using (log.LogScope("Creating directory..."))
                                {
                                    Directory.CreateDirectory(Path.GetDirectoryName(destinationFilePath));
                                }
                            }

                            using (log.LogScope("Performing copy..."))
                            {
                                File.Copy(filePath, destinationFilePath, true);
                                filePath = destinationFilePath;                                                   //!!!
                            }
                        }
                        #endregion

                        try
                        {
                            log.AddLine("Checking against .eml file size...");
                            long?emlFileSize = BuildingBlocks.IO.FileSystem.GetFileSizeInBytes(filePath, false);
                            if (emlFileSize == null)
                            {
                                throw new Exception("Could not determine .eml file size.");
                            }
                            if (emlFileSize > maxSizeOfEmlFileThatWillBeProcessedInKBytes * 1024)
                            {
                                log.AddLine(".eml too big for processing. assume it seems clean.", false);
                                r = whichNumberToReturnIfIsNotVirus;
                            }
                            else
                            {
                                using (log.LogScope("Parsing .eml file..."))
                                {
                                    string extractedEmlFolderPath = Path.Combine(Path.GetDirectoryName(filePath), "_extractedEml_" + nowUniqueString);
                                    log.AddLine("extractedEmlFolderPath=" + extractedEmlFolderPath);
                                    if (!Directory.Exists(extractedEmlFolderPath))
                                    {
                                        using (log.LogScope("Creating extractedEmlFolderPath folder..."))
                                        {
                                            Directory.CreateDirectory(extractedEmlFolderPath);
                                        }
                                    }

                                    try
                                    {
                                        List <string> emailBodyAndAttachmentsFilePaths = null;
                                        using (log.LogScope("Extracting .eml file..."))
                                        {
                                            try
                                            {
                                                var msgReader = new MsgReader.Reader();                                                 //https://www.codeproject.com/Tips/712072/Reading-an-Outlook-MSG-File-in-Csharp
                                                emailBodyAndAttachmentsFilePaths = msgReader.ExtractToFolder(filePath, extractedEmlFolderPath).ToList();
                                                if (emailBodyAndAttachmentsFilePaths.Count == 0)
                                                {
                                                    throw new Exception("Nothing extracted from .eml file.");
                                                }
                                            }
                                            catch (Exception e)
                                            {
                                                throw new Exception("Error occured during extraction of .eml file.", e);
                                            }
                                        }

                                        string eMailHeadingAndBody = File.ReadAllText(emailBodyAndAttachmentsFilePaths[0]).ToNonNullString();
                                        log.AddLine("Checking against virus phrases...");
                                        if (virusPhrases.Any(p => eMailHeadingAndBody.ToLower().Contains(p.ToLower())))
                                        {
                                            log.AddLine("Virus phrase found. virus detected.");
                                            r = whichNumberToReturnIfIsVirus;
                                        }
                                        else
                                        {
                                            log.AddLine("Virus phrase not found. needs further checks.");


                                            bool isPlainText = emailBodyAndAttachmentsFilePaths[0].ToLower().EndsWith(".txt");
                                            log.AddLine("isPlainText: " + isPlainText);

                                            using (log.LogScope("Finding sender's email..."))
                                            {
                                                if (isPlainText)
                                                {
                                                    var lines = File.ReadAllLines(emailBodyAndAttachmentsFilePaths[0]);
                                                    sendersEMailAddress   = lines[0].GetSubstring("<", ">");
                                                    subject               = lines[3].GetSubstringAfter("Subject:").ToNonNullString().Trim();
                                                    receiversEMailAddress = lines[2].GetSubstring("<", ">");
                                                }
                                                else
                                                {
                                                    //find first '"mailto:' phrase or @ sign
                                                    //var text = File.ReadAllText(emailBodyAndAttachmentsFilePaths[0]);
                                                    sendersEMailAddress   = eMailHeadingAndBody.GetSubstring("&lt;", "&gt;");                                                   //>Example: From:</td><td>f4cio&nbsp&lt;[email protected]&gt;</td></tr>
                                                    subject               = eMailHeadingAndBody.GetSubstring("Subject:</td><td>", "<br/>");
                                                    receiversEMailAddress = eMailHeadingAndBody.GetSubstring("To:</td><td>", "</td>");
                                                }
                                                if (sendersEMailAddress.IsNullOrWhiteSpace())
                                                {
                                                    throw new Exception("Failed to parse sender's email.");
                                                }
                                                log.AddLine("   FROM: " + sendersEMailAddress.ToNonNullString("null"));
                                                log.AddLine("     TO: " + receiversEMailAddress.ToNonNullString("null"));
                                                log.AddLine("SUBJECT: " + subject.ToNonNullString("null"));
                                            }

                                            log.AddLine("Checking against trusted emails...");
                                            if (trustedEMails.Any(e => string.Compare(e, sendersEMailAddress, StringComparison.OrdinalIgnoreCase) == 0))
                                            {
                                                log.AddLine("EMail is from trusted sender. seems clean.");
                                                r = whichNumberToReturnIfIsNotVirus;
                                            }
                                            else
                                            {
                                                log.AddLine("EMail is not from trusted sender. Needs further checks.");

                                                if (emailBodyAndAttachmentsFilePaths.Count == 1)
                                                {
                                                    log.AddLine("There are no attachments. Nothing more to check. seems clean.");
                                                    r = whichNumberToReturnIfIsNotVirus;
                                                }
                                                else
                                                {
                                                    if (emailBodyAndAttachmentsFilePaths.Count > 2)
                                                    {
                                                        log.AddLine("There is more than one attachment. Assuming it is not virus. seems clean.");
                                                        r = whichNumberToReturnIfIsNotVirus;
                                                    }
                                                    else
                                                    {
                                                        using (log.LogScope("Checking attachment..."))
                                                        {
                                                            string attachmentFilePath = emailBodyAndAttachmentsFilePaths[1];
                                                            if (!File.Exists(attachmentFilePath))
                                                            {
                                                                throw new Exception("File not found:" + attachmentFilePath.ToNonNullString());
                                                            }

                                                            log.AddLine("Checking against file size...");
                                                            long?fileSize = BuildingBlocks.IO.FileSystem.GetFileSizeInBytes(attachmentFilePath, false);
                                                            if (fileSize == null)
                                                            {
                                                                throw new Exception("Could not determine file size.");
                                                            }
                                                            if (fileSize > virusIsNeverLargerThanXKBytes * 1024)
                                                            {
                                                                log.AddLine("seems clean.", false);
                                                                r = whichNumberToReturnIfIsNotVirus;
                                                            }
                                                            else
                                                            {
                                                                log.AddLine("Needs further checks.", false);
                                                                log.AddLine("Checking against extension...");
                                                                var ext = Path.GetExtension(attachmentFilePath).Trim('.');
                                                                if (virusFileNameExtensions.Exists(e => string.Compare(e, ext, StringComparison.OrdinalIgnoreCase) == 0))
                                                                {
                                                                    log.AddLine("virus detected.");
                                                                    r = whichNumberToReturnIfIsVirus;
                                                                }
                                                                else
                                                                {
                                                                    log.AddLine("Needs further checks.", false);
                                                                    log.AddLine("Checking wether is zip...");
                                                                    if (string.Compare(ext, "zip", StringComparison.OrdinalIgnoreCase) != 0)
                                                                    {
                                                                        log.AddLine("not zip. seems clean.", false);
                                                                        r = whichNumberToReturnIfIsNotVirus;
                                                                    }
                                                                    else
                                                                    {
                                                                        log.AddLine("it is zip. Needs further checks.", false);
                                                                        using (ZipFile zip = ZipFile.Read(attachmentFilePath))
                                                                        {
                                                                            log.AddLine("Zipfile:" + zip.Name);
                                                                            log.AddLine("Comment:" + zip.Comment.ToNonNullString("null"));

                                                                            if (zip.Count == 0)
                                                                            {
                                                                                log.AddLine("No files in zip. seems clean.");
                                                                                r = whichNumberToReturnIfIsNotVirus;
                                                                            }
                                                                            else if (zip.Count > virusNeverHasMoreThanXFilesPackedInZip)
                                                                            {
                                                                                log.AddLine("Too many files in zip. seems clean.");
                                                                                r = whichNumberToReturnIfIsNotVirus;
                                                                            }
                                                                            else
                                                                            {
                                                                                foreach (ZipEntry e in zip)
                                                                                {
                                                                                    log.AddLine(string.Format("FileName={0}, LastModified={1}, Uncompressed={2} bytes, Compression={3:0.00}%, Compressed={4} bytes, Encrypted={5}",
                                                                                                              e.FileName,
                                                                                                              e.LastModified.ToString("yyyy-MM-dd HH:mm:ss"),
                                                                                                              e.UncompressedSize,
                                                                                                              e.CompressionRatio,
                                                                                                              e.CompressedSize,
                                                                                                              (e.UsesEncryption) ? "Y" : "N"));

                                                                                    if (virusFileNameExtensionsInZip.Exists(e1 => string.Compare(e1, Path.GetExtension(e.FileName).Trim('.'), StringComparison.OrdinalIgnoreCase) == 0))
                                                                                    {
                                                                                        log.AddLine("virus detected.");
                                                                                        r = whichNumberToReturnIfIsVirus;
                                                                                    }
                                                                                }
                                                                                if (r == null)
                                                                                {
                                                                                    log.AddLine("seems clean.");
                                                                                    r = whichNumberToReturnIfIsNotVirus;
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    finally
                                    {
                                        if (shouldDeleteExtractedEmlContentAfterProcessing)
                                        {
                                            try
                                            {
                                                Directory.Delete(extractedEmlFolderPath, true);
                                            }
                                            catch (Exception e)
                                            {
                                                log.AddLine("Couldn't delete extractedEmlFolderPath folder. Error:" + e.Message);
                                            }
                                        }
                                    }
                                }
                            }

                            if (r == whichNumberToReturnIfIsVirus && shouldCopyVirusToDestinationFolder)
                            {
                                using (log.LogScope("Copying .eml to destinationFolderForDetectedVirus..."))
                                {
                                    if (destinationFolderForDetectedVirus.StartsWith(@"\"))
                                    {
                                        destinationFolderForDetectedVirus = Path.Combine(BuildingBlocks.Common.Misc.ApplicationRootFolderPath, destinationFolderForDetectedVirus.TrimStart('\\'));
                                    }
                                    destinationFolderForDetectedVirus = destinationFolderForDetectedVirus.Replace("{0}", nowUniqueString);
                                    string destinationFilePath = Path.Combine(destinationFolderForDetectedVirus, nowUniqueString + " From " + sendersEMailAddress.ToNonNullString("null") + " to " + receiversEMailAddress.ToNonNullString("null") + " " + subject.ReplaceNonAlphaNumericCharacters("_").FirstXChars(100, "... ") + ".eml");
                                    log.AddLine("destinationFilePath=" + destinationFilePath);
                                    if (!Directory.Exists(Path.GetDirectoryName(destinationFilePath)))
                                    {
                                        using (log.LogScope("Creating directory..."))
                                        {
                                            Directory.CreateDirectory(Path.GetDirectoryName(destinationFilePath));
                                        }
                                    }

                                    using (log.LogScope("Performing copy..."))
                                    {
                                        File.Copy(filePath, destinationFilePath, true);
                                    }

                                    try
                                    {
                                        File.WriteAllText(Path.ChangeExtension(destinationFilePath, ".txt"), log.ToString());
                                    }
                                    catch (Exception)
                                    {
                                    }
                                }
                            }
                        }
                        finally
                        {
                            try
                            {
                                using (log.LogScope("Cleaning up cloned file/its folder..."))
                                {
                                    if (shouldDeleteClonedEmlFileFolderAfterProcessing)
                                    {
                                        Directory.Delete(Path.GetDirectoryName(filePath), true);
                                    }
                                    else if (shouldDeleteClonedEmlFileAfterProcessing)
                                    {
                                        File.Delete(filePath);
                                    }
                                    else
                                    {
                                        try
                                        {
                                            File.WriteAllText(Path.Combine(Path.GetDirectoryName(filePath), nowUniqueString + ".txt"), log.ToString());
                                        }
                                        catch (Exception)
                                        {
                                        }
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                log.AddLine("Couldn't delete cloned file/its folder. Error:" + e.Message);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                log.AddLine("Error:" + e.Message);
                e = BuildingBlocks.Common.Misc.GetDeepestException(e);
                log.AddLine("Deepest exception:" + e.Message);
                log.AddLine("StackTrace:" + e.StackTrace);
                r = whichNumberToReturnIfErrorOccured;
            }

            log.AddLine("Exit code: " + r);
            return(r.Value);
        }
예제 #20
0
 private static void CustomTraceLogAddLinePostProcessingEvent(CustomTraceLog sender, string line, bool inNewLine, int level, string lineVersionSuitableForLineEnding, string lineVersionSuitableForNewLine)
 {
     HandlerForLoging.LogAction(line);
     Console.WriteLine(line);
 }
예제 #21
0
        public static bool Execute(CustomTraceLog log)
        {
            string        iniFilePath = Misc.ApplicationPhysicalExeFilePathWithoutExtension + ".ini";
            var           lines       = File.ReadLines(iniFilePath);
            List <string> tasks       = null;

            foreach (string line in lines)
            {
                if (line.Trim().StartsWith("[Tasks]"))
                {
                    tasks = new List <string>();
                }
                else if (tasks != null && line.Trim().Length > 0 && !line.StartsWith("--"))
                {
                    tasks.Add(line.Trim());
                }
            }
            log.DecreaseIdent();

            bool ExecuteNextTaskAfterError    = FileSystem.GetSettingFromIniFile("ExecuteNextTaskAfterError", iniFilePath, true, false, true, false, false, false);
            int  TimeoutForEveryTaskInMinutes = FileSystem.GetSettingFromIniFile("TimeoutForEveryTaskInMinutes", iniFilePath, true, -1, true, -1, false, -1);

            string timestamp = DateTime.Now.ToDateAndTimeInSortableFormatForAzureBlob();

            int    taskIndex  = 0;
            bool   allSuccess = true;
            string task;

            foreach (string taskTemplate in tasks)
            {
                task = taskTemplate.Replace("[T]", timestamp);
                if (task.ToLower().StartsWith("run"))
                {
                    try
                    {
                        allSuccess = HandlerForTask_Run.Execute(log, taskIndex, task, timestamp, TimeoutForEveryTaskInMinutes) && allSuccess;
                    }
                    catch (Exception exception)
                    {
                        HandlerForLoging.LogException(exception, log);
                        log.AddLine(exception.Message);
                        allSuccess = false;
                        if (!ExecuteNextTaskAfterError)
                        {
                            log.AddLine("Aborting...");
                            break;
                        }
                    }
                }
                else if (task.ToLower().StartsWith("azcopy"))
                {
                    try
                    {
                        allSuccess = HandlerForTask_AzCopy.Execute(log, taskIndex, task, timestamp, TimeoutForEveryTaskInMinutes) && allSuccess;
                    }
                    catch (Exception exception)
                    {
                        HandlerForLoging.LogException(exception, log);
                        log.AddLine(exception.Message);
                        allSuccess = false;
                        if (!ExecuteNextTaskAfterError)
                        {
                            log.AddLine("Aborting...");
                            break;
                        }
                    }
                }
                else if (task.ToLower().StartsWith("delete"))
                {
                    try
                    {
                        allSuccess = HandlerForTask_Delete.Execute(taskIndex, taskTemplate, timestamp, log) && allSuccess;
                    }
                    catch (Exception exception)
                    {
                        HandlerForLoging.LogException(exception, log);
                        allSuccess = false;
                        if (!ExecuteNextTaskAfterError)
                        {
                            log.AddLine("Aborting...");
                            break;
                        }
                    }
                    log.DecreaseIdent();
                }
                else
                {
                    throw new Exception("Task not recognized. Task index:" + taskIndex);
                }
                taskIndex++;
            }

            return(allSuccess);
        }
예제 #22
0
 private static void CustomTraceLogAddLinePreProcessingEvent(CustomTraceLog sender, ref string line, ref bool inNewLine, ref int level)
 {
     line = HandlerForCommon.HideSensitiveInformation(line);
 }
예제 #23
0
 private static void CustomTraceLogAddLinePostProcessingEvent(CustomTraceLog sender, string line, bool inNewLine, int level, string lineVersionSuitableForLineEnding, string lineVersionSuitableForNewLine)
 {
     Logging.AddActionLog(line);
 }
예제 #24
0
        public static bool Execute(int taskIndex, string taskTemplate, string currentTimestampString, CustomTraceLog log)
        {
            bool allSuccess = true;

            log.AddLineAndIncreaseIdent("Deleting folders or azure storage items...");


            List <string> parts = taskTemplate.GetParameters();

            //extract destination path:
            string destination = parts[1];

            log.AddLine("Item(s) to delete: " + destination);

            //extract destination key:
            string azureStorageKey = taskTemplate.GetParameterValue <string>("/destkey", false, null, true, null, false, null, '/', ':');

            log.AddLine("Azure storage key: " + (azureStorageKey == null ? "This is not Azure storage" : azureStorageKey.Bubble(20, "...")));

            if (destination.OccurrencesCount("[T]") > 1)
            {
                throw new Exception(String.Format("Under task t{0} in destination path '{1}' multiple timestamps [T] were found. Only none or one occurrance is supported.", taskIndex, destination));
            }
            else if (destination.OccurrencesCount("[T]") == 0)
            {            //just delete or empty single item
                //extract /DeleteOnlyContent info
                bool deleteOnlyContent = taskTemplate.GetParameterPresence("/DeleteOnlyContent", false, false, '/');
                log.AddLine("DeleteOnlyContent:" + deleteOnlyContent);

                if (string.IsNullOrEmpty(azureStorageKey))
                {                //just delete or empty single folder
                    log.AddLine("Deleting folder '" + destination + "' ...");
                    try
                    {
                        if (!deleteOnlyContent)
                        {
                            Directory.Delete(destination, true);
                            log.AddLine("Deleted.");
                        }
                        else
                        {
                            List <string> subfoldersPaths = CraftSynth.BuildingBlocks.IO.FileSystem.GetFolderPaths(destination);
                            List <string> filesPaths      = CraftSynth.BuildingBlocks.IO.FileSystem.GetFilePaths(destination);
                            log.AddLine("Subitems left to process: ");
                            int i = subfoldersPaths.Count + filesPaths.Count;
                            log.AddLine("..." + i);
                            foreach (string subfolder in subfoldersPaths)
                            {
                                Directory.Delete(subfolder, true);
                                i--;
                                log.AddLine("..." + i);
                            }
                            foreach (string filePath in filesPaths)
                            {
                                File.Delete(filePath);
                                i--;
                                log.AddLine("..." + i);
                            }
                            log.AddLine("Emptied.");
                        }
                    }
                    catch (Exception e)
                    {
                        log.AddLine("Failed.");
                        HandlerForLoging.LogException(e, log);
                        allSuccess = false;
                    }
                }
                else
                {                //just delete or empty single container,directory or blob
                    BlobUrl destinationUrl = new BlobUrl(destination);
                    destinationUrl.Key = azureStorageKey;
                    log.AddLine(string.Format("Deleting azure storage item: {0}", destinationUrl.Url));
                    try
                    {
                        var h = new CraftSynth.BuildingBlocks.IO.AzureStorage.HandlerForBlobs();
                        if (!deleteOnlyContent)
                        {
                            allSuccess = h.Delete(destinationUrl) > 0 && allSuccess;
                            log.AddLine("Deleted.");
                        }
                        else
                        {
                            var childs = h.GetChildren(destinationUrl, true);
                            log.AddLine("Subitems left to process: ");
                            int i = childs.Count;
                            log.AddLine("..." + i);
                            foreach (BlobUrl child in childs)
                            {
                                allSuccess = h.Delete(child) > 0 && allSuccess;
                                i--;
                                log.AddLine("..." + i);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        log.AddLine("Failed.");
                        HandlerForLoging.LogException(e, log);
                        allSuccess = false;
                    }
                }
            }
            else
            {
                //extract /KeepLastXDays:1 info:
                int?keepLastXDays = null;
                if (taskTemplate.GetParameterPresence("/KeepLastXDays", false, false, '/', ':'))
                {
                    keepLastXDays = taskTemplate.GetParameterValue <int>("/KeepLastXDays", true, -1, true, -1, false, -1, '/', ':');
                }
                log.AddLine("KeepLastXDays: " + (keepLastXDays == null ? "No" : keepLastXDays.Value.ToString()));

                //extract /KeepLastInMonth info
                bool keepLastInMonth = taskTemplate.GetParameterPresence("/KeepLastInMonth", false, false, '/');
                log.AddLine("KeepLastInMonth:" + keepLastInMonth);

                //extract /KeepLastInYear info
                bool keepLastInYear = taskTemplate.GetParameterPresence("/KeepLastInYear", false, false, '/');
                log.AddLine("KeepLastInYear:" + keepLastInYear);

                //extract /DeleteOnlyContent info
                bool deleteOnlyContent = taskTemplate.GetParameterPresence("/DeleteOnlyContent", false, false, '/');
                log.AddLine("DeleteOnlyContent:" + deleteOnlyContent);

                //Traverse all children, collect ones with timestamp in name, check age and delete if too old:

                if (azureStorageKey.IsNullOrWhiteSpace())
                {                //folders
                    var deletionItems = HandlerForPaths.GetDestinationsFromDestinationWithWildcard(destination, null, taskIndex);

                    //leave only very old items so they can be deleted:
                    deletionItems = ConsiderRetentionAndReturnItemsForDeletion(deletionItems, currentTimestampString, keepLastXDays, keepLastInMonth, keepLastInYear);

                    //delete old items:
                    log.AddLine("Deleting " + deletionItems.Count + " folder(s) ...");
                    int deletedCount = 0;
                    foreach (KeyValuePair <object, DateTime> deletionItem in deletionItems)
                    {
                        try
                        {
                            if (!deleteOnlyContent)
                            {
                                Directory.Delete(deletionItem.Key.ToString(), true);
                            }
                            else
                            {
                                List <string> subfoldersPaths = CraftSynth.BuildingBlocks.IO.FileSystem.GetFolderPaths(deletionItem.Key.ToString());
                                List <string> filesPaths      = CraftSynth.BuildingBlocks.IO.FileSystem.GetFilePaths(deletionItem.Key.ToString());
                                log.AddLine("Subitems left to process: ");
                                int i = subfoldersPaths.Count + filesPaths.Count;
                                log.AddLine("..." + i);
                                foreach (string subfolder in subfoldersPaths)
                                {
                                    Directory.Delete(subfolder, true);
                                    i--;
                                    log.AddLine("..." + i);
                                }
                                foreach (string filePath in filesPaths)
                                {
                                    File.Delete(filePath);
                                    i--;
                                    log.AddLine("..." + i);
                                }
                            }
                            deletedCount++;
                        }
                        catch (Exception e)
                        {
                            HandlerForLoging.LogException(e, log);
                            allSuccess = false;
                        }
                    }
                    if (deletedCount == deletionItems.Count)
                    {
                        if (deleteOnlyContent)
                        {
                            log.AddLine("Emptied successfully.");
                        }
                        else
                        {
                            log.AddLine("Deleted successfully.");
                        }
                    }
                    else
                    {
                        if (deleteOnlyContent)
                        {
                            log.AddLine("Emptied just " + deletedCount + " items.");
                        }
                        else
                        {
                            log.AddLine("Deleted just " + deletedCount + " items.");
                        }
                    }
                }
                else
                {                //azure storage items
                    var deletionItems = HandlerForPaths.GetDestinationsFromDestinationWithWildcard(destination, azureStorageKey, taskIndex);

                    //leave only very old items so they can be deleted:
                    deletionItems = ConsiderRetentionAndReturnItemsForDeletion(deletionItems, currentTimestampString, keepLastXDays, keepLastInMonth, keepLastInYear);

                    //delete old items:
                    log.AddLine(string.Format("Deleting {0} Azure storage item(s) ...", deletionItems.Count));
                    int             deletedCount = 0;
                    HandlerForBlobs h            = new HandlerForBlobs();
                    foreach (KeyValuePair <object, DateTime> deletionItem in deletionItems)
                    {
                        (deletionItem.Key as BlobUrl).Key = azureStorageKey;
                        try
                        {
                            bool r = false;
                            if (!deleteOnlyContent)
                            {
                                r = h.Delete(deletionItem.Key as BlobUrl) > 0;
                            }
                            else
                            {
                                var childs = h.GetChildren((deletionItem.Key as BlobUrl), true);
                                log.AddLine("Subitems left to process: ");
                                int i = childs.Count;
                                log.AddLine("..." + i);
                                r = true;
                                foreach (BlobUrl child in childs)
                                {
                                    r = h.Delete(child) > 0 && r;
                                    i--;
                                    log.AddLine("..." + i);
                                }
                            }
                            allSuccess = allSuccess && r;
                            deletedCount++;
                        }
                        catch (Exception e)
                        {
                            HandlerForLoging.LogException(e, log);
                            allSuccess = false;
                        }
                    }
                    if (deletedCount == deletionItems.Count)
                    {
                        log.AddLine("Deleted successfully.");
                    }
                    else
                    {
                        log.AddLine("Deleted just " + deletedCount + " items.");
                    }
                }
            }

            return(allSuccess);
        }
예제 #25
0
 private static void CustomTraceLogAddLinePostProcessingEvent(CustomTraceLog sender, string line, bool inNewLine, int level, string lineVersionSuitableForLineEnding, string lineVersionSuitableForNewLine)
 {
     Console.WriteLine(line);
     CraftSynth.BuildingBlocks.Logging.Misc.AddTimestampedLineToApplicationWideLog(line, true, null, false);
 }
예제 #26
0
        public static CommandAzCopy Parse(string line, bool handleCommentsAsNormalCommands, CustomTraceLog log)
        {
            CommandAzCopy r = null;

            try
            {
                r = new CommandAzCopy();

                line = line.Trim();

                if (handleCommentsAsNormalCommands)
                {
                    line = line.TrimStart('-').Trim();
                }

                if (line.ToLower().Split(' ')[0] != "azcopy")
                {
                    throw new Exception("This is not AzCopy command.");
                }

                r.line                   = line;
                r.CommandName            = "AzCopy";
                r.SkipFixingEmptyFolders = line.GetParameterPresence("/skipFixingEmptyFolders", false, false, '/', null);

                var parameters = line.GetParameters(false, false, '"');
                if (parameters.Count < 2)
                {
                    throw new Exception("Invalid number of parameters.");
                }

                r.SourceLocation      = parameters[0];
                r.DestinationLocation = parameters[1];
                if (line.GetParameterPresence("/sourceKey", false, false, '/', ':'))
                {
                    r.SourceKey = line.GetParameterValue <string>("/sourceKey", true, null, true, null, false, null, '/', ':');
                }
                if (line.GetParameterPresence("/destKey", false, false, '/', ':'))
                {
                    r.DestinationKey = line.GetParameterValue <string>("/destKey", true, null, true, null, false, null, '/', ':');
                }
            }
            catch (Exception e)
            {
                HandlerForLoging.LogException(e, log);
                r = null;
            }
            return(r);
        }
예제 #27
0
        public static bool Execute(CustomTraceLog log, int taskIndex, string task, string timestamp, int TimeoutForEveryTaskInMinutes)
        {
            bool allSuccess = true;

            log.AddLineAndIncreaseIdent("Executing task: [t" + taskIndex + "] " + task);            //task.Split(' ')[1].Trim() + " ---> " +task.Split(' ')[2].Trim());

            task = task.Substring("run ".Length).Trim();

            string successIndicator = task.GetParameterValue <string>("/successIndicator", false, null, true, null, false, null, '/', ':', true, '"');
            string failIndicator    = task.GetParameterValue <string>("/failIndicator", false, null, true, null, false, null, '/', ':', true, '"');

            string taskWithoutCustomParameters = RemoveCustomParameters(task);

            string command = null;

            if (taskWithoutCustomParameters.StartsWith("\""))
            {
                command = taskWithoutCustomParameters.GetSubstring("\"", "\"");
            }
            else
            {
                command = taskWithoutCustomParameters.Split(' ')[0].Trim();
            }

            string workingFolder = null;

            try
            {
                if (File.Exists(command))
                {
                    workingFolder = Path.GetDirectoryName(command);
                }
            }
            catch (Exception)
            {
                workingFolder = null;
            }

            string parameters = null;

            try
            {
                if (taskWithoutCustomParameters.StartsWith("\""))
                {
                    parameters = taskWithoutCustomParameters.Substring(("\"" + command + "\"").Length).Trim();
                }
                else
                {
                    parameters = taskWithoutCustomParameters.Substring(command.Length).Trim();
                }
            }catch (Exception)
            {
                parameters = null;
            }

            allSuccess = Console.ExecuteCommand(command, parameters, TimeoutForEveryTaskInMinutes * 60000, workingFolder, log, false, true, successIndicator, failIndicator) == 0 && allSuccess;
            //12 percent processed.
            //21 percent processed.
            //30 percent processed.
            //43 percent processed.
            //51 percent processed.
            //60 percent processed.
            //73 percent processed.
            //82 percent processed.
            //90 percent processed.
            //Processed 184 pages for database 'Test1', file 'Test1' on file 1.
            //100 percent processed.
            //Processed 1 pages for database 'Test1', file 'Test1_log' on file 1.
            //BACKUP DATABASE successfully processed 185 pages in 0.146 seconds (9.899 MB/sec)
            //.
            //1>
            //List<string> logLines = log.ToString().Split('\n').ToList();
            //int totalFilesTransferred = -1;
            //int transferSuccessfully = -1;
            //int transferFailed = -1;

            //for (int i = logLines.Count - 1; i >= 0 && i >= logLines.Count - 10; i--)
            //{
            //	if (logLines[i].Contains("Transfer failed:"))
            //	{
            //		transferFailed = Int32.Parse(logLines[i].GetSubstringAfterLastOccurence("Transfer failed:").Trim());
            //	}
            //	else if (logLines[i].Contains("Transfer successfully:"))
            //	{
            //		transferSuccessfully = Int32.Parse(logLines[i].GetSubstringAfterLastOccurence("Transfer successfully:").Trim());
            //	}
            //	else if (logLines[i].Contains("Total files transferred:"))
            //	{
            //		totalFilesTransferred = Int32.Parse(logLines[i].GetSubstringAfterLastOccurence("Total files transferred:").Trim());
            //	}

            //	if (totalFilesTransferred > -1 && transferSuccessfully > -1 && transferFailed > -1)
            //	{
            //		break;
            //	}
            //}

            //if (totalFilesTransferred == -1 || transferSuccessfully == -1 || transferFailed == -1)
            //{
            //	throw new Exception("Can not parse AzCopy result. Did it execute?");
            //}
            //else
            //{
            //	allSuccess = allSuccess && (totalFilesTransferred == transferSuccessfully && transferFailed == 0);
            //}

            return(allSuccess);
        }