Пример #1
0
        /// <summary>
        /// Sets authorization header.
        /// </summary>
        /// <returns>Authorization header</returns>
        private async Task <string> AuthorizationHeader()
        {
            if (!string.IsNullOrEmpty(_authorizationHeader) &&
                (DateTime.UtcNow.AddSeconds(60) < AuthenticationResult.ExpiresOn))
            {
                return(_authorizationHeader);
            }

            var uri = new UriBuilder(_settings.AzureAuthEndpoint)
            {
                Path = _settings.AadTenant
            };

            var aosUriAuthUri             = new Uri(_settings.AosUri);
                        string aosUriAuth = aosUriAuthUri.GetLeftPart(UriPartial.Authority);

            var authenticationContext = new AuthenticationContext(uri.ToString(), validateAuthority: false);

            if (_settings.UseServiceAuthentication)
            {
                var credentials = new ClientCredential(_settings.AadClientId.ToString(), _settings.AadClientSecret);

                AuthenticationResult = await _retryPolicy.ExecuteAsync(() => authenticationContext.AcquireTokenAsync(aosUriAuth, credentials));
            }
            else
            {
                var credentials = new UserPasswordCredential(_settings.UserName, _settings.UserPassword);

                AuthenticationResult = await _retryPolicy.ExecuteAsync(() => authenticationContext.AcquireTokenAsync(aosUriAuth, _settings.AadClientId.ToString(), credentials));
            }

            return(_authorizationHeader = AuthenticationResult.CreateAuthorizationHeader());
        }
Пример #2
0
        static private async Task _Log(string msg, LogType type)
        {
            await retryInfiniteNoLog.ExecuteAsync(async() =>
            {
                if (Fs == null)
                {
                    Fs = new StreamWriter(file, true);
                }

                if (type == LogType.FATAL)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    string dashes           = "-----------------------------------------------------------------------------";
                    msg = dashes + BR + msg + BR + dashes;
                }
                if (type != LogType.DEBUG)
                {
                    Console.WriteLine(msg);
                }

                await Fs.WriteLineAsync(msg.ToString());
                await Fs.FlushAsync();

                Console.ResetColor();
            });
        }
Пример #3
0
        public override async Task RetrieveResources(SessionHostsStartInfo sessionHostsStartInfo)
        {
            string registryWithImageName = $"{sessionHostsStartInfo.ImageDetails.Registry}/{sessionHostsStartInfo.ImageDetails.ImageName}";
            string imageTag = sessionHostsStartInfo.ImageDetails.ImageTag;
            string username = sessionHostsStartInfo.ImageDetails.Username;
            string password = sessionHostsStartInfo.ImageDetails.Password;

            if (string.IsNullOrEmpty(imageTag))
            {
                imageTag = "latest";
            }

            _logger.LogInformation($"Starting image pull for: {registryWithImageName}:{imageTag}.");
            LogReporter logReporter = new LogReporter(_logger);

            Polly.Retry.AsyncRetryPolicy retryPolicy = Policy
                                                       .Handle <Exception>((Exception e) =>
            {
                _logger.LogError($"Exception encountered when creating image: {e.ToString()}");
                return(true);
            })
                                                       .WaitAndRetryAsync(_maxRetryAttempts, i => TimeSpan.FromMinutes(_createImageRetryTimeMins / _maxRetryAttempts));

            await retryPolicy.ExecuteAsync(async() =>
            {
                await _dockerClient.Images.CreateImageAsync(
                    new ImagesCreateParameters {
                    FromImage = registryWithImageName, Tag = imageTag
                },
                    new AuthConfig()
                {
                    Username = username, Password = password
                },
                    logReporter);

                // Making sure that the image was actually downloaded properly
                // We have seen some cases where Docker Registry API returns 'success' on pull while the image has not been properly downloaded
                IEnumerable <ImagesListResponse> images = await _dockerClient.Images.ListImagesAsync(new ImagesListParameters {
                    All = true
                });
                if (images.All(image => !image.RepoTags.Contains($"{registryWithImageName}:{imageTag}")))
                {
                    throw new ApplicationException("CreateImageAsync is completed but the image doesn't exist");
                }
            });

            _logger.LogEvent(MetricConstants.PullImage, null, new Dictionary <string, double>
            {
                { MetricConstants.DownloadDurationInMilliseconds, logReporter.DownloadSummary?.DurationInMilliseconds ?? 0d },
                { MetricConstants.ExtractDurationInMilliseconds, logReporter.ExtractionSummary?.DurationInMilliseconds ?? 0d },
                { MetricConstants.SizeInBytes, logReporter.DownloadSummary?.TotalSizeInBytes ?? 0d }
            }
                             );
        }
Пример #4
0
        public static async Task <HttpResponseMessage> CheckConnection(string uri)
        {
            Polly.Retry.AsyncRetryPolicy retryPolly = Policy
                                                      .Handle <HttpRequestException>()
                                                      .WaitAndRetryAsync(MaxTry, i => TimeSpan.FromSeconds(Seconds));

            var client   = new HttpClient();
            var response = await retryPolly
                           .ExecuteAsync(() => client.GetAsync(uri));

            return(response);
        }
        /// <summary>
        /// Post request for files
        /// </summary>
        /// <param name="uri">Enqueue endpoint URI</param>
        /// <param name="bodyStream">Body stream</param>
        /// <param name="externalCorrelationHeaderValue">ActivityMessage context</param>
        /// <returns></returns>
        public async Task <HttpResponseMessage> PostStreamRequestAsync(Uri uri, Stream bodyStream, string externalCorrelationHeaderValue = null)
        {
            _httpClient.DefaultRequestHeaders.Clear();
            _httpClient.DefaultRequestHeaders.Authorization = await _authenticationHelper.GetValidAuthenticationHeader();

            // Add external correlation id header if specified and valid
            if (!string.IsNullOrEmpty(externalCorrelationHeaderValue))
            {
                _httpClient.DefaultRequestHeaders.Add("x-ms-dyn-externalidentifier", externalCorrelationHeaderValue);
            }

            if (bodyStream != null)
            {
                return(await _retryPolicy.ExecuteAsync(() => _httpClient.PostAsync(uri, new StreamContent(bodyStream))));
            }
            else
            {
                return(new HttpResponseMessage
                {
                    Content = new StringContent(Resources.Request_failed_at_client, Encoding.ASCII),
                    StatusCode = HttpStatusCode.PreconditionFailed
                });
            }
        }
Пример #6
0
        /// <summary>
        /// return map of repo name (in lowercase) -> repoID
        /// </summary>
        /// <returns></returns>
        public async Task <IReadOnlyDictionary <string, string> > ensureProjectLoadedAsync()
        {
            if (null != _mapProjectNamesToId)
            {
                return(_mapProjectNamesToId);
            }

            Console.WriteLine("Loading projects");
            var response = await _retryPolicy.ExecuteAsync(() => _httpClient.GetAsync(completedRevUrl("/api/projects")));

            var projects = await HttpHelpers.handleHttpResponse(new Dictionary <string, ProjectDef>(), response);

            try
            {
                _mapProjectNamesToId = projects.ToDictionary(kv => kv.Value.name.ToLower(), kv => kv.Key);
                return(_mapProjectNamesToId);
            }
            catch (Exception ex)
            {
                throw new Exception("Failed to create repository maps. Please check if there are duplicated names", ex);
            }
        }