3
3
using System . IO ;
4
4
using System . Linq ;
5
5
using System . Threading ;
6
- using Microsoft . Win32 ;
7
- using Microsoft . WindowsAzure . ServiceRuntime ;
8
6
using Octopus . Client ;
7
+ using Octopus . Client . Exceptions ;
9
8
using Octopus . Client . Model ;
10
9
using Serilog ;
11
10
@@ -15,14 +14,14 @@ internal class OctopusDeploy
15
14
{
16
15
private const string InstanceArg = "--instance \" Tentacle\" " ;
17
16
private readonly string _machineName ;
18
- private readonly ConfigSettings _config ;
17
+ private readonly IConfigSettings _config ;
19
18
private readonly IProcessRunner _processRunner ;
20
19
private readonly IRegistryEditor _registryEditor ;
21
20
private readonly IOctopusRepository _repository ;
22
21
private readonly string _tentaclePath ;
23
22
private readonly string _tentacleInstallPath ;
24
23
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 )
26
25
{
27
26
_machineName = machineName ;
28
27
_config = config ;
@@ -87,36 +86,63 @@ public void DeleteMachine()
87
86
88
87
public void DeployAllCurrentReleasesToThisMachine ( )
89
88
{
89
+ const string targetRolePropertyName = "Octopus.Action.TargetRoles" ;
90
90
List < TaskState > currentStatuses ;
91
91
try
92
92
{
93
93
var machineId = _repository . Machines . FindByName ( _machineName ) . Id ;
94
94
var environment = _repository . Environments . FindByName ( _config . TentacleEnvironment ) . Id ;
95
95
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
+
96
107
var dashboard = _repository . Dashboards . GetDashboard ( ) ;
97
- var releaseTasks = dashboard . Items
108
+ var dashboardItems = dashboard . Items
98
109
. 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
102
119
{
103
120
Comments = "Automated startup deployment by " + _machineName ,
104
121
EnvironmentId = currentRelease . EnvironmentId ,
105
122
ReleaseId = currentRelease . ReleaseId ,
106
123
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
+
111
135
Log . Information ( "Triggered deployments with task ids: {taskIds}" , releaseTasks ) ;
112
136
113
137
var taskStatuses = releaseTasks . Select ( taskId => _repository . Tasks . Get ( taskId ) . State ) ;
114
138
139
+ // ReSharper disable once PossibleMultipleEnumeration
115
140
currentStatuses = taskStatuses . ToList ( ) ;
116
141
while ( currentStatuses . Any ( s => s == TaskState . Executing || s == TaskState . Queued ) )
117
142
{
118
143
Log . Debug ( "Waiting for deployments; current statuses: {statuses}" , currentStatuses ) ;
119
144
Thread . Sleep ( 1000 ) ;
145
+ // ReSharper disable once PossibleMultipleEnumeration
120
146
currentStatuses = taskStatuses . ToList ( ) ;
121
147
}
122
148
@@ -132,7 +158,7 @@ public void DeployAllCurrentReleasesToThisMachine()
132
158
throw new Exception ( "Failed to deploy to this role - at least one necessary deployment either failed or timed out - recycling role to try again" ) ;
133
159
}
134
160
135
- public static IOctopusRepository GetRepository ( ConfigSettings config )
161
+ public static IOctopusRepository GetRepository ( IConfigSettings config )
136
162
{
137
163
return new OctopusRepository ( new OctopusServerEndpoint ( config . OctopusServer , config . OctopusApiKey ) ) ;
138
164
}
0 commit comments