-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathContainerService.cpp
More file actions
491 lines (411 loc) · 17.2 KB
/
ContainerService.cpp
File metadata and controls
491 lines (411 loc) · 17.2 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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*++
Copyright (c) Microsoft. All rights reserved.
Module Name:
ContainerService.cpp
Abstract:
This file contains the ContainerService implementation
--*/
#include <precomp.h>
#include "ContainerService.h"
#include "ConsoleService.h"
#include "ImageService.h"
#include "PullImageCallback.h"
#include <wslutil.h>
#include <WSLCProcessLauncher.h>
#include <CommandLine.h>
#include <filesystem>
#include <fstream>
#include <unordered_map>
#include <wslc.h>
namespace wsl::windows::wslc::services {
using wsl::windows::common::ClientRunningWSLCProcess;
using wsl::windows::common::wslc_schema::InspectContainer;
using wsl::windows::common::wslutil::PrintMessage;
using namespace wsl::windows::common::wslutil;
using namespace wsl::shared;
using namespace wsl::windows::wslc::models;
using namespace std::chrono_literals;
static void SetContainerArguments(WSLCProcessOptions& options, std::vector<const char*>& argsStorage)
{
options.CommandLine = {.Values = argsStorage.data(), .Count = static_cast<ULONG>(argsStorage.size())};
}
static void WriteContainerIdToFile(const std::optional<std::wstring>& cidFilePath, const std::string& containerId)
{
if (!cidFilePath.has_value())
{
return;
}
const auto path = std::filesystem::path(cidFilePath.value());
HANDLE file = ::CreateFileW(path.c_str(), GENERIC_WRITE, 0, nullptr, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr);
if (file == INVALID_HANDLE_VALUE)
{
const auto error = ::GetLastError();
const auto errorMessage = wsl::shared::string::MultiByteToWide(std::system_category().message(error));
THROW_HR_WITH_USER_ERROR(HRESULT_FROM_WIN32(error), Localization::MessageWslcFailedToOpenFile(*cidFilePath, errorMessage));
}
DWORD bytesWritten{};
const bool writeSuccess = ::WriteFile(file, containerId.data(), static_cast<DWORD>(containerId.size()), &bytesWritten, nullptr) != FALSE;
const HRESULT closeResult = ::CloseHandle(file) ? S_OK : HRESULT_FROM_WIN32(GetLastError());
if (!writeSuccess || bytesWritten != containerId.size())
{
const auto error = writeSuccess ? ERROR_WRITE_FAULT : ::GetLastError();
const auto errorMessage = wsl::shared::string::MultiByteToWide(std::system_category().message(error));
THROW_HR_WITH_USER_ERROR(HRESULT_FROM_WIN32(error), Localization::MessageWslcFailedToOpenFile(*cidFilePath, errorMessage));
}
THROW_IF_FAILED(closeResult);
}
static wsl::windows::common::RunningWSLCContainer CreateInternal(Session& session, const std::string& image, const ContainerOptions& options)
{
auto processFlags = WSLCProcessFlagsNone;
WI_SetFlagIf(processFlags, WSLCProcessFlagsStdin, options.Interactive);
WI_SetFlagIf(processFlags, WSLCProcessFlagsTty, options.TTY);
auto containerFlags = WSLCContainerFlagsNone;
WI_SetFlagIf(containerFlags, WSLCContainerFlagsRm, options.Remove);
wsl::windows::common::WSLCContainerLauncher containerLauncher(
image, options.Name, options.Arguments, options.EnvironmentVariables, WSLCContainerNetworkTypeBridged, processFlags);
// Set port options if provided
for (const auto& port : options.Ports)
{
auto portMapping = PublishPort::Parse(port);
{
// https://github.com/microsoft/WSL/issues/14433
// The following scenarios are currently not implemented:
// - Ephemeral host port mappings
// - Host port mappings with a specific host IP
// - Host port mappings with UDP protocol
if (portMapping.HostPort().IsEphemeral() || portMapping.HostIP().has_value() ||
portMapping.PortProtocol() == PublishPort::Protocol::UDP)
{
THROW_HR_WITH_USER_ERROR(
HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED),
"Port mappings with ephemeral host ports, specific host IPs, or UDP protocol are not currently supported");
}
}
auto containerPort = portMapping.ContainerPort();
for (uint16_t i = 0; i < containerPort.Count(); ++i)
{
auto currentContainerPort = static_cast<uint16_t>(containerPort.Start() + i);
auto currentHostPort = static_cast<uint16_t>(portMapping.HostPort().Start() + i);
containerLauncher.AddPort(currentHostPort, currentContainerPort, AF_INET);
}
}
// Add volumes if specified
for (const auto& volumeSpec : options.Volumes)
{
auto volume = VolumeMount::Parse(volumeSpec);
auto host = volume.HostPath();
auto container = volume.ContainerPath();
containerLauncher.AddVolume(host, container, volume.IsReadOnly());
}
containerLauncher.SetContainerFlags(containerFlags);
if (!options.Entrypoint.empty())
{
auto entrypoints = options.Entrypoint;
containerLauncher.SetEntrypoint(std::move(entrypoints));
}
if (options.User.has_value())
{
auto user = options.User.value();
containerLauncher.SetUser(std::move(user));
}
for (const auto& tmpfsSpec : options.Tmpfs)
{
auto tmpfsMount = TmpfsMount::Parse(tmpfsSpec);
containerLauncher.AddTmpfs(tmpfsMount.ContainerPath(), tmpfsMount.Options());
}
auto [result, runningContainer] = containerLauncher.CreateNoThrow(*session.Get());
if (result == WSLC_E_IMAGE_NOT_FOUND)
{
{
// Attempt to pull the image if not found
PullImageCallback callback;
PrintMessage(Localization::WSLCCLI_ImageNotFoundPulling(wsl::shared::string::MultiByteToWide(image)), stderr);
ImageService imageService;
imageService.Pull(session, image, &callback);
}
return containerLauncher.Create(*session.Get());
}
THROW_IF_FAILED(result);
ASSERT(runningContainer);
return std::move(*runningContainer);
}
static PortInformation PortInformationFromWSLCPortMapping(const WSLCPortMapping& mapping)
{
return PortInformation{
.HostPort = mapping.HostPort,
.ContainerPort = mapping.ContainerPort,
.Protocol = static_cast<int>(mapping.Protocol),
.BindingAddress = mapping.BindingAddress,
};
}
std::wstring ContainerService::FormatRelativeTime(ULONGLONG timestamp)
{
if (timestamp == 0)
{
return L"";
}
constexpr LONGLONG SecondsPerMinute = std::chrono::duration_cast<std::chrono::seconds>(1min).count();
constexpr LONGLONG SecondsPerHour = std::chrono::duration_cast<std::chrono::seconds>(1h).count();
constexpr LONGLONG SecondsPerDay = std::chrono::duration_cast<std::chrono::seconds>(24h).count();
constexpr LONGLONG SecondsPerWeek = SecondsPerDay * 7;
constexpr LONGLONG SecondsPerMonth = SecondsPerDay * 30;
constexpr LONGLONG SecondsPerYear = SecondsPerDay * 365;
auto elapsed = static_cast<LONGLONG>(std::time(nullptr)) - static_cast<LONGLONG>(timestamp);
if (elapsed < 0)
{
elapsed = 0;
}
auto pluralize = [](LONGLONG count, const wchar_t* singular, const wchar_t* plural) {
return std::format(L"{} {} ago", count, (count == 1 ? singular : plural));
};
if (elapsed < SecondsPerMinute)
{
return pluralize(elapsed, L"second", L"seconds");
}
else if (elapsed < SecondsPerHour)
{
return pluralize(elapsed / SecondsPerMinute, L"minute", L"minutes");
}
else if (elapsed < SecondsPerDay)
{
return pluralize(elapsed / SecondsPerHour, L"hour", L"hours");
}
else if (elapsed < SecondsPerWeek)
{
return pluralize(elapsed / SecondsPerDay, L"day", L"days");
}
else if (elapsed < SecondsPerMonth)
{
return pluralize(elapsed / SecondsPerWeek, L"week", L"weeks");
}
else if (elapsed < SecondsPerYear)
{
return pluralize(elapsed / SecondsPerMonth, L"month", L"months");
}
return pluralize(elapsed / SecondsPerYear, L"year", L"years");
}
int ContainerService::Attach(Session& session, const std::string& id)
{
wil::com_ptr<IWSLCContainer> container;
THROW_IF_FAILED(session.Get()->OpenContainer(id.c_str(), &container));
wil::com_ptr<IWSLCProcess> process;
THROW_IF_FAILED(container->GetInitProcess(&process));
WSLCProcessFlags processFlags{};
THROW_IF_FAILED(process->GetFlags(&processFlags));
ClientRunningWSLCProcess runningProcess(std::move(process), processFlags);
COMOutputHandle stdinLogs{};
COMOutputHandle stdoutLogs{};
COMOutputHandle stderrLogs{};
THROW_IF_FAILED(container->Attach(nullptr, &stdinLogs, &stdoutLogs, &stderrLogs));
if (!stdoutLogs.Empty())
{
// Non-TTY process - relay separate stdout/stderr streams
WI_ASSERT(!stderrLogs.Empty());
ConsoleService::RelayNonTtyProcess(stdinLogs.Release(), stdoutLogs.Release(), stderrLogs.Release());
}
else
{
// TTY process - relay using interactive TTY handling
WI_ASSERT(stderrLogs.Empty());
if (!ConsoleService::RelayInteractiveTty(runningProcess, stdinLogs.Release().get(), true))
{
wsl::windows::common::wslutil::PrintMessage(L"[detached]", stderr);
return 0; // Exit early if user detached
}
}
// Wait for the container process to exit
return runningProcess.Wait();
}
std::wstring ContainerService::ContainerStateToString(WSLCContainerState state, ULONGLONG stateChangedAt)
{
std::wstring stateString;
switch (state)
{
case WSLCContainerState::WslcContainerStateCreated:
stateString = L"created";
break;
case WSLCContainerState::WslcContainerStateRunning:
stateString = L"running";
break;
case WSLCContainerState::WslcContainerStateDeleted:
stateString = L"stopped";
break;
case WSLCContainerState::WslcContainerStateExited:
stateString = L"exited";
break;
case WSLCContainerState::WslcContainerStateInvalid:
return L"invalid";
default:
THROW_HR(E_UNEXPECTED);
}
if (stateChangedAt == 0)
{
return stateString;
}
return std::format(L"{} {}", stateString, FormatRelativeTime(stateChangedAt));
}
std::wstring ContainerService::FormatPorts(WSLCContainerState state, const std::vector<PortInformation>& ports)
{
if (state != WslcContainerStateRunning || ports.empty())
{
return L"";
}
std::wstring result;
for (size_t i = 0; i < ports.size(); ++i)
{
const auto& port = ports[i];
std::wstring hostIp = wsl::shared::string::MultiByteToWide(port.BindingAddress);
std::wstring protocol = (port.Protocol == IPPROTO_TCP) ? L"tcp"
: (port.Protocol == IPPROTO_UDP) ? L"udp"
: std::format(L"{}", port.Protocol);
if (i > 0)
{
result += L", ";
}
result += std::format(
L"{}:{}->{}/{}", (hostIp.find(L':') != std::wstring::npos) ? std::format(L"[{}]", hostIp) : hostIp, port.HostPort, port.ContainerPort, protocol);
}
return result;
}
int ContainerService::Run(Session& session, const std::string& image, ContainerOptions runOptions)
{
// Create the container
auto runningContainer = CreateInternal(session, image, runOptions);
runningContainer.SetDeleteOnClose(false);
auto& container = runningContainer.Get();
WSLCContainerId containerId{};
THROW_IF_FAILED(container.GetId(containerId));
WriteContainerIdToFile(runOptions.CidFile, containerId);
// Start the created container
WSLCContainerStartFlags startFlags{};
WI_SetFlagIf(startFlags, WSLCContainerStartFlagsAttach, !runOptions.Detach);
THROW_IF_FAILED(container.Start(startFlags, nullptr)); // TODO: Error message, detach keys
// Handle attach if requested
if (WI_IsFlagSet(startFlags, WSLCContainerStartFlagsAttach))
{
ConsoleService consoleService;
return consoleService.AttachToCurrentConsole(runningContainer.GetInitProcess());
}
PrintMessage(L"%hs", stdout, containerId);
return 0;
}
CreateContainerResult ContainerService::Create(Session& session, const std::string& image, ContainerOptions runOptions)
{
auto runningContainer = CreateInternal(session, image, runOptions);
runningContainer.SetDeleteOnClose(false);
auto& container = runningContainer.Get();
WSLCContainerId id{};
THROW_IF_FAILED(container.GetId(id));
WriteContainerIdToFile(runOptions.CidFile, id);
return {.Id = id};
}
int ContainerService::Start(Session& session, const std::string& id, bool attach)
{
wil::com_ptr<IWSLCContainer> container;
THROW_IF_FAILED(session.Get()->OpenContainer(id.c_str(), &container));
WSLCContainerStartFlags flags = attach ? WSLCContainerStartFlagsAttach : WSLCContainerStartFlagsNone;
THROW_IF_FAILED(container->Start(flags, nullptr));
if (!attach)
{
return 0;
}
wil::com_ptr<IWSLCProcess> process;
THROW_IF_FAILED(container->GetInitProcess(&process));
WSLCProcessFlags processFlags{};
THROW_IF_FAILED(process->GetFlags(&processFlags));
ClientRunningWSLCProcess runningProcess(std::move(process), processFlags);
ConsoleService consoleService;
return consoleService.AttachToCurrentConsole(std::move(runningProcess));
}
void ContainerService::Stop(Session& session, const std::string& id, StopContainerOptions options)
{
wil::com_ptr<IWSLCContainer> container;
THROW_IF_FAILED(session.Get()->OpenContainer(id.c_str(), &container));
THROW_IF_FAILED(container->Stop(options.Signal, options.Timeout));
}
void ContainerService::Kill(Session& session, const std::string& id, WSLCSignal signal)
{
wil::com_ptr<IWSLCContainer> container;
THROW_IF_FAILED(session.Get()->OpenContainer(id.c_str(), &container));
THROW_IF_FAILED(container->Kill(signal));
}
void ContainerService::Delete(Session& session, const std::string& id, bool force)
{
wil::com_ptr<IWSLCContainer> container;
THROW_IF_FAILED(session.Get()->OpenContainer(id.c_str(), &container));
THROW_IF_FAILED(container->Delete(force ? WSLCDeleteFlagsForce : WSLCDeleteFlagsNone));
}
std::vector<ContainerInformation> ContainerService::List(Session& session)
{
std::vector<ContainerInformation> result;
wil::unique_cotaskmem_array_ptr<WSLCContainerEntry> containers;
wil::unique_cotaskmem_array_ptr<WSLCContainerPortMapping> ports;
THROW_IF_FAILED(session.Get()->ListContainers(&containers, containers.size_address<ULONG>(), &ports, ports.size_address<ULONG>()));
for (const auto& current : containers)
{
ContainerInformation entry;
entry.Name = current.Name;
entry.Image = current.Image;
entry.State = current.State;
entry.Id = current.Id;
entry.StateChangedAt = current.StateChangedAt;
entry.CreatedAt = current.CreatedAt;
for (const auto& port : ports)
{
if (strcmp(port.Id, current.Id) == 0)
{
entry.Ports.push_back(PortInformationFromWSLCPortMapping(port.PortMapping));
}
}
result.emplace_back(std::move(entry));
}
return result;
}
int ContainerService::Exec(Session& session, const std::string& id, ContainerOptions options)
{
wil::com_ptr<IWSLCContainer> container;
THROW_IF_FAILED(session.Get()->OpenContainer(id.c_str(), &container));
auto execFlags = WSLCProcessFlagsNone;
WI_SetFlagIf(execFlags, WSLCProcessFlagsStdin, options.Interactive);
WI_SetFlagIf(execFlags, WSLCProcessFlagsTty, options.TTY);
auto processLauncher = wsl::windows::common::WSLCProcessLauncher({}, options.Arguments, options.EnvironmentVariables, execFlags);
if (options.User.has_value())
{
auto user = options.User.value();
processLauncher.SetUser(std::move(user));
}
if (!options.WorkingDirectory.empty())
{
processLauncher.SetWorkingDirectory(std::move(options.WorkingDirectory));
}
return ConsoleService::AttachToCurrentConsole(processLauncher.Launch(*container));
}
InspectContainer ContainerService::Inspect(Session& session, const std::string& id)
{
wil::com_ptr<IWSLCContainer> container;
THROW_IF_FAILED(session.Get()->OpenContainer(id.c_str(), &container));
wil::unique_cotaskmem_ansistring output;
THROW_IF_FAILED(container->Inspect(&output));
return wsl::shared::FromJson<InspectContainer>(output.get());
}
void ContainerService::Logs(Session& session, const std::string& id, bool follow)
{
wil::com_ptr<IWSLCContainer> container;
THROW_IF_FAILED(session.Get()->OpenContainer(id.c_str(), &container));
COMOutputHandle stdoutHandle;
COMOutputHandle stderrHandle;
WSLCLogsFlags flags = WSLCLogsFlagsNone;
WI_SetFlagIf(flags, WSLCLogsFlagsFollow, follow);
THROW_IF_FAILED(container->Logs(flags, &stdoutHandle, &stderrHandle, 0, 0, 0));
wsl::windows::common::relay::MultiHandleWait io;
io.AddHandle(std::make_unique<wsl::windows::common::relay::RelayHandle<wsl::windows::common::relay::ReadHandle>>(
stdoutHandle.Release(), GetStdHandle(STD_OUTPUT_HANDLE)));
if (!stderrHandle.Empty()) // This handle is only used for non-tty processes.
{
io.AddHandle(std::make_unique<wsl::windows::common::relay::RelayHandle<wsl::windows::common::relay::ReadHandle>>(
stderrHandle.Release(), GetStdHandle(STD_ERROR_HANDLE)));
}
// TODO: Handle ctrl-c.
io.Run({});
}
} // namespace wsl::windows::wslc::services