generated from SwissLife-OSS/template
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathContainerResource.cs
More file actions
154 lines (128 loc) · 4.14 KB
/
ContainerResource.cs
File metadata and controls
154 lines (128 loc) · 4.14 KB
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
152
153
154
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
namespace Squadron;
/// <summary>
/// Base resource for container based resources
/// </summary>
/// <typeparam name="TOptions">The type of the options.</typeparam>
public class ContainerResource<TOptions>
where TOptions : ContainerResourceOptions, new()
{
/// <summary>
/// Gets or sets the continer settings.
/// </summary>
/// <value>
/// The settings.
/// </value>
protected ContainerResourceSettings Settings { get; set; }
/// <summary>
/// The container manager
/// </summary>
protected IDockerContainerManager Manager;
/// <summary>
/// The container initializer
/// </summary>
protected ContainerInitializer Initializer;
/// <summary>
/// The resource options
/// </summary>
protected TOptions ResourceOptions;
private List<string> _composeVariables;
private IEnumerable<string> _composeNetworks = new List<string>();
public ContainerInstance Instance => Manager.Instance;
/// <summary>
/// Initializes the resources
/// </summary>
public virtual async Task InitializeAsync()
{
SetupContainerResource();
await Manager.CreateAndStartContainerAsync();
}
protected virtual void SetupContainerResource()
{
ResourceOptions = new TOptions();
var builder = ContainerResourceBuilder.New();
ResourceOptions.Configure(builder);
AddNetworksToBuilder(builder);
Settings = builder.Build();
SetComposeVariables();
OnSettingsBuilded(Settings);
ValidateSettings(Settings);
Manager = new TestcontainersDockerManager(Settings);
Initializer = new ContainerInitializer(Manager, Settings);
}
private void AddNetworksToBuilder(ContainerResourceBuilder builder)
{
foreach(string network in _composeNetworks)
{
builder.AddNetwork(network);
}
}
private void SetComposeVariables()
{
if (_composeVariables != null)
{
foreach (var envVar in _composeVariables)
{
Settings.EnvironmentVariables.Add(envVar);
}
}
// Add docker hostname variable
Settings.EnvironmentVariables.Add($"DockerHost:Name={Settings.UniqueContainerName}");
}
public void SetEnvironmentVariables(IEnumerable<string> variables)
{
_composeVariables = variables.ToList();
}
public void SetNetworks(IEnumerable<string> composeNetworks)
{
_composeNetworks = composeNetworks;
}
private void ValidateSettings(ContainerResourceSettings settings)
{
if (string.IsNullOrEmpty(settings.Name))
throw new ArgumentException("Can not be null or empty.", nameof(settings.Name));
if (string.IsNullOrEmpty(settings.Image))
throw new ArgumentException("Can not be null or empty.", nameof(settings.Image));
if (settings.InternalPort == 0)
throw new ArgumentException("Can not be 0", nameof(settings.InternalPort));
}
/// <summary>
/// Called when after settings are build
/// </summary>
/// <param name="settings">The settings.</param>
protected virtual void OnSettingsBuilded(ContainerResourceSettings settings)
{ }
public Task PauseContainer(TimeSpan pauseTime)
{
return Task.CompletedTask;
}
public virtual Dictionary<string, string> GetComposeExports()
{
return new Dictionary<string, string>
{
{ "ADDRESS", Manager.Instance.Address },
{ "NAME", Manager.Instance.Name },
{ "PORT", Manager.Instance.HostPort.ToString() },
};
}
/// <summary>
/// Cleans up the resource
/// </summary>
public async Task DisposeAsync()
{
try
{
await Manager.StopContainerAsync();
await Manager.RemoveContainerAsync();
Manager.Dispose();
}
catch (Exception ex)
{
Settings.Logger.Error($"Could not cleanup container [id: {Manager.Instance.Id}]", ex);
}
}
}