private static async Task <List <RuntimeFrameworkVersion> > GetFrameworkVersionsAsync(ServerOptions options) { if (string.IsNullOrWhiteSpace(options.DotNetPath)) { throw new InvalidOperationException(); } var processStartInfo = new ProcessStartInfo { FileName = options.DotNetPath, Arguments = "--info", CreateNoWindow = true, RedirectStandardOutput = true, RedirectStandardError = true, RedirectStandardInput = true, UseShellExecute = false }; Process process = null; try { process = Process.Start(processStartInfo); process.EnableRaisingEvents = true; } catch (Exception e) { process?.Kill(); throw new InvalidOperationException($"Unable to execute dotnet to retrieve list of installed runtimes.{Environment.NewLine}Command was: {Environment.NewLine}{processStartInfo.WorkingDirectory}> {processStartInfo.FileName} {processStartInfo.Arguments}", e); } var insideRuntimes = false; var runtimeLines = new List <string>(); await ProcessHelper.ReadOutput(process.StandardOutput, elapsed : null, options, (line, builder) => { line = line.Trim(); if (line.StartsWith(".NET runtimes installed:") || line.StartsWith(".NET Core runtimes installed:")) { insideRuntimes = true; return(Task.FromResult(false)); } if (insideRuntimes && line.StartsWith("Microsoft.NETCore.App")) { runtimeLines.Add(line); } return(Task.FromResult(false)); }).ConfigureAwait(false); var runtimes = new List <RuntimeFrameworkVersion>(); foreach (string runtimeLine in runtimeLines) // Microsoft.NETCore.App 5.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] { var values = runtimeLine.Split(' '); if (values.Length < 2) { throw new InvalidOperationException($"Invalid runtime line. Expected 'Microsoft.NETCore.App x.x.x', but was '{runtimeLine}'."); } runtimes.Add(new RuntimeFrameworkVersion(values[1])); } return(runtimes); }
public static Process Run(ServerOptions options) { if (string.IsNullOrWhiteSpace(options.ServerDirectory)) { throw new ArgumentNullException(nameof(options.ServerDirectory)); } if (string.IsNullOrWhiteSpace(options.DataDirectory)) { throw new ArgumentNullException(nameof(options.DataDirectory)); } var serverDllPath = Path.Combine(options.ServerDirectory, "Raven.Server.dll"); if (File.Exists(serverDllPath) == false) { throw new FileNotFoundException("Server file was not found", serverDllPath); } if (string.IsNullOrWhiteSpace(options.DotNetPath)) { throw new ArgumentNullException(nameof(options.DotNetPath)); } using (var currentProcess = Process.GetCurrentProcess()) { options.CommandLineArgs.Add($"--Embedded.ParentProcessId={currentProcess.Id}"); } options.CommandLineArgs.Add($"--License.Eula.Accepted={options.AcceptEula}"); options.CommandLineArgs.Add("--Setup.Mode=None"); options.CommandLineArgs.Add($"--DataDir={CommandLineArgumentEscaper.EscapeSingleArg(options.DataDirectory)}"); if (options.Security != null) { if (string.IsNullOrWhiteSpace(options.ServerUrl)) { options.ServerUrl = "https://127.0.0.1:0"; } if (options.Security.CertificatePath != null) { options.CommandLineArgs.Add($"--Security.Certificate.Path={CommandLineArgumentEscaper.EscapeSingleArg(options.Security.CertificatePath)}"); if (options.Security.CertificatePassword != null) { options.CommandLineArgs.Add($"--Security.Certificate.Password={CommandLineArgumentEscaper.EscapeSingleArg(options.Security.CertificatePassword)}"); } } else { options.CommandLineArgs.Add($"--Security.Certificate.Exec={CommandLineArgumentEscaper.EscapeSingleArg(options.Security.CertificateExec)}"); options.CommandLineArgs.Add($"--Security.Certificate.Exec.Arguments={CommandLineArgumentEscaper.EscapeSingleArg(options.Security.CertificateArguments)}"); } options.CommandLineArgs.Add($"--Security.WellKnownCertificates.Admin={CommandLineArgumentEscaper.EscapeSingleArg(options.Security.ClientCertificate.Thumbprint)}"); } else { if (string.IsNullOrWhiteSpace(options.ServerUrl)) { options.ServerUrl = "http://127.0.0.1:0"; } } options.CommandLineArgs.Add($"--ServerUrl={options.ServerUrl}"); options.CommandLineArgs.Insert(0, CommandLineArgumentEscaper.EscapeSingleArg(serverDllPath)); if (string.IsNullOrWhiteSpace(options.FrameworkVersion) == false) { options.CommandLineArgs.Insert(0, $"--fx-version {options.FrameworkVersion}"); } var argumentsString = string.Join(" ", options.CommandLineArgs); var processStartInfo = new ProcessStartInfo { FileName = options.DotNetPath, Arguments = argumentsString, CreateNoWindow = true, RedirectStandardOutput = true, RedirectStandardError = true, RedirectStandardInput = true, UseShellExecute = false }; RemoveEnvironmentVariables(processStartInfo); Process process = null; try { process = Process.Start(processStartInfo); } catch (Exception e) { process?.Kill(); throw new InvalidOperationException("Unable to execute server." + Environment.NewLine + "Command was: " + Environment.NewLine + (processStartInfo.WorkingDirectory ?? Directory.GetCurrentDirectory()) + "> " + processStartInfo.FileName + " " + processStartInfo.Arguments, e); } return(process); }
public static Process Run(ServerOptions options) { if (string.IsNullOrWhiteSpace(options.ServerDirectory)) { throw new ArgumentNullException(nameof(options.ServerDirectory)); } if (string.IsNullOrWhiteSpace(options.DataDirectory)) { throw new ArgumentNullException(nameof(options.DataDirectory)); } if (string.IsNullOrWhiteSpace(options.LogsPath)) { throw new ArgumentNullException(nameof(options.LogsPath)); } (string exec, string fstArg) = GetExecAndFirstArgument(options); var commandLineArgs = new List <string>(options.CommandLineArgs); using (var currentProcess = Process.GetCurrentProcess()) { commandLineArgs.Add($"--Embedded.ParentProcessId={currentProcess.Id}"); } commandLineArgs.Add($"--License.Eula.Accepted={options.AcceptEula}"); commandLineArgs.Add("--Setup.Mode=None"); commandLineArgs.Add($"--DataDir={CommandLineArgumentEscaper.EscapeSingleArg(options.DataDirectory)}"); commandLineArgs.Add($"--Logs.Path={CommandLineArgumentEscaper.EscapeSingleArg(options.LogsPath)}"); if (options.Security != null) { if (string.IsNullOrWhiteSpace(options.ServerUrl)) { options.ServerUrl = "https://127.0.0.1:0"; } if (options.Security.CertificatePath != null) { commandLineArgs.Add($"--Security.Certificate.Path={CommandLineArgumentEscaper.EscapeSingleArg(options.Security.CertificatePath)}"); if (options.Security.CertificatePassword != null) { commandLineArgs.Add($"--Security.Certificate.Password={CommandLineArgumentEscaper.EscapeSingleArg(options.Security.CertificatePassword)}"); } } else { commandLineArgs.Add($"--Security.Certificate.Load.Exec={CommandLineArgumentEscaper.EscapeSingleArg(options.Security.CertificateLoadExec)}"); commandLineArgs.Add($"--Security.Certificate.Load.Exec.Arguments={CommandLineArgumentEscaper.EscapeSingleArg(options.Security.CertificateLoadExecArguments)}"); } commandLineArgs.Add($"--Security.WellKnownCertificates.Admin={CommandLineArgumentEscaper.EscapeSingleArg(options.Security.ClientCertificate.Thumbprint)}"); } else { if (string.IsNullOrWhiteSpace(options.ServerUrl)) { options.ServerUrl = "http://127.0.0.1:0"; } } commandLineArgs.Add($"--ServerUrl={options.ServerUrl}"); if (fstArg != null) { commandLineArgs.Insert(0, CommandLineArgumentEscaper.EscapeSingleArg(fstArg)); if (string.IsNullOrWhiteSpace(options.FrameworkVersion) == false) { commandLineArgs.Insert(0, $"--fx-version {options.FrameworkVersion}"); } } var argumentsString = string.Join(" ", commandLineArgs); var processStartInfo = new ProcessStartInfo { FileName = exec, Arguments = argumentsString, CreateNoWindow = true, RedirectStandardOutput = true, RedirectStandardError = true, RedirectStandardInput = true, UseShellExecute = false }; RemoveEnvironmentVariables(processStartInfo); Process process = null; try { process = Process.Start(processStartInfo); process.EnableRaisingEvents = true; } catch (Exception e) { process?.Kill(); throw new InvalidOperationException("Unable to execute server." + Environment.NewLine + "Command was: " + Environment.NewLine + (processStartInfo.WorkingDirectory ?? Directory.GetCurrentDirectory()) + "> " + processStartInfo.FileName + " " + processStartInfo.Arguments, e); } return(process); }
internal static async Task <string?> ReadOutput(StreamReader output, Stopwatch elapsed, ServerOptions options, Func <string, StringBuilder, Task <bool> >?onLine) { var sb = new StringBuilder(); Task <string>?readLineTask = null; while (true) { readLineTask ??= output.ReadLineAsync(); var hasResult = await readLineTask.WaitWithTimeout(TimeSpan.FromSeconds(5)).ConfigureAwait(false); if (elapsed != null && elapsed.Elapsed > options.MaxServerStartupTimeDuration) { return(null); } if (hasResult == false) { continue; } var line = readLineTask.Result; readLineTask = null; var shouldStop = false; if (line != null) { sb.AppendLine(line); if (onLine != null) { shouldStop = await onLine(line, sb).ConfigureAwait(false); } } if (shouldStop) { break; } if (line == null) { break; } } return(sb.ToString()); }