Welcome to mirror list, hosted at ThFree Co, Russian Federation.

IISDeployer.cs « src « IntegrationTesting.IIS « IIS « Servers « src - github.com/dotnet/aspnetcore.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 509f81db631c3512ab0cbcc64cf4d93f3b2c098c (plain)
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Globalization;
using System.ServiceProcess;
using System.Text;
using System.Xml.Linq;
using Microsoft.AspNetCore.Server.IntegrationTesting.Common;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Logging;
using Microsoft.Web.Administration;

namespace Microsoft.AspNetCore.Server.IntegrationTesting.IIS;

/// <summary>
/// Deployer for IIS.
/// </summary>
public class IISDeployer : IISDeployerBase
{
    private const string DetailedErrorsEnvironmentVariable = "ASPNETCORE_DETAILEDERRORS";

    private static readonly TimeSpan _timeout = TimeSpan.FromSeconds(60);
    private static readonly TimeSpan _retryDelay = TimeSpan.FromMilliseconds(100);

    private readonly CancellationTokenSource _hostShutdownToken = new CancellationTokenSource();

    private string _configPath;
    private string _applicationHostConfig;
    private string _debugLogFile;
    private bool _disposed;

    public Process HostProcess { get; set; }

    protected override string ApplicationHostConfigPath => _applicationHostConfig;

    public IISDeployer(DeploymentParameters deploymentParameters, ILoggerFactory loggerFactory)
        : base(new IISDeploymentParameters(deploymentParameters), loggerFactory)
    {
    }

    public IISDeployer(IISDeploymentParameters deploymentParameters, ILoggerFactory loggerFactory)
        : base(deploymentParameters, loggerFactory)
    {
    }

    public override void Dispose()
    {
        if (_disposed)
        {
            return;
        }

        _disposed = true;
        Dispose(gracefulShutdown: false);
    }

    public override void Dispose(bool gracefulShutdown)
    {
        Stop();

        TriggerHostShutdown(_hostShutdownToken);

        GetLogsFromFile();

        CleanPublishedOutput();
        InvokeUserApplicationCleanup();

        StopTimer();
    }

    public override Task<DeploymentResult> DeployAsync()
    {
        using (Logger.BeginScope("Deployment"))
        {
            StartTimer();

            Logger.LogInformation(Environment.OSVersion.ToString());

            if (string.IsNullOrEmpty(DeploymentParameters.ServerConfigTemplateContent))
            {
                DeploymentParameters.ServerConfigTemplateContent = File.ReadAllText("IIS.config");
            }

            // For now, only support using published output
            DeploymentParameters.PublishApplicationBeforeDeployment = true;
            // Move ASPNETCORE_DETAILEDERRORS to web config env variables
            if (IISDeploymentParameters.EnvironmentVariables.ContainsKey(DetailedErrorsEnvironmentVariable))
            {
                IISDeploymentParameters.WebConfigBasedEnvironmentVariables[DetailedErrorsEnvironmentVariable] =
                    IISDeploymentParameters.EnvironmentVariables[DetailedErrorsEnvironmentVariable];

                IISDeploymentParameters.EnvironmentVariables.Remove(DetailedErrorsEnvironmentVariable);
            }
            // Do not override settings set on parameters
            if (!IISDeploymentParameters.HandlerSettings.ContainsKey("debugLevel") &&
                !IISDeploymentParameters.HandlerSettings.ContainsKey("debugFile"))
            {
                _debugLogFile = Path.GetTempFileName();
                IISDeploymentParameters.HandlerSettings["debugLevel"] = "file";
                IISDeploymentParameters.HandlerSettings["debugFile"] = _debugLogFile;
            }

            DotnetPublish();
            var contentRoot = DeploymentParameters.PublishedApplicationRootPath;

            RunWebConfigActions(contentRoot);

            var uri = TestUriHelper.BuildTestUri(ServerType.IIS, DeploymentParameters.ApplicationBaseUriHint);
            StartIIS(uri, contentRoot);

            // Warm up time for IIS setup.
            Logger.LogInformation("Successfully finished IIS application directory setup.");
            return Task.FromResult<DeploymentResult>(new IISDeploymentResult(
                LoggerFactory,
                IISDeploymentParameters,
                applicationBaseUri: uri.ToString(),
                contentRoot: contentRoot,
                hostShutdownToken: _hostShutdownToken.Token,
                hostProcess: HostProcess
            ));
        }
    }

