-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGrpcDeploymentAgent.cs
263 lines (217 loc) · 9.9 KB
/
GrpcDeploymentAgent.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Xml.Serialization;
using AspNetDeploy.Contracts;
using AspNetDeploy.Contracts.Exceptions;
using AspNetDeploy.Contracts.MachineSummary;
using AspNetDeploy.Model;
using DeploymentServices;
using Google.Protobuf;
using Google.Protobuf.WellKnownTypes;
using Grpc.Net.Client;
using Grpc.Net.Client.Web;
using GrpcSatellite;
using MachineServices;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Org.BouncyCastle.Crypto.Tls;
namespace DeploymentServices.Grpc
{
public class GrpcDeploymentAgent: IDeploymentAgent, IDisposable
{
private readonly IVariableProcessor variableProcessor;
private readonly Deployment.DeploymentClient deploymentClient;
private readonly X509Certificate2 clientCertificate;
private readonly X509Certificate2 authority;
public GrpcDeploymentAgent(IVariableProcessor variableProcessor, IPathServices pathServices, string endpoint, string login, string password)
{
CertificateManager certificateManager = new CertificateManager(pathServices);
this.variableProcessor = variableProcessor;
this.authority = certificateManager.GetRootCertificate(false);
this.clientCertificate = certificateManager.GetClientCertificate();
var handler = new HttpClientHandler();
handler.ServerCertificateCustomValidationCallback = (request, cert, chain, errors) =>
{
bool isValid = chain.ChainElements
.Cast<X509ChainElement>()
.Any(el => el.Certificate.Thumbprint == this.authority.Thumbprint);
return isValid;
};
handler.ClientCertificates.Add(clientCertificate);
handler.SslProtocols = SslProtocols.Tls;
GrpcWebHandler grpcWebHandler = new GrpcWebHandler(handler);
grpcWebHandler.HttpVersion = new Version(1, 0);
var options = new GrpcChannelOptions()
{
HttpHandler = grpcWebHandler
};
var channel = GrpcChannel.ForAddress(new Uri(endpoint), options);
this.deploymentClient = new Deployment.DeploymentClient(channel);
}
public bool IsReady()
{
return this.deploymentClient.IsReady(new Empty()).IsReady;
}
public int GetVersion()
{
throw new NotImplementedException();
}
public bool BeginPublication(int publicationId)
{
return this.deploymentClient.ExecuteNextOperation(new Empty()).IsSuccess;
}
public bool ExecuteNextOperation()
{
return this.deploymentClient.ExecuteNextOperation(new Empty()).IsSuccess;
}
public bool Complete()
{
return this.deploymentClient.ExecuteNextOperation(new Empty()).IsSuccess;
}
public void Rollback()
{
this.deploymentClient.Rollback(new Empty());
}
public void UploadPackage(string file, Action<int, int> progress = null)
{
using (FileStream stream = new FileStream(file, FileMode.Open, FileAccess.Read))
{
int length = (int)stream.Length;
int bufferSize = 1024 * 500; //kb
int position = 0;
var uploadBufferRequest = this.deploymentClient.UploadPackageBuffer();
while (length > 0)
{
int readSize = length > bufferSize ? bufferSize : length;
byte[] buffer = new byte[readSize];
stream.Read(buffer, 0, readSize);
position += readSize;
length -= readSize;
uploadBufferRequest.RequestStream.WriteAsync(
new UploadPackageBufferRequest()
{
Buffer = UnsafeByteOperations.UnsafeWrap(buffer)
});
if (progress != null)
{
progress(position, length);
}
Thread.Sleep(50);
}
uploadBufferRequest.RequestStream.CompleteAsync();
}
}
public void ResetPackage()
{
this.deploymentClient.ResetPackage(new Empty());
}
public void ProcessDeploymentStep(DeploymentStep deploymentStep)
{
switch (deploymentStep.Type)
{
case DeploymentStepType.DeployWebSite:
this.ProcessWebSiteDeploymentStep(deploymentStep);
break;
case DeploymentStepType.Configuration:
this.ProcessConfigurationStep(deploymentStep);
break;
case DeploymentStepType.CopyFiles:
this.ProcessCopyFilesStep(deploymentStep);
break;
case DeploymentStepType.UpdateHostsFile:
this.ProcessHostsStep(deploymentStep);
break;
case DeploymentStepType.RunSQLScript:
this.ProcessSQLStep(deploymentStep);
break;
case DeploymentStepType.DeployDacpac:
this.ProcessDacpacStep(deploymentStep);
break;
default:
throw new AspNetDeployException("Deployment step type is not supported: " + deploymentStep.Type);
}
}
private void ProcessDacpacStep(DeploymentStep deploymentStep)
{
dynamic customConfig = JsonConvert.DeserializeObject(this.variableProcessor.ProcessValue(deploymentStep.GetStringProperty("CustomConfiguration")));
this.deploymentClient.ApplyDacPac(new ApplyDacPacRequest()
{
DacPacFilename = "database.dacpac",
ConnectionString = this.variableProcessor.ProcessValue(deploymentStep.GetStringProperty("ConnectionString")),
TargetDatabase = this.variableProcessor.ProcessValue(deploymentStep.GetStringProperty("TargetDatabase")),
ProjectId = deploymentStep.GetIntProperty("ProjectId"),
BackupDatabaseBeforeChanges = (bool)customConfig.backupDatabaseBeforeChanges,
BlockOnPossibleDataLos = (bool)customConfig.blockOnPossibleDataLoss,
});
}
private void ProcessSQLStep(DeploymentStep deploymentStep)
{
this.deploymentClient.RunSQLScripts(new RunSQLScriptsRequest()
{
ConnectionString =
this.variableProcessor.ProcessValue(deploymentStep.GetStringProperty("ConnectionString")),
Command = this.variableProcessor.ProcessValue(deploymentStep.GetStringProperty("Command")),
});
}
private void ProcessHostsStep(DeploymentStep deploymentStep)
{
this.deploymentClient.UpdateHostsFile(new UpdateHostsFileRequest()
{
ConfigurationJson = this.variableProcessor.ProcessValue(deploymentStep.GetStringProperty("ConfigurationJson"))
});
}
private void ProcessCopyFilesStep(DeploymentStep deploymentStep)
{
string mode = "replace";
if (!string.IsNullOrWhiteSpace(deploymentStep.GetStringProperty("CustomConfiguration")))
{
dynamic customConfig = deploymentStep.GetDynamicProperty("CustomConfiguration");
mode = customConfig.mode ?? "replace";
}
this.deploymentClient.CopyFiles(new CopyFilesRequest()
{
Destination = this.variableProcessor.ProcessValue(deploymentStep.GetStringProperty("DestinationPath")),
ProjectId = deploymentStep.GetIntProperty("ProjectId"),
Mode = mode
});
}
private void ProcessWebSiteDeploymentStep(DeploymentStep deploymentStep)
{
dynamic bindings = JsonConvert.DeserializeObject(this.variableProcessor.ProcessValue(deploymentStep.GetStringProperty("IIS.Bindings")) ?? "[]");
this.deploymentClient.DeployWebSite(new DeployWebSiteRequest()
{
Destination = this.variableProcessor.ProcessValue(deploymentStep.GetStringProperty("IIS.DestinationPath")),
SiteName = this.variableProcessor.ProcessValue(deploymentStep.GetStringProperty("IIS.SiteName")),
ApplicationPoolName = this.variableProcessor.ProcessValue(deploymentStep.GetStringProperty("IIS.SiteName")),
ProjectId = deploymentStep.GetIntProperty("ProjectId"),
Bindings = JsonConvert.SerializeObject((IEnumerable<dynamic>)bindings)
});
}
private void ProcessConfigurationStep(DeploymentStep deploymentStep)
{
this.deploymentClient.ProcessConfigFile(new ProccesConfigFileRequest()
{
File = this.variableProcessor.ProcessValue(deploymentStep.GetStringProperty("File")),
Content = this.variableProcessor.ProcessValue(deploymentStep.GetStringProperty("SetValues"))
});
}
public IExceptionInfo GetLastException()
{
throw new NotImplementedException();
}
public IServerSummary GetServerSummary()
{
throw new NotImplementedException();
}
public void Dispose()
{
throw new NotImplementedException();
}
}
}