|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Runtime.InteropServices; |
| 5 | +using System.Threading; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Docker.DotNet; |
| 8 | +using Docker.DotNet.Models; |
| 9 | + |
| 10 | +namespace EasyNetQ.IntegrationTests |
| 11 | +{ |
| 12 | + public class DockerProxy : IDisposable |
| 13 | + { |
| 14 | + private readonly DockerClient client; |
| 15 | + private readonly DockerClientConfiguration dockerConfiguration; |
| 16 | + |
| 17 | + public DockerProxy(Uri uri) |
| 18 | + { |
| 19 | + dockerConfiguration = new DockerClientConfiguration(uri); |
| 20 | + client = dockerConfiguration.CreateClient(); |
| 21 | + } |
| 22 | + |
| 23 | + public void Dispose() |
| 24 | + { |
| 25 | + dockerConfiguration.Dispose(); |
| 26 | + client.Dispose(); |
| 27 | + } |
| 28 | + |
| 29 | + public async Task<OSPlatform> GetDockerEngineOsAsync(CancellationToken token = default) |
| 30 | + { |
| 31 | + var response = await client.System.GetSystemInfoAsync(token).ConfigureAwait(false); |
| 32 | + return OSPlatform.Create(response.OSType.ToUpper()); |
| 33 | + } |
| 34 | + |
| 35 | + public async Task CreateNetworkAsync(string name, CancellationToken token = default) |
| 36 | + { |
| 37 | + var networksCreateParameters = new NetworksCreateParameters |
| 38 | + { |
| 39 | + Name = name |
| 40 | + }; |
| 41 | + await client.Networks.CreateNetworkAsync(networksCreateParameters, token).ConfigureAwait(false); |
| 42 | + } |
| 43 | + |
| 44 | + public async Task PullImageAsync(string image, string tag, CancellationToken token = default) |
| 45 | + { |
| 46 | + var createParameters = new ImagesCreateParameters |
| 47 | + { |
| 48 | + FromImage = image, |
| 49 | + Tag = tag |
| 50 | + }; |
| 51 | + var progress = new Progress<JSONMessage>(jsonMessage => { }); |
| 52 | + await client.Images.CreateImageAsync(createParameters, null, progress, token).ConfigureAwait(false); |
| 53 | + } |
| 54 | + |
| 55 | + public async Task<string> CreateContainerAsync(string image, string name, |
| 56 | + IDictionary<string, ISet<string>> portMappings, string networkName = null, IList<string> envVars = null, |
| 57 | + CancellationToken token = default) |
| 58 | + { |
| 59 | + var createParameters = new CreateContainerParameters |
| 60 | + { |
| 61 | + Image = image, |
| 62 | + Env = envVars ?? Enumerable.Empty<string>().ToList(), |
| 63 | + Name = name, |
| 64 | + Hostname = name, |
| 65 | + HostConfig = new HostConfig |
| 66 | + { |
| 67 | + PortBindings = PortBindings(portMappings), |
| 68 | + NetworkMode = networkName |
| 69 | + }, |
| 70 | + ExposedPorts = portMappings.ToDictionary(x => x.Key, x => new EmptyStruct()) |
| 71 | + }; |
| 72 | + var response = await client.Containers.CreateContainerAsync(createParameters, token).ConfigureAwait(false); |
| 73 | + return response.ID; |
| 74 | + } |
| 75 | + |
| 76 | + public async Task StartContainerAsync(string id, CancellationToken token = default) |
| 77 | + { |
| 78 | + await client.Containers.StartContainerAsync(id, new ContainerStartParameters(), token) |
| 79 | + .ConfigureAwait(false); |
| 80 | + } |
| 81 | + |
| 82 | + public async Task<string> GetContainerIpAsync(string id, CancellationToken token = default) |
| 83 | + { |
| 84 | + var response = await client.Containers.InspectContainerAsync(id, token).ConfigureAwait(false); |
| 85 | + var networks = response.NetworkSettings.Networks; |
| 86 | + return networks.Select(x => x.Value.IPAddress).First(x => !string.IsNullOrEmpty(x)); |
| 87 | + } |
| 88 | + |
| 89 | + public async Task StopContainerAsync(string name, CancellationToken token = default) |
| 90 | + { |
| 91 | + var ids = await FindContainerIdsAsync(name).ConfigureAwait(false); |
| 92 | + var stopTasks = ids.Select(x => |
| 93 | + client.Containers.StopContainerAsync(x, new ContainerStopParameters(), token)); |
| 94 | + await Task.WhenAll(stopTasks); |
| 95 | + } |
| 96 | + |
| 97 | + public async Task RemoveContainerAsync(string name, CancellationToken token = default) |
| 98 | + { |
| 99 | + var ids = await FindContainerIdsAsync(name).ConfigureAwait(false); |
| 100 | + var containerRemoveParameters = new ContainerRemoveParameters {Force = true, RemoveVolumes = true}; |
| 101 | + var removeTasks = |
| 102 | + ids.Select(x => client.Containers.RemoveContainerAsync(x, containerRemoveParameters, token)); |
| 103 | + await Task.WhenAll(removeTasks); |
| 104 | + } |
| 105 | + |
| 106 | + public async Task DeleteNetworkAsync(string name, CancellationToken token = default) |
| 107 | + { |
| 108 | + var ids = await FindNetworkIdsAsync(name).ConfigureAwait(false); |
| 109 | + var deleteTasks = ids.Select(x => client.Networks.DeleteNetworkAsync(x, token)); |
| 110 | + await Task.WhenAll(deleteTasks); |
| 111 | + } |
| 112 | + |
| 113 | + private static IDictionary<string, IList<PortBinding>> PortBindings( |
| 114 | + IDictionary<string, ISet<string>> portMappings) |
| 115 | + { |
| 116 | + return portMappings |
| 117 | + .Select(x => new {ContainerPort = x.Key, HostPorts = HostPorts(x.Value)}) |
| 118 | + .ToDictionary(x => x.ContainerPort, x => (IList<PortBinding>) x.HostPorts); |
| 119 | + } |
| 120 | + |
| 121 | + private static List<PortBinding> HostPorts(IEnumerable<string> hostPorts) |
| 122 | + { |
| 123 | + return hostPorts.Select(x => new PortBinding {HostPort = x}).ToList(); |
| 124 | + } |
| 125 | + |
| 126 | + private async Task<IEnumerable<string>> FindContainerIdsAsync(string name) |
| 127 | + { |
| 128 | + var containers = await client.Containers |
| 129 | + .ListContainersAsync(new ContainersListParameters {All = true, Filters = ListFilters(name)}) |
| 130 | + .ConfigureAwait(false); |
| 131 | + return containers.Select(x => x.ID); |
| 132 | + } |
| 133 | + |
| 134 | + private async Task<IEnumerable<string>> FindNetworkIdsAsync(string name) |
| 135 | + { |
| 136 | + var networks = await client.Networks |
| 137 | + .ListNetworksAsync(new NetworksListParameters {Filters = ListFilters(name)}).ConfigureAwait(false); |
| 138 | + return networks.Select(x => x.ID); |
| 139 | + } |
| 140 | + |
| 141 | + private static Dictionary<string, IDictionary<string, bool>> ListFilters(string name) |
| 142 | + { |
| 143 | + return new Dictionary<string, IDictionary<string, bool>> |
| 144 | + { |
| 145 | + {"name", new Dictionary<string, bool> {{name, true}}} |
| 146 | + }; |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments