Exemple #1
0
        /// <summary>
        /// Creates a new container and lists it to output.
        /// </summary>
        protected override void ProcessRecord()
        {
            base.ProcessRecord();

            foreach (var id in ParameterResolvers.GetImageIds(Image, Id))
            {
                var createResult = ContainerOperations.CreateContainer(
                    id,
                    this.MemberwiseClone() as CreateContainerCmdlet,
                    DkrClient);

                if (createResult.Warnings != null)
                {
                    foreach (var w in createResult.Warnings)
                    {
                        if (!String.IsNullOrEmpty(w))
                        {
                            WriteWarning(w);
                        }
                    }
                }

                if (!String.IsNullOrEmpty(createResult.ID))
                {
                    if (!DkrClient.Containers.StartContainerAsync(
                            createResult.ID, HostConfiguration).AwaitResult())
                    {
                        throw new ApplicationFailedException("The container has already started.");
                    }

                    var waitResponse = DkrClient.Containers.WaitContainerAsync(
                        createResult.ID,
                        CancelSignal.Token).AwaitResult();

                    WriteVerbose("Status Code: " + waitResponse.StatusCode.ToString());
                    ContainerOperations.ThrowOnProcessExitCode(waitResponse.StatusCode);

                    if (RemoveAutomatically.ToBool())
                    {
                        DkrClient.Containers.RemoveContainerAsync(createResult.ID,
                                                                  new ContainerRemoveParameters()).WaitUnwrap();
                    }
                    else if (PassThru.ToBool())
                    {
                        WriteObject(ContainerOperations.GetContainerById(createResult.ID, DkrClient));
                    }
                }
            }
        }
        /// <summary>
        /// Creates a new container and lists it to output.
        /// </summary>
        protected override async Task ProcessRecordAsync()
        {
            foreach (var id in ParameterResolvers.GetImageIds(Image, Id))
            {
                var createResult = await ContainerOperations.CreateContainer(
                    id,
                    this.MemberwiseClone() as CreateContainerCmdlet,
                    DkrClient);

                if (createResult.Warnings != null)
                {
                    foreach (var w in createResult.Warnings)
                    {
                        if (!String.IsNullOrEmpty(w))
                        {
                            WriteWarning(w);
                        }
                    }
                }

                if (!String.IsNullOrEmpty(createResult.ID))
                {
                    MultiplexedStream stream = null;
                    Task streamTask          = null;
                    try
                    {
                        if (!Detach)
                        {
                            var parameters = new ContainerAttachParameters
                            {
                                Stdin  = Input,
                                Stdout = true,
                                Stderr = true,
                                Stream = true
                            };

                            stream = await DkrClient.Containers.AttachContainerAsync(createResult.ID, Terminal, parameters, CmdletCancellationToken);

                            streamTask = stream.CopyToConsoleAsync(Terminal, Input, CmdletCancellationToken);
                        }

                        if (!await DkrClient.Containers.StartContainerAsync(createResult.ID, new ContainerStartParameters()))
                        {
                            throw new ApplicationFailedException("The container has already started.");
                        }

                        if (!Detach)
                        {
                            await streamTask;
                        }
                    }
                    finally
                    {
                        stream?.Dispose();
                    }

                    if (RemoveAutomatically && !Detach)
                    {
                        await DkrClient.Containers.RemoveContainerAsync(createResult.ID,
                                                                        new ContainerRemoveParameters());
                    }
                    else if (PassThru)
                    {
                        WriteObject((await ContainerOperations.GetContainersById(createResult.ID, DkrClient)).Single());
                    }
                }
            }
        }