Exemplo n.º 1
0
        public static Stream CreateTarStream(IList <string> paths, CancellationToken cancellationToken, IProgress <string> progress = null)
        {
            var pipe   = new Pipe();
            var reader = new PipeReadStream(pipe);

            try
            {
                var tarTask = Task.Run(async() =>
                {
                    using (var writer = new PipeWriteStream(pipe))
                    {
                        try
                        {
                            var tar = new TarWriter(writer);
                            foreach (var path in paths)
                            {
                                var fi = new FileInfo(path);
                                if (fi.Attributes.HasFlag(FileAttributes.Directory))
                                {
                                    await tar.CreateEntriesFromDirectoryAsync(path, ".", cancellationToken, progress);
                                }
                                else
                                {
                                    if (progress != null)
                                    {
                                        progress.Report(path);
                                    }

                                    await tar.CreateEntryFromFileAsync(path, Path.GetFileName(path), cancellationToken);
                                }
                            }
                            await tar.CloseAsync();
                        }
                        catch (Exception e)
                        {
                            writer.Close(e);
                            throw;
                        }

                        writer.Close();
                    }
                }, cancellationToken);
            }
            catch (Exception e)
            {
                reader.Close(e);
                throw;
            }

            return(reader);
        }
Exemplo n.º 2
0
        public static Stream CreateTarStream(IList<string> paths, CancellationToken cancellationToken, IProgress<string> progress = null)
        {
            var pipe = new Pipe();
            var reader = new PipeReadStream(pipe);
            try
            {
                var tarTask = Task.Run(async () =>
                {
                    using (var writer = new PipeWriteStream(pipe))
                    {
                        try
                        {
                            var tar = new TarWriter(writer);
                            foreach (var path in paths)
                            {
                                var fi = new FileInfo(path);
                                if (fi.Attributes.HasFlag(FileAttributes.Directory))
                                {
                                    await tar.CreateEntriesFromDirectoryAsync(path, ".", cancellationToken, progress);
                                }
                                else
                                {
                                    if (progress != null)
                                    {
                                        progress.Report(path);
                                    }

                                    await tar.CreateEntryFromFileAsync(path, Path.GetFileName(path), cancellationToken);
                                }
                            }
                            await tar.CloseAsync();
                        }
                        catch (Exception e)
                        {
                            writer.Close(e);
                            throw;
                        }

                        writer.Close();
                    }
                }, cancellationToken);
            }
            catch (Exception e)
            {
                reader.Close(e);
                throw;
            }

            return reader;
        }
        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));
            }
        }