    protected override IEnumerable<Action<XElement, string>> GetWebConfigActions()
    {
        yield return WebConfigHelpers.AddOrModifyAspNetCoreSection(
            key: "hostingModel",
            value: DeploymentParameters.HostingModel.ToString());

        yield return (element, _) =>
        {
            var aspNetCore = element
                .Descendants("system.webServer")
                .Single()
                .GetOrAdd("aspNetCore");

            // Expand path to dotnet because IIS process would not inherit PATH variable
            if (aspNetCore.Attribute("processPath")?.Value.StartsWith("dotnet", StringComparison.Ordinal) == true)
            {
                aspNetCore.SetAttributeValue("processPath", DotNetCommands.GetDotNetExecutable(DeploymentParameters.RuntimeArchitecture));
            }
        };

        yield return WebConfigHelpers.AddOrModifyHandlerSection(
            key: "modules",
            value: AspNetCoreModuleV2ModuleName);

        foreach (var action in base.GetWebConfigActions())
        {
            yield return action;
        }
    }

    private void GetLogsFromFile()
    {
        try
        {
            // Handle cases where debug file is redirected by test
            var debugLogLocations = new List<string>();
            if (IISDeploymentParameters.HandlerSettings.TryGetValue("debugFile", out var debugFile))
            {
                debugLogLocations.Add(debugFile);
            }

            if (DeploymentParameters.EnvironmentVariables.TryGetValue("ASPNETCORE_MODULE_DEBUG_FILE", out debugFile))
            {
                debugLogLocations.Add(debugFile);
            }

            // default debug file name
            debugLogLocations.Add("aspnetcore-debug.log");

            foreach (var debugLogLocation in debugLogLocations)
            {
                if (string.IsNullOrEmpty(debugLogLocation))
                {
                    continue;
                }

                var file = Path.Combine(DeploymentParameters.PublishedApplicationRootPath, debugLogLocation);
                if (File.Exists(file))
                {
                    var lines = File.ReadAllLines(file);
                    if (!lines.Any())
                    {
                        Logger.LogInformation($"Debug log file {file} found but was empty");
                        continue;
                    }

                    foreach (var line in lines)
                    {
                        Logger.LogInformation(line);
                    }
                    return;
                }
            }
        }
        finally
        {
            if (File.Exists(_debugLogFile))
            {
                File.Delete(_debugLogFile);
            }
        }
    }

    public void StartIIS(Uri uri, string contentRoot)
    {
        // Backup currently deployed apphost.config file
        using (Logger.BeginScope("StartIIS"))
        {
            var port = uri.Port;
            if (port == 0)
            {
                throw new NotSupportedException("Cannot set port 0 for IIS.");
            }

            AddTemporaryAppHostConfig(contentRoot, port);

            WaitUntilSiteStarted(contentRoot);
        }
    }

    private void WaitUntilSiteStarted(string contentRoot)
    {
        ServiceController serviceController = new ServiceController("w3svc");
        Logger.LogInformation("W3SVC status " + serviceController.Status);

        if (serviceController.Status != ServiceControllerStatus.Running &&
            serviceController.Status != ServiceControllerStatus.StartPending)
        {
            Logger.LogInformation("Starting W3SVC");

            serviceController.Start();
            serviceController.WaitForStatus(ServiceControllerStatus.Running, _timeout);
        }

        RetryServerManagerAction(serverManager =>
        {
            var site = FindSite(serverManager, contentRoot);
            if (site == null)
            {
                PreserveConfigFiles("nositetostart");
                throw new InvalidOperationException($"Can't find site for: {contentRoot} to start.");
            }

            var appPool = serverManager.ApplicationPools.Single();
            if (appPool.State != ObjectState.Started && appPool.State != ObjectState.Starting)
            {
                var state = appPool.Start();
                Logger.LogInformation($"Starting pool, state: {state}");
            }

            if (site.State != ObjectState.Started && site.State != ObjectState.Starting)
            {
                var state = site.Start();
                Logger.LogInformation($"Starting site, state: {state}");
            }

            if (site.State != ObjectState.Started)
            {
                throw new InvalidOperationException("Site not started yet");
            }

            var workerProcess = appPool.WorkerProcesses.SingleOrDefault();
            if (workerProcess == null)
            {
                PreserveConfigFiles("noworkerprocess");
                throw new InvalidOperationException("Site is started but no worker process found");
            }

            HostProcess = Process.GetProcessById(workerProcess.ProcessId);

            // Ensure w3wp.exe is killed if test process termination is non-graceful.
            // Prevents locked files when stop debugging unit test.
            ProcessTracker.Add(HostProcess);

            // cache the process start time for verifying log file name.
            var _ = HostProcess.StartTime;

            Logger.LogInformation("Site has started.");
        });
    }

