33using System . IO ;
44using System . Linq ;
55using System . Threading ;
6- using Microsoft . Win32 ;
7- using Microsoft . WindowsAzure . ServiceRuntime ;
86using Octopus . Client ;
7+ using Octopus . Client . Exceptions ;
98using Octopus . Client . Model ;
109using 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 }
0 commit comments