示例#1
0
        private List <CommandAzCopy> ExtractAzCopyCommandsFromIni()
        {
            List <CommandAzCopy> r = null;

            try
            {
                string iniFilePath = CraftSynth.BuildingBlocks.Common.Misc.ApplicationRootFolderPath + "AzCopyBatch.ini";
                var    lines       = File.ReadLines(iniFilePath);
                foreach (string line in lines)
                {
                    if (line.Trim().StartsWith("[Tasks]"))
                    {
                        r = new List <CommandAzCopy>();
                    }
                    else if (r != null && line.Trim().Length > 0)                    // && !line.StartsWith("--"))
                    {
                        var newCommand = CommandAzCopy.Parse(line, true, new CustomTraceLog());
                        if (newCommand != null)
                        {
                            r.Add(newCommand);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                AzCopyGui.HandlerForLoging.LogException(exception, this._appLog);
            }

            return(r);
        }
示例#2
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);
        }
示例#3
0
        private void RefreshControls(bool temporarlyDisableControls, bool useInfoFromTask, bool swapSourceAndDestination, bool repopulateBackupDates, bool regenerateDeleteCommand, bool regenerateAzCopyCommand)
        {
            DisableEvents();
            if (temporarlyDisableControls)
            {
                DisableControls();
            }
            this.lblPleaseWait.Visible = true;
            Application.DoEvents();

            string currentlySelectedTask = this.cbTasks.SelectedIndex < 0?string.Empty:(this.cbTasks.SelectedItem as CommandAzCopy).ToString();

            PopulateTasks();
            if (!string.IsNullOrEmpty(currentlySelectedTask))
            {
                try
                {
                    int i = 0;
                    foreach (var item in this.cbTasks.Items)
                    {
                        if ((item as CommandAzCopy).ToString() == currentlySelectedTask)
                        {
                            this.cbTasks.SelectedIndex = i;
                            break;
                        }
                        i++;
                    }
                }
                catch (Exception)
                {
                }
                Application.DoEvents();
            }


            CommandAzCopy task = null;

            if (useInfoFromTask && this.cbTasks.SelectedIndex >= 0)
            {
                task = this.cbTasks.SelectedItem as CommandAzCopy;

                this.tbSourceLocation.Text = task.DestinationLocation;
                this.tbSourceKey.Text      = task.DestinationKey;

                this.tbDestinationLocation.Text = task.SourceLocation;
                this.tbDestinationKey.Text      = task.SourceKey;

                Application.DoEvents();
            }

            if (swapSourceAndDestination)
            {
                string t = this.tbSourceLocation.Text;
                this.tbSourceLocation.Text      = this.tbDestinationLocation.Text;
                this.tbDestinationLocation.Text = t;

                t = this.tbSourceKey.Text;
                this.tbSourceKey.Text      = this.tbDestinationKey.Text;
                this.tbDestinationKey.Text = t;

                Application.DoEvents();
            }

            if (repopulateBackupDates)
            {
                DateTime?currentSourceDate = this.cbSourceDate.SelectedIndex < 0 ? (DateTime?)null : ((Backup)this.cbSourceDate.SelectedItem).Date;
                this.cbSourceDate.Items.Clear();
                List <KeyValuePair <object, DateTime> > pathsAndDates = new List <KeyValuePair <object, DateTime> >();
                if (!string.IsNullOrWhiteSpace(this.tbSourceLocation.Text) && this.tbSourceLocation.Text.Contains("[T]"))
                {
                    try
                    {
                        pathsAndDates = AzCopyBatch.HandlerForPaths.GetDestinationsFromDestinationWithWildcard(this.tbSourceLocation.Text, string.IsNullOrWhiteSpace(this.tbSourceKey.Text) ? null : this.tbSourceKey.Text);
                    }
                    catch (Exception e)
                    {
                        HandlerForLoging.LogException(e, this._appLog);
                    }
                }
                this.cbSourceDate.DisplayMember = "DateAsString";
                foreach (KeyValuePair <object, DateTime> pathAndDate in pathsAndDates)
                {
                    this.cbSourceDate.Items.Add(new Backup()
                    {
                        Date = pathAndDate.Value, PathOrUrl = pathAndDate.Key, Key = this.tbSourceKey.Text
                    });
                }
                if (currentSourceDate != null)
                {
                    try
                    {
                        int i = 0;
                        foreach (Backup item in this.cbSourceDate.Items)
                        {
                            if (item.Date == currentSourceDate)
                            {
                                this.cbSourceDate.SelectedIndex = i;
                                break;
                            }
                            i++;
                        }
                    }
                    catch (Exception)
                    {
                    }
                }

                Application.DoEvents();
            }

            this.tbDeleteDestinationCommand.Enabled = this.cbDeleteAllItemsAtDestination.Checked;
            //if (this.cbDeleteAllItemsAtDestination.Checked)
            //{
            //	CommandDelete deleteCommand = null;
            //	try
            //	{
            //		deleteCommand = new CommandDelete(this.tbDestinationLocation.Text, string.IsNullOrWhiteSpace(this.tbDestinationKey.Text) ? null : this.tbDestinationKey.Text, true);
            //	}
            //	catch (Exception e)
            //	{
            //		deleteCommand = null;
            //	}

            //	if (deleteCommand == null)
            //	{
            //		this.btnDeleteAllItemsAtDestination.Enabled = false;
            //	}
            //	else
            //	{
            //		this.tbDeleteDestinationCommand.Text = deleteCommand.ToString();
            //	}

            //	Application.DoEvents();
            //}
            if (regenerateDeleteCommand)
            {
                CommandDelete deleteCommand = null;
                try
                {
                    deleteCommand = new CommandDelete(this.tbDestinationLocation.Text,
                                                      string.IsNullOrWhiteSpace(this.tbDestinationKey.Text) ? null : this.tbDestinationKey.Text, true);
                }
                catch (Exception e)
                {
                    deleteCommand = null;
                }

                string destLocation = this.tbDestinationLocation.Text.ToNonNullNonEmptyString("{local-path-or-azure-storage-item-url}").Trim();
                if (deleteCommand == null)
                {
                    this.tbDeleteDestinationCommand.Text = "delete " + destLocation + " /DeleteOnlyContent";
                }
                else
                {
                    this.tbDeleteDestinationCommand.Text = deleteCommand.ToString();
                }

                bool destKeyPresent = this.tbDeleteDestinationCommand.Text.GetParameterPresence("/destKey", true, false, '/', ':');
                if (!destKeyPresent && (destLocation.ToLower().StartsWith("http://") || destLocation.ToLower().StartsWith("https://")))
                {
                    string destKey = this.tbDestinationKey.Text.ToNonNullNonEmptyString("{azure-storage-key}");
                    this.tbDeleteDestinationCommand.Text = this.tbDeleteDestinationCommand.Text.Trim() + " /destKey:" + destKey;
                }
            }

            if (regenerateAzCopyCommand)
            {
                //if (task != null)
                //{
                //	this.tbAzCopyCommand.Text = task.ToString();
                //}
                //else
                //{
                string sourceLocation = this.tbSourceLocation.Text.ToNonNullNonEmptyString("{local-path-or-azure-storage-item-url}").Trim();
                string destLocation   = this.tbDestinationLocation.Text.ToNonNullNonEmptyString("{local-path-or-azure-storage-item-url}").Trim();
                string sourceKey      = this.tbSourceKey.Text.ToNonNullNonEmptyString("{azure-storage-key}");
                string destKey        = this.tbDestinationKey.Text.ToNonNullNonEmptyString("{azure-storage-key}");
                this.tbAzCopyCommand.Text = string.Format("AzCopy {0} {1}", sourceLocation, destLocation);
                if (sourceLocation.ToLower().StartsWith("http://") || sourceLocation.ToLower().StartsWith("https://"))
                {
                    this.tbAzCopyCommand.Text = this.tbAzCopyCommand.Text.Trim() + " /sourceKey:" + sourceKey;
                }
                if (destLocation.ToLower().StartsWith("http://") || destLocation.ToLower().StartsWith("https://"))
                {
                    this.tbAzCopyCommand.Text = this.tbAzCopyCommand.Text.Trim() + " /destKey:" + destKey;
                }
                else
                {
                    if (destLocation != "{local-path-or-azure-storage-item-url}" && !Directory.Exists(destLocation))
                    {
                        string newQuestion = "Folder '" + destLocation + "' does not exist. Create?";
                        if (newQuestion != lastQuestion)
                        {
                            lastQuestion = newQuestion;
                            if (MessageBox.Show(lastQuestion, "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
                                DialogResult.Yes)
                            {
                                try
                                {
                                    DirectoryInfo di = Directory.CreateDirectory(destLocation);
                                    this._appLog.AddLine(string.Format("Folder '{0}' created.", di.FullName));
                                }
                                catch (Exception de)
                                {
                                    HandlerForLoging.LogException(de, this._appLog);
                                    this._appLog.AddLine("Failed to create folder.");
                                }
                            }
                        }
                    }
                }
                //}

                if (this.tbAzCopyCommand.Text.Contains("[T]") && !string.IsNullOrWhiteSpace(this.cbSourceDate.Text))
                {
                    this.tbAzCopyCommand.Text = tbAzCopyCommand.Text.Replace("[T]", this.cbSourceDate.Text);
                }

                if (!string.IsNullOrWhiteSpace(this.tbAzCopyCommand.Text) &&
                    !this.tbAzCopyCommand.Text.ToUpper().Contains("/S ") && !this.tbAzCopyCommand.Text.ToUpper().Trim().EndsWith("/S"))
                {
                    this.tbAzCopyCommand.Text = this.tbAzCopyCommand.Text.Trim() + " /S";
                }

                bool fixEmptyFoldersParam = !this.tbAzCopyCommand.Text.GetParameterPresence("/skipFixingEmptyFolders", true, false, '/', null);
                if (!fixEmptyFoldersParam && this.cbFixEmptyFolders.Checked)
                {
                    this.tbAzCopyCommand.Text = this.tbAzCopyCommand.Text.RemoveParameter("/skipFixingEmptyFolders", true, this.tbAzCopyCommand.Text, '/', '"', null);
                }
                else if (fixEmptyFoldersParam && !this.cbFixEmptyFolders.Checked)
                {
                    this.tbAzCopyCommand.Text = this.tbAzCopyCommand.Text.Trim() + " /skipFixingEmptyFolders";
                }
            }

            this.btnDeleteAllItemsAtDestination.Enabled = this.cbDeleteAllItemsAtDestination.Checked && !string.IsNullOrEmpty(this.tbDeleteDestinationCommand.Text);
            this.btnOverwriteDestination.Enabled        = !string.IsNullOrWhiteSpace(this.tbAzCopyCommand.Text);


            this._appLog.AddLine("Successfully refreshed controls.");
            this.lblPleaseWait.Visible = false;
            EnableEvents();
            if (temporarlyDisableControls)
            {
                EnableControls();
            }

            RefreshLog();
            Application.DoEvents();

            if (this._scheduledButton != null)
            {
                this._scheduledButton = null;
                this._scheduledButton.PerformClick();
            }
        }
示例#4
0
        private void btnOverwriteDestination_Click(object sender, EventArgs e)
        {
            CommandAzCopy commandAzCopy = null;

            try
            {
                commandAzCopy = CommandAzCopy.Parse(this.tbAzCopyCommand.Text, false, this._appLog);
            }
            catch (Exception ex)
            {
                HandlerForLoging.LogException(ex, this._appLog);
                //commandAzCopy = CommandAzCopy.Parse("azcopy {local-path-or-azure-storage-item-url}", null, true);
            }

            if (commandAzCopy != null)
            {
                if (commandAzCopy.SourceLocation.Contains("[T]"))
                {
                    MessageBox.Show("Source location can not contain wildcard [T]. Make sure that you selected date from dropdown box.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.tbSourceLocation.Focus();
                }
                else if ((!commandAzCopy.SourceLocation.ToLower().StartsWith("http://") &&
                          !commandAzCopy.SourceLocation.ToLower().StartsWith("https://")) &&
                         !Directory.Exists(commandAzCopy.SourceLocation))
                {
                    MessageBox.Show("Source location does not exist.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.tbSourceLocation.Focus();
                }
                else if (commandAzCopy.SourceLocation == "{local-path-or-azure-storage-item-url}")
                {
                    MessageBox.Show("Source location not specified.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.tbSourceLocation.Focus();
                }
                else if ((commandAzCopy.SourceLocation.ToLower().StartsWith("http://") ||
                          commandAzCopy.SourceLocation.ToLower().StartsWith("https://")) &&
                         (commandAzCopy.SourceKey == null || commandAzCopy.SourceKey == "{azure-storage-key}"))
                {
                    MessageBox.Show("Source key not specified.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.tbSourceKey.Focus();
                }
                else if ((!commandAzCopy.SourceLocation.ToLower().StartsWith("http://") &&
                          !commandAzCopy.SourceLocation.ToLower().StartsWith("https://")) &&
                         !string.IsNullOrEmpty(commandAzCopy.SourceKey))
                {
                    MessageBox.Show("Source key should not be specified for local folder.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.tbSourceKey.Focus();
                }
                else                 //-----------------
                if (commandAzCopy.DestinationLocation.Contains("[T]"))
                {
                    MessageBox.Show("Destination location can not contain wildcard [T].", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.tbDestinationLocation.Focus();
                }
                else if ((!commandAzCopy.DestinationLocation.ToLower().StartsWith("http://") &&
                          !commandAzCopy.DestinationLocation.ToLower().StartsWith("https://")) &&
                         !Directory.Exists(commandAzCopy.DestinationLocation))
                {
                    MessageBox.Show("Destination location does not exist.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.tbDestinationLocation.Focus();
                }
                else if (commandAzCopy.DestinationLocation == "{local-path-or-azure-storage-item-url}")
                {
                    MessageBox.Show("Destination location not specified.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.tbDestinationLocation.Focus();
                }
                else if ((commandAzCopy.DestinationLocation.ToLower().StartsWith("http://") ||
                          commandAzCopy.DestinationLocation.ToLower().StartsWith("https://")) &&
                         (commandAzCopy.DestinationKey == null || commandAzCopy.DestinationKey == "{azure-storage-key}"))
                {
                    MessageBox.Show("Destination key not specified.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.tbDestinationKey.Focus();
                }
                else if ((!commandAzCopy.DestinationLocation.ToLower().StartsWith("http://") &&
                          !commandAzCopy.DestinationLocation.ToLower().StartsWith("https://")) &&
                         !string.IsNullOrEmpty(commandAzCopy.DestinationKey))
                {
                    MessageBox.Show("Destination key should not be specified for local folder.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.tbDestinationKey.Focus();
                }
                else
                {
                    if (MessageBox.Show("This will overwrite '" + commandAzCopy.DestinationLocation + "'. Proceed?", "Confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
                    {
                        try
                        {
                            bool r = AzCopyBatch.HandlerForTask_AzCopy.Execute(this._appLog, 0, this.tbAzCopyCommand.Text, DateTime.Now.ToDateAndTimeInSortableFormatForAzureBlob() + "_Manual_", 30);
                            this._appLog.AddLine(r ? "Operation succeeded!" : "Operation failed!");
                        }
                        catch (Exception exx)
                        {
                            HandlerForLoging.LogException(exx, this._appLog);
                            this._appLog.AddLine("Operation failed.");
                        }
                    }
                }
            }
        }