public override Task <IEnumerable <RaftItem> > GetRaftItemsAsync(RaftItemType type) { switch (type) { case RaftItemType.ServerConfigurationPlan: case RaftItemType.RoleConfigurationPlan: case RaftItemType.OrchestrationPlan: break; case RaftItemType.Module: return(this.FindPlansAsync(Domains.PlanTypes.Module)); case RaftItemType.Script: return(this.FindScriptsAsync()); case RaftItemType.File: break; case RaftItemType.DeploymentPlan: return(this.FindPlansAsync(Domains.PlanTypes.Deployment)); case RaftItemType.TextTemplate: return(this.FindTextTemplatesAsync()); case RaftItemType.Pipeline: return(this.FindPipelinesAsync()); case RaftItemType.DeploymentSetTemplate: break; default: throw new NotImplementedException("Unhandled raft item type: " + type); } return(Task.FromResult(Enumerable.Empty <RaftItem>())); }
public sealed override Task DeleteRaftItemAsync(RaftItemType type, string name) { if (this.disposed) { throw new ObjectDisposedException(nameof(GitRaftRepositoryBase)); } if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException(name); } if (this.ReadOnly) { throw new NotSupportedException(); } EnsureRelativePath(name); var entry = this.FindEntry(type, name); if (entry != null) { this.lazyCurrentTree.Value.Remove(entry.Path); this.dirty = true; } return(InedoLib.NullTask); }
public override Task <RaftItem> GetRaftItemAsync(RaftItemType type, string name, string version) { if (this.disposed) { throw new ObjectDisposedException(nameof(GitRaftRepositoryBase)); } return(Task.FromResult(inner())); RaftItem inner() { var entry = this.FindEntry(type, name, version); if (entry != null) { long?size = null; if (entry.TargetType == TreeEntryTargetType.Blob) { var blob = (Blob)entry.Target; size = blob.Size; } Commit commit = null; if (string.IsNullOrEmpty(version)) { commit = this.lazyRepository.Value.Commits.QueryBy(entry.Path, new CommitFilter { IncludeReachableFrom = this.lazyRepository.Value.Branches[this.CurrentBranchName], FirstParentOnly = false, SortBy = CommitSortStrategies.Time }).FirstOrDefault()?.Commit; } else { commit = this.lazyRepository.Value.Lookup <Commit>(version); } if (commit != null) { return(new RaftItem(type, name, commit.Committer?.When ?? DateTimeOffset.Now, commit.Committer?.Name ?? "NA", size, commit.Id?.ToString() ?? "NA")); } else { // Handles situations where the commits are empty, even though the // file has been committed and pushed. There is likely a root cause, but // it is not known as of yet. return(new RaftItem(type, name, DateTimeOffset.Now)); } } return(null); } }
public override Task <Stream> OpenRaftItemAsync(RaftItemType type, string name, FileMode fileMode, FileAccess fileAccess, string version) { if (version != null) { throw new NotSupportedException("This raft does not support versioning."); } if (fileAccess == FileAccess.Read) { return(this.OpenRaftItemReadAsync(type, name, fileMode, version)); } throw new NotImplementedException(); }
public override Task <RaftItem> GetRaftItemAsync(RaftItemType type, string name, string version) { if (this.disposed) { throw new ObjectDisposedException(nameof(GitRaftRepositoryBase)); } return(Task.FromResult(inner())); RaftItem inner() { var entry = this.FindEntry(type, name, version); if (entry != null) { long?size = null; if (entry.TargetType == TreeEntryTargetType.Blob) { var blob = (Blob)entry.Target; size = blob.Size; } if (string.IsNullOrEmpty(version)) { var commit = this.lazyRepository.Value.Commits.QueryBy(entry.Path, new CommitFilter { IncludeReachableFrom = this.lazyRepository.Value.Branches[this.BranchName], FirstParentOnly = false, SortBy = CommitSortStrategies.Time }).FirstOrDefault()?.Commit; return(new RaftItem(type, name, commit?.Committer?.When ?? DateTimeOffset.Now, commit?.Committer?.Name, size, commit?.Id?.ToString())); } else { var commit = this.lazyRepository.Value.Lookup <Commit>(version); return(new RaftItem(type, name, commit.Committer.When, commit.Committer.Name, size, commit.Id.ToString())); } } return(null); } }
private Task <Stream> OpenRaftItemReadAsync(RaftItemType type, string name, FileMode fileMode, string version) { if (fileMode != FileMode.Open) { throw new NotSupportedException("FileMode must be Open for reading raft items."); } switch (type) { case RaftItemType.ServerConfigurationPlan: case RaftItemType.RoleConfigurationPlan: case RaftItemType.OrchestrationPlan: break; case RaftItemType.Module: return(this.OpenPlanReadAsync(Domains.PlanTypes.Module, name)); case RaftItemType.Script: return(this.OpenScriptReadAsync(name)); case RaftItemType.File: break; case RaftItemType.DeploymentPlan: return(this.OpenPlanReadAsync(Domains.PlanTypes.Deployment, name)); case RaftItemType.TextTemplate: return(this.OpenTextTemplateReadAsync(name)); case RaftItemType.Pipeline: return(this.OpenPipelineReadAsync(name)); case RaftItemType.DeploymentSetTemplate: break; default: throw new NotImplementedException("Unhandled raft item type: " + type); } throw new NotSupportedException("Unsupported raft item type: " + type); }
protected async Task <bool> RaftItemsEqualAsync(RaftRepository raft1, RaftRepository raft2, RaftItemType itemType, string itemName) { // This relies on raft items (usually) being pretty small. If that's no longer the case, we need to read the files in chunks instead. var contents = await Task.WhenAll( getItemContentsAsync(raft1), getItemContentsAsync(raft2) ); return(contents[0].SequenceEqual(contents[1])); async Task <byte[]> getItemContentsAsync(RaftRepository raft) { using (var stream = await raft.OpenRaftItemAsync(itemType, itemName, FileMode.Open, FileAccess.Read)) using (var memory = new MemoryStream()) { await stream.CopyToAsync(memory); return(memory.ToArray()); } } }
private TreeEntry FindEntry(RaftItemType type, string name, string hash = null) => this.FindEntry(PathEx.Combine('/', GetStandardTypeName(type), name), hash);
public override Task <IEnumerable <RaftItemVersion> > GetRaftItemVersionsAsync(RaftItemType type, string name) { if (this.disposed) { throw new ObjectDisposedException(nameof(GitRaftRepositoryBase)); } if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException(name); } return(Task.FromResult(inner())); IEnumerable <RaftItemVersion> inner() { var entry = this.FindEntry(type, name); if (entry != null) { var commits = this.lazyRepository.Value.Commits.QueryBy(entry.Path, new CommitFilter { IncludeReachableFrom = this.lazyRepository.Value.Branches[this.BranchName], FirstParentOnly = false, SortBy = CommitSortStrategies.Time }); foreach (var c in commits) { var commit = c.Commit; var committer = commit.Committer; yield return(new RaftItemVersion(commit.Id.ToString(), committer.When, committer.Name)); } } } }
public sealed override Task <Stream> OpenRaftItemAsync(RaftItemType type, string name, FileMode fileMode, FileAccess fileAccess, string version) { if (this.disposed) { throw new ObjectDisposedException(nameof(GitRaftRepositoryBase)); } if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException(nameof(name)); } if (this.ReadOnly && (fileAccess & FileAccess.Write) != 0) { throw new NotSupportedException(); } EnsureRelativePath(name); var itemPath = PathEx.Combine('/', GetStandardTypeName(type), name); if (!string.IsNullOrEmpty(this.RepositoryRoot)) { itemPath = PathEx.Combine('/', this.RepositoryRoot, itemPath); } var entry = this.FindEntry(type, name, version); return(Task.FromResult(inner())); Stream inner() { switch (fileMode) { case FileMode.CreateNew: if (entry != null) { throw new InvalidOperationException($"A {type} named \"{name}\" already exists in this raft."); } goto case FileMode.Create; case FileMode.Create: if ((fileAccess & FileAccess.Write) == 0) { throw new ArgumentOutOfRangeException(nameof(fileAccess)); } return(new RaftItemStream(new SlimMemoryStream(), this, itemPath, fileAccess, dirty: entry == null)); case FileMode.Open: if (entry == null) { return(null); } return(new RaftItemStream(this.OpenEntry(entry), this, itemPath, fileAccess)); case FileMode.OpenOrCreate: if (entry == null) { return(new RaftItemStream(new SlimMemoryStream(), this, itemPath, fileAccess)); } return(new RaftItemStream(this.OpenEntry(entry), this, itemPath, fileAccess, dirty: entry == null)); case FileMode.Truncate: if (entry == null) { return(null); } if ((fileAccess & FileAccess.Write) == 0) { throw new ArgumentOutOfRangeException(nameof(fileAccess)); } return(new RaftItemStream(new SlimMemoryStream(), this, itemPath, fileAccess, dirty: true)); case FileMode.Append: if ((fileAccess & FileAccess.Write) == 0) { throw new ArgumentOutOfRangeException(nameof(fileAccess)); } var s = new RaftItemStream(this.OpenEntry(entry), this, itemPath, fileAccess); s.Seek(0, SeekOrigin.End); return(s); default: throw new ArgumentOutOfRangeException(nameof(fileMode)); } } }
public override async Task DeleteRaftItemAsync(RaftItemType type, string name) { using (var db = new DB.Context()) { Tables.Plans_Extended plan; Tables.ScriptAssets script; Tables.TextTemplates tmpl; Tables.Pipelines pipeline; db.BeginTransaction(); switch (type) { case RaftItemType.ServerConfigurationPlan: case RaftItemType.RoleConfigurationPlan: case RaftItemType.OrchestrationPlan: break; case RaftItemType.Module: plan = await db.Plans_GetPlanByNameAsync(this.ApplicationId, name, Domains.PlanTypes.Module); if (plan == null || plan.Application_Id != this.ApplicationId) { return; } await db.Plans_DeletePlanAsync(plan.Plan_Id); db.CommitTransaction(); return; case RaftItemType.Script: script = await db.ScriptAssets_GetScriptByNameAsync(name, this.ApplicationId); if (script == null || script.Application_Id != this.ApplicationId) { return; } await db.ScriptAssets_DeleteScriptAsync(script.ScriptAsset_Id); db.CommitTransaction(); return; case RaftItemType.File: break; case RaftItemType.DeploymentPlan: plan = await db.Plans_GetPlanByNameAsync(this.ApplicationId, name, Domains.PlanTypes.Deployment); if (plan == null || plan.Application_Id != this.ApplicationId) { return; } await db.Plans_DeletePlanAsync(plan.Plan_Id); db.CommitTransaction(); return; case RaftItemType.TextTemplate: tmpl = await db.TextTemplates_GetTemplateByNameAsync(this.ApplicationId, name); if (tmpl == null || tmpl.Application_Id != this.ApplicationId) { return; } await db.TextTemplates_DeleteTemplateAsync(tmpl.TextTemplate_Id); db.CommitTransaction(); return; case RaftItemType.Pipeline: pipeline = (await db.Pipelines_GetPipelinesAsync(this.ApplicationId)) .FirstOrDefault(p => p.Application_Id == this.ApplicationId && p.Pipeline_Name == name); if (pipeline == null) { return; } await db.Pipelines_DeletePipelineAsync(pipeline.Pipeline_Id); db.CommitTransaction(); return; case RaftItemType.DeploymentSetTemplate: break; default: throw new NotImplementedException("Unhandled raft item type: " + type); } } throw new NotSupportedException("Unsupported raft item type: " + type); }