/// <summary> /// Installs the package locally and pushes the package onto the destination server /// Uses OptimizedPackage much like the 'nuget push' command to prevent issues caused by /// holding onto package as a stream in memory /// </summary> private void MirrorPackage(DataServicePackageWithCreated package, PackageServer destinationServer, IPackageManager tempPackageManager, LocalPackageRepository tempLocalRepo, string apiKey, int timeOut) { // Download the package locally into a temp folder. This prevents storing the package in memory // which becomes an issue with large packages. Push command uses OptimizedZipPackage and we will too string localPackagePath = String.Empty; OptimizedZipPackage localPackage = null; try { Log.AddingPackageLocally(package.ToString(), package.Created.Value.DateTime.ToString()); tempPackageManager.InstallPackage(package.Id, package.SemanticVersion, ignoreDependencies: true, allowPrereleaseVersions: true); Log.AddedPackageLocally(package.ToString()); } catch (Exception ex) { throw new SourceException(ex); } try { // Push the local package onto destination Repository var localInstallPath = tempLocalRepo.PathResolver.GetInstallPath(package); localPackagePath = Path.Combine(localInstallPath, tempLocalRepo.PathResolver.GetPackageFileName(package)); localPackage = new OptimizedZipPackage(localPackagePath); Log.PushingPackage(localPackage.ToString()); destinationServer.PushPackage(apiKey, localPackage, new FileInfo(localPackagePath).Length, timeOut, disableBuffering: false); } catch (Exception ex) { throw new DestinationException(ex); } }
public override void ExecuteCommand() { //First argument should be the package ID string packageId = Arguments[0]; //Second argument should be the package Version string packageVersion = Arguments[1]; //If the user passed a source use it for the gallery location string source = SourceProvider.ResolveAndValidateSource(Source) ?? NuGetConstants.DefaultGalleryServerUrl; var gallery = new PackageServer(source, CommandLineConstants.UserAgent); //If the user did not pass an API Key look in the config file string apiKey = GetApiKey(source); string sourceDisplayName = CommandLineUtility.GetSourceDisplayName(source); if (NoPrompt || Console.Confirm(String.Format(CultureInfo.CurrentCulture, NuGetResources.DeleteCommandConfirm, packageId, packageVersion, sourceDisplayName))) { Console.WriteLine(NuGetResources.DeleteCommandDeletingPackage, packageId, packageVersion, sourceDisplayName); gallery.DeletePackage(apiKey, packageId, packageVersion); Console.WriteLine(NuGetResources.DeleteCommandDeletedPackage, packageId, packageVersion); } else { Console.WriteLine(NuGetResources.DeleteCommandCanceled); } }
private void PushPackage(string packagePath, string source, string apiKey, TimeSpan timeout) { var packageServer = new PackageServer(source, CommandLineConstants.UserAgent); packageServer.SendingRequest += (sender, e) => { if (Console.Verbosity == NuGet.Verbosity.Detailed) { Console.WriteLine(ConsoleColor.Green, "{0} {1}", e.Request.Method, e.Request.RequestUri); } }; IEnumerable <string> packagesToPush = GetPackagesToPush(packagePath); if (!packagesToPush.Any()) { Console.WriteError(String.Format(CultureInfo.CurrentCulture, NuGetResources.UnableToFindFile, packagePath)); return; } foreach (string packageToPush in packagesToPush) { PushPackageCore(source, apiKey, packageServer, packageToPush, timeout); } }
void WritePackage(PackageBuilder builder, ManifestMetadata metadata) { //Create the Nuget package if (!Directory.Exists(NugetRepository)) { Directory.CreateDirectory(NugetRepository); } var package = NugetRepository + metadata.Id + NugetSeparator + metadata.Version + ".nupkg"; using (FileStream stream = File.Open(package, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read)) { builder.Populate(metadata); builder.Save(stream); } Trace.TraceInformation("Package {0} created locally", package); var localRepo = PackageRepositoryFactory.Default.CreateRepository(new DirectoryInfo("./nuget-repository").FullName); var nuGetPackage = PackageHelper.ResolvePackage(localRepo, metadata.Id, new SemanticVersion(metadata.Version)); var packageFile = new FileInfo(package); var size = packageFile.Length; var ps = new PackageServer(_repository, "None"); ps.PushPackage(_credential, nuGetPackage, size, 50000, false); Trace.TraceInformation("Package {0} uploaded to {1}", package, _repository); }
public void Push(string url, string key, Stream stream) { var server = new PackageServer(url, "SymbolSource"); var length = stream.Length; var package = new ZipPackage(stream); server.PushPackage(key, package, length, 5000, false); }
public void GetEndPointUrlDoesNotAppendServicePathIfOnlyHostNameIsSpecified(string baseUrl, string path, string expectedEndpoint) { // Act var value = PackageServer.GetServiceEndpointUrl(new Uri(baseUrl), path); // Assert Assert.Equal(expectedEndpoint, value.OriginalString); }
public void PublishPackage(string file, string apiKey) { var packageServer = new PackageServer("https://nuget.org/", "ripple"); var package = new OptimizedZipPackage(file); RippleLog.Info("Publishing " + file + " with " + apiKey); packageServer.PushPackage(apiKey, package, (int)60.Minutes().TotalMilliseconds); }
protected virtual void Push(ZipPackage package, long packageSize, string apiUrl, string apiKey, int timeoutInMilliseconds, bool disableBuffering) { var ps = new PackageServer(apiUrl, "MetaPack Packaging API"); ps.PushPackage(apiKey, package, packageSize, timeoutInMilliseconds, disableBuffering); }
public void PublishPackage(string pathToNupkg, string apiKey) { var fi = new FileInfo(pathToNupkg); var localRepo = PackageRepositoryFactory.Default.CreateRepository(fi.Directory.FullName); var package = localRepo.FindPackagesById(fi.Name.Replace(fi.Extension, string.Empty)).First(); var size = fi.Length; var ps = new PackageServer(PublishDestination.ToString(), "userAgent"); ps.PushPackage(apiKey, package, size, 1800, false); }
public void PrepareSamplePackage() { package = LoadSamplePackage("Package", "1.0.0"); using (var stream = package.GetStream()) { packageSize = stream.Length; } server = new PackageServer(ServerUrl + "api/packages", ""); }
private void PushPackageCore(string source, string apiKey, PackageServer packageServer, string packageToPush, TimeSpan timeout) { // Push the package to the server var package = new ZipPackage(packageToPush); string sourceName = CommandLineUtility.GetSourceDisplayName(source); Console.WriteLine(NuGetResources.PushCommandPushingPackage, package.GetFullName(), sourceName); packageServer.PushPackage(apiKey, package.GetStream, Convert.ToInt32(timeout.TotalMilliseconds)); Console.WriteLine(NuGetResources.PushCommandPackagePushed); }
public static void push_package(ChocolateyConfiguration config, string nupkgFilePath) { var timeout = TimeSpan.FromSeconds(Math.Abs(config.CommandExecutionTimeoutSeconds)); if (timeout.Seconds <= 0) { timeout = TimeSpan.FromMinutes(300); // Default to 5 hours if there is a zero (infinite) timeout } const bool disableBuffering = false; var packageServer = new PackageServer(config.Sources, ApplicationParameters.UserAgent); packageServer.SendingRequest += (sender, e) => { if (config.Verbose) { "chocolatey".Log().Info(ChocolateyLoggers.Verbose, "{0} {1}".format_with(e.Request.Method, e.Request.RequestUri)); } }; var package = new OptimizedZipPackage(nupkgFilePath); try { packageServer.PushPackage( config.PushCommand.Key, package, new FileInfo(nupkgFilePath).Length, Convert.ToInt32(timeout.TotalMilliseconds), disableBuffering); } catch (InvalidOperationException ex) { var message = ex.Message; if (!string.IsNullOrWhiteSpace(message)) { if (config.Sources == ApplicationParameters.ChocolateyCommunityFeedPushSource && message.Contains("already exists and cannot be modified")) { throw new ApplicationException("An error has occurred. This package version already exists on the repository and cannot be modified.{0}Package versions that are approved, rejected, or exempted cannot be modified.{0}See https://docs.chocolatey.org/en-us/community-repository/moderation/ for more information".format_with(Environment.NewLine), ex); } if (message.Contains("(406)") || message.Contains("(409)")) { // Let this fall through so the actual error message is shown when the exception is re-thrown "chocolatey".Log().Error("An error has occurred. It's possible the package version already exists on the repository or a nuspec element is invalid. See error below..."); } } throw; } "chocolatey".Log().Info(ChocolateyLoggers.Important, () => "{0} was pushed successfully to {1}".format_with(package.GetFullName(), config.Sources)); }
// POST /api/sign public async Task <HttpResponseMessage> Post([FromBody] WebHookEvent payload) { if (payload.Payload.PackageIdentifier.EndsWith(ConfigurationManager.AppSettings["Signature:PackageIdSuffix"])) { return(new HttpResponseMessage(HttpStatusCode.OK) { ReasonPhrase = "Package is already signed. " }); } string tempPath = Path.GetTempFileName(); try { // Download the package var httpClient = new HttpClient(); var packageStream = await httpClient.GetStreamAsync(payload.Payload.PackageDownloadUrl); using (var stream = new FileStream(tempPath, FileMode.OpenOrCreate, FileAccess.Write)) { packageStream.CopyTo(stream); } // Sign the package PackageSigner signer = new PackageSigner(); if (signer.SignPackage(tempPath, tempPath, System.Web.Hosting.HostingEnvironment.MapPath("~/" + ConfigurationManager.AppSettings["Signature:KeyFile"]), ConfigurationManager.AppSettings["Signature:KeyFilePassword"], payload.Payload.PackageIdentifier + ConfigurationManager.AppSettings["Signature:PackageIdSuffix"])) { var server = new PackageServer(ConfigurationManager.AppSettings["Signature:NuGetFeedUrl"], "Signature/1.0"); server.PushPackage(ConfigurationManager.AppSettings["Signature:NuGetFeedApiKey"], new OptimizedZipPackage(tempPath), new FileInfo(tempPath).Length, 60 * 1000, true); OptimizedZipPackage.PurgeCache(); return(new HttpResponseMessage(HttpStatusCode.Created) { ReasonPhrase = "Package has been signed." }); } } finally { File.Delete(tempPath); } return(new HttpResponseMessage(HttpStatusCode.OK) { ReasonPhrase = "Package is already signed." }); }
private void PushPackageCore(string source, string apiKey, PackageServer packageServer, string packageToPush, TimeSpan timeout) { // Push the package to the server var package = new OptimizedZipPackage(packageToPush); string sourceName = CommandLineUtility.GetSourceDisplayName(source); Console.WriteLine(LocalizedResourceManager.GetString("PushCommandPushingPackage"), package.GetFullName(), sourceName); packageServer.PushPackage( apiKey, package, new FileInfo(packageToPush).Length, Convert.ToInt32(timeout.TotalMilliseconds)); Console.WriteLine(LocalizedResourceManager.GetString("PushCommandPackagePushed")); }
/// <summary>Publishes to the NuGet main repository.</summary> public static void PublishToNuget(string packagePath, string nugetApiKey) { TraceSource.TraceEvent(TraceEventType.Verbose, "Pushing \"{0}\" to NuGet repository", packagePath); PackageServer packageServer = new PackageServer("https://nuget.org/", "Google.Apis"); try { packageServer.PushPackage(nugetApiKey, new ZipPackage(packagePath), NuGetPushTimeoutMills); TraceSource.TraceEvent(TraceEventType.Information, "\"{0}\" was pushed successfully", packagePath); } catch (Exception ex) { TraceSource.TraceEvent(TraceEventType.Error, "Can't publish \"{0}\" to NuGet repository. " + "Exception is: {1}", packagePath, ex.Message); } }
public void Push(string serverUrl, string apiKey = null, double timeout = 600.0) { if (_packages.Count == 0) { throw new InvalidOperationException("No input files"); } var server = new PackageServer(serverUrl, UserAgent); var ms = (int)(timeout * 1000.0); foreach (var upk in _packages) { using (Log.StartAnimation("Uploading " + upk, ConsoleColor.Blue)) server.PushPackage(apiKey, upk.ZipPackage, upk.ZipPackageSize, ms, false); } }
private void PushPackage(string packagePath, string source, string apiKey, TimeSpan timeout) { var packageServer = new PackageServer(source, CommandLineConstants.UserAgent); IEnumerable <string> packagesToPush = GetPackagesToPush(packagePath); if (!packagesToPush.Any()) { Console.WriteError(String.Format(CultureInfo.CurrentCulture, NuGetResources.UnableToFindFile, packagePath)); return; } foreach (string packageToPush in packagesToPush) { PushPackageCore(source, apiKey, packageServer, packageToPush, timeout); } }
public static void delete_package(MyNugetPackage packageToDelete, ChocolateyConfiguration config) { var timeout = TimeSpan.FromSeconds(Math.Abs(config.CommandExecutionTimeoutSeconds)); if (timeout.Seconds <= 0) { int timeoutMinutes = 10; "chocolatey".Log().Debug(ChocolateyLoggers.Verbose, "Updating timeout to: {0} minutes because configured timeout value is invalid: {1}".format_with(timeoutMinutes, config.CommandExecutionTimeoutSeconds)); timeout = TimeSpan.FromMinutes(timeoutMinutes); // Default to 10 minutes hours if there is a zero (infinite) timeout } var packageServer = new PackageServer(config.Sources, ApplicationParameters.UserAgent); packageServer.SendingRequest += (sender, e) => { if (config.Verbose) { "chocolatey".Log().Info(ChocolateyLoggers.Verbose, "{0} {1}".format_with(e.Request.Method, e.Request.RequestUri)); } }; string packageId = packageToDelete.Id; string packageVersion = packageToDelete.Version.ToString(); string packageFullName = "{0}-{1}".format_with(packageId, packageVersion); try { packageServer.DeletePackage( config.PushCommand.Key, packageId, packageVersion); } catch (InvalidOperationException ex) { var message = ex.Message; if (!string.IsNullOrWhiteSpace(message)) { if (message.Contains("(404)")) { // Let this fall through so the actual error message is shown when the exception is re-thrown "chocolatey".Log().Error("An error has occurred. It's very much possible that the package version doesn't exist on the repository. See error below..."); } } throw; } "chocolatey".Log().Info(ChocolateyLoggers.Important, () => "{0} was successfully deleted from source: {1}".format_with(packageFullName, config.Sources)); }
public PackageServerRepository(IPackageRepository sourceRepository, PackageServer destination, string apiKey, TimeSpan timeout, ILogger logger) { if (sourceRepository == null) { throw new ArgumentNullException("sourceRepository"); } if (destination == null) { throw new ArgumentNullException("destination"); } _source = sourceRepository; _destination = destination; _apiKey = apiKey; _timeout = timeout; _logger = logger ?? NullLogger.Instance; }
public override void ExecuteCommand() { if (NoPrompt) { Console.WriteWarning(NuGetResources.Warning_NoPromptDeprecated); NonInteractive = true; } //First argument should be the package ID string packageId = Arguments[0]; //Second argument should be the package Version string packageVersion = Arguments[1]; //If the user passed a source use it for the gallery location string source = SourceProvider.ResolveAndValidateSource(Source) ?? NuGetConstants.DefaultGalleryServerUrl; var gallery = new PackageServer(source, CommandLineConstants.UserAgent); gallery.SendingRequest += (sender, e) => { if (Console.Verbosity == NuGet.Verbosity.Detailed) { Console.WriteLine(ConsoleColor.Green, "{0} {1}", e.Request.Method, e.Request.RequestUri); } }; //If the user did not pass an API Key look in the config file string apiKey = GetApiKey(source); string sourceDisplayName = CommandLineUtility.GetSourceDisplayName(source); if (String.IsNullOrEmpty(apiKey)) { Console.WriteWarning(NuGetResources.NoApiKeyFound, sourceDisplayName); } if (NonInteractive || Console.Confirm(String.Format(CultureInfo.CurrentCulture, NuGetResources.DeleteCommandConfirm, packageId, packageVersion, sourceDisplayName))) { Console.WriteLine(NuGetResources.DeleteCommandDeletingPackage, packageId, packageVersion, sourceDisplayName); gallery.DeletePackage(apiKey, packageId, packageVersion); Console.WriteLine(NuGetResources.DeleteCommandDeletedPackage, packageId, packageVersion); } else { Console.WriteLine(NuGetResources.DeleteCommandCanceled); } }
public static void push_package(ChocolateyConfiguration config, string nupkgFilePath) { var timeout = TimeSpan.FromSeconds(Math.Abs(config.PushCommand.TimeoutInSeconds)); if (timeout.Seconds <= 0) { timeout = TimeSpan.FromMinutes(5); // Default to 5 minutes } const bool disableBuffering = false; var packageServer = new PackageServer(config.Sources, ApplicationParameters.UserAgent); packageServer.SendingRequest += (sender, e) => { if (config.Verbose) { "chocolatey".Log().Info(ChocolateyLoggers.Verbose, "{0} {1}".format_with(e.Request.Method, e.Request.RequestUri)); } }; var package = new OptimizedZipPackage(nupkgFilePath); try { packageServer.PushPackage( config.PushCommand.Key, package, new FileInfo(nupkgFilePath).Length, Convert.ToInt32(timeout.TotalMilliseconds), disableBuffering); } catch (InvalidOperationException ex) { var message = ex.Message; if (!string.IsNullOrWhiteSpace(message) && message.Contains("(500) Internal Server Error")) { throw new ApplicationException("There was an internal server error, which might mean the package already exists on a Simple OData Server.", ex); } throw; } "chocolatey".Log().Info(ChocolateyLoggers.Important, () => "{0} was pushed successfully to {1}".format_with(package.GetFullName(), config.Sources)); }
public static void push_package(ChocolateyConfiguration config, string nupkgFilePath) { var timeout = TimeSpan.FromSeconds(Math.Abs(config.CommandExecutionTimeoutSeconds)); if (timeout.Seconds <= 0) { timeout = TimeSpan.FromMinutes(300); // Default to 5 hours if there is a zero (infinite) timeout } const bool disableBuffering = false; var packageServer = new PackageServer(config.Sources, ApplicationParameters.UserAgent); packageServer.SendingRequest += (sender, e) => { if (config.Verbose) { "chocolatey".Log().Info(ChocolateyLoggers.Verbose, "{0} {1}".format_with(e.Request.Method, e.Request.RequestUri)); } }; var package = new OptimizedZipPackage(nupkgFilePath); try { packageServer.PushPackage( config.PushCommand.Key, package, new FileInfo(nupkgFilePath).Length, Convert.ToInt32(timeout.TotalMilliseconds), disableBuffering); } catch (InvalidOperationException ex) { var message = ex.Message; if (!string.IsNullOrWhiteSpace(message) && (message.Contains("(406)") || message.Contains("(409)"))) { throw new ApplicationException("An error has occurred. It's possible the package version already exists on the repository.", ex); } throw; } "chocolatey".Log().Info(ChocolateyLoggers.Important, () => "{0} was pushed successfully to {1}".format_with(package.GetFullName(), config.Sources)); }
protected internal override IPackagePushLocation GetPushLocation() { if (ServerPath == null) { ServerPath = new Uri("https://nuget.org"); } PackageServer server = new PackageServer(ServerPath.ToString(), "NAnt.NuGet"); string apiKey = ApiKey; if (apiKey == null && ApiKeyFile != null) { if (!ApiKeyFile.Exists) { throw new BuildException(String.Format("Api key-file '{0}' does not exist.", ApiKeyFile.FullName)); } apiKey = File.ReadAllText(ApiKeyFile.FullName); } return(new ServerPushLocation(apiKey, server)); }
private void PushPackage(string packagePath, string source, string apiKey, TimeSpan timeout) { var packageServer = new PackageServer(source, CommandLineConstants.UserAgent); packageServer.SendingRequest += (sender, e) => { if (Console.Verbosity == NuGet.Verbosity.Detailed) { Console.WriteLine(ConsoleColor.Green, "{0} {1}", e.Request.Method, e.Request.RequestUri); } }; IEnumerable <string> packagesToPush = GetPackagesToPush(packagePath); EnsurePackageFileExists(packagePath, packagesToPush); foreach (string packageToPush in packagesToPush) { PushPackageCore(source, apiKey, packageServer, packageToPush, timeout); } }
public void Publish(string packagePath, string apiKey) { var logPath = _pathService.GetRelative(packagePath); _context.WriteTrace(LogEntryType.Information, $"Pushing \"{logPath}\" to NuGet repository"); var packageServer = new PackageServer(_sourceUrl, _userAgent); try { var package = new ZipPackage(packagePath); using (var stream = package.GetStream()) { packageServer.PushPackage(apiKey, package, stream.Length, _publishTimeout, true); _context.WriteTrace(LogEntryType.Information, $"\"{logPath}\" was pushed successfully"); } } catch (Exception ex) { Trace.TraceError("*Exception: {0}, StackTrace", ex.Message, ex.StackTrace); throw; } }
public async Task PublishNugetPackage(PublishOptions options) { try { await Task.Run(() => { IPackage localPackage = GetLocalPackage(options); FileInfo packageFileInfo = new FileInfo(options.PackagePath); PackageServer packageServer = new PackageServer("https://www.nuget.org/api/v2/package", "OneClickNuget"); packageServer.PushPackage(options.ApiKey, localPackage, packageFileInfo.Length, 180000, false); }); } catch (Exception ex) { throw new Exception("Could not publish package. " + ex.Message); } }
private void PushPackage(string packagePath, string source, string apiKey) { var packageServer = new PackageServer(source, "NuGet Command Line"); // Push the package to the server var package = new ZipPackage(packagePath); bool complete = false; //HACK no pretty source name, as they have made the call to CommandLineUtility.GetSourceDisplayName(source) internal Console.WriteLine("Pushing {0} to {1}", package.GetFullName(), source); try { using (Stream stream = package.GetStream()) { packageServer.PushPackage(apiKey, stream, 60000); } } catch { if (!complete) { Console.WriteLine(); } throw; } // Publish the package on the server var cmd = new PublishCommand(); cmd.Console = Console; cmd.Source = source; cmd.Arguments.AddRange(new List <string> { package.Id, package.Version.ToString(), apiKey }); cmd.Execute(); }
private void PushPackage(string packagePath, string source, string apiKey) { var packageServer = new PackageServer(source, "NuGet Command Line"); // Push the package to the server var package = new ZipPackage(packagePath); //HACK no pretty source name, as they have made the call to CommandLineUtility.GetSourceDisplayName(source) internal Console.WriteLine("Pushing {0} to {1}", package.GetFullName(), source); try { using (package.GetStream()) { var fileSize = new FileInfo(packagePath).Length; packageServer.PushPackage(apiKey, package, fileSize, 60000, false); } } catch { Console.WriteLine(); throw; } }
public IPublishReportItem PublishPackage(string serverUrl, string file, string apiKey) { var packageServer = new PackageServer(serverUrl, "ripple"); var package = new OptimizedZipPackage(file); RippleLog.Info("Publishing " + file + " with " + apiKey); try { var info = new FileInfo(file); packageServer.PushPackage(apiKey, package, info.Length, (int)60.Minutes().TotalMilliseconds); return(new PublishSuccessful(package)); } catch (Exception ex) { // TODO -- Hate this if (ex.Message.Contains("exists")) { return(new VersionAlreadyExists(package)); } return(new PublishFailure(package, ex)); } }
protected async internal override Task Execute() { // Validate mandatory parameters if (String.IsNullOrEmpty(SourceV2Feed)) { throw new ArgumentException("SourceV2Feed cannot be null or empty"); } // If the SourceV2Feed is not a URI, the following line will throw URIFormatException var sourceV2FeedUri = new Uri(SourceV2Feed); if (String.IsNullOrEmpty(DestinationUri)) { throw new ArgumentException("DestinationUri cannot be null or empty"); } if (String.IsNullOrEmpty(ApiKey)) { throw new ArgumentException("ApiKey cannot be null or empty"); } // Packages are in Legacy account. PackageDatabase is the InitialCatalog in the legacy account var account = Config.Storage.Legacy; var cstr = Config.Sql.Legacy; if (cstr == null) { throw new ArgumentNullException("Legacy sql cannot be null"); } // Arrange or set defaults for parameters that are not provided UserAgent = String.Format("{0}v2FeedMirrorer", sourceV2FeedUri.DnsSafeHost); var timeOutPerPush = TimeSpan.FromMinutes(PushTimeOutInMinutes); MirrorStorage = MirrorStorage ?? Config.Storage.Legacy; if (MirrorStorage == null) { throw new ArgumentNullException("MirrorStorage", "Mirror storage is not provided or present in the config"); } MirrorBlobContainerName = String.IsNullOrEmpty(MirrorBlobContainerName) ? DefaultMirrorBlobContainerName : MirrorBlobContainerName; MirrorBlobContainer = MirrorStorage.CreateCloudBlobClient().GetContainerReference(MirrorBlobContainerName); MirrorBlobName = String.IsNullOrEmpty(MirrorBlobName) ? DefaultMirrorBlobName : MirrorBlobName; var sourceUri = new Uri(sourceV2FeedUri, "/api/v2/"); var serviceContext = new DataServices.DataServiceContext(sourceUri) { MergeOption = DataServices.MergeOption.OverwriteChanges, IgnoreMissingProperties = true, }; var mirrorJson = await GetJObject(MirrorBlobContainer, MirrorBlobName); if (!IsMirrorJsonValid(mirrorJson)) { throw new InvalidOperationException("mirrorJson is not valid. Either packageIndex array is not present. Or, the elements are not sorted by SourceCreatedDate"); } Exception caughtException = null; if (ExecuteDeletes) { try { await NuGetV2RepositoryMirrorPackageDeletor.DeleteAndSetListedPackages(sourceUri, mirrorJson, account, cstr); } catch (Exception ex) { caughtException = ex; } await SetJObject(MirrorBlobContainer, MirrorBlobName, mirrorJson); if (caughtException != null) { throw caughtException; } return; } var oldLastCreated = mirrorJson.Value <DateTime>(LastCreatedKey); var lastCreated = oldLastCreated; Log.PreparingToMirror(MirrorBlobName, lastCreated.ToString(DateTimeFormatSpecifier), lastCreated.Kind.ToString(), sourceUri.AbsoluteUri, DestinationUri); int retries = 0; int count = 0; int skipIndex = 0; // // POSSIBLE ACTIONS when an error is encountered // A) Always Skip to next package // B) Retry 'MaxRetries' times and skip to next package // C) Retry 'MaxRetries' times and fail // // KNOWN ERRORS // 1) '409 Conflict' from Destination- Package already exists in destination // i) Action: (A). Always Skip to next package // ii) Before skipping, Update lastMirroredPackage.Created locally, i.e inside the while loop // 2) '403 Forbidden' from Source. For reasons unknown, certain listed packages are not available for download. Source returns "Access Denied" // i) Action: (B). Retry 'MaxRetries' times and Skip to next package // ii) Log every retry. Before skipping, Update lastMirroredPackage.Created locally, i.e inside the while loop // 3) '404 Not Found' from Source. Package is available on the feed but not available for download already // i) Action: (C). Retry 'MaxRetries' times and Fail // ii) Log every retry. Update lastMirroredPackage.Created in blob storage // 4) Unknown Error // i) Action: (C). Retry 'MaxRetries' times and Fail // ii) Log every retry. Update lastMirroredPackage.Created in blob storage // // Test Cases // 1) Add a new package to the source. COVERED // Result: The new package should be present on the destination // 2) Add a package as unlisted to the source. COVERED // Result: The new package should be present on the destination as unlisted // 3) Delete a package version from the source. COVERED // Result: The package should be deleted from the destination // 4) Delete a package version from the source which is the last version with the package Id. NOT COVERED // Result: The package should be deleted from the destination. And, PackageRegistration should be deleted too as appropriate // 5) Delete a package version from the source. And, add a new package with same Id and version as the deleted one. COVERED // Result: Old package with Id and version must be deleted from the destination. And, the new package with the same Id and Version must be added // 6) Mark a listed package as unlisted. COVERED // Result: The package should be unlisted in the destination too // 7) Mark an unlisted package as listed. COVERED // Result: The package should be listed in the destination too // try { do { if ((Invocation.NextVisibleAt - DateTimeOffset.UtcNow) < TimeSpan.FromMinutes(120)) { // Based on default configuration on the repository, there are at most 40 packages returned by a single query (which will get downloaded and pushed) // With a conservative estimate of 3 minutes per package, expecting a minimum of // 120 minutes to run 1 iteration of this do-while loop await Extend(TimeSpan.FromMinutes(120)); } // In each query, at most, 40 packages may be returned. So, continue performing the queries // in this do-while, so long as there are results returned // Query for packages created since oldLastCreated PackageServer destinationServer = new PackageServer(DestinationUri, UserAgent); var lastMirroredPackage = QueryAndMirrorBatch(serviceContext, destinationServer, oldLastCreated, ApiKey, timeOutPerPush.Milliseconds, mirrorJson, ref retries, ref count, ref skipIndex, cstr, account); if (lastMirroredPackage != null) { if (!lastMirroredPackage.Created.HasValue) { throw new InvalidOperationException("Last mirrored package : " + lastMirroredPackage.ToString() + "has a null Created Time.. WRONG!!!"); } // Note that the Created DateTime is always stored in UTC, but the DateTimeKind of the value obtained is Unspecified // So, store it as UTC lastCreated = new DateTime(lastMirroredPackage.Created.Value.DateTime.Ticks, DateTimeKind.Utc); Log.EndOfIteration(lastCreated.ToString(DateTimeFormatSpecifier)); } else if (retries == 0 || retries > MaxRetries) { break; } } while (true); } catch (Exception ex) { // Catch the exception here so that new lastCreated is always stored // We can throw this exception at the end caughtException = ex; } if (!oldLastCreated.Equals(lastCreated)) { mirrorJson[LastCreatedKey] = lastCreated.ToString(DateTimeFormatSpecifier); await SetJObject(MirrorBlobContainer, MirrorBlobName, mirrorJson); Log.NewLastCreatedAtEndOfInvocation(MirrorBlobName, lastCreated.ToString(DateTimeFormatSpecifier), lastCreated.Kind.ToString()); } if (caughtException != null) { throw caughtException; } }
private void PushPackage(string packagePath, string source, string apiKey) { var packageServer = new PackageServer(source, CommandLineConstants.UserAgent); // Push the package to the server var package = new ZipPackage(packagePath); string sourceName = CommandLineUtility.GetSourceDisplayName(source); Console.WriteLine(NuGetResources.PushCommandPushingPackage, package.GetFullName(), sourceName); using (Stream stream = package.GetStream()) { packageServer.PushPackage(apiKey, stream); } if (CreateOnly) { Console.WriteWarning(NuGetResources.Warning_PublishPackageDeprecated); } Console.WriteLine(NuGetResources.PushCommandPackagePushed); }
private void PushPackage(string packagePath, string source, string apiKey, TimeSpan timeout) { var packageServer = new PackageServer(source, CommandLineConstants.UserAgent); packageServer.SendingRequest += (sender, e) => { if (Console.Verbosity == NuGet.Verbosity.Detailed) { Console.WriteLine(ConsoleColor.Green, "{0} {1}", e.Request.Method, e.Request.RequestUri); } }; IEnumerable<string> packagesToPush = GetPackagesToPush(packagePath); if (!packagesToPush.Any()) { Console.WriteError(String.Format(CultureInfo.CurrentCulture, NuGetResources.UnableToFindFile, packagePath)); return; } foreach (string packageToPush in packagesToPush) { PushPackageCore(source, apiKey, packageServer, packageToPush, timeout); } }
private void PushPackageCore(string source, string apiKey, PackageServer packageServer, string packageToPush, TimeSpan timeout) { // Push the package to the server var package = new OptimizedZipPackage(packageToPush); string sourceName = CommandLineUtility.GetSourceDisplayName(source); Console.WriteLine(LocalizedResourceManager.GetString("PushCommandPushingPackage"), package.GetFullName(), sourceName); packageServer.PushPackage( apiKey, package, new FileInfo(packageToPush).Length, Convert.ToInt32(timeout.TotalMilliseconds), DisableBuffering); Console.WriteLine(LocalizedResourceManager.GetString("PushCommandPackagePushed")); }
private void PushPackage(string packagePath, string source, string apiKey) { var packageServer = new PackageServer(source, "NuGet Command Line"); // Push the package to the server var package = new ZipPackage(packagePath); bool complete = false; //HACK no pretty source name, as they have made the call to CommandLineUtility.GetSourceDisplayName(source) internal Console.WriteLine("Pushing {0} to {1}", package.GetFullName(), source); try { using (Stream stream = package.GetStream()) { packageServer.PushPackage(apiKey, stream, 60000); } } catch { if (!complete) { Console.WriteLine(); } throw; } // Publish the package on the server var cmd = new PublishCommand(); cmd.Console = Console; cmd.Source = source; cmd.Arguments.AddRange(new List<string> { package.Id, package.Version.ToString(), apiKey }); cmd.Execute(); }
private void PushPackage(string packagePath, string source, string apiKey, TimeSpan timeout) { var packageServer = new PackageServer(source, CommandLineConstants.UserAgent); IEnumerable<string> packagesToPush = GetPackagesToPush(packagePath); if (!packagesToPush.Any()) { Console.WriteError(String.Format(CultureInfo.CurrentCulture, NuGetResources.UnableToFindFile, packagePath)); return; } foreach (string packageToPush in packagesToPush) { PushPackageCore(source, apiKey, packageServer, packageToPush, timeout); } }
private void PushPackageCore(string source, string apiKey, PackageServer packageServer, string packageToPush, TimeSpan timeout) { // Push the package to the server var package = new ZipPackage(packageToPush); string sourceName = CommandLineUtility.GetSourceDisplayName(source); Console.WriteLine(NuGetResources.PushCommandPushingPackage, package.GetFullName(), sourceName); using (Stream stream = package.GetStream()) { packageServer.PushPackage(apiKey, stream, Convert.ToInt32(timeout.TotalMilliseconds)); } if (CreateOnly) { Console.WriteWarning(NuGetResources.Warning_PublishPackageDeprecated); } Console.WriteLine(NuGetResources.PushCommandPackagePushed); }
private void PushPackageCore(string source, string apiKey, PackageServer packageServer, string packageToPush, TimeSpan timeout) { // Push the package to the server var package = new OptimizedZipPackage(packageToPush); string sourceName = CommandLineUtility.GetSourceDisplayName(source); Console.WriteLine(NuGetResources.PushCommandPushingPackage, package.GetFullName(), sourceName); packageServer.PushPackage(apiKey, package, Convert.ToInt32(timeout.TotalMilliseconds)); Console.WriteLine(NuGetResources.PushCommandPackagePushed); }
private void PushPackage(string packagePath, string source, string apiKey, TimeSpan timeout) { var packageServer = new PackageServer(source, CommandLineConstants.UserAgent); packageServer.SendingRequest += (sender, e) => { if (Console.Verbosity == NuGet.Verbosity.Detailed) { Console.WriteLine(ConsoleColor.Green, "{0} {1}", e.Request.Method, e.Request.RequestUri); } }; IEnumerable<string> packagesToPush = GetPackagesToPush(packagePath); EnsurePackageFileExists(packagePath, packagesToPush); foreach (string packageToPush in packagesToPush) { PushPackageCore(source, apiKey, packageServer, packageToPush, timeout); } }
/// <summary> /// Queries for packages created after 'lastCreated'. At most, 40 packages may be returned /// Mirror that batch of packages to the destination server /// </summary> /// <returns>Returns lastMirroredPackage or null</returns> private DataServicePackageWithCreated QueryAndMirrorBatch(DataServices.DataServiceContext serviceContext, PackageServer destinationServer, DateTime lastCreated, string apiKey, int timeOut, JObject mirrorJson, ref int retries, ref int count, ref int skipIndex, SqlConnectionStringBuilder cstr, CloudStorageAccount account) { var newPackages = GetNewPackagesToMirror(serviceContext, lastCreated); // Push packages var tempFolderPath = GetTempFolderPath(serviceContext.BaseUri.DnsSafeHost); Log.TempFolderPath(tempFolderPath); var tempLocalRepo = new LocalPackageRepository(tempFolderPath); var tempPackageManager = new PackageManager(new DataServicePackageRepository(serviceContext.BaseUri), tempFolderPath); DataServicePackageWithCreated currentPackage = null; DataServicePackageWithCreated lastMirroredPackage = null; try { do { // The following code deletes the temp folder if one exists and creates a new one GetTempFolderPath(serviceContext.BaseUri.DnsSafeHost); var newPackagesList = newPackages.Skip(skipIndex).ToList(); Log.PackagesCopyCount(newPackagesList.Count); if (newPackagesList.Count == 0) { break; } foreach (DataServicePackageWithCreated package in newPackagesList) { try { currentPackage = package; MirrorPackage(package, destinationServer, tempPackageManager, tempLocalRepo, apiKey, timeOut); var jObject = AddNewPackage(mirrorJson, package); if (!package.IsListed) { // The new package being pushed is not listed. Mark it as unlisted NuGetV2RepositoryMirrorPackageDeletor.SetListed(cstr, jObject, package.Id, package.SemanticVersion.ToString(), false).Wait(); } Log.PushedToDestination(++count); } catch (SourceException ex) { ThrowSourceExceptionIfNeeded(ex, ref retries, package); } catch (DestinationException ex) { ThrowDestinationExceptionIfNeeded(ex, package, mirrorJson, cstr, account).Wait(); } lastMirroredPackage = package; retries = 0; ++skipIndex; } } while (true); } catch (Exception ex) { retries++; Log.ServerUnreachable(retries, ex.Message); if (currentPackage != null) { Log.MirrorFailed(currentPackage.ToString()); } } // Delete the packages stored locally Directory.Delete(tempFolderPath, recursive: true); Log.DeletedTempFolder(tempFolderPath); return(lastMirroredPackage); }
private void PushPackage(string packagePath, string source, string apiKey) { var packageServer = new PackageServer(source, "NuGet Command Line"); // Push the package to the server var package = new ZipPackage(packagePath); //HACK no pretty source name, as they have made the call to CommandLineUtility.GetSourceDisplayName(source) internal Console.WriteLine("Pushing {0} to {1}", package.GetFullName(), source); try { using (package.GetStream()) { packageServer.PushPackage(apiKey, package, 60000); } } catch { Console.WriteLine(); throw; } }