Exemple #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());
            }
        }