Ejemplo n.º 1
0
        public async Task CollectLogs(string id, string logsFolder, ISessionHostManager sessionHostManager)
        {
            try
            {
                _logger.LogVerbose($"Collecting logs for container {id}.");
                string destinationFileName = Path.Combine(logsFolder, ConsoleLogCaptureFileName);

                if (sessionHostManager.LinuxContainersOnWindows)
                {
                    // we do this for lcow since containers are running on a Hyper-V Linux machine
                    // which the host Windows machine does not have "copy" access to, to get the logs with a FileCopy
                    // this is only supposed to run on LocalMultiplayerAgent running on lcow
                    StringBuilder sb         = new StringBuilder();
                    Stream        logsStream = await _dockerClient.Containers.GetContainerLogsAsync(id,
                                                                                                    new ContainerLogsParameters()
                    {
                        ShowStdout = true, ShowStderr = true
                    });

                    using (StreamReader sr = new StreamReader(logsStream))
                    {
                        Stopwatch sw = new Stopwatch();
                        while (!sr.EndOfStream)
                        {
                            if (sw.Elapsed.Seconds > 3) // don't flood STDOUT with messages, output one every 3 seconds if logs are too many
                            {
                                _logger.LogVerbose($"Gathering logs for container {id}, please wait...");
                                sw.Restart();
                            }
                            _systemOperations.FileAppendAllText(destinationFileName, sr.ReadLine() + Environment.NewLine);
                        }
                    }
                    _logger.LogVerbose($"Written logs for container {id} to {destinationFileName}.");
                }
                else
                {
                    ContainerInspectResponse containerInspectResponse = await _dockerClient.Containers.InspectContainerAsync(id);

                    string dockerLogsPath = containerInspectResponse?.LogPath;
                    if (!string.IsNullOrEmpty(dockerLogsPath) && _systemOperations.FileExists(dockerLogsPath))
                    {
                        _logger.LogVerbose($"Copying log file {dockerLogsPath} for container {id} to {destinationFileName}.");
                        _systemOperations.FileCopy(dockerLogsPath, destinationFileName);
                    }
                }
            }
            catch (DockerContainerNotFoundException)
            {
                _logger.LogInformation($"Docker container {id} not found.");
            }
        }
Ejemplo n.º 2
0
        public void ExtractAssets(string assetFileName, string targetFolder)
        {
            string fileExtension = Path.GetExtension(assetFileName).ToLowerInvariant();

            // If the OS is windows use the native .NET zip extraction (we only support .zip for Windows).
            if (_systemOperations.IsOSPlatform(OSPlatform.Windows))
            {
                if (fileExtension != Constants.ZipExtension)
                {
                    throw new AssetExtractionFailedException($"Only Zip format is supported in Windows. Unable to extract {assetFileName}.");
                }

                if (Directory.Exists(targetFolder))
                {
                    Directory.Delete(targetFolder, true);
                }

                _systemOperations.ExtractToDirectory(assetFileName, targetFolder);
            }
            else
            {
                // If the OS is Linux, we need to extract files using Linux Command Line.
                if (!LinuxSupportedFileExtensions.Any(extension => assetFileName.EndsWith(extension, StringComparison.InvariantCultureIgnoreCase)))
                {
                    throw new AssetExtractionFailedException($"Only Tar, Tar.gz and Zip formats are supported in Linux. Unable to extract {assetFileName}.");
                }

                _systemOperations.CreateDirectory(targetFolder);

                ProcessStartInfo processStartInfo = Path.GetExtension(assetFileName).ToLowerInvariant() == Constants.ZipExtension
                   ? GetProcessStartInfoForZip(assetFileName, targetFolder)
                   : GetProcessStartInfoForTarOrGZip(assetFileName, targetFolder);

                _logger.LogInformation($"Starting asset extraction with command arguments: {processStartInfo.Arguments}");

                using (Process process = new Process())
                {
                    process.StartInfo = processStartInfo;
                    (int exitCode, string stdOut, string stdErr) = _systemOperations.RunProcessWithStdCapture(process);

                    if (!string.IsNullOrEmpty(stdOut))
                    {
                        _logger.LogVerbose(stdOut);
                    }

                    if (!string.IsNullOrEmpty(stdErr))
                    {
                        _logger.LogError(stdErr);
                    }

                    if (exitCode != 0)
                    {
                        throw new AssetExtractionFailedException($"Asset extraction for file {assetFileName} failed. Errors: {stdErr ?? string.Empty}");
                    }

                    _systemOperations.SetUnixOwnerIfNeeded(targetFolder, true);
                }
            }
        }
Ejemplo n.º 3
0
        public void SetUnixOwnerIfNeeded(string path, bool applyToAllContents = false)
        {
            if (_vmConfiguration != null && _vmConfiguration.RunContainersInUserMode)
            {
                _logger?.LogVerbose($"Setting unix owner for {path}");
                _fileSystemOperations.SetUnixOwner(path, User);
                if (_fileSystemOperations.IsDirectory(path) && applyToAllContents)
                {
                    DirectoryInfo dirInfo = new DirectoryInfo(path);
                    IEnumerable <FileSystemInfo> childItems = _fileSystemOperations.GetFileSystemInfos(dirInfo, true);

                    foreach (FileSystemInfo child in childItems)
                    {
                        _fileSystemOperations.SetUnixOwner(child.FullName, User);
                    }
                }
            }
        }