-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
243 lines (232 loc) · 8.76 KB
/
Program.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using System;
using System.Collections.Generic;
using System.Linq;
using ContainerKiller.Models;
using Microsoft.Extensions.Configuration;
namespace ContainerKiller
{
enum ContainerAction
{
Stop,
Kill,
NetworkDown
}
class Program
{
private static ContainerAction CurrentMode;
private static List<Container> Containers;
private static KillerConfig Config;
private static NetworkMemory NetworkMemory;
private static int CurrentIndex = 1;
static void Main(string[] args)
{
var configurationRoot = new ConfigurationBuilder().SetBasePath(AppContext.BaseDirectory).AddJsonFile("config.json").Build();
Config = new KillerConfig
{
ImageName = configurationRoot.GetSection("ImageName").Value,
ContainerName = configurationRoot.GetSection("ContainerName").Value,
ExpectedNetwork = configurationRoot.GetSection("ExpectedNetwork").Value
};
CurrentMode = ContainerAction.Kill;
Containers = GetContainers();
try
{
NetworkMemory = new NetworkMemory(Containers, Config.ExpectedNetwork);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
return;
}
ReDraw();
char input = ' ';
do
{
var key = Console.ReadKey();
input = key.KeyChar;
Console.WriteLine("");
Containers = GetContainers();
if(int.TryParse(input.ToString(), out var id))
{
id--;
ExecuteCommand(id);
}
else
{
switch(key.Key)
{
case ConsoleKey.R:
{
ReDraw();
break;
}
case ConsoleKey.S:
{
CurrentMode = ContainerAction.Stop;
ReDraw();
break;
}
case ConsoleKey.K:
{
CurrentMode = ContainerAction.Kill;
ReDraw();
break;
}
case ConsoleKey.N:
{
CurrentMode = ContainerAction.NetworkDown;
ReDraw();
break;
}
case ConsoleKey.UpArrow:
{
if(CurrentIndex > 1)
CurrentIndex--;
ReDraw();
break;
}
case ConsoleKey.DownArrow:
{
if(CurrentIndex < Containers.Count)
CurrentIndex++;
ReDraw();
break;
}
case ConsoleKey.Spacebar:
{
ExecuteCommand(CurrentIndex - 1);
ReDraw();
break;
}
default:
{
Write($"Unknown command: {input}");
break;
}
}
}
} while(input != 'q');
}
private static void ExecuteCommand(int id)
{
if(id < 0 || id > Containers.Count)
{
Write("Invalid id");
return;
}
var container = Containers[id];
DockerResponse result;
if(container.State == "running")
{
switch(CurrentMode)
{
case ContainerAction.Kill:
result = Finder.KillContainer(container.Id);
break;
case ContainerAction.Stop:
result = Finder.StopContainer(container.Id);
break;
case ContainerAction.NetworkDown:
{
var network = container.NetworkSettings.Networks.FirstOrDefault();
if(network.Key != null && network.Key.Equals(Config.ExpectedNetwork, StringComparison.InvariantCultureIgnoreCase))
result = Finder.DisconnectContainer(container.Id, network.Value.NetworkID);
else
result = Finder.ConnectContainer(container.Id, NetworkMemory.NetworkId, NetworkMemory.GetOriginalIpAddressFor(container.Id));
break;
}
default:
result = DockerResponse.Ok;
break;
}
}
else if(container.State == "stopped" || container.State == "exited")
result = Finder.StartContainer(container.Id);
else
{
Containers = GetContainers();
Write($"Unknown state: {container.State}");
return;
}
Containers = GetContainers();
if(result == DockerResponse.Ok)
ReDraw();
else
Write($"Result: {result.ToString()}");
}
private static void Write(string text)
{
Console.WriteLine($"{text}");
Console.ReadLine();
Console.Clear();
ReDraw();
}
private static List<Container> GetContainers()
{
IEnumerable<Container> containers;
if(Config.ImageName?.Contains("*") == true)
{
containers = Finder.GetAllContainers();
if(!Config.ImageName.Equals("*")) {
var like = Config.ImageName.Replace("*", "");
containers = containers.Where(c => c.Image.StartsWith(like));
}
}
else
{
containers = Finder.GetContainersMatchingImage(Config.ImageName);
}
foreach(var container in containers) {
container.Names = container.Names.Select(cn => cn.Substring(1)).ToArray();
}
if(Config.ContainerName?.Contains("*") == true)
{
if(!Config.ContainerName.Equals("*")) {
var like = Config.ContainerName.Replace("*", "");
containers = containers.Where(c => c.Names.Any(cn => cn.StartsWith(like)));
}
}
else if(!String.IsNullOrWhiteSpace(Config.ContainerName))
{
containers = containers.Where(c => c.Names.Any(cn => cn.Equals(Config.ContainerName)));
}
return containers.OrderBy(c => c.Names.First()).ToList();
}
private static void ReDraw()
{
Console.Clear();
var index = 1;
var spaceCount = Containers.Count.ToString().Length + 1;
foreach (var container in Containers)
{
var itemSpaceCount = spaceCount;
if(index == CurrentIndex)
{
Console.Write("➤");
itemSpaceCount--;
}
itemSpaceCount -= index.ToString().Length;
for(var i = 0; i < itemSpaceCount; i++)
{
Console.Write(" ");
}
Console.Write($" {index}: {container.Names.Last()} - ");
if(container.State.Equals("running"))
{
if(container.NetworkSettings.Networks.Any())
// All good
Console.Write("♡");
else
// Network segmentation
Console.Write("⌁");
}
else
// Container dead
Console.Write("☠");
Console.WriteLine();
index++;
}
Console.WriteLine($"Execution mode: {CurrentMode.ToString()}");
}
}
}