/
SitecorePackageInstallerHandler.cs
322 lines (282 loc) · 11.2 KB
/
SitecorePackageInstallerHandler.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.Serialization;
using log4net;
using log4net.Repository.Hierarchy;
using log4net.spi;
using Sitecore.Diagnostics;
using Sitecore.Globalization;
using Sitecore.Update;
using Sitecore.Update.Installer;
using Sitecore.Update.Installer.Exceptions;
using Sitecore.Update.Installer.Installer.Utils;
using Sitecore.Update.Installer.Utils;
using Sitecore.Update.Metadata;
using Sitecore.Update.Utils;
using Sitecore.Web;
using Sidewalk.SC.PackageInstaller.Common;
namespace Sidewalk.SC.PackageInstaller.Service
{
public class SitecorePackageInstallerHandler : IHttpHandler, ILog
{
private static readonly ILog log = LogManager.GetLogger(typeof(SitecorePackageInstallerHandler));
public ILogger Logger { get; }
private List<string> logEntries;
private int commandsProcessedCount;
private int countOfCommands;
public SitecorePackageInstallerHandler()
{
this.Logger = new RootLogger(Level.ALL);
}
/// <summary>
/// Installs a Sitecore Update Package.
/// </summary>
/// <param name="path">A path to a package that is reachable by the web server</param>
public string InstallTdsPackage(string path)
{
using (new ShutdownGuard())
{
logEntries = new List<string>();
PackageInstallationInfo installationInfo = new PackageInstallationInfo
{
Mode = InstallMode.Update,
Action = UpgradeAction.Upgrade,
Path = path
};
string historyPath = null;
List<ContingencyEntry> entries = null;
try
{
var action = (installationInfo.Action == UpgradeAction.Preview) ? "Analyzing" : "Installing";
this.WriteMessage($"{action} package: {installationInfo.Path}", null, Level.INFO, false);
entries = UpdateHelper.Install(installationInfo, this, out historyPath);
}
catch (PostStepInstallerException exception)
{
entries = exception.Entries;
historyPath = exception.HistoryPath;
foreach (var entry in entries)
{
this.WriteMessage(entry.Message, exception, Level.ERROR);
}
throw exception;
}
finally
{
//UpdateHelper.SaveInstallationMessages(entries, historyPath);
}
return historyPath;
}
}
#region WriteMessages
protected void WriteMessage(object message, Exception ex, Level level)
{
this.WriteMessage(message, ex, level, true);
}
protected void WriteMessage(object message, Exception ex, Level level, bool isCommandText)
{
var messageInfo = new MessageInfo()
{
Level = level.Name,
Message = message.ToString(),
Date = DateTime.Now
};
if (ex != null)
{
messageInfo.Exception = new MessageInfoException()
{
ErrorMessage = ex.Message,
Source = ex.Source,
StackTrace = ex.StackTrace
};
}
if (isCommandText && ((this.commandsProcessedCount < this.countOfCommands) || (this.countOfCommands <= 0)))
{
double a = (++this.commandsProcessedCount * 100.0) / ((this.countOfCommands == 0) ? ((double)1) : ((double)this.countOfCommands));
messageInfo.Progress = new MessageInfoProgress()
{
Percentage = Math.Round(a),
Processed = commandsProcessedCount,
TotalToProcess = countOfCommands
};
log.Info($"Progress: ({ messageInfo.Progress.Processed}/{ messageInfo.Progress.TotalToProcess} - { messageInfo.Progress.Percentage}%)");
}
else
{
if (level == Level.FATAL)
log.Fatal(message, ex);
else if (level == Level.ERROR)
log.Error(message, ex);
else if (level == Level.DEBUG)
log.Debug(message);
else
log.Info(message);
}
var memoryStream = MessageToXmlString(messageInfo);
HttpContext.Current.Response.Write(Utf8ByteArrayToString(memoryStream.ToArray()));
HttpContext.Current.Response.Flush();
}
private static MemoryStream MessageToXmlString(MessageInfo messageInfo)
{
using (var memoryStream = new MemoryStream())
{
var xs = new XmlSerializer(typeof (MessageInfo));
var settings = new XmlWriterSettings() {OmitXmlDeclaration = true, Encoding = Encoding.UTF8};
using (var xmlTextWriter = XmlWriter.Create(memoryStream, settings))
{
xs.Serialize(xmlTextWriter, messageInfo);
return memoryStream;
}
}
}
private static string Utf8ByteArrayToString(byte[] characters)
{
var encoding = new UTF8Encoding();
string constructedString = encoding.GetString(characters);
return constructedString;
}
public void Info(object message)
{
this.WriteMessage(message, null, Level.INFO);
}
public void Info(object message, Exception t)
{
this.WriteMessage(message, t, Level.INFO);
}
public void Warn(object message)
{
this.WriteMessage(message, null, Level.WARN);
}
public void Warn(object message, Exception t)
{
this.WriteMessage(message, t, Level.WARN);
}
public void Debug(object message)
{
this.WriteMessage(message, null, Level.DEBUG);
}
public void Debug(object message, Exception t)
{
this.WriteMessage(message, t, Level.DEBUG);
}
public void Error(object message, Exception t)
{
this.WriteMessage(message, t, Level.ERROR);
}
public void Error(object message)
{
this.WriteMessage(message, null, Level.ERROR);
}
public void Fatal(object message)
{
this.WriteMessage(message, null, Level.FATAL);
}
public void Fatal(object message, Exception t)
{
this.WriteMessage(message, t, Level.FATAL);
}
public bool IsDebugEnabled => true;
public bool IsInfoEnabled => true;
public bool IsWarnEnabled => true;
public bool IsErrorEnabled => true;
public bool IsFatalEnabled => true;
#endregion
public static MetadataView GetMetadata(string packagePath, out string error)
{
Assert.IsNotNull(packagePath, "package path");
error = string.Empty;
MetadataView metadataFromCache = null;
try
{
metadataFromCache = UpdateHelper.LoadMetadata(packagePath);
}
catch
{
}
if ((metadataFromCache == null) || string.IsNullOrEmpty(metadataFromCache.PackageName))
{
error = Translate.Text("The package \"{0}\" could not be loaded.<p>The file is not an update package.</p>", new object[] { packagePath });
return null;
}
return metadataFromCache;
}
public void ProcessRequest(HttpContext context)
{
try
{
HttpContext.Current.Response.Write("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
HttpContext.Current.Response.Write("<Response>");
HttpContext.Current.Response.Flush();
this.ExtractQueryParameters();
if (string.IsNullOrEmpty(this.PackagePath))
{
string msg = $"The package \"{PackagePath}\" not found.";
Fatal(msg, new ArgumentNullException(msg));
}
else
{
string str;
MetadataView metadata = GetMetadata(this.PackagePath, out str);
if ((metadata == null) || !string.IsNullOrEmpty(str))
{
string msg = $"The package \"{PackagePath}\" could not be loaded.<p>The file is not an update package.";
Fatal(msg, new ArgumentNullException(msg));
}
else
{
this.countOfCommands = metadata.CommandsCount;
string packageInfos = $@"Package name: {metadata.PackageName}
Package version: {metadata.Version} (revision: {metadata.Revision})
Author: {metadata.PackageName}
Publisher: {metadata.Publisher}
Readme: {metadata.Readme}
Comment: {metadata.Comment}";
Info(packageInfos);
this.InstallTdsPackage(this.PackagePath);
}
}
}
catch (Exception exception)
{
Error("Unknow error", exception);
}
finally
{
HttpContext.Current.Response.Write("</Response>");
HttpContext.Current.Response.Flush();
}
}
protected virtual void ExtractQueryParameters()
{
string queryString = WebUtil.GetQueryString("package");
if (!string.IsNullOrEmpty(queryString))
{
string packagePath = queryString;
if (!string.IsNullOrEmpty(packagePath) && !packagePath.Contains(@"\"))
{
packagePath = UpdateHelper.GetPackagePath(queryString);
}
if (!string.IsNullOrEmpty(packagePath) && File.Exists(packagePath))
{
this.PackagePath = packagePath;
}
}
this.InstallMode = (WebUtil.GetQueryString("install") == "1") ? InstallMode.Install : InstallMode.Update;
this.UpgradeAction = (WebUtil.GetQueryString("upgrade") == "1") ? UpgradeAction.Upgrade : UpgradeAction.Preview;
string str3 = WebUtil.GetQueryString("history");
if (!string.IsNullOrEmpty(str3))
{
this.InstallationHistoryRoot = UpdateHelper.GetHistoryPath(HttpContext.Current.Server.UrlDecode(str3));
}
}
public string InstallationHistoryRoot { get; set; }
public string PackagePath { get; set; }
public UpgradeAction UpgradeAction { get; set; }
public InstallMode InstallMode { get; set; }
public bool IsReusable { get; private set; }
}
}