/// <summary>
        /// An impersonation safe way to get the time for which to base aging
        /// </summary>
        /// <param name="targetSource">The target of the time</param>
        /// <returns>The time to use for ageing sources</returns>
        public virtual DateTime GetAgeBasis(string targetSource)
        {
            try
            {
                STEM.Sys.IO.Listing.FileInfo fi = null;

                try
                {
                    fi = GetFileInfo(targetSource);
                }
                catch { }

                if (fi == null)
                {
                    STEM.Sys.IO.Listing.DirectoryInfo di = GetDirectoryInfo(targetSource);

                    if (di == null)
                    {
                        return(DateTime.MinValue);
                    }

                    switch (SelectedOrigin)
                    {
                    case STEM.Surge.AgeOrigin.LastWriteTime:
                        return(di.LastWriteTimeUtc);

                    case STEM.Surge.AgeOrigin.LastAccessTime:
                        return(di.LastAccessTimeUtc);

                    case STEM.Surge.AgeOrigin.CreationTime:
                        return(di.CreationTimeUtc);
                    }
                }
                else
                {
                    switch (SelectedOrigin)
                    {
                    case STEM.Surge.AgeOrigin.LastWriteTime:
                        return(fi.LastWriteTimeUtc);

                    case STEM.Surge.AgeOrigin.LastAccessTime:
                        return(fi.LastAccessTimeUtc);

                    case STEM.Surge.AgeOrigin.CreationTime:
                        return(fi.CreationTimeUtc);
                    }
                }
            }
            catch { }

            return(DateTime.MinValue);
        }
