protected override async Task ProcessRecordAsync() { var directory = System.IO.Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, Path ?? ""); // Ensure the path is a directory. if (!Directory.Exists(directory)) { throw new DirectoryNotFoundException(directory); } WriteVerbose(string.Format("Archiving the contents of {0}", directory)); using (var reader = Archiver.CreateTarStream(new List <string> { directory }, CmdletCancellationToken)) { var parameters = new ImageBuildParameters { NoCache = SkipCache.ToBool(), ForceRemove = ForceRemoveIntermediateContainers.ToBool(), Remove = !PreserveIntermediateContainers.ToBool(), }; string repoTag = null; if (!string.IsNullOrEmpty(Repository)) { repoTag = Repository; if (!string.IsNullOrEmpty(Tag)) { repoTag += ":"; repoTag += Tag; } parameters.Tags = new List <string> { repoTag }; } else if (!string.IsNullOrEmpty(Tag)) { throw new Exception("You must specify a repository name in order to specify a tag."); } string imageId = null; bool failed = false; var progress = new Progress <ProgressReader.Status>(); var progressRecord = new ProgressRecord(0, "Dockerfile context", "Uploading"); progress.ProgressChanged += (o, status) => { if (status.Complete) { progressRecord.CurrentOperation = null; progressRecord.StatusDescription = "Processing"; } else { progressRecord.StatusDescription = string.Format("Uploaded {0} bytes", status.TotalBytesRead); } WriteProgress(progressRecord); }; var progressReader = new ProgressReader(reader, progress, 512 * 1024); var buildTask = DkrClient.Miscellaneous.BuildImageFromDockerfileAsync(progressReader, parameters, CmdletCancellationToken); var messageWriter = new JsonMessageWriter(this); using (var buildStream = await buildTask) { // Complete the upload progress bar. progressRecord.RecordType = ProgressRecordType.Completed; WriteProgress(progressRecord); // ReadLineAsync is not cancellable without closing the whole stream, so register a callback to do just that. using (CmdletCancellationToken.Register(() => buildStream.Close())) using (var buildReader = new StreamReader(buildStream, new UTF8Encoding(false))) { string line; while ((line = await buildReader.ReadLineAsync()) != null) { var message = JsonConvert.DeserializeObject <JsonMessage>(line); if (message.Stream != null && message.Stream.StartsWith(_successfullyBuilt)) { // This is probably the image ID. imageId = message.Stream.Substring(_successfullyBuilt.Length).Trim(); } if (message.Error != null) { failed = true; } messageWriter.WriteJsonMessage(message); } } } messageWriter.ClearProgress(); if (imageId != null) { WriteObject(await ContainerOperations.GetImageById(imageId, DkrClient)); } else if (!failed) { throw new Exception("Could not find image, but no error was returned"); } } }
protected override void ProcessRecord() { base.ProcessRecord(); using (var reader = new AnonymousPipeServerStream(PipeDirection.In, HandleInheritability.None, 65536)) { var tarTask = Task.Run(async() => { using (var writer = new AnonymousPipeClientStream(PipeDirection.Out, reader.ClientSafePipeHandle)) { var tar = new TarWriter(writer); await tar.CreateEntriesFromDirectoryAsync(string.IsNullOrEmpty(Path) ? "." : Path, "."); await tar.CloseAsync(); writer.Close(); } }); var parameters = new ImageBuildParameters { NoCache = SkipCache.ToBool(), ForceRemove = ForceRemoveIntermediateContainers.ToBool(), Remove = !PreserveIntermediateContainers.ToBool(), }; string repoTag = null; if (!string.IsNullOrEmpty(Repository)) { repoTag = Repository; if (!string.IsNullOrEmpty(Tag)) { repoTag += ":"; repoTag += Tag; } parameters.Tags.Add(repoTag); } else if (!string.IsNullOrEmpty(Tag)) { throw new Exception("tag without a repo???"); } string imageId = null; bool failed = false; var buildTask = DkrClient.Miscellaneous.BuildImageFromDockerfileAsync(reader, parameters, CancelSignal.Token); using (var progress = buildTask.AwaitResult()) using (var progressReader = new StreamReader(progress, new UTF8Encoding(false))) { string line; while ((line = progressReader.ReadLine()) != null) { var message = JsonConvert.DeserializeObject <JsonMessage>(line); if (message.Stream != null) { if (message.Stream.StartsWith(_successfullyBuilt)) { // This is probably the image ID. imageId = message.Stream.Substring(_successfullyBuilt.Length).Trim(); } var infoRecord = new HostInformationMessage(); infoRecord.Message = message.Stream; WriteInformation(infoRecord, new string[] { "PSHOST" }); } if (message.Error != null) { var error = new ErrorRecord(new Exception(message.Error.Message), null, ErrorCategory.OperationStopped, null); WriteError(error); failed = true; } } } tarTask.WaitUnwrap(); if (imageId == null && !failed) { throw new Exception("Could not find image, but no error was returned"); } WriteObject(ContainerOperations.GetImageById(imageId, DkrClient)); } }