This repository has been archived by the owner on Apr 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 88
/
StartContainerProcess.cs
96 lines (82 loc) · 3.17 KB
/
StartContainerProcess.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System.Management.Automation;
using Docker.DotNet.Models;
using System.Threading.Tasks;
using Docker.PowerShell.Support;
namespace Docker.PowerShell.Cmdlets
{
[Cmdlet(VerbsLifecycle.Start, "ContainerProcess",
DefaultParameterSetName = CommonParameterSetNames.Default)]
[OutputType(typeof(ContainerListResponse))]
[Alias("Exec-Container")]
public class StartContainerProcess : SingleContainerOperationCmdlet
{
#region Parameters
/// <summary>
/// The command to use by default when starting new container.
/// </summary>
[Parameter(ParameterSetName = CommonParameterSetNames.Default,
ValueFromRemainingArguments = true,
Position = 1)]
[Parameter(ParameterSetName = CommonParameterSetNames.ContainerObject,
ValueFromRemainingArguments = true,
Position = 1)]
[ValidateNotNullOrEmpty]
public string[] Command { get; set; }
/// <summary>
/// Whether or not to start the process in detached mode.
/// </summary>
[Parameter]
public SwitchParameter Detached { get; set; }
/// <summary>
/// Whether or not to use stdin of the started process.
/// </summary>
[Parameter]
public SwitchParameter Input { get; set; }
/// <summary>
/// Whether or not to use terminal emulation.
/// </summary>
[Parameter]
public SwitchParameter Terminal { get; set; }
/// <summary>
/// Whether or not to start the process in privileged mode.
/// </summary>
[Parameter]
public SwitchParameter Privileged { get; set; }
/// <summary>
/// The user context under which the process should be started.
/// </summary>
[Parameter]
public string User { get; set; }
#endregion
#region Overrides
protected override async Task ProcessRecordAsync()
{
var id = ContainerIdOrName ?? Container.ID;
var execConfig = new ExecConfig()
{
Cmd = Command,
Privileged = Privileged,
User = User,
AttachStdin = !Detached && Input,
AttachStdout = !Detached,
AttachStderr = !Detached,
Detach = Detached,
Tty = Terminal,
};
var procCreateResponse = await DkrClient.Containers.ExecCreateContainerAsync(id, new ContainerExecCreateParameters(execConfig));
if (Detached)
{
await DkrClient.Containers.StartContainerExecAsync(procCreateResponse.ID, CmdletCancellationToken);
WriteObject(await DkrClient.Containers.InspectContainerExecAsync(procCreateResponse.ID, CmdletCancellationToken));
}
else
{
using (var stream = await DkrClient.Containers.StartWithConfigContainerExecAsync(procCreateResponse.ID, execConfig, CmdletCancellationToken))
{
await stream.CopyToConsoleAsync(Terminal, Input, CmdletCancellationToken);
}
}
}
#endregion
}
}