internal void CopyFile( [Required, Description("Machine to copy from")] string host, [Required, Description("Expected content hash")] string hashString, [Required, Description("Path to destination file")] string destinationPath, [Description("Whether or not GZip is enabled"), DefaultValue(false)] bool useCompressionForCopies, [Description("File name where the GRPC port can be found when using cache service. 'CASaaS GRPC port' if not specified")] string grpcPortFileName, [Description("The GRPC port"), DefaultValue(0)] int grpcPort) { Initialize(); var context = new Context(_logger); var retryPolicy = RetryPolicyFactory.GetLinearPolicy(ex => ex is ClientCanRetryException, (int)_retryCount, TimeSpan.FromSeconds(_retryIntervalSeconds)); if (grpcPort == 0) { grpcPort = Helpers.GetGrpcPortFromFile(_logger, grpcPortFileName); } if (!ContentHash.TryParse(hashString, out var hash)) { throw new CacheException($"Invalid content hash string provided: {hashString}"); } try { var config = new GrpcCopyClientConfiguration(); using var clientCache = new GrpcCopyClientCache(context, new GrpcCopyClientCacheConfiguration() { GrpcCopyClientConfiguration = config }); var finalPath = new AbsolutePath(destinationPath); var copyFileResult = clientCache.UseAsync(new OperationContext(context), host, grpcPort, (nestedContext, rpcClient) => { return(retryPolicy.ExecuteAsync( () => rpcClient.CopyFileAsync(nestedContext, hash, finalPath, new CopyOptions(bandwidthConfiguration: null) { CompressionHint = useCompressionForCopies ? CopyCompression.Gzip : CopyCompression.None, }), _cancellationToken)); }).GetAwaiter().GetResult(); if (!copyFileResult.Succeeded) { throw new CacheException(copyFileResult.ErrorMessage); } else { _logger.Debug($"Copy of {hashString} to {finalPath} was successful"); } } catch (Exception ex) { throw new CacheException(ex.ToString()); } }
internal void CopyFileTo( [Required, Description("Machine to copy to")] string host, [Required, Description("Path to source file")] string sourcePath, [Description("File name where the GRPC port can be found when using cache service. 'CASaaS GRPC port' if not specified")] string grpcPortFileName, [Description("The GRPC port"), DefaultValue(0)] int grpcPort) { Initialize(); var context = new Context(_logger); var operationContext = new OperationContext(context, CancellationToken.None); var retryPolicy = RetryPolicyFactory.GetLinearPolicy(ex => ex is ClientCanRetryException, (int)_retryCount, TimeSpan.FromSeconds(_retryIntervalSeconds)); if (grpcPort == 0) { grpcPort = Helpers.GetGrpcPortFromFile(_logger, grpcPortFileName); } var hasher = ContentHashers.Get(HashType.MD5); var bytes = File.ReadAllBytes(sourcePath); var hash = hasher.GetContentHash(bytes); try { var path = new AbsolutePath(sourcePath); using Stream stream = File.OpenRead(path.Path); var config = GrpcCopyClientConfiguration.WithGzipCompression(false); config.BandwidthCheckerConfiguration = BandwidthChecker.Configuration.Disabled; using var clientCache = new GrpcCopyClientCache(context, new GrpcCopyClientCacheConfiguration() { GrpcCopyClientConfiguration = config }); var copyFileResult = clientCache.UseAsync(operationContext, host, grpcPort, (nestedContext, rpcClient) => { return(retryPolicy.ExecuteAsync( () => rpcClient.PushFileAsync(nestedContext, hash, stream, new CopyOptions(bandwidthConfiguration: null)), _cancellationToken)); }).GetAwaiter().GetResult(); if (!copyFileResult.Succeeded) { _tracer.Error(context, $"{copyFileResult}"); throw new CacheException(copyFileResult.ErrorMessage); } else { _tracer.Info(context, $"Copy of {sourcePath} was successful"); } } catch (Exception ex) { throw new CacheException(ex.ToString()); } }