Skip to content

Commit 1b93909

Browse files
committed
Merge pull request #36 from MRCollective/octo3-support
OctopusDeploy 3 support
2 parents ff55c50 + a2011af commit 1b93909

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

AzureWebFarm.OctopusDeploy.Example/ServiceDefinition.csdef

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<InputEndpoint name="HTTP" protocol="http" port="80" />
2929
</Endpoints>
3030
<Imports>
31+
<Import moduleName="Diagnostics" />
3132
<Import moduleName="RemoteAccess" />
3233
<Import moduleName="RemoteForwarder" />
3334
</Imports>

AzureWebFarm.OctopusDeploy/Infrastructure/OctopusDeploy.cs

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
using System.IO;
44
using System.Linq;
55
using System.Threading;
6-
using Microsoft.Win32;
7-
using Microsoft.WindowsAzure.ServiceRuntime;
86
using Octopus.Client;
7+
using Octopus.Client.Exceptions;
98
using Octopus.Client.Model;
109
using Serilog;
1110

@@ -15,14 +14,14 @@ internal class OctopusDeploy
1514
{
1615
private const string InstanceArg = "--instance \"Tentacle\"";
1716
private readonly string _machineName;
18-
private readonly ConfigSettings _config;
17+
private readonly IConfigSettings _config;
1918
private readonly IProcessRunner _processRunner;
2019
private readonly IRegistryEditor _registryEditor;
2120
private readonly IOctopusRepository _repository;
2221
private readonly string _tentaclePath;
2322
private readonly string _tentacleInstallPath;
2423

25-
public OctopusDeploy(string machineName, ConfigSettings config, IOctopusRepository repository, IProcessRunner processRunner, IRegistryEditor registryEditor)
24+
public OctopusDeploy(string machineName, IConfigSettings config, IOctopusRepository repository, IProcessRunner processRunner, IRegistryEditor registryEditor)
2625
{
2726
_machineName = machineName;
2827
_config = config;
@@ -87,36 +86,63 @@ public void DeleteMachine()
8786

8887
public void DeployAllCurrentReleasesToThisMachine()
8988
{
89+
const string targetRolePropertyName = "Octopus.Action.TargetRoles";
9090
List<TaskState> currentStatuses;
9191
try
9292
{
9393
var machineId = _repository.Machines.FindByName(_machineName).Id;
9494
var environment = _repository.Environments.FindByName(_config.TentacleEnvironment).Id;
9595

96+
var projects = _repository.Projects.FindAll()
97+
.Where(p => _repository.DeploymentProcesses
98+
.Get(p.DeploymentProcessId)
99+
.Steps
100+
.Any(s =>
101+
{
102+
string value;
103+
return s.Properties.TryGetValue(targetRolePropertyName, out value) && value == _config.TentacleRole;
104+
}))
105+
.Select(p => p.Id);
106+
96107
var dashboard = _repository.Dashboards.GetDashboard();
97-
var releaseTasks = dashboard.Items
108+
var dashboardItems = dashboard.Items
98109
.Where(i => i.EnvironmentId == environment)
99-
.ToList()
100-
.Select(currentRelease => _repository.Deployments.Create(
101-
new DeploymentResource
110+
.Where(i => projects.Contains(i.ProjectId))
111+
.ToList();
112+
113+
var releaseTasks = new List<string>();
114+
foreach (var currentRelease in dashboardItems)
115+
{
116+
try
117+
{
118+
var deployment = _repository.Deployments.Create(new DeploymentResource
102119
{
103120
Comments = "Automated startup deployment by " + _machineName,
104121
EnvironmentId = currentRelease.EnvironmentId,
105122
ReleaseId = currentRelease.ReleaseId,
106123
SpecificMachineIds = new ReferenceCollection(new[] { machineId })
107-
}
108-
))
109-
.Select(d => d.TaskId)
110-
.ToList();
124+
});
125+
releaseTasks.Add(deployment.TaskId);
126+
}
127+
catch (OctopusValidationException ex)
128+
{
129+
Log.Error("Attempted to create a deployment that the OctopusDeploy server didn't like.", ex);
130+
// Rethowing the exception here so Azure doesn't fail trying to serialize it
131+
throw new Exception("Attempted to create a deployment that the OctopusDeploy server didn't like.", ex);
132+
}
133+
}
134+
111135
Log.Information("Triggered deployments with task ids: {taskIds}", releaseTasks);
112136

113137
var taskStatuses = releaseTasks.Select(taskId => _repository.Tasks.Get(taskId).State);
114138

139+
// ReSharper disable once PossibleMultipleEnumeration
115140
currentStatuses = taskStatuses.ToList();
116141
while (currentStatuses.Any(s => s == TaskState.Executing || s == TaskState.Queued))
117142
{
118143
Log.Debug("Waiting for deployments; current statuses: {statuses}", currentStatuses);
119144
Thread.Sleep(1000);
145+
// ReSharper disable once PossibleMultipleEnumeration
120146
currentStatuses = taskStatuses.ToList();
121147
}
122148

@@ -132,7 +158,7 @@ public void DeployAllCurrentReleasesToThisMachine()
132158
throw new Exception("Failed to deploy to this role - at least one necessary deployment either failed or timed out - recycling role to try again");
133159
}
134160

135-
public static IOctopusRepository GetRepository(ConfigSettings config)
161+
public static IOctopusRepository GetRepository(IConfigSettings config)
136162
{
137163
return new OctopusRepository(new OctopusServerEndpoint(config.OctopusServer, config.OctopusApiKey));
138164
}

ExampleWebFarm/WebRole.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Threading;
2+
using System.Threading.Tasks;
23
using Microsoft.WindowsAzure.ServiceRuntime;
34

45
namespace WebFarm
@@ -19,7 +20,7 @@ public override bool OnStart()
1920

2021
public override void Run()
2122
{
22-
_webFarmRole.Run(new CancellationTokenSource().Token).Wait();
23+
Task.Run(() => _webFarmRole.Run(new CancellationTokenSource().Token)).Wait();
2324
}
2425

2526
public override void OnStop()

0 commit comments

Comments
 (0)