Ejemplo n.º 1
0
        public async Task <int> RunAsync()
        {
            int exitCode = 1;

            try
            {
                if (_args.Length == 0)
                {
                    ShowUsage();
                }
                else
                {
                    string?downloadDirectory = _args.GetCommandLineValue(CommandExtensions.DownloadDirectory);
                    string?exeVersion        = _args.GetCommandLineValue(CommandExtensions.ExeVersion);

                    bool force = _args.Any(arg =>
                                           arg.Equals(CommandExtensions.Force, StringComparison.OrdinalIgnoreCase));

                    if (string.IsNullOrWhiteSpace(downloadDirectory))
                    {
                        ShowUsage();
                    }
                    else
                    {
                        if (downloadDirectory.Equals("default", StringComparison.OrdinalIgnoreCase))
                        {
                            downloadDirectory = null;
                        }

                        NuGetDownloadResult nuGetDownloadResult = await new NuGetDownloadClient().DownloadNuGetAsync(
                            new NuGetDownloadSettings(downloadDirectory: downloadDirectory, nugetExeVersion: exeVersion, force: force),
                            _logger).ConfigureAwait(false);

                        if (nuGetDownloadResult.Succeeded)
                        {
                            exitCode = 0;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Could not download NuGet");
            }
            finally
            {
                _logger.Verbose("Exit code is {ExitCode}", exitCode);
            }

            if (Debugger.IsAttached)
            {
                Debugger.Break();
            }

            return(exitCode);
        }
Ejemplo n.º 2
0
        public async Task <HealthCheckResult> CheckHealthAsync(CancellationToken cancellationToken)
        {
            NuGetDownloadResult nuGetDownloadResult = await _nuGetDownloadClient.DownloadNuGetAsync(
                NuGetDownloadSettings.Default,
                _logger,
                _httpClient.CreateClient("NuGet"),
                cancellationToken);

            if (!nuGetDownloadResult.Succeeded)
            {
                return(new HealthCheckResult(false));
            }

            var args = new List <string>
            {
                "Sources",
                "-Format",
                "Short"
            };

            var lines = new List <string>();

            ExitCode exitCode = await ProcessRunner.ExecuteProcessAsync(nuGetDownloadResult.NuGetExePath,
                                                                        args,
                                                                        (message, _) =>
            {
                lines.Add(message);
                _logger.Verbose("{Message}", message);
            },
                                                                        (message, category) => _logger.Verbose("{Category}{Message}", category, message),
                                                                        (message, category) => _logger.Verbose("{Category}{Message}", category, message),
                                                                        debugAction : (message, category) => _logger.Verbose("{Category}{Message}", category, message),
                                                                        cancellationToken : cancellationToken);

            if (!exitCode.IsSuccess)
            {
                return(new HealthCheckResult(false));
            }

            ConcurrentDictionary <Uri, bool?> nugetFeeds = GetFeedUrls(lines);

            List <Task> tasks = nugetFeeds.Keys
                                .Select(nugetFeed => CheckFeedAsync(cancellationToken, nugetFeed, nugetFeeds))
                                .ToList();

            await Task.WhenAll(tasks);

            bool allSucceeded = nugetFeeds.All(pair => pair.Value == true);

            return(new HealthCheckResult(allSucceeded));
        }
Ejemplo n.º 3
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            await Task.Yield();

            string?nugetExePath = "";

            _logger.Debug("Ensuring nuget.exe exists");

            if (!int.TryParse(_configuration[DeployerAppConstants.NuGetDownloadTimeoutInSeconds],
                              out int initialNuGetDownloadTimeoutInSeconds) || initialNuGetDownloadTimeoutInSeconds <= 0)
            {
                initialNuGetDownloadTimeoutInSeconds = 100;
            }

            try
            {
                var fromSeconds = TimeSpan.FromSeconds(initialNuGetDownloadTimeoutInSeconds);

                using CancellationTokenSource cts = _timeoutHelper.CreateCancellationTokenSource(fromSeconds);
                string?downloadDirectory = _configuration[DeployerAppConstants.NuGetExeDirectory].WithDefault();
                string?exeVersion        = _configuration[DeployerAppConstants.NuGetExeVersion].WithDefault();

                HttpClient httpClient = _httpClientFactory.CreateClient();

                var nuGetDownloadClient = new NuGetDownloadClient();
                NuGetDownloadResult nuGetDownloadResult = await nuGetDownloadClient.DownloadNuGetAsync(
                    new NuGetDownloadSettings(downloadDirectory : downloadDirectory, nugetExeVersion : exeVersion),
                    _logger,
                    httpClient,
                    cts.Token);

                if (nuGetDownloadResult.Succeeded)
                {
                    nugetExePath = nuGetDownloadResult.NuGetExePath;
                }
            }
            catch (Exception ex)
            {
                _logger.Warning(ex, "Could not download nuget.exe");
            }

            if (_configuration is { } && _nugetConfiguration is {})