Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
Initial take on refactoring server info
Browse files Browse the repository at this point in the history
  • Loading branch information
davidfowl committed Feb 3, 2012
1 parent e2d12c8 commit a3aeda1
Show file tree
Hide file tree
Showing 17 changed files with 244 additions and 110 deletions.
7 changes: 7 additions & 0 deletions Kudu.Contracts/Deployment/IDeploymentManagerFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Kudu.Core.Deployment
{
public interface IDeploymentManagerFactory
{
IDeploymentManager CreateDeploymentManager();
}
}
3 changes: 3 additions & 0 deletions Kudu.Contracts/Kudu.Contracts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<Compile Include="Deployment\DeployResult.cs" />
<Compile Include="Deployment\DeployStatus.cs" />
<Compile Include="Deployment\IDeploymentManager.cs" />
<Compile Include="Deployment\IDeploymentManagerFactory.cs" />
<Compile Include="Deployment\LogEntry.cs" />
<Compile Include="Deployment\LogEntryType.cs" />
<Compile Include="Editor\IProjectSystem.cs" />
Expand All @@ -73,7 +74,9 @@
<Compile Include="SourceControl\IClonableRepository.cs" />
<Compile Include="SourceControl\IRepository.cs" />
<Compile Include="SourceControl\IRepositoryManager.cs" />
<Compile Include="SourceControl\IServerRepository.cs" />
<Compile Include="SourceControl\LineDiff.cs" />
<Compile Include="SourceControl\PushInfo.cs" />
<Compile Include="SourceControl\RepositoryType.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
2 changes: 1 addition & 1 deletion Kudu.Contracts/SourceControl/Git/IGitServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Kudu.Core.SourceControl.Git
{
public interface IGitServer
public interface IGitServer : IServerRepository
{
void AdvertiseUploadPack(Stream output);
void AdvertiseReceivePack(Stream output);
Expand Down
12 changes: 12 additions & 0 deletions Kudu.Contracts/SourceControl/IServerRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Kudu.Core.SourceControl
{
public interface IServerRepository
{
string CurrentId { get; }
PushInfo GetPushInfo();
void Initialize();
ChangeSet GetChangeSet(string id);
void Update(string id);
void Update();
}
}
9 changes: 9 additions & 0 deletions Kudu.Contracts/SourceControl/PushInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Kudu.Core.SourceControl
{
public class PushInfo
{
public string OldId { get; set; }
public string NewId { get; set; }
public Branch Branch { get; set; }
}
}
68 changes: 38 additions & 30 deletions Kudu.Core/Deployment/DeploymentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Kudu.Core.Deployment
{
public class DeploymentManager : IDeploymentManager
{
private readonly IRepositoryManager _repositoryManager;
private readonly IServerRepository _serverRepository;
private readonly ISiteBuilderFactory _builderFactory;
private readonly IEnvironment _environment;
private readonly IDeploymentSettingsManager _settingsManager;
Expand All @@ -21,14 +21,14 @@ public class DeploymentManager : IDeploymentManager

public event Action<DeployResult> StatusChanged;

public DeploymentManager(IRepositoryManager repositoryManager,
public DeploymentManager(IServerRepository serverRepository,
ISiteBuilderFactory builderFactory,
IEnvironment environment,
IDeploymentSettingsManager settingsManager,
IFileSystem fileSystem,
IProfilerFactory profilerFactory)
{
_repositoryManager = repositoryManager;
_serverRepository = serverRepository;
_builderFactory = builderFactory;
_environment = environment;
_settingsManager = settingsManager;
Expand Down Expand Up @@ -152,13 +152,6 @@ public void Deploy(string id)
{
deployStep = profiler.Step("DeploymentManager.Deploy(id)");

IRepository repository = _repositoryManager.GetRepository();

if (repository == null)
{
return;
}

// Check to see if we have a deployment with this id already
string trackingFilePath = GetTrackingFilePath(id);

Expand All @@ -171,7 +164,7 @@ public void Deploy(string id)
using (profiler.Step("Updating to specific changeset"))
{
// Update to the the specific changeset
repository.Update(id);
_serverRepository.Update(id);
}

// Perform the build deployment of this changeset
Expand All @@ -189,44 +182,46 @@ public void Deploy(string id)

public void Deploy()
{
IRepository repository = _repositoryManager.GetRepository();

if (repository == null)
{
return;
}

var profiler = _profilerFactory.GetProfiler();
IDisposable deployStep = null;

try
{
deployStep = profiler.Step("Deploy");
PushInfo pushInfo = _serverRepository.GetPushInfo();

// Store the current head
ChangeSet lastChange = repository.GetChanges(0, 1).Single();
string id = lastChange.Id;
// Something went wrong here since we weren't able to
if (pushInfo == null || !pushInfo.Branch.IsMaster)
{
ReportCompleted();
return;
}

using (profiler.Step("Update to specific changeset"))
{
// Update to the default branch
repository.Update();
_serverRepository.Update();
}

// The default branch wasn't pushed then noop
if (id != repository.CurrentId)
{
return;
}
// Get the pointer to the default branch
string id = _serverRepository.CurrentId;

// If nothing changed then do nothing
if (id.Equals(ActiveDeploymentId, StringComparison.OrdinalIgnoreCase))
{
ReportCompleted();
return;
}

using (profiler.Step("Collecting changeset information"))
{
// Create the tracking file and store information about the commit
DeploymentStatusFile statusFile = CreateTrackingFile(id);
statusFile.Id = id;
statusFile.Message = lastChange.Message;
statusFile.Author = lastChange.AuthorName;
statusFile.AuthorEmail = lastChange.AuthorEmail;
ChangeSet changeSet = _serverRepository.GetChangeSet(id);
statusFile.Message = changeSet.Message;
statusFile.Author = changeSet.AuthorName;
statusFile.AuthorEmail = changeSet.AuthorEmail;
statusFile.Save(_fileSystem);
}

Expand All @@ -238,6 +233,8 @@ public void Deploy()
{
deployStep.Dispose();
}

ReportCompleted();
}
}

Expand Down Expand Up @@ -484,6 +481,17 @@ private void ReportStatus(string id)
}
}

private void ReportCompleted()
{
if (StatusChanged != null)
{
StatusChanged(new DeployResult
{
Complete = true
});
}
}

private DeploymentStatusFile OpenTrackingFile(string id)
{
return DeploymentStatusFile.Open(_fileSystem, GetTrackingFilePath(id));
Expand Down
8 changes: 0 additions & 8 deletions Kudu.Core/SourceControl/Git/GitExeRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,7 @@ public void Initialize()
{
_gitExe.Execute("init");

InitializePush();
}

public void InitializePush()
{
_gitExe.Execute("config core.autocrlf true");

// Allow getting pushes even though we're not bare
_gitExe.Execute("config receive.denyCurrentBranch ignore");
}

public IEnumerable<FileStatus> GetStatus()
Expand Down
100 changes: 97 additions & 3 deletions Kudu.Core/SourceControl/Git/GitExeServer.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
using System.IO;
using Kudu.Core.Infrastructure;
using System;
using System.IO;
using System.Linq;
using Kudu.Contracts;
using Kudu.Core.Infrastructure;
using Kudu.Core.Performance;

namespace Kudu.Core.SourceControl.Git
{
public class GitExeServer : IGitServer
public class GitExeServer : IGitServer, IServerRepository
{
private readonly Executable _gitExe;
private readonly IProfilerFactory _profilerFactory;
private readonly GitExeRepository _repository;
private readonly Action _initialize;

public GitExeServer(string path, IProfilerFactory profilerFactory)
: this(GitUtility.ResolveGitPath(), path, profilerFactory)
Expand All @@ -19,6 +23,24 @@ public GitExeServer(string pathToGitExe, string path, IProfilerFactory profilerF
{
_gitExe = new Executable(pathToGitExe, path);
_profilerFactory = profilerFactory;
_repository = new GitExeRepository(path);
_initialize = () => new LibGitRepository(path).Initialize();
}

private string PostReceiveHookPath
{
get
{
return Path.Combine(_gitExe.WorkingDirectory, ".git", "hooks", "post-receive");
}
}

public string CurrentId
{
get
{
return _repository.CurrentId;
}
}

public void AdvertiseReceivePack(Stream output)
Expand Down Expand Up @@ -66,5 +88,77 @@ private void ServiceRpc(string serviceName, Stream input, Stream output)
{
_gitExe.Execute(input, output, @"{0} --stateless-rpc ""{1}""", serviceName, _gitExe.WorkingDirectory);
}

public PushInfo GetPushInfo()
{
string path = Path.Combine(_gitExe.WorkingDirectory, ".git", "pushinfo");

if (!File.Exists(path))
{
return null;
}

string[] pushDetails = File.ReadAllText(path).Split(' ');

if (pushDetails.Length == 3)
{
string oldId = pushDetails[0];
string newId = pushDetails[1];
string reference = pushDetails[2];
string branch = reference.Split('/').Last().Trim();

return new PushInfo
{
OldId = oldId,
NewId = newId,
Branch = new GitBranch(newId, branch, false)
};
}

return null;
}

public void Initialize()
{
IProfiler profiler = _profilerFactory.GetProfiler();
using (profiler.Step("GitExeServer.Initialize"))
{
// If we already have a repository then do nothing
RepositoryType repositoryType = RepositoryManager.GetRepositoryType(_gitExe.WorkingDirectory);
if (repositoryType == RepositoryType.Git)
{
return;
}

// Initialize using LibGit2Sharp
_initialize();

// Allow getting pushes even though we're not bare
_gitExe.Execute("config receive.denyCurrentBranch ignore");

FileSystemHelpers.EnsureDirectory(Path.GetDirectoryName(PostReceiveHookPath));

File.WriteAllText(PostReceiveHookPath, @"#!/bin/sh
read i
echo $i > pushinfo
echo ""Queued for deployment.""
");
}
}

public ChangeSet GetChangeSet(string id)
{
return _repository.GetChangeSet(id);
}

public void Update(string id)
{
_repository.Update(id);
}

public void Update()
{
_repository.Update();
}
}
}
4 changes: 1 addition & 3 deletions Kudu.Core/SourceControl/Git/HybridGitRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public string CurrentId

public void Initialize()
{
_libgitRepository.Initialize();

_exeRepository.InitializePush();
_exeRepository.Initialize();
}

public IEnumerable<Branch> GetBranches()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Kudu.FunctionalTests.Infrastructure
{
public static class ApplicationManagerExtensions
{
private static readonly TimeSpan _defaultTimeOut = TimeSpan.FromMinutes(2);
private static readonly TimeSpan _defaultTimeOut = TimeSpan.FromMinutes(3);
private static bool _errorCallbackInitialized;

public static void GitDeploy(this ApplicationManager appManager, string repositoryName)
Expand Down
Loading

0 comments on commit a3aeda1

Please sign in to comment.