-
Notifications
You must be signed in to change notification settings - Fork 238
/
Copy pathconfiguration.ts
237 lines (217 loc) · 8.52 KB
/
configuration.ts
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as path from 'path';
import { URI } from 'vscode-uri';
import { FileHost, parentURI, uriToFsPath } from './configurationCommonUtils';
import { RemoteDocuments } from './editableFiles';
export type DevContainerConfig = DevContainerFromImageConfig | DevContainerFromDockerfileConfig | DevContainerFromDockerComposeConfig;
export interface PortAttributes {
label: string | undefined;
onAutoForward: string | undefined;
elevateIfNeeded: boolean | undefined;
}
export type UserEnvProbe = 'none' | 'loginInteractiveShell' | 'interactiveShell' | 'loginShell';
export type DevContainerConfigCommand = 'initializeCommand' | 'onCreateCommand' | 'updateContentCommand' | 'postCreateCommand' | 'postStartCommand' | 'postAttachCommand';
export interface HostRequirements {
cpus?: number;
memory?: string;
storage?: string;
}
export interface DevContainerFeature {
id: string;
options: boolean | string | Record<string, boolean | string | undefined>;
}
export interface DevContainerFromImageConfig {
configFilePath: URI;
image: string;
name?: string;
forwardPorts?: (number | string)[];
appPort?: number | string | (number | string)[];
portsAttributes?: Record<string, PortAttributes>;
otherPortsAttributes?: PortAttributes;
runArgs?: string[];
shutdownAction?: 'none' | 'stopContainer';
overrideCommand?: boolean;
initializeCommand?: string | string[];
onCreateCommand?: string | string[];
updateContentCommand?: string | string[];
postCreateCommand?: string | string[];
postStartCommand?: string | string[];
postAttachCommand?: string | string[];
waitFor?: DevContainerConfigCommand;
/** remote path to folder or workspace */
workspaceFolder?: string;
workspaceMount?: string;
mounts?: string[];
containerEnv?: Record<string, string>;
remoteEnv?: Record<string, string | null>;
containerUser?: string;
remoteUser?: string;
updateRemoteUserUID?: boolean;
userEnvProbe?: UserEnvProbe;
features?: DevContainerFeature[] | Record<string, string | boolean | Record<string, string | boolean>>;
hostRequirements?: HostRequirements;
}
export type DevContainerFromDockerfileConfig = {
configFilePath: URI;
name?: string;
forwardPorts?: (number | string)[];
appPort?: number | string | (number | string)[];
portsAttributes?: Record<string, PortAttributes>;
otherPortsAttributes?: PortAttributes;
runArgs?: string[];
shutdownAction?: 'none' | 'stopContainer';
overrideCommand?: boolean;
initializeCommand?: string | string[];
onCreateCommand?: string | string[];
updateContentCommand?: string | string[];
postCreateCommand?: string | string[];
postStartCommand?: string | string[];
postAttachCommand?: string | string[];
waitFor?: DevContainerConfigCommand;
/** remote path to folder or workspace */
workspaceFolder?: string;
workspaceMount?: string;
mounts?: string[];
containerEnv?: Record<string, string>;
remoteEnv?: Record<string, string | null>;
containerUser?: string;
remoteUser?: string;
updateRemoteUserUID?: boolean;
userEnvProbe?: UserEnvProbe;
features?: DevContainerFeature[] | Record<string, string | boolean | Record<string, string | boolean>>;
hostRequirements?: HostRequirements;
} & (
{
dockerFile: string;
context?: string;
build?: {
target?: string;
args?: Record<string, string>;
cacheFrom?: string | string[];
};
}
|
{
build: {
dockerfile: string;
context?: string;
target?: string;
args?: Record<string, string>;
cacheFrom?: string | string[];
};
}
);
export interface DevContainerFromDockerComposeConfig {
configFilePath: URI;
dockerComposeFile: string | string[];
service: string;
workspaceFolder: string;
name?: string;
forwardPorts?: (number | string)[];
portsAttributes?: Record<string, PortAttributes>;
otherPortsAttributes?: PortAttributes;
shutdownAction?: 'none' | 'stopCompose';
overrideCommand?: boolean;
initializeCommand?: string | string[];
onCreateCommand?: string | string[];
updateContentCommand?: string | string[];
postCreateCommand?: string | string[];
postStartCommand?: string | string[];
postAttachCommand?: string | string[];
waitFor?: DevContainerConfigCommand;
runServices?: string[];
remoteEnv?: Record<string, string | null>;
remoteUser?: string;
updateRemoteUserUID?: boolean;
userEnvProbe?: UserEnvProbe;
features?: DevContainerFeature[] | Record<string, string | boolean | Record<string, string | boolean>>;
hostRequirements?: HostRequirements;
}
interface DevContainerVSCodeConfig {
extensions?: string[];
settings?: object;
devPort?: number;
}
export function updateFromOldProperties<T extends DevContainerConfig & DevContainerVSCodeConfig & { customizations?: { vscode?: DevContainerVSCodeConfig } }>(original: T): T {
// https://github.com/microsoft/dev-container-spec/issues/1
if (!(original.extensions || original.settings || original.devPort !== undefined)) {
return original;
}
const copy = { ...original };
const customizations = copy.customizations || (copy.customizations = {});
const vscode = customizations.vscode || (customizations.vscode = {});
if (copy.extensions) {
vscode.extensions = (vscode.extensions || []).concat(copy.extensions);
delete copy.extensions;
}
if (copy.settings) {
vscode.settings = {
...copy.settings,
...(vscode.settings || {}),
};
delete copy.settings;
}
if (copy.devPort !== undefined && vscode.devPort === undefined) {
vscode.devPort = copy.devPort;
delete copy.devPort;
}
return copy;
}
export function getConfigFilePath(cliHost: { platform: NodeJS.Platform }, config: { configFilePath: URI }, relativeConfigFilePath: string) {
return resolveConfigFilePath(cliHost, config.configFilePath, relativeConfigFilePath);
}
export function resolveConfigFilePath(cliHost: { platform: NodeJS.Platform }, configFilePath: URI, relativeConfigFilePath: string) {
const folder = parentURI(configFilePath);
return configFilePath.with({
path: path.posix.resolve(folder.path, (cliHost.platform === 'win32' && configFilePath.scheme !== RemoteDocuments.scheme) ? (path.win32.isAbsolute(relativeConfigFilePath) ? '/' : '') + relativeConfigFilePath.replace(/\\/g, '/') : relativeConfigFilePath)
});
}
export function isDockerFileConfig(config: DevContainerConfig): config is DevContainerFromDockerfileConfig {
return 'dockerFile' in config || ('build' in config && 'dockerfile' in config.build);
}
export function getDockerfilePath(cliHost: { platform: NodeJS.Platform }, config: DevContainerFromDockerfileConfig) {
return getConfigFilePath(cliHost, config, getDockerfile(config));
}
export function getDockerfile(config: DevContainerFromDockerfileConfig) {
return 'dockerFile' in config ? config.dockerFile : config.build.dockerfile;
}
export async function getDockerComposeFilePaths(cliHost: FileHost, config: DevContainerFromDockerComposeConfig, envForComposeFile?: NodeJS.ProcessEnv, cwdForDefaultFiles?: string) {
if (Array.isArray(config.dockerComposeFile)) {
if (config.dockerComposeFile.length) {
return config.dockerComposeFile.map(composeFile => uriToFsPath(getConfigFilePath(cliHost, config, composeFile), cliHost.platform));
}
} else if (typeof config.dockerComposeFile === 'string') {
return [uriToFsPath(getConfigFilePath(cliHost, config, config.dockerComposeFile), cliHost.platform)];
}
if (cwdForDefaultFiles) {
const envComposeFile = envForComposeFile?.COMPOSE_FILE;
if (envComposeFile) {
return envComposeFile.split(cliHost.path.delimiter)
.map(composeFile => cliHost.path.resolve(cwdForDefaultFiles, composeFile));
}
try {
const envPath = cliHost.path.join(cwdForDefaultFiles, '.env');
const buffer = await cliHost.readFile(envPath);
const match = /^COMPOSE_FILE=(.+)$/m.exec(buffer.toString());
const envFileComposeFile = match && match[1].trim();
if (envFileComposeFile) {
return envFileComposeFile.split(cliHost.path.delimiter)
.map(composeFile => cliHost.path.resolve(cwdForDefaultFiles, composeFile));
}
} catch (err) {
if (!(err && (err.code === 'ENOENT' || err.code === 'EISDIR'))) {
throw err;
}
}
const defaultFiles = [cliHost.path.resolve(cwdForDefaultFiles, 'docker-compose.yml')];
const override = cliHost.path.resolve(cwdForDefaultFiles, 'docker-compose.override.yml');
if (await cliHost.isFile(override)) {
defaultFiles.push(override);
}
return defaultFiles;
}
return [];
}