| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | WSLCContainerLauncher.h |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | This file contains the definition for WSLCContainerLauncher. |
| 12 | |
| 13 | --*/ |
| 14 | |
| 15 | #pragma once |
| 16 | #include "MountSpecParsing.h" |
| 17 | #include "WSLCProcessLauncher.h" |
| 18 | #include "docker_schema.h" |
| 19 | #include "wslc_schema.h" |
| 20 | |
| 21 | namespace wsl::windows::common { |
| 22 | |
| 23 | class RunningWSLCContainer |
| 24 | { |
| 25 | public: |
| 26 | NON_COPYABLE(RunningWSLCContainer); |
| 27 | DEFAULT_MOVABLE(RunningWSLCContainer); |
| 28 | RunningWSLCContainer(wil::com_ptr<IWSLCContainer>&& Container, WSLCProcessFlags Flags); |
| 29 | ~RunningWSLCContainer(); |
| 30 | IWSLCContainer& Get(); |
| 31 | |
| 32 | WSLCContainerState State(); |
| 33 | ClientRunningWSLCProcess GetInitProcess(); |
| 34 | void SetDeleteOnClose(bool deleteOnClose); |
| 35 | void Reset(); |
| 36 | wslc_schema::InspectContainer Inspect(); |
| 37 | std::string Id(); |
| 38 | std::string Name(); |
| 39 | std::map<std::string, std::string> Labels(); |
| 40 | |
| 41 | private: |
| 42 | wil::com_ptr<IWSLCContainer> m_container; |
| 43 | WSLCProcessFlags m_flags; |
| 44 | bool m_deleteOnClose = true; |
| 45 | }; |
| 46 | |
| 47 | class WSLCContainerLauncher : private WSLCProcessLauncher |
| 48 | { |
| 49 | public: |
| 50 | NON_COPYABLE(WSLCContainerLauncher); |
| 51 | NON_MOVABLE(WSLCContainerLauncher); |
| 52 | |
| 53 | WSLCContainerLauncher( |
| 54 | const std::string& Image, |
| 55 | const std::string& Name = "", |
| 56 | const std::vector<std::string>& Arguments = {}, |
| 57 | const std::vector<std::string>& Environment = {}, |
| 58 | std::string networkMode = "host", |
| 59 | WSLCProcessFlags Flags = WSLCProcessFlagsNone); |
| 60 | |
| 61 | void AddVolume(const std::wstring& HostPath, const std::string& ContainerPath, bool ReadOnly); |
| 62 | void AddNamedVolume(const std::string& Name, const std::string& ContainerPath, bool ReadOnly); |
| 63 | void AddMount(const mount::Spec& Mount); |
| 64 | void AddPort(uint16_t WindowsPort, uint16_t ContainerPort, int Family, int Protocol = IPPROTO_TCP, const std::optional<std::string>& BindingAddress = {}); |
| 65 | void AddLabel(const std::string& Key, const std::string& Value); |
| 66 | void AddTmpfs(const std::string& ContainerPath, const std::string& Options); |
| 67 | void AddAdditionalNetwork(const std::string& Name); |
| 68 | void AddAdditionalNetwork(const std::string& Name, const std::vector<std::string>& Aliases); |
| 69 | void AddPrimaryNetworkAlias(const std::string& Alias); |
| 70 | void SetPrimaryNetworkIpAddress(std::string&& Address); |
| 71 | |
| 72 | std::pair<HRESULT, std::optional<RunningWSLCContainer>> CreateNoThrow(IWSLCSession& Session, IWarningCallback* WarningCallback = nullptr); |
| 73 | RunningWSLCContainer Create(IWSLCSession& Session, IWarningCallback* WarningCallback = nullptr); |
| 74 | |
| 75 | RunningWSLCContainer Launch(IWSLCSession& Session, WSLCContainerStartFlags Flags = WSLCContainerStartFlagsAttach, IWarningCallback* WarningCallback = nullptr); |
| 76 | std::pair<HRESULT, std::optional<RunningWSLCContainer>> LaunchNoThrow( |
| 77 | IWSLCSession& Session, WSLCContainerStartFlags Flags = WSLCContainerStartFlagsAttach, IWarningCallback* WarningCallback = nullptr); |
| 78 | |
| 79 | void SetName(std::string&& Name); |
| 80 | void SetEntrypoint(std::vector<std::string>&& entrypoint); |
| 81 | void SetDefaultStopSignal(WSLCSignal Signal); |
| 82 | void SetStopTimeout(LONG Timeout); |
| 83 | void SetShmSize(int64_t ShmSize); |
| 84 | void SetHealthCmd(std::string&& HealthCmd); |
| 85 | void SetHealthInterval(int64_t Nanoseconds); |
| 86 | void SetHealthTimeout(int64_t Nanoseconds); |
| 87 | void SetHealthStartPeriod(int64_t Nanoseconds); |
| 88 | void SetHealthRetries(LONG Retries); |
| 89 | void SetNoHealthcheck(); |
| 90 | void SetContainerFlags(WSLCContainerFlags Flags); |
| 91 | void SetHostname(std::string&& Hostname); |
| 92 | void SetDomainname(std::string&& Domainame); |
| 93 | void SetDnsServers(std::vector<std::string>&& DnsServers); |
| 94 | void SetDnsSearchDomains(std::vector<std::string>&& DnsSearchDomains); |
| 95 | void SetDnsOptions(std::vector<std::string>&& DnsOptions); |
| 96 | void SetMemoryLimit(std::int64_t Bytes); |
| 97 | void SetNanoCpus(std::int64_t NanoCpus); |
| 98 | void AddUlimit(const std::string& Name, std::int64_t Soft, std::int64_t Hard); |
| 99 | |
| 100 | using WSLCProcessLauncher::FormatResult; |
| 101 | using WSLCProcessLauncher::SetTtySize; |
| 102 | using WSLCProcessLauncher::SetUser; |
| 103 | using WSLCProcessLauncher::SetWorkingDirectory; |
| 104 | |
| 105 | private: |
| 106 | struct NetworkConnection |
| 107 | { |
| 108 | std::string Name; |
| 109 | std::vector<std::string> Aliases; |
| 110 | }; |
| 111 | |
| 112 | std::string m_image; |
| 113 | std::string m_name; |
| 114 | std::vector<WSLCPortMapping> m_ports; |
| 115 | std::vector<WSLCMountSpec> m_mounts; |
| 116 | std::deque<std::wstring> m_mountSources; |
| 117 | std::deque<std::string> m_mountTargets; |
| 118 | std::deque<std::string> m_mountTmpfsOptions; |
| 119 | std::string m_networkMode; |
| 120 | std::vector<std::string> m_entrypoint; |
| 121 | WSLCSignal m_stopSignal = WSLCSignalNone; |
| 122 | std::optional<LONG> m_stopTimeout; |
| 123 | int64_t m_shmSize = 0; |
| 124 | std::optional<std::string> m_healthCmd; |
| 125 | std::optional<int64_t> m_healthInterval; |
| 126 | std::optional<int64_t> m_healthTimeout; |
| 127 | std::optional<int64_t> m_healthStartPeriod; |
| 128 | std::optional<LONG> m_healthRetries; |
| 129 | WSLCContainerFlags m_containerFlags = WSLCContainerFlagsNone; |
| 130 | std::string m_hostname; |
| 131 | std::string m_domainname; |
| 132 | std::vector<std::string> m_dnsServers; |
| 133 | std::vector<std::string> m_dnsSearchDomains; |
| 134 | std::vector<std::string> m_dnsOptions; |
| 135 | std::vector<NetworkConnection> m_additionalNetworks; |
| 136 | std::vector<std::string> m_primaryNetworkAliases; |
| 137 | std::optional<std::string> m_primaryNetworkIpAddress; |
| 138 | std::vector<WSLCLabel> m_labels; |
| 139 | std::deque<std::string> m_labelKeys; |
| 140 | std::deque<std::string> m_labelValues; |
| 141 | std::int64_t m_memoryBytes = 0; |
| 142 | std::int64_t m_nanoCpus = 0; |
| 143 | std::vector<WSLCUlimit> m_ulimits; |
| 144 | std::deque<std::string> m_ulimitNames; |
| 145 | }; |
| 146 | } // namespace wsl::windows::common |