    private static Site FindSite(ServerManager serverManager, string contentRoot)
    {
        foreach (var site in serverManager.Sites)
        {
            var app = site.Applications.FirstOrDefault();
            if (app != null && app.VirtualDirectories.FirstOrDefault()?.PhysicalPath == contentRoot)
            {
                return site;
            }
        }
        return null;
    }

    private void AddTemporaryAppHostConfig(string contentRoot, int port)
    {
        _configPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("D"));
        _applicationHostConfig = Path.Combine(_configPath, "applicationHost.config");
        Directory.CreateDirectory(_configPath);
        var config = XDocument.Parse(DeploymentParameters.ServerConfigTemplateContent ?? File.ReadAllText("IIS.config"));

        ConfigureAppHostConfig(config.Root, contentRoot, port);

        config.Save(_applicationHostConfig);

        RetryServerManagerAction(serverManager =>
        {
            var redirectionConfiguration = serverManager.GetRedirectionConfiguration();
            var redirectionSection = redirectionConfiguration.GetSection("configurationRedirection");

            if ((bool)redirectionSection.Attributes["enabled"].Value)
            {
                // redirection wasn't removed before starting another site.
                redirectionSection.Attributes["enabled"].Value = false;
                var redirectedFilePath = (string)redirectionSection.Attributes["path"].Value;
                Logger.LogWarning($"Name of redirected file: {redirectedFilePath}");

                serverManager.CommitChanges();

                PreserveConfigFiles("redirectionbetween");
                throw new InvalidOperationException("Redirection is enabled between test runs.");
            }

            redirectionSection.Attributes["path"].Value = _configPath;

            redirectionSection.Attributes["enabled"].Value = true;

            Logger.LogInformation("applicationhost.config path {configPath}", _configPath);

            serverManager.CommitChanges();
        });
    }

    private void ConfigureAppHostConfig(XElement config, string contentRoot, int port)
    {
        ConfigureModuleAndBinding(config, contentRoot, port);

        // In IISExpress system.webServer/modules in under location element
        config
            .RequiredElement("system.webServer")
            .RequiredElement("modules")
            .GetOrAdd("add", "name", AspNetCoreModuleV2ModuleName);

        var pool = config
            .RequiredElement("system.applicationHost")
            .RequiredElement("applicationPools")
            .RequiredElement("add");

        if (DeploymentParameters.EnvironmentVariables.Any())
        {
            var environmentVariables = pool
                .GetOrAdd("environmentVariables");

            foreach (var tuple in DeploymentParameters.EnvironmentVariables)
            {
                environmentVariables
                    .GetOrAdd("add", "name", tuple.Key)
                    .SetAttributeValue("value", tuple.Value);
            }

        }

        if (DeploymentParameters.RuntimeArchitecture == RuntimeArchitecture.x86)
        {
            pool.SetAttributeValue("enable32BitAppOnWin64", "true");
        }

        RunServerConfigActions(config, contentRoot);
    }

    private void Stop()
    {
        try
        {
            RetryServerManagerAction(serverManager =>
            {
                // Stop all sites
                foreach (var site in serverManager.Sites)
                {
                    if (site.State != ObjectState.Stopped && site.State != ObjectState.Stopping)
                    {
                        var state = site.Stop();
                        Logger.LogInformation($"Stopping site, state: {state}");
                    }
                }

                // Stop all app pools
                foreach (var appPool in serverManager.ApplicationPools)
                {
                    if (appPool.State != ObjectState.Stopped && appPool.State != ObjectState.Stopping)
                    {
                        var state = appPool.Stop();
                        Logger.LogInformation($"Stopping pool, state: {state}");
                    }
                }

                // Make sure all sites are stopped
                foreach (var site in serverManager.Sites)
                {
                    if (site.State != ObjectState.Stopped)
                    {
                        throw new InvalidOperationException($"Site {site.Name} not stopped yet");
                    }
                }

                try
                {
                    foreach (var appPool in serverManager.ApplicationPools)
                    {
                        if (appPool.WorkerProcesses != null &&
                            appPool.WorkerProcesses.Any(wp =>
                                wp.State == WorkerProcessState.Running ||
                                wp.State == WorkerProcessState.Stopping))
                        {
                            throw new InvalidOperationException("WorkerProcess not stopped yet");
                        }
                    }
                }
                // If WAS was stopped for some reason appPool.WorkerProcesses
                // would throw UnauthorizedAccessException.
                // check if it's the case and continue shutting down deployer
                catch (UnauthorizedAccessException)
                {
                    var serviceController = new ServiceController("was");
                    if (serviceController.Status != ServiceControllerStatus.Stopped)
                    {
                        throw;
                    }
                }

                if (HostProcess is not null && !HostProcess.HasExited)
                {
                    throw new InvalidOperationException("Site is stopped but host process is not");
                }

                Logger.LogInformation($"Site has stopped successfully.");
            });
        }
        finally
        {
            // Undo redirection.config changes unconditionally
            RetryServerManagerAction(serverManager =>
            {
                var redirectionConfiguration = serverManager.GetRedirectionConfiguration();
                var redirectionSection = redirectionConfiguration.GetSection("configurationRedirection");

                redirectionSection.Attributes["enabled"].Value = false;

                serverManager.CommitChanges();
                if (Directory.Exists(_configPath))
                {
                    Directory.Delete(_configPath, true);
                }
            });
        }
    }

    private void RetryServerManagerAction(Action<ServerManager> action)
    {
        List<Exception> exceptions = null;
        var sw = Stopwatch.StartNew();
        int retryCount = 0;
        var delay = _retryDelay;

        while (sw.Elapsed < _timeout)
        {
            try
            {
                using (var serverManager = new ServerManager())
                {
                    action(serverManager);
                }

                return;
            }
            catch (Exception ex)
            {
                if (exceptions == null)
                {
                    exceptions = new List<Exception>();
                }

                exceptions.Add(ex);
            }

            retryCount++;
            Thread.Sleep(delay);
            delay *= 1.5;
        }

        // Try to upload the applicationHost config on helix to help debug
        PreserveConfigFiles("serverManagerRetryFailed");

        throw new AggregateException($"Operation did not succeed after {retryCount} retries, serverManagerConfig: {DumpServerManagerConfig()}", exceptions.ToArray());
    }

    private void PreserveConfigFiles(string fileNamePrefix)
    {
        HelixHelper.PreserveFile(Path.Combine(DeploymentParameters.PublishedApplicationRootPath, "web.config"), fileNamePrefix+".web.config");
        HelixHelper.PreserveFile(Path.Combine(_configPath, "applicationHost.config"), fileNamePrefix + ".applicationHost.config");
        HelixHelper.PreserveFile(Path.Combine(Environment.SystemDirectory, @"inetsrv\config\ApplicationHost.config"), fileNamePrefix + ".inetsrv.applicationHost.config");
        HelixHelper.PreserveFile(Path.Combine(Environment.SystemDirectory, @"inetsrv\config\redirection.config"), fileNamePrefix + ".inetsrv.redirection.config");
        var tmpFile = Path.GetRandomFileName();
        File.WriteAllText(tmpFile, DumpServerManagerConfig());
        HelixHelper.PreserveFile(tmpFile, fileNamePrefix + ".serverManager.dump.txt");
    }

    private static string DumpServerManagerConfig()
    {
        var configDump = new StringBuilder();
        using (var serverManager = new ServerManager())
        {
            foreach (var site in serverManager.Sites)
            {
                configDump.AppendLine(CultureInfo.InvariantCulture, $"Site Name:{site.Name} Id:{site.Id} State:{site.State}");
            }
            foreach (var appPool in serverManager.ApplicationPools)
            {
                configDump.AppendLine(CultureInfo.InvariantCulture, $"AppPool Name:{appPool.Name} Id:{appPool.ProcessModel} State:{appPool.State}");
            }
        }
        return configDump.ToString();
    }
}