This repository was archived by the owner on Apr 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathContainerOperations.cs
151 lines (136 loc) · 5.44 KB
/
ContainerOperations.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Linq;
using Docker.PowerShell.Cmdlets;
using System.Collections.Generic;
namespace Docker.PowerShell.Objects
{
using System.Threading.Tasks;
using DotNet.Models;
public enum IsolationType
{
Default,
None,
HyperV
}
public class ContainerProcessExitException : Exception
{
public ContainerProcessExitException(long exitCode) : base(String.Format("Container process exited with non-zero exit code: {0}", exitCode)) { }
}
internal static class ContainerOperations
{
/// <summary>
/// Creates the container
/// </summary>
/// <param name="id"></param>
/// <param name="cmdlet"></param>
/// <param name="dkrClient"></param>
/// <returns></returns>
internal static Task<CreateContainerResponse> CreateContainer(
string id,
CreateContainerCmdlet cmdlet,
DotNet.DockerClient dkrClient)
{
var configuration = cmdlet.Configuration;
if (configuration == null)
{
configuration = new Config();
}
if (!String.IsNullOrEmpty(id))
{
configuration.Image = id;
}
if (cmdlet.Command != null)
{
configuration.Cmd = cmdlet.Command;
}
var hostConfiguration = cmdlet.HostConfiguration;
if (hostConfiguration == null)
{
hostConfiguration = new HostConfig();
}
if (String.IsNullOrEmpty(hostConfiguration.Isolation))
{
hostConfiguration.Isolation = cmdlet.Isolation.ToString();
}
configuration.Tty = cmdlet.Terminal.ToBool();
configuration.OpenStdin = cmdlet.Input.ToBool();
configuration.AttachStdin = cmdlet.Input.ToBool();
configuration.AttachStdout = true;
configuration.AttachStderr = true;
return dkrClient.Containers.CreateContainerAsync(
new CreateContainerParameters(configuration)
{
Name = cmdlet.Name,
HostConfig = hostConfiguration
});
}
internal static Task<IList<ContainerListResponse>> GetContainersById(string id, DotNet.DockerClient dkrClient)
{
return (dkrClient.Containers.ListContainersAsync(new ContainersListParameters
{
All = true,
Filters = new Dictionary<string, IDictionary<string, bool>>
{
{"id", new Dictionary<string, bool>
{
{id, true}
}
}
}
}));
}
internal static Task<IList<ContainerListResponse>> GetContainersByName(string name, DotNet.DockerClient dkrClient)
{
return (dkrClient.Containers.ListContainersAsync(new ContainersListParameters
{
All = true,
Filters = new Dictionary<string, IDictionary<string, bool>>
{
{"name", new Dictionary<string, bool>
{
{name, true}
}
}
}
}));
}
/// <summary>
/// Gets a single container object from the client by id or name.
/// </summary>
/// <param name="id">The container identifier to retrieve.</param>
/// <param name="dkrClient">The client to request the container from.</param>
/// <returns>The single container object matching the id.</returns>
internal static async Task<ContainerListResponse> GetContainerByIdOrName(string id, DotNet.DockerClient dkrClient)
{
return (await GetContainersByName(id, dkrClient)).Where(c => c.Names.Contains($"/{id}")).Concat(await GetContainersById(id, dkrClient)).Single();
}
/// <summary>
/// Gets a single image object from the client by id.
/// </summary>
/// <param name="id">The image identifier to retrieve.</param>
/// <param name="dkrClient">The client to request the image from.</param>
/// <returns>The single image object matching the id.</returns>
internal static async Task<ImagesListResponse> GetImageById(string id, DotNet.DockerClient dkrClient)
{
var shaId = id;
if (!shaId.StartsWith("sha256:"))
{
shaId = "sha256:" + shaId;
}
// TODO - Have a better way to get the image list response given the ID.
return (await dkrClient.Images.ListImagesAsync(new ImagesListParameters() { All = true }))
.Single(c => c.ID.StartsWith(shaId));
}
/// <summary>
/// Throws a ContainerProcessExitException if the given exit code is non-zero.
/// </summary>
/// <param name="exitCode">The process exit code.</param>
internal static void ThrowOnProcessExitCode(long exitCode)
{
if (exitCode != 0)
{
throw new ContainerProcessExitException(exitCode);
}
}
}
}