From 3325bfc653583c1e1bb76929dad9cdd003fd4ff2 Mon Sep 17 00:00:00 2001 From: Kiran Challa Date: Sat, 12 Dec 2015 22:12:01 -0800 Subject: [PATCH] Fix how we chose target runtime in deployers --- .../Deployers/ApplicationDeployer.cs | 23 ++-- .../Deployers/ApplicationDeployerFactory.cs | 5 - .../Deployers/MonoDeployer.cs | 128 ------------------ 3 files changed, 11 insertions(+), 145 deletions(-) delete mode 100644 src/Microsoft.AspNet.Server.Testing/Deployers/MonoDeployer.cs diff --git a/src/Microsoft.AspNet.Server.Testing/Deployers/ApplicationDeployer.cs b/src/Microsoft.AspNet.Server.Testing/Deployers/ApplicationDeployer.cs index df1d0bcbe3..643c2d8310 100644 --- a/src/Microsoft.AspNet.Server.Testing/Deployers/ApplicationDeployer.cs +++ b/src/Microsoft.AspNet.Server.Testing/Deployers/ApplicationDeployer.cs @@ -103,27 +103,26 @@ namespace Microsoft.AspNet.Server.Testing string currentRuntimeBinPath = PlatformServices.Default.Runtime.RuntimePath; Logger.LogInformation($"Current runtime path is : {currentRuntimeBinPath}"); - var targetRuntimeName = new StringBuilder() + string targetRuntimeName; + if (DeploymentParameters.RuntimeFlavor == RuntimeFlavor.Mono) + { + targetRuntimeName = "dnx-mono"; + } + else + { + targetRuntimeName = new StringBuilder() .Append("dnx") .Append((DeploymentParameters.RuntimeFlavor == RuntimeFlavor.CoreClr) ? "-coreclr" : "-clr") .Append($"-{OSPrefix}") .Append((DeploymentParameters.RuntimeArchitecture == RuntimeArchitecture.x86) ? "-x86" : "-x64") .ToString(); - - string targetRuntimeBinPath; - // Ex: When current runtime is Mono and the tests are being run for CoreClr - if (currentRuntimeBinPath.Contains("dnx-mono")) - { - targetRuntimeBinPath = currentRuntimeBinPath.Replace("dnx-mono", targetRuntimeName); } - else - { - targetRuntimeBinPath = Regex.Replace( + + var targetRuntimeBinPath = Regex.Replace( currentRuntimeBinPath, - "dnx-(clr|coreclr)-(win|linux|darwin)-(x86|x64)", + "dnx-(mono|((clr|coreclr)-(win|linux|darwin)-(x86|x64)))", targetRuntimeName, RegexOptions.IgnoreCase); - } var targetRuntimeBinDir = new DirectoryInfo(targetRuntimeBinPath); if (targetRuntimeBinDir == null || !targetRuntimeBinDir.Exists) diff --git a/src/Microsoft.AspNet.Server.Testing/Deployers/ApplicationDeployerFactory.cs b/src/Microsoft.AspNet.Server.Testing/Deployers/ApplicationDeployerFactory.cs index 2ceaa03eed..4e4ae9d1e0 100644 --- a/src/Microsoft.AspNet.Server.Testing/Deployers/ApplicationDeployerFactory.cs +++ b/src/Microsoft.AspNet.Server.Testing/Deployers/ApplicationDeployerFactory.cs @@ -29,11 +29,6 @@ namespace Microsoft.AspNet.Server.Testing throw new ArgumentNullException(nameof(logger)); } - if (deploymentParameters.RuntimeFlavor == RuntimeFlavor.Mono) - { - return new MonoDeployer(deploymentParameters, logger); - } - switch (deploymentParameters.ServerType) { case ServerType.IISExpress: diff --git a/src/Microsoft.AspNet.Server.Testing/Deployers/MonoDeployer.cs b/src/Microsoft.AspNet.Server.Testing/Deployers/MonoDeployer.cs deleted file mode 100644 index 00e1d9b999..0000000000 --- a/src/Microsoft.AspNet.Server.Testing/Deployers/MonoDeployer.cs +++ /dev/null @@ -1,128 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Linq; -using System.Threading; -using Microsoft.Extensions.Logging; - -namespace Microsoft.AspNet.Server.Testing -{ - /// - /// Deployer for Kestrel on Mono. - /// - public class MonoDeployer : ApplicationDeployer - { - private Process _hostProcess; - - public MonoDeployer(DeploymentParameters deploymentParameters, ILogger logger) - : base(deploymentParameters, logger) - { - } - - public override DeploymentResult Deploy() - { - // Start timer - StartTimer(); - - var path = Environment.GetEnvironmentVariable("PATH"); - var runtimeBin = path.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries). - Where(c => c.Contains("dnx-mono")).FirstOrDefault(); - - if (string.IsNullOrWhiteSpace(runtimeBin)) - { - throw new Exception("Runtime not detected on the machine."); - } - - var runtimeBinDir = new DirectoryInfo(runtimeBin); - ChosenRuntimePath = runtimeBinDir.FullName; - ChosenRuntimeName = runtimeBinDir.Parent.Name; - DeploymentParameters.DnxRuntime = ChosenRuntimeName; - - if (DeploymentParameters.PublishApplicationBeforeDeployment) - { - // We use full path to runtime to pack. - DnuPublish(); - } - - DeploymentParameters.EnvironmentVariables - .Add(new KeyValuePair("DNX_APPBASE", DeploymentParameters.ApplicationPath)); - - // Launch the host process. - var hostExitToken = StartMonoHost(); - - return new DeploymentResult - { - WebRootLocation = DeploymentParameters.ApplicationPath, - DeploymentParameters = DeploymentParameters, - ApplicationBaseUri = DeploymentParameters.ApplicationBaseUriHint, - HostShutdownToken = hostExitToken - }; - } - - private CancellationToken StartMonoHost() - { - if (DeploymentParameters.ServerType != ServerType.Kestrel) - { - throw new InvalidOperationException("kestrel is the only valid ServerType for Mono"); - } - - var dnxPath = Path.Combine(ChosenRuntimePath, DnxCommandName); - var dnxArgs = $"-p \"{DeploymentParameters.ApplicationPath}\" kestrel --server.urls {DeploymentParameters.ApplicationBaseUriHint}"; - Logger.LogInformation($"Executing command {dnxPath} {dnxArgs}"); - - var startInfo = new ProcessStartInfo - { - FileName = dnxPath, - Arguments = dnxArgs, - UseShellExecute = false, - CreateNoWindow = true, - RedirectStandardError = true, - RedirectStandardOutput = true, - // Trying a work around for https://github.com/aspnet/Hosting/issues/140. - RedirectStandardInput = true - }; - - AddEnvironmentVariablesToProcess(startInfo); - - _hostProcess = new Process() { StartInfo = startInfo }; - _hostProcess.ErrorDataReceived += (sender, dataArgs) => { Logger.LogError(dataArgs.Data ?? string.Empty); }; - _hostProcess.OutputDataReceived += (sender, dataArgs) => { Logger.LogInformation(dataArgs.Data ?? string.Empty); }; - _hostProcess.EnableRaisingEvents = true; - var hostExitTokenSource = new CancellationTokenSource(); - _hostProcess.Exited += (sender, e) => - { - TriggerHostShutdown(hostExitTokenSource); - }; - _hostProcess.Start(); - _hostProcess.BeginErrorReadLine(); - _hostProcess.BeginOutputReadLine(); - - if (_hostProcess.HasExited) - { - Logger.LogError("Host process {processName} exited with code {exitCode} or failed to start.", startInfo.FileName, _hostProcess.ExitCode); - throw new Exception("Failed to start host"); - } - - Logger.LogInformation("Started {fileName}. Process Id : {processId}", startInfo.FileName, _hostProcess.Id); - return hostExitTokenSource.Token; - } - - public override void Dispose() - { - ShutDownIfAnyHostProcess(_hostProcess); - - if (DeploymentParameters.PublishApplicationBeforeDeployment) - { - CleanPublishedOutput(); - } - - InvokeUserApplicationCleanup(); - - StopTimer(); - } - } -} \ No newline at end of file