예제 #2
0
        bool Execute()
        {
            try
            {
                if (Direction == S3Direction.ToS3Bucket)
                {
                    ExpandDestination = false;
                }
                else
                {
                    ExpandSource = false;
                }

                List <string> sources = new List <string>();
                if (ExpandSource)
                {
                    sources = STEM.Sys.IO.Path.ExpandRangedPath(SourcePath);
                }
                else
                {
                    sources.Add(SourcePath);
                }

                List <string> destinations = new List <string>();
                if (ExpandDestination)
                {
                    destinations = STEM.Sys.IO.Path.ExpandRangedPath(DestinationPath);
                }
                else
                {
                    destinations.Add(DestinationPath);
                }

                List <string> sourceFiles = new List <string>();

                int filesActioned = 0;

                foreach (string src in sources)
                {
                    List <S3Object> items = new List <S3Object>();

                    if (Direction == S3Direction.ToS3Bucket)
                    {
                        sourceFiles = STEM.Sys.IO.Directory.STEM_GetFiles(src, FileFilter, DirectoryFilter, (RecurseSource ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly), false);
                    }
                    else
                    {
                        string bucket = Authentication.BucketFromPath(src);
                        string prefix = Authentication.PrefixFromPath(src);
                        if (PopulatePostMortemMeta)
                        {
                            PostMortemMetaData["Bucket"] = bucket;
                            PostMortemMetaData["Prefix"] = prefix;
                        }

                        items = Authentication.ListObjects(bucket, prefix, STEM.Sys.IO.Listing.ListingType.File, RecurseSource, DirectoryFilter, FileFilter);

                        sourceFiles = items.Select(i => Authentication.ToString(i)).ToList();
                    }

                    foreach (string s in sourceFiles)
                    {
                        try
                        {
                            bool success = false;

                            Exception lastEX = null;

                            foreach (string d in destinations)
                            {
                                try
                                {
                                    string dFile = "";

                                    long fileSz = 0;

                                    if (Direction == S3Direction.ToS3Bucket)
                                    {
                                        fileSz = new FileInfo(s).Length;
                                    }
                                    else
                                    {
                                        fileSz = Authentication.GetFileInfo(s).Size;
                                    }

                                    _PartSize[s] = (int)Math.Max(MinimumPartSize, fileSz / MaximumNumberOfParts);

                                    try
                                    {
                                        if (PopulatePostMortemMeta)
                                        {
                                            PostMortemMetaData["SourceIP"]      = STEM.Sys.IO.Path.IPFromPath(s);
                                            PostMortemMetaData["DestinationIP"] = STEM.Sys.IO.Path.IPFromPath(d);
                                            PostMortemMetaData["FileSize"]      = fileSz.ToString();
                                        }
                                    }
                                    catch { }

                                    if (Direction == S3Direction.ToS3Bucket)
                                    {
                                        string dPath = STEM.Sys.IO.Path.AdjustPath(d);
                                        if (RecurseSource && RecreateTree)
                                        {
                                            dPath = System.IO.Path.Combine(dPath, STEM.Sys.IO.Path.GetDirectoryName(s).Replace(STEM.Sys.IO.Path.AdjustPath(src), "").Trim(System.IO.Path.DirectorySeparatorChar));
                                        }

                                        dPath = System.IO.Path.Combine(dPath, DestinationFilename);

                                        if (dPath.Contains("*.*"))
                                        {
                                            dPath = dPath.Replace("*.*", STEM.Sys.IO.Path.GetFileName(s));
                                        }

                                        if (dPath.Contains("*"))
                                        {
                                            dPath = dPath.Replace("*", STEM.Sys.IO.Path.GetFileNameWithoutExtension(s));
                                        }

                                        if (!Authentication.DirectoryExists(STEM.Sys.IO.Path.GetDirectoryName(dPath)))
                                        {
                                            Authentication.CreateDirectory(STEM.Sys.IO.Path.GetDirectoryName(dPath));
                                        }

                                        if (Authentication.FileExists(dPath))
                                        {
                                            switch (ExistsAction)
                                            {
                                            case Sys.IO.FileExistsAction.Overwrite:
                                                Authentication.DeleteFile(dPath);
                                                dFile = dPath;
                                                break;

                                            case Sys.IO.FileExistsAction.OverwriteIfNewer:

                                                if (Authentication.GetFileInfo(dPath).LastWriteTimeUtc >= File.GetLastWriteTimeUtc(s))
                                                {
                                                    continue;
                                                }

                                                Authentication.DeleteFile(dPath);
                                                dFile = dPath;
                                                break;

                                            case Sys.IO.FileExistsAction.Skip:
                                                continue;

                                            case Sys.IO.FileExistsAction.Throw:
                                                throw new IOException("Destination file exists. (" + dPath + ")");

                                            case Sys.IO.FileExistsAction.MakeUnique:
                                                dFile = Authentication.UniqueFilename(dPath);
                                                break;
                                            }
                                        }
                                        else
                                        {
                                            dFile = dPath;
                                        }

                                        string bucket = Authentication.BucketFromPath(dFile);
                                        string prefix = Authentication.PrefixFromPath(dFile);
                                        if (PopulatePostMortemMeta)
                                        {
                                            PostMortemMetaData["Bucket"] = bucket;
                                            PostMortemMetaData["Prefix"] = prefix;
                                        }

                                        TransferUtilityUpload(bucket, prefix, s, s);
                                    }
                                    else
                                    {
                                        string dPath = STEM.Sys.IO.Path.AdjustPath(d);
                                        if (RecurseSource && RecreateTree)
                                        {
                                            dPath = System.IO.Path.Combine(dPath, STEM.Sys.IO.Path.GetDirectoryName(s).Replace(STEM.Sys.IO.Path.AdjustPath(src), "").Trim(System.IO.Path.DirectorySeparatorChar));
                                        }

                                        dPath = System.IO.Path.Combine(dPath, DestinationFilename);

                                        if (dPath.Contains("*.*"))
                                        {
                                            dPath = dPath.Replace("*.*", STEM.Sys.IO.Path.GetFileName(s));
                                        }

                                        if (dPath.Contains("*"))
                                        {
                                            dPath = dPath.Replace("*", STEM.Sys.IO.Path.GetFileNameWithoutExtension(s));
                                        }

                                        if (File.Exists(dPath))
                                        {
                                            switch (ExistsAction)
                                            {
                                            case Sys.IO.FileExistsAction.Overwrite:
                                                File.Delete(dPath);
                                                dFile = dPath;
                                                break;

                                            case Sys.IO.FileExistsAction.OverwriteIfNewer:

                                                if (File.GetLastWriteTimeUtc(dPath) >= Authentication.GetFileInfo(s).LastWriteTimeUtc)
                                                {
                                                    continue;
                                                }

                                                File.Delete(dPath);
                                                dFile = dPath;
                                                break;

                                            case Sys.IO.FileExistsAction.Skip:
                                                continue;

                                            case Sys.IO.FileExistsAction.Throw:
                                                throw new IOException("Destination file exists. (" + dPath + ")");

                                            case Sys.IO.FileExistsAction.MakeUnique:
                                                dFile = STEM.Sys.IO.File.UniqueFilename(dPath);
                                                break;
                                            }
                                        }
                                        else
                                        {
                                            dFile = dPath;
                                        }

                                        if (!Directory.Exists(STEM.Sys.IO.Path.GetDirectoryName(dFile)))
                                        {
                                            Directory.CreateDirectory(STEM.Sys.IO.Path.GetDirectoryName(dFile));
                                        }

                                        string bucket = Authentication.BucketFromPath(s);
                                        string prefix = Authentication.PrefixFromPath(s);
                                        if (PopulatePostMortemMeta)
                                        {
                                            PostMortemMetaData["Bucket"] = bucket;
                                            PostMortemMetaData["Prefix"] = prefix;
                                        }

                                        STEM.Sys.IO.Listing.FileInfo fi = Authentication.GetFileInfo(s);

                                        if (fi != null)
                                        {
                                            string tmp = "";
                                            try
                                            {
                                                tmp = Path.Combine(STEM.Sys.IO.Path.GetDirectoryName(dFile), "TEMP");

                                                if (!Directory.Exists(tmp))
                                                {
                                                    Directory.CreateDirectory(tmp);
                                                }

                                                tmp = Path.Combine(tmp, STEM.Sys.IO.Path.GetFileName(dFile));

                                                TransferUtilityDownload(bucket, prefix, tmp);

                                                try
                                                {
                                                    File.SetLastWriteTimeUtc(tmp, fi.LastWriteTimeUtc);
                                                }
                                                catch { }

                                                File.Move(tmp, STEM.Sys.IO.Path.AdjustPath(dFile));
                                            }
                                            finally
                                            {
                                                try
                                                {
                                                    if (File.Exists(tmp))
                                                    {
                                                        File.Delete(tmp);
                                                    }
                                                }
                                                catch { }
                                            }
                                        }
                                        else
                                        {
                                            throw new System.IO.FileNotFoundException(s);
                                        }
                                    }

                                    if (!String.IsNullOrEmpty(dFile))
                                    {
                                        filesActioned++;

                                        _FilesActioned[s] = dFile;

                                        if (Action == ActionType.Move)
                                        {
                                            AppendToMessage(s + " moved to " + dFile);
                                        }
                                        else
                                        {
                                            AppendToMessage(s + " copied to " + dFile);
                                        }
                                    }

                                    success = true;

                                    if (DestinationActionRule == DestinationRule.FirstSuccess)
                                    {
                                        break;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    lastEX = ex;

                                    if (DestinationActionRule == DestinationRule.AllOrNone)
                                    {
                                        throw ex;
                                    }
                                }
                            }

                            if (!success)
                            {
                                throw new Exception("No successful actions taken for " + s, lastEX); // + "\r\n" + ((lastEX == null) ? "No additional information." : lastEX.ToString()));
                            }
                            if (Action == ActionType.Move)
                            {
                                if (Direction == S3Direction.ToS3Bucket)
                                {
                                    File.Delete(s);
                                }
                                else
                                {
                                    Authentication.DeleteFile(s);
                                }
                            }
                        }
                        catch (AggregateException ex)
                        {
                            foreach (Exception e in ex.InnerExceptions)
                            {
                                AppendToMessage(e.Message);
                            }
                            Exceptions.Add(ex);                             //add the entire message to the collection to maintain the top level exception's stack
                        }
                        catch (Exception ex)
                        {
                            AppendToMessage(ex.Message);
                            Exceptions.Add(ex);
                        }
                    }
                }

                if (PopulatePostMortemMeta)
                {
                    PostMortemMetaData["FilesActioned"] = filesActioned.ToString();
                }
            }
            catch (AggregateException ex)
            {
                foreach (Exception e in ex.InnerExceptions)
                {
                    AppendToMessage(e.Message);
                }
                Exceptions.Add(ex); //add the entire message to the collection to maintain the top level exception's stack
            }
            catch (Exception ex)
            {
                AppendToMessage(ex.Message);
                Exceptions.Add(ex);
            }

            if (_FilesActioned.Count == 0)
            {
                switch (ZeroFilesAction)
                {
                case FailureAction.SkipRemaining:
                    SkipRemaining();
                    return(true);

                case FailureAction.SkipNext:
                    SkipNext();
                    return(true);

                case FailureAction.SkipToLabel:
                    SkipForwardToFlowControlLabel(FailureActionLabel);
                    return(true);

                case FailureAction.Rollback:
                    RollbackAllPreceedingAndSkipRemaining();
                    break;

                case FailureAction.Continue:
                    return(true);
                }

                Message = "0 Files Actioned\r\n" + Message;
            }

            return(Exceptions.Count == 0);
        }
        /// <summary>
        /// Customize an iSetTemplate by applying the TemplateKVP map to the InstructionsXml
        /// </summary>
        /// <param name="iSetTemplate">A clone of the template to be modified in this method</param>
        /// <param name="map">The TemplateKVP map used to modify the iSetTemplate</param>
        /// <param name="branchIP">The branchIP this will be assigned to</param>
        /// <param name="initiationSource">The initiationSource passed in to GenerateDeploymentDetails()</param>
        /// <param name="cloneMap">Should the map be cloned as it will be modified in this method</param>
        public override void CustomizeInstructionSet(_InstructionSet iSetTemplate, System.Collections.Generic.Dictionary <string, string> map, string branchIP, string initiationSource, bool cloneMap = true)
        {
            if (iSetTemplate == null)
            {
                throw new ArgumentNullException(nameof(iSetTemplate));
            }

            if (map == null)
            {
                throw new ArgumentNullException(nameof(map));
            }

            if (String.IsNullOrEmpty(branchIP))
            {
                throw new ArgumentNullException(nameof(branchIP));
            }

            if (String.IsNullOrEmpty(initiationSource))
            {
                throw new ArgumentNullException(nameof(initiationSource));
            }

            System.Collections.Generic.Dictionary <string, string> kvp = map;

            if (cloneMap)
            {
                kvp = new System.Collections.Generic.Dictionary <string, string>(map);
            }

            kvp["[TargetPath]"] = STEM.Sys.IO.Path.GetDirectoryName(initiationSource);

            if (!String.IsNullOrEmpty(kvp["[TargetPath]"]))
            {
                kvp["[TargetDirectoryName]"]  = STEM.Sys.IO.Path.GetFileName(kvp["[TargetPath]"]);
                kvp["[TargetName]"]           = STEM.Sys.IO.Path.GetFileName(initiationSource);
                kvp["[TargetNameWithoutExt]"] = STEM.Sys.IO.Path.GetFileNameWithoutExtension(initiationSource);
                kvp["[TargetExt]"]            = STEM.Sys.IO.Path.GetExtension(initiationSource);
            }
            else
            {
                kvp["[TargetPath]"]            = "";
                kvp["[TargetDirectoryName]"]   = "";
                kvp["[TargetName]"]            = "";
                kvp["[TargeteNameWithoutExt]"] = "";
                kvp["[TargetExt]"]             = "";
            }

            kvp["[SourceAddress]"]     = STEM.Sys.IO.Path.IPFromPath(initiationSource);
            kvp["[SourceMachineName]"] = STEM.Sys.IO.Net.MachineName(kvp["[SourceAddress]"]);

            string xml = iSetTemplate.SerializationSourceInstructionDocument;

            bool getFileInfo = false;

            if (xml.IndexOf("[LastWriteTimeUtc]", StringComparison.InvariantCultureIgnoreCase) >= 0)
            {
                getFileInfo = true;
            }

            if (getFileInfo == false)
            {
                if (xml.IndexOf("[LastAccessTimeUtc]", StringComparison.InvariantCultureIgnoreCase) >= 0)
                {
                    getFileInfo = true;
                }
            }

            if (getFileInfo == false)
            {
                if (xml.IndexOf("[CreationTimeUtc]", StringComparison.InvariantCultureIgnoreCase) >= 0)
                {
                    getFileInfo = true;
                }
            }

            if (getFileInfo == false)
            {
                if (xml.IndexOf("[FileSize]", StringComparison.InvariantCultureIgnoreCase) >= 0)
                {
                    getFileInfo = true;
                }
            }

            if (getFileInfo == false)
            {
                foreach (string k in kvp.Keys)
                {
                    if (kvp[k] != null)
                    {
                        if (kvp[k].IndexOf("[LastWriteTimeUtc]", StringComparison.InvariantCultureIgnoreCase) >= 0)
                        {
                            getFileInfo = true;
                            break;
                        }
                        if (kvp[k].IndexOf("[LastAccessTimeUtc]", StringComparison.InvariantCultureIgnoreCase) >= 0)
                        {
                            getFileInfo = true;
                            break;
                        }
                        if (kvp[k].IndexOf("[CreationTimeUtc]", StringComparison.InvariantCultureIgnoreCase) >= 0)
                        {
                            getFileInfo = true;
                            break;
                        }
                        if (kvp[k].IndexOf("[FileSize]", StringComparison.InvariantCultureIgnoreCase) >= 0)
                        {
                            getFileInfo = true;
                            break;
                        }
                    }
                }
            }

            if (getFileInfo)
            {
                STEM.Sys.IO.Listing.FileInfo info = GetFileInfo(initiationSource);

                if (info == null)
                {
                    info = new STEM.Sys.IO.Listing.FileInfo {
                        CreationTimeUtc = DateTime.MinValue, LastAccessTimeUtc = DateTime.MinValue, LastWriteTimeUtc = DateTime.MinValue, Size = 0
                    }
                }
                ;

                foreach (string key in kvp.Keys.Where(i => i.Equals("[LastWriteTimeUtc]", StringComparison.InvariantCultureIgnoreCase)).ToList())
                {
                    try
                    {
                        kvp[key] = info.LastWriteTimeUtc.ToString(kvp[key], System.Globalization.CultureInfo.CurrentCulture);
                    }
                    catch { }
                }

                foreach (string key in kvp.Keys.Where(i => i.Equals("[LastAccessTimeUtc]", StringComparison.InvariantCultureIgnoreCase)).ToList())
                {
                    try
                    {
                        kvp[key] = info.LastAccessTimeUtc.ToString(kvp[key], System.Globalization.CultureInfo.CurrentCulture);
                    }
                    catch { }
                }

                foreach (string key in kvp.Keys.Where(i => i.Equals("[CreationTimeUtc]", StringComparison.InvariantCultureIgnoreCase)).ToList())
                {
                    try
                    {
                        kvp[key] = info.CreationTimeUtc.ToString(kvp[key], System.Globalization.CultureInfo.CurrentCulture);
                    }
                    catch { }
                }

                foreach (string key in kvp.Keys.Where(i => i.Equals("[FileSize]", StringComparison.InvariantCultureIgnoreCase)).ToList())
                {
                    try
                    {
                        kvp[key] = info.Size.ToString();
                    }
                    catch { }
                }
            }

            string subDir = null;

            if (!String.IsNullOrEmpty(RecreateSubFromRootOf) && RecreateSubFromRootOf.Trim().Length > 0)
            {
                if (initiationSource.ToUpper(System.Globalization.CultureInfo.CurrentCulture).IndexOf(RecreateSubFromRootOf.ToUpper(System.Globalization.CultureInfo.CurrentCulture), StringComparison.InvariantCultureIgnoreCase) >= 0)
                {
                    int startIndex = initiationSource.ToUpper(System.Globalization.CultureInfo.CurrentCulture).IndexOf(RecreateSubFromRootOf.ToUpper(System.Globalization.CultureInfo.CurrentCulture), StringComparison.InvariantCultureIgnoreCase) + RecreateSubFromRootOf.Length + 1;

                    if (startIndex < STEM.Sys.IO.Path.GetDirectoryName(initiationSource).Length)
                    {
                        subDir = initiationSource.Substring(startIndex, STEM.Sys.IO.Path.GetDirectoryName(initiationSource).Length - startIndex);
                    }
                }
            }

            if (subDir == null)
            {
                subDir = "";
            }

            kvp["[SubDir]"] = subDir;

            base.CustomizeInstructionSet(iSetTemplate, kvp, branchIP, initiationSource, false);
        }
예제 #4
0
        protected override void _Rollback()
        {
            if (ExecutionMode == ExecuteOn.ForwardExecution)
            {
                foreach (string d in _FilesActioned.Keys)
                {
                    try
                    {
                        string s = _FilesActioned[d];

                        if (Action == ActionType.Move)
                        {
                            if (Direction == S3Direction.ToS3Bucket)
                            {
                                string bucket = Authentication.BucketFromPath(s);
                                string prefix = Authentication.PrefixFromPath(s);

                                STEM.Sys.IO.Listing.FileInfo fi = Authentication.GetFileInfo(s);

                                if (fi != null)
                                {
                                    string tmp = "";

                                    try
                                    {
                                        tmp = Path.Combine(STEM.Sys.IO.Path.GetDirectoryName(d), "TEMP");

                                        if (!Directory.Exists(tmp))
                                        {
                                            Directory.CreateDirectory(tmp);
                                        }

                                        tmp = Path.Combine(tmp, STEM.Sys.IO.Path.GetFileName(d));

                                        TransferUtilityDownload(bucket, prefix, tmp);

                                        try
                                        {
                                            File.SetLastWriteTimeUtc(tmp, fi.LastWriteTimeUtc);
                                        }
                                        catch { }

                                        File.Move(tmp, STEM.Sys.IO.Path.AdjustPath(d));
                                    }
                                    finally
                                    {
                                        try
                                        {
                                            if (File.Exists(tmp))
                                            {
                                                File.Delete(tmp);
                                            }
                                        }
                                        catch { }
                                    }
                                }
                                else
                                {
                                    throw new System.IO.FileNotFoundException(s);
                                }
                            }
                            else
                            {
                                string bucket = Authentication.BucketFromPath(d);
                                string prefix = Authentication.PrefixFromPath(d);

                                TransferUtilityUpload(bucket, prefix, s, d);
                            }
                        }

                        if (Direction == S3Direction.ToS3Bucket)
                        {
                            Authentication.DeleteFile(s);
                        }
                        else
                        {
                            STEM.Sys.IO.File.STEM_Delete(s, false, Retry, RetryDelaySeconds);
                        }
                    }
                    catch { }
                }
            }
            else
            {
                Execute();
            }
        }