示例#1
0
        protected override async Task ProcessRecordAsync()
        {
            foreach (var item in FilePath)
            {
                var filePath = System.IO.Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, item);

                string imageId = null;
                bool   failed  = false;

                using (var file = File.OpenRead(filePath))
                {
                    var loadTask = DkrClient.Miscellaneous.LoadImageFromTarball(file, new ImageLoadParameters {
                        Quiet = false
                    }, CmdletCancellationToken);

                    var messageWriter = new JsonMessageWriter(this);

                    using (var pushStream = await loadTask)
                    {
                        // ReadLineAsync is not cancellable without closing the whole stream, so register a callback to do just that.
                        using (CmdletCancellationToken.Register(() => pushStream.Dispose()))
                            using (var pullReader = new StreamReader(pushStream, new UTF8Encoding(false)))
                            {
                                string line;
                                while ((line = await pullReader.ReadLineAsync()) != null)
                                {
                                    var message = JsonConvert.DeserializeObject <JsonMessage>(line);
                                    if (message.Stream != null && message.Stream.StartsWith(LoadedImage))
                                    {
                                        // This is probably the image ID.
                                        imageId = message.Stream.Substring(LoadedImage.Length).Trim();
                                    }

                                    if (message.Error != null)
                                    {
                                        failed = true;
                                    }

                                    messageWriter.WriteJsonMessage(JsonConvert.DeserializeObject <JsonMessage>(line));
                                }
                            }
                    }

                    messageWriter.ClearProgress();
                    if (imageId != null)
                    {
                        WriteObject((await ContainerOperations.GetImagesByRepoTag(imageId, DkrClient)).Single());
                    }
                    else if (!failed)
                    {
                        throw new Exception("Could not find image, but no error was returned");
                    }
                }
            }
        }
        protected override async Task ProcessRecordAsync()
        {
            string repoTag  = null;
            bool   failed   = false;
            var    pullTask = DkrClient.Images.PullImageAsync(new ImagesPullParameters()
            {
                All = All, Parent = Repository, Tag = Tag ?? "latest",
            }, Authorization);
            var messageWriter = new JsonMessageWriter(this);

            using (var pullStream = await pullTask)
            {
                // ReadLineAsync is not cancellable without closing the whole stream, so register a callback to do just that.
                using (CmdletCancellationToken.Register(() => pullStream.Dispose()))
                    using (var pullReader = new StreamReader(pullStream, new UTF8Encoding(false)))
                    {
                        string line;
                        while ((line = await pullReader.ReadLineAsync()) != null)
                        {
                            var message = JsonConvert.DeserializeObject <JsonMessage>(line);
                            if (message.Status != null)
                            {
                                if (message.Status.StartsWith(StatusUpToDate))
                                {
                                    // This is probably the image repository:tag.
                                    repoTag = message.Status.Substring(StatusUpToDate.Length).Trim();
                                }
                                else if (message.Status.StartsWith(StatusDownloadedNewer))
                                {
                                    // This is probably the image repository:tag.
                                    repoTag = message.Status.Substring(StatusDownloadedNewer.Length).Trim();
                                }
                            }

                            if (message.Error != null)
                            {
                                failed = true;
                            }

                            messageWriter.WriteJsonMessage(message);
                        }
                    }
            }

            messageWriter.ClearProgress();
            if (repoTag != null)
            {
                WriteObject((await ContainerOperations.GetImagesByRepoTag(repoTag, DkrClient)).Single());
            }
            else if (!failed)
            {
                throw new Exception("Could not find image, but no error was returned");
            }
        }
        protected override async Task ProcessRecordAsync()
        {
            string repoTag = null;

            if (Id != null)
            {
                repoTag = Id;
            }
            else if (Image.RepoTags.Count != 1)
            {
                throw new Exception("Ambiguous repository and tag: Supplied image must have only one repository:tag.");
            }
            else
            {
                repoTag = Image.RepoTags[0];
            }

            var pushTask      = DkrClient.Images.PushImageAsync(repoTag, new ImagePushParameters(), Authorization);
            var messageWriter = new JsonMessageWriter(this);

            using (var pushStream = await pushTask)
            {
                // ReadLineAsync is not cancellable without closing the whole stream, so register a callback to do just that.
                using (CmdletCancellationToken.Register(() => pushStream.Close()))
                    using (var pullReader = new StreamReader(pushStream, new UTF8Encoding(false)))
                    {
                        string line;
                        while ((line = await pullReader.ReadLineAsync()) != null)
                        {
                            messageWriter.WriteJsonMessage(JsonConvert.DeserializeObject <JsonMessage>(line));
                        }
                    }
            }

            messageWriter.ClearProgress();
            if (PassThru)
            {
                WriteObject((await ContainerOperations.GetImagesByRepoTag(repoTag, DkrClient)).Single());
            }
        }
        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,
                    ForceRemove = ForceRemoveIntermediateContainers,
                    Remove      = !PreserveIntermediateContainers,
                };

                if (this.Isolation != IsolationType.Default)
                {
                    parameters.Isolation = this.Isolation.ToString();
                }

                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.Dispose()))
                        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 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");
                }
            }
        }