public void DownloadPackage( string packageId, IVersion version, Uri feedUri, ICredentials feedCredentials, string targetFilePath, int maxDownloadAttempts, TimeSpan downloadAttemptBackoff, Action <string, IVersion, Uri, ICredentials, string> action) { if (maxDownloadAttempts <= 0) { throw new ArgumentException($"The number of download attempts should be greater than zero, but was {maxDownloadAttempts}", nameof(maxDownloadAttempts)); } var tempTargetFilePath = targetFilePath + Download.NuGetPackageDownloader.DownloadingExtension; // The RetryTracker is a bit finicky to set up... var numberOfRetriesOnFailure = maxDownloadAttempts - 1; var retry = new RetryTracker(numberOfRetriesOnFailure, timeLimit: null, retryInterval: new LinearRetryInterval(downloadAttemptBackoff)); while (retry.Try()) { Log.Verbose($"Downloading package (attempt {retry.CurrentTry} of {maxDownloadAttempts})"); try { action(packageId, version, feedUri, feedCredentials, tempTargetFilePath); fileSystem.MoveFile(tempTargetFilePath, targetFilePath); return; } catch (Exception ex) { if (ex is WebException webException && webException.Response is HttpWebResponse response && response.StatusCode == HttpStatusCode.Unauthorized) { throw new Exception($"Unable to download package: {webException.Message}", ex); } Log.Verbose($"Attempt {retry.CurrentTry} of {maxDownloadAttempts}: {ex.Message}"); fileSystem.DeleteFile(tempTargetFilePath, FailureOptions.IgnoreFailure); fileSystem.DeleteFile(targetFilePath, FailureOptions.IgnoreFailure); if (retry.CanRetry()) { var wait = TimeSpan.FromMilliseconds(retry.Sleep()); Log.Verbose($"Going to wait {wait.TotalSeconds}s before attempting the download from the external feed again."); Thread.Sleep(wait); } else { var helpfulFailure = $"The package {packageId} version {version} could not be downloaded from the external feed '{feedUri}' after making {maxDownloadAttempts} attempts over a total of {Math.Floor(retry.TotalElapsed.TotalSeconds)}s. Make sure the package is pushed to the external feed and try the deployment again. For a detailed troubleshooting guide go to http://g.octopushq.com/TroubleshootMissingPackages"; helpfulFailure += $"{Environment.NewLine}{ex}"; throw new Exception(helpfulFailure, ex); } } } }
PackagePhysicalFileMetadata DownloadChart(string packageId, IVersion version, Uri feedUri, ICredentials feedCredentials, string cacheDirectory) { var cred = feedCredentials.GetCredential(feedUri, "basic"); var tempDirectory = fileSystem.CreateTemporaryDirectory(); using (new TemporaryDirectory(tempDirectory)) { var homeDir = Path.Combine(tempDirectory, "helm"); if (!Directory.Exists(homeDir)) { Directory.CreateDirectory(homeDir); } var stagingDir = Path.Combine(tempDirectory, "staging"); if (!Directory.Exists(stagingDir)) { Directory.CreateDirectory(stagingDir); } var log = new LogWrapper(); Invoke($"init --home \"{homeDir}\" --client-only", tempDirectory, log); Invoke($"repo add --home \"{homeDir}\" {(string.IsNullOrEmpty(cred.UserName) ? "" : $"--username \"{cred.UserName}\" --password \"{cred.Password}\"")} {TempRepoName} {feedUri.ToString()}", tempDirectory, log); Invoke($"fetch --home \"{homeDir}\" --version \"{version}\" --destination \"{stagingDir}\" {TempRepoName}/{packageId}", tempDirectory, log); var localDownloadName = Path.Combine(cacheDirectory, PackageName.ToCachedFileName(packageId, version, Extension)); fileSystem.MoveFile(Directory.GetFiles(stagingDir)[0], localDownloadName); return(PackagePhysicalFileMetadata.Build(localDownloadName)); }
private PackagePhysicalFileMetadata DownloadChart(string packageId, IVersion version, Uri feedUri, ICredentials feedCredentials, string cacheDirectory) { var cred = feedCredentials.GetCredential(feedUri, "basic"); var syntax = new[] { ScriptSyntax.PowerShell, ScriptSyntax.Bash }.First(syntx => scriptEngine.GetSupportedTypes().Contains(syntx)); var tempDirectory = fileSystem.CreateTemporaryDirectory(); using (new TemporaryDirectory(tempDirectory)) { var file = GetFetchScript(tempDirectory, syntax); var result = scriptEngine.Execute(new Script(file), new CalamariVariableDictionary() { ["Password"] = cred.Password, ["Username"] = cred.UserName, ["Version"] = version.OriginalString, ["Url"] = feedUri.ToString(), ["Package"] = packageId, }, commandLineRunner, new Dictionary <string, string>()); if (!result.HasErrors) { var localDownloadName = Path.Combine(cacheDirectory, PackageName.ToCachedFileName(packageId, version, Extension)); var packageFile = fileSystem.EnumerateFiles(Path.Combine(tempDirectory, "staging")).First(); fileSystem.MoveFile(packageFile, localDownloadName); return(PackagePhysicalFileMetadata.Build(localDownloadName)); } else { throw new Exception("Unable to download chart"); } } }