-
Notifications
You must be signed in to change notification settings - Fork 278
/
Copy pathdevContainersSpecCLI.ts
1416 lines (1320 loc) · 76.5 KB
/
devContainersSpecCLI.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
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*---------------------------------------------------------------------------------------------
* 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 yargs, { Argv } from 'yargs';
import textTable from 'text-table';
import * as jsonc from 'jsonc-parser';
import { createDockerParams, createLog, launch, ProvisionOptions } from './devContainers';
import { SubstitutedConfig, createContainerProperties, envListToObj, inspectDockerImage, isDockerFileConfig, SubstituteConfig, addSubstitution, findContainerAndIdLabels, getCacheFolder, runAsyncHandler } from './utils';
import { URI } from 'vscode-uri';
import { ContainerError } from '../spec-common/errors';
import { Log, LogDimensions, LogLevel, makeLog, mapLogLevel } from '../spec-utils/log';
import { probeRemoteEnv, runLifecycleHooks, runRemoteCommand, UserEnvProbe, setupInContainer } from '../spec-common/injectHeadless';
import { extendImage } from './containerFeatures';
import { dockerCLI, DockerCLIParameters, dockerPtyCLI, inspectContainer } from '../spec-shutdown/dockerUtils';
import { buildAndExtendDockerCompose, dockerComposeCLIConfig, getDefaultImageName, getProjectName, readDockerComposeConfig, readVersionPrefix } from './dockerCompose';
import { DevContainerFromDockerComposeConfig, DevContainerFromDockerfileConfig, getDockerComposeFilePaths } from '../spec-configuration/configuration';
import { workspaceFromPath } from '../spec-utils/workspaces';
import { readDevContainerConfigFile } from './configContainer';
import { getDefaultDevContainerConfigPath, getDevContainerConfigPathIn, uriToFsPath } from '../spec-configuration/configurationCommonUtils';
import { CLIHost, getCLIHost } from '../spec-common/cliHost';
import { loadNativeModule, processSignals } from '../spec-common/commonUtils';
import { loadVersionInfo } from '../spec-configuration/containerFeaturesConfiguration';
import { featuresTestOptions, featuresTestHandler } from './featuresCLI/test';
import { featuresPackageHandler, featuresPackageOptions } from './featuresCLI/package';
import { featuresPublishHandler, featuresPublishOptions } from './featuresCLI/publish';
import { beforeContainerSubstitute, containerSubstitute, substitute } from '../spec-common/variableSubstitution';
import { getPackageConfig, } from '../spec-utils/product';
import { getDevcontainerMetadata, getImageBuildInfo, getImageMetadataFromContainer, ImageMetadataEntry, lifecycleCommandOriginMapFromMetadata, mergeConfiguration, MergedDevContainerConfig } from './imageMetadata';
import { templatesPublishHandler, templatesPublishOptions } from './templatesCLI/publish';
import { templateApplyHandler, templateApplyOptions } from './templatesCLI/apply';
import { featuresInfoHandler as featuresInfoHandler, featuresInfoOptions } from './featuresCLI/info';
import { bailOut, buildNamedImageAndExtend } from './singleContainer';
import { Event, NodeEventEmitter } from '../spec-utils/event';
import { ensureNoDisallowedFeatures } from './disallowedFeatures';
import { featuresResolveDependenciesHandler, featuresResolveDependenciesOptions } from './featuresCLI/resolveDependencies';
import { getFeatureIdWithoutVersion } from '../spec-configuration/containerFeaturesOCI';
import { featuresUpgradeHandler, featuresUpgradeOptions } from './upgradeCommand';
import { readFeaturesConfig } from './featureUtils';
import { featuresGenerateDocsHandler, featuresGenerateDocsOptions } from './featuresCLI/generateDocs';
import { templatesGenerateDocsHandler, templatesGenerateDocsOptions } from './templatesCLI/generateDocs';
import { mapNodeOSToGOOS, mapNodeArchitectureToGOARCH } from '../spec-configuration/containerCollectionsOCI';
import { templateMetadataHandler, templateMetadataOptions } from './templatesCLI/metadata';
const defaultDefaultUserEnvProbe: UserEnvProbe = 'loginInteractiveShell';
const mountRegex = /^type=(bind|volume),source=([^,]+),target=([^,]+)(?:,external=(true|false))?$/;
(async () => {
const packageFolder = path.join(__dirname, '..', '..');
const version = getPackageConfig().version;
const argv = process.argv.slice(2);
const restArgs = argv[0] === 'exec' && argv[1] !== '--help'; // halt-at-non-option doesn't work in subcommands: https://github.com/yargs/yargs/issues/1417
const y = yargs([])
.parserConfiguration({
// By default, yargs allows `--no-myoption` to set a boolean `--myoption` to false
// Disable this to allow `--no-cache` on the `build` command to align with `docker build` syntax
'boolean-negation': false,
'halt-at-non-option': restArgs,
})
.scriptName('devcontainer')
.version(version)
.demandCommand()
.strict();
y.wrap(Math.min(120, y.terminalWidth()));
y.command('up', 'Create and run dev container', provisionOptions, provisionHandler);
y.command('set-up', 'Set up an existing container as a dev container', setUpOptions, setUpHandler);
y.command('build [path]', 'Build a dev container image', buildOptions, buildHandler);
y.command('run-user-commands', 'Run user commands', runUserCommandsOptions, runUserCommandsHandler);
y.command('read-configuration', 'Read configuration', readConfigurationOptions, readConfigurationHandler);
y.command('outdated', 'Show current and available versions', outdatedOptions, outdatedHandler);
y.command('upgrade', 'Upgrade lockfile', featuresUpgradeOptions, featuresUpgradeHandler);
y.command('features', 'Features commands', (y: Argv) => {
y.command('test [target]', 'Test Features', featuresTestOptions, featuresTestHandler);
y.command('package <target>', 'Package Features', featuresPackageOptions, featuresPackageHandler);
y.command('publish <target>', 'Package and publish Features', featuresPublishOptions, featuresPublishHandler);
y.command('info <mode> <feature>', 'Fetch metadata for a published Feature', featuresInfoOptions, featuresInfoHandler);
y.command('resolve-dependencies', 'Read and resolve dependency graph from a configuration', featuresResolveDependenciesOptions, featuresResolveDependenciesHandler);
y.command('generate-docs', 'Generate documentation', featuresGenerateDocsOptions, featuresGenerateDocsHandler);
});
y.command('templates', 'Templates commands', (y: Argv) => {
y.command('apply', 'Apply a template to the project', templateApplyOptions, templateApplyHandler);
y.command('publish <target>', 'Package and publish templates', templatesPublishOptions, templatesPublishHandler);
y.command('metadata <templateId>', 'Fetch a published Template\'s metadata', templateMetadataOptions, templateMetadataHandler);
y.command('generate-docs', 'Generate documentation', templatesGenerateDocsOptions, templatesGenerateDocsHandler);
});
y.command(restArgs ? ['exec', '*'] : ['exec <cmd> [args..]'], 'Execute a command on a running dev container', execOptions, execHandler);
y.epilog(`devcontainer@${version} ${packageFolder}`);
y.parse(restArgs ? argv.slice(1) : argv);
})().catch(console.error);
export type UnpackArgv<T> = T extends Argv<infer U> ? U : T;
function provisionOptions(y: Argv) {
return y.options({
'docker-path': { type: 'string', description: 'Docker CLI path.' },
'docker-compose-path': { type: 'string', description: 'Docker Compose CLI path.' },
'container-data-folder': { type: 'string', description: 'Container data folder where user data inside the container will be stored.' },
'container-system-data-folder': { type: 'string', description: 'Container system data folder where system data inside the container will be stored.' },
'workspace-folder': { type: 'string', description: 'Workspace folder path. The devcontainer.json will be looked up relative to this path.', default: '.', defaultDescription: 'Current Working Directory' },
'workspace-mount-consistency': { choices: ['consistent' as 'consistent', 'cached' as 'cached', 'delegated' as 'delegated'], default: 'cached' as 'cached', description: 'Workspace mount consistency.' },
'gpu-availability': { choices: ['all' as 'all', 'detect' as 'detect', 'none' as 'none'], default: 'detect' as 'detect', description: 'Availability of GPUs in case the dev container requires any. `all` expects a GPU to be available.' },
'mount-workspace-git-root': { type: 'boolean', default: true, description: 'Mount the workspace using its Git root.' },
'id-label': { type: 'string', description: 'Id label(s) of the format name=value. These will be set on the container and used to query for an existing container. If no --id-label is given, one will be inferred from the --workspace-folder path.' },
'config': { type: 'string', description: 'devcontainer.json path. The default is to use .devcontainer/devcontainer.json or, if that does not exist, .devcontainer.json in the workspace folder.' },
'override-config': { type: 'string', description: 'devcontainer.json path to override any devcontainer.json in the workspace folder (or built-in configuration). This is required when there is no devcontainer.json otherwise.' },
'log-level': { choices: ['info' as 'info', 'debug' as 'debug', 'trace' as 'trace'], default: 'info' as 'info', description: 'Log level for the --terminal-log-file. When set to trace, the log level for --log-file will also be set to trace.' },
'log-format': { choices: ['text' as 'text', 'json' as 'json'], default: 'text' as 'text', description: 'Log format.' },
'terminal-columns': { type: 'number', implies: ['terminal-rows'], description: 'Number of columns to render the output for. This is required for some of the subprocesses to correctly render their output.' },
'terminal-rows': { type: 'number', implies: ['terminal-columns'], description: 'Number of rows to render the output for. This is required for some of the subprocesses to correctly render their output.' },
'default-user-env-probe': { choices: ['none' as 'none', 'loginInteractiveShell' as 'loginInteractiveShell', 'interactiveShell' as 'interactiveShell', 'loginShell' as 'loginShell'], default: defaultDefaultUserEnvProbe, description: 'Default value for the devcontainer.json\'s "userEnvProbe".' },
'update-remote-user-uid-default': { choices: ['never' as 'never', 'on' as 'on', 'off' as 'off'], default: 'on' as 'on', description: 'Default for updating the remote user\'s UID and GID to the local user\'s one.' },
'remove-existing-container': { type: 'boolean', default: false, description: 'Removes the dev container if it already exists.' },
'build-no-cache': { type: 'boolean', default: false, description: 'Builds the image with `--no-cache` if the container does not exist.' },
'expect-existing-container': { type: 'boolean', default: false, description: 'Fail if the container does not exist.' },
'skip-post-create': { type: 'boolean', default: false, description: 'Do not run onCreateCommand, updateContentCommand, postCreateCommand, postStartCommand or postAttachCommand and do not install dotfiles.' },
'skip-non-blocking-commands': { type: 'boolean', default: false, description: 'Stop running user commands after running the command configured with waitFor or the updateContentCommand by default.' },
prebuild: { type: 'boolean', default: false, description: 'Stop after onCreateCommand and updateContentCommand, rerunning updateContentCommand if it has run before.' },
'user-data-folder': { type: 'string', description: 'Host path to a directory that is intended to be persisted and share state between sessions.' },
'mount': { type: 'string', description: 'Additional mount point(s). Format: type=<bind|volume>,source=<source>,target=<target>[,external=<true|false>]' },
'remote-env': { type: 'string', description: 'Remote environment variables of the format name=value. These will be added when executing the user commands.' },
'cache-from': { type: 'string', description: 'Additional image to use as potential layer cache during image building' },
'cache-to': { type: 'string', description: 'Additional image to use as potential layer cache during image building' },
'buildkit': { choices: ['auto' as 'auto', 'never' as 'never'], default: 'auto' as 'auto', description: 'Control whether BuildKit should be used' },
'additional-features': { type: 'string', description: 'Additional features to apply to the dev container (JSON as per "features" section in devcontainer.json)' },
'skip-feature-auto-mapping': { type: 'boolean', default: false, hidden: true, description: 'Temporary option for testing.' },
'skip-post-attach': { type: 'boolean', default: false, description: 'Do not run postAttachCommand.' },
'dotfiles-repository': { type: 'string', description: 'URL of a dotfiles Git repository (e.g., https://github.com/owner/repository.git)' },
'dotfiles-install-command': { type: 'string', description: 'The command to run after cloning the dotfiles repository. Defaults to run the first file of `install.sh`, `install`, `bootstrap.sh`, `bootstrap`, `setup.sh` and `setup` found in the dotfiles repository`s root folder.' },
'dotfiles-target-path': { type: 'string', default: '~/dotfiles', description: 'The path to clone the dotfiles repository to. Defaults to `~/dotfiles`.' },
'container-session-data-folder': { type: 'string', description: 'Folder to cache CLI data, for example userEnvProbe results' },
'omit-config-remote-env-from-metadata': { type: 'boolean', default: false, hidden: true, description: 'Omit remoteEnv from devcontainer.json for container metadata label' },
'secrets-file': { type: 'string', description: 'Path to a json file containing secret environment variables as key-value pairs.' },
'experimental-lockfile': { type: 'boolean', default: false, hidden: true, description: 'Write lockfile' },
'experimental-frozen-lockfile': { type: 'boolean', default: false, hidden: true, description: 'Ensure lockfile remains unchanged' },
'omit-syntax-directive': { type: 'boolean', default: false, hidden: true, description: 'Omit Dockerfile syntax directives' },
'include-configuration': { type: 'boolean', default: false, description: 'Include configuration in result.' },
'include-merged-configuration': { type: 'boolean', default: false, description: 'Include merged configuration in result.' },
})
.check(argv => {
const idLabels = (argv['id-label'] && (Array.isArray(argv['id-label']) ? argv['id-label'] : [argv['id-label']])) as string[] | undefined;
if (idLabels?.some(idLabel => !/.+=.+/.test(idLabel))) {
throw new Error('Unmatched argument format: id-label must match <name>=<value>');
}
const mounts = (argv.mount && (Array.isArray(argv.mount) ? argv.mount : [argv.mount])) as string[] | undefined;
if (mounts?.some(mount => !mountRegex.test(mount))) {
throw new Error('Unmatched argument format: mount must match type=<bind|volume>,source=<source>,target=<target>[,external=<true|false>]');
}
const remoteEnvs = (argv['remote-env'] && (Array.isArray(argv['remote-env']) ? argv['remote-env'] : [argv['remote-env']])) as string[] | undefined;
if (remoteEnvs?.some(remoteEnv => !/.+=.*/.test(remoteEnv))) {
throw new Error('Unmatched argument format: remote-env must match <name>=<value>');
}
return true;
});
}
type ProvisionArgs = UnpackArgv<ReturnType<typeof provisionOptions>>;
function provisionHandler(args: ProvisionArgs) {
runAsyncHandler(provision.bind(null, args));
}
async function provision({
'user-data-folder': persistedFolder,
'docker-path': dockerPath,
'docker-compose-path': dockerComposePath,
'container-data-folder': containerDataFolder,
'container-system-data-folder': containerSystemDataFolder,
'workspace-folder': workspaceFolderArg,
'workspace-mount-consistency': workspaceMountConsistency,
'gpu-availability': gpuAvailability,
'mount-workspace-git-root': mountWorkspaceGitRoot,
'id-label': idLabel,
config,
'override-config': overrideConfig,
'log-level': logLevel,
'log-format': logFormat,
'terminal-rows': terminalRows,
'terminal-columns': terminalColumns,
'default-user-env-probe': defaultUserEnvProbe,
'update-remote-user-uid-default': updateRemoteUserUIDDefault,
'remove-existing-container': removeExistingContainer,
'build-no-cache': buildNoCache,
'expect-existing-container': expectExistingContainer,
'skip-post-create': skipPostCreate,
'skip-non-blocking-commands': skipNonBlocking,
prebuild,
mount,
'remote-env': addRemoteEnv,
'cache-from': addCacheFrom,
'cache-to': addCacheTo,
'buildkit': buildkit,
'additional-features': additionalFeaturesJson,
'skip-feature-auto-mapping': skipFeatureAutoMapping,
'skip-post-attach': skipPostAttach,
'dotfiles-repository': dotfilesRepository,
'dotfiles-install-command': dotfilesInstallCommand,
'dotfiles-target-path': dotfilesTargetPath,
'container-session-data-folder': containerSessionDataFolder,
'omit-config-remote-env-from-metadata': omitConfigRemotEnvFromMetadata,
'secrets-file': secretsFile,
'experimental-lockfile': experimentalLockfile,
'experimental-frozen-lockfile': experimentalFrozenLockfile,
'omit-syntax-directive': omitSyntaxDirective,
'include-configuration': includeConfig,
'include-merged-configuration': includeMergedConfig,
}: ProvisionArgs) {
const workspaceFolder = path.resolve(process.cwd(), workspaceFolderArg);
const addRemoteEnvs = addRemoteEnv ? (Array.isArray(addRemoteEnv) ? addRemoteEnv as string[] : [addRemoteEnv]) : [];
const addCacheFroms = addCacheFrom ? (Array.isArray(addCacheFrom) ? addCacheFrom as string[] : [addCacheFrom]) : [];
const additionalFeatures = additionalFeaturesJson ? jsonc.parse(additionalFeaturesJson) as Record<string, string | boolean | Record<string, string | boolean>> : {};
const providedIdLabels = idLabel ? Array.isArray(idLabel) ? idLabel as string[] : [idLabel] : undefined;
const cwd = workspaceFolder || process.cwd();
const cliHost = await getCLIHost(cwd, loadNativeModule, logFormat === 'text');
const secretsP = readSecretsFromFile({ secretsFile, cliHost });
const options: ProvisionOptions = {
dockerPath,
dockerComposePath,
containerDataFolder,
containerSystemDataFolder,
workspaceFolder,
workspaceMountConsistency,
gpuAvailability,
mountWorkspaceGitRoot,
configFile: config ? URI.file(path.resolve(process.cwd(), config)) : undefined,
overrideConfigFile: overrideConfig ? URI.file(path.resolve(process.cwd(), overrideConfig)) : undefined,
logLevel: mapLogLevel(logLevel),
logFormat,
log: text => process.stderr.write(text),
terminalDimensions: terminalColumns && terminalRows ? { columns: terminalColumns, rows: terminalRows } : undefined,
defaultUserEnvProbe,
removeExistingContainer,
buildNoCache,
expectExistingContainer,
postCreateEnabled: !skipPostCreate,
skipNonBlocking,
prebuild,
persistedFolder,
additionalMounts: mount ? (Array.isArray(mount) ? mount : [mount]).map(mount => {
const [, type, source, target, external] = mountRegex.exec(mount)!;
return {
type: type as 'bind' | 'volume',
source,
target,
external: external === 'true'
};
}) : [],
dotfiles: {
repository: dotfilesRepository,
installCommand: dotfilesInstallCommand,
targetPath: dotfilesTargetPath,
},
updateRemoteUserUIDDefault,
remoteEnv: envListToObj(addRemoteEnvs),
secretsP,
additionalCacheFroms: addCacheFroms,
useBuildKit: buildkit,
buildxPlatform: undefined,
buildxPush: false,
additionalLabels: [],
buildxOutput: undefined,
buildxCacheTo: addCacheTo,
additionalFeatures,
skipFeatureAutoMapping,
skipPostAttach,
containerSessionDataFolder,
skipPersistingCustomizationsFromFeatures: false,
omitConfigRemotEnvFromMetadata,
experimentalLockfile,
experimentalFrozenLockfile,
omitSyntaxDirective,
includeConfig,
includeMergedConfig,
};
const result = await doProvision(options, providedIdLabels);
const exitCode = result.outcome === 'error' ? 1 : 0;
await new Promise<void>((resolve, reject) => {
process.stdout.write(JSON.stringify(result) + '\n', err => err ? reject(err) : resolve());
});
if (result.outcome === 'success') {
await result.finishBackgroundTasks();
}
await result.dispose();
process.exit(exitCode);
}
async function doProvision(options: ProvisionOptions, providedIdLabels: string[] | undefined) {
const disposables: (() => Promise<unknown> | undefined)[] = [];
const dispose = async () => {
await Promise.all(disposables.map(d => d()));
};
try {
const result = await launch(options, providedIdLabels, disposables);
return {
outcome: 'success' as 'success',
dispose,
...result,
};
} catch (originalError) {
const originalStack = originalError?.stack;
const err = originalError instanceof ContainerError ? originalError : new ContainerError({
description: 'An error occurred setting up the container.',
originalError
});
if (originalStack) {
console.error(originalStack);
}
return {
outcome: 'error' as 'error',
message: err.message,
description: err.description,
containerId: err.containerId,
disallowedFeatureId: err.data.disallowedFeatureId,
didStopContainer: err.data.didStopContainer,
learnMoreUrl: err.data.learnMoreUrl,
dispose,
};
}
}
function setUpOptions(y: Argv) {
return y.options({
'docker-path': { type: 'string', description: 'Docker CLI path.' },
'container-data-folder': { type: 'string', description: 'Container data folder where user data inside the container will be stored.' },
'container-system-data-folder': { type: 'string', description: 'Container system data folder where system data inside the container will be stored.' },
'container-id': { type: 'string', required: true, description: 'Id of the container.' },
'config': { type: 'string', description: 'devcontainer.json path.' },
'log-level': { choices: ['info' as 'info', 'debug' as 'debug', 'trace' as 'trace'], default: 'info' as 'info', description: 'Log level for the --terminal-log-file. When set to trace, the log level for --log-file will also be set to trace.' },
'log-format': { choices: ['text' as 'text', 'json' as 'json'], default: 'text' as 'text', description: 'Log format.' },
'terminal-columns': { type: 'number', implies: ['terminal-rows'], description: 'Number of columns to render the output for. This is required for some of the subprocesses to correctly render their output.' },
'terminal-rows': { type: 'number', implies: ['terminal-columns'], description: 'Number of rows to render the output for. This is required for some of the subprocesses to correctly render their output.' },
'default-user-env-probe': { choices: ['none' as 'none', 'loginInteractiveShell' as 'loginInteractiveShell', 'interactiveShell' as 'interactiveShell', 'loginShell' as 'loginShell'], default: defaultDefaultUserEnvProbe, description: 'Default value for the devcontainer.json\'s "userEnvProbe".' },
'skip-post-create': { type: 'boolean', default: false, description: 'Do not run onCreateCommand, updateContentCommand, postCreateCommand, postStartCommand or postAttachCommand and do not install dotfiles.' },
'skip-non-blocking-commands': { type: 'boolean', default: false, description: 'Stop running user commands after running the command configured with waitFor or the updateContentCommand by default.' },
'user-data-folder': { type: 'string', description: 'Host path to a directory that is intended to be persisted and share state between sessions.' },
'remote-env': { type: 'string', description: 'Remote environment variables of the format name=value. These will be added when executing the user commands.' },
'dotfiles-repository': { type: 'string', description: 'URL of a dotfiles Git repository (e.g., https://github.com/owner/repository.git)' },
'dotfiles-install-command': { type: 'string', description: 'The command to run after cloning the dotfiles repository. Defaults to run the first file of `install.sh`, `install`, `bootstrap.sh`, `bootstrap`, `setup.sh` and `setup` found in the dotfiles repository`s root folder.' },
'dotfiles-target-path': { type: 'string', default: '~/dotfiles', description: 'The path to clone the dotfiles repository to. Defaults to `~/dotfiles`.' },
'container-session-data-folder': { type: 'string', description: 'Folder to cache CLI data, for example userEnvProbe results' },
'include-configuration': { type: 'boolean', default: false, description: 'Include configuration in result.' },
'include-merged-configuration': { type: 'boolean', default: false, description: 'Include merged configuration in result.' },
})
.check(argv => {
const remoteEnvs = (argv['remote-env'] && (Array.isArray(argv['remote-env']) ? argv['remote-env'] : [argv['remote-env']])) as string[] | undefined;
if (remoteEnvs?.some(remoteEnv => !/.+=.*/.test(remoteEnv))) {
throw new Error('Unmatched argument format: remote-env must match <name>=<value>');
}
return true;
});
}
type SetUpArgs = UnpackArgv<ReturnType<typeof setUpOptions>>;
function setUpHandler(args: SetUpArgs) {
runAsyncHandler(setUp.bind(null, args));
}
async function setUp(args: SetUpArgs) {
const result = await doSetUp(args);
const exitCode = result.outcome === 'error' ? 1 : 0;
await new Promise<void>((resolve, reject) => {
process.stdout.write(JSON.stringify(result) + '\n', err => err ? reject(err) : resolve());
});
await result.dispose();
process.exit(exitCode);
}
async function doSetUp({
'user-data-folder': persistedFolder,
'docker-path': dockerPath,
'container-data-folder': containerDataFolder,
'container-system-data-folder': containerSystemDataFolder,
'container-id': containerId,
config: configParam,
'log-level': logLevel,
'log-format': logFormat,
'terminal-rows': terminalRows,
'terminal-columns': terminalColumns,
'default-user-env-probe': defaultUserEnvProbe,
'skip-post-create': skipPostCreate,
'skip-non-blocking-commands': skipNonBlocking,
'remote-env': addRemoteEnv,
'dotfiles-repository': dotfilesRepository,
'dotfiles-install-command': dotfilesInstallCommand,
'dotfiles-target-path': dotfilesTargetPath,
'container-session-data-folder': containerSessionDataFolder,
'include-configuration': includeConfig,
'include-merged-configuration': includeMergedConfig,
}: SetUpArgs) {
const disposables: (() => Promise<unknown> | undefined)[] = [];
const dispose = async () => {
await Promise.all(disposables.map(d => d()));
};
try {
const addRemoteEnvs = addRemoteEnv ? (Array.isArray(addRemoteEnv) ? addRemoteEnv as string[] : [addRemoteEnv]) : [];
const configFile = configParam ? URI.file(path.resolve(process.cwd(), configParam)) : undefined;
const params = await createDockerParams({
dockerPath,
dockerComposePath: undefined,
containerSessionDataFolder,
containerDataFolder,
containerSystemDataFolder,
workspaceFolder: undefined,
mountWorkspaceGitRoot: false,
configFile,
overrideConfigFile: undefined,
logLevel: mapLogLevel(logLevel),
logFormat,
log: text => process.stderr.write(text),
terminalDimensions: terminalColumns && terminalRows ? { columns: terminalColumns, rows: terminalRows } : undefined,
defaultUserEnvProbe,
removeExistingContainer: false,
buildNoCache: false,
expectExistingContainer: false,
postCreateEnabled: !skipPostCreate,
skipNonBlocking,
prebuild: false,
persistedFolder,
additionalMounts: [],
updateRemoteUserUIDDefault: 'never',
remoteEnv: envListToObj(addRemoteEnvs),
additionalCacheFroms: [],
useBuildKit: 'auto',
buildxPlatform: undefined,
buildxPush: false,
additionalLabels: [],
buildxOutput: undefined,
buildxCacheTo: undefined,
skipFeatureAutoMapping: false,
skipPostAttach: false,
skipPersistingCustomizationsFromFeatures: false,
dotfiles: {
repository: dotfilesRepository,
installCommand: dotfilesInstallCommand,
targetPath: dotfilesTargetPath,
},
}, disposables);
const { common } = params;
const { cliHost, output } = common;
const configs = configFile && await readDevContainerConfigFile(cliHost, undefined, configFile, params.mountWorkspaceGitRoot, output, undefined, undefined);
if (configFile && !configs) {
throw new ContainerError({ description: `Dev container config (${uriToFsPath(configFile, cliHost.platform)}) not found.` });
}
const config0 = configs?.config || {
raw: {},
config: {},
substitute: value => substitute({ platform: cliHost.platform, env: cliHost.env }, value)
};
const container = await inspectContainer(params, containerId);
if (!container) {
bailOut(common.output, 'Dev container not found.');
}
const config = addSubstitution(config0, config => beforeContainerSubstitute(undefined, config));
const imageMetadata = getImageMetadataFromContainer(container, config, undefined, undefined, output).config;
const mergedConfig = mergeConfiguration(config.config, imageMetadata);
const containerProperties = await createContainerProperties(params, container.Id, configs?.workspaceConfig.workspaceFolder, mergedConfig.remoteUser);
const res = await setupInContainer(common, containerProperties, config.config, mergedConfig, lifecycleCommandOriginMapFromMetadata(imageMetadata));
return {
outcome: 'success' as 'success',
configuration: includeConfig ? res.updatedConfig : undefined,
mergedConfiguration: includeMergedConfig ? res.updatedMergedConfig : undefined,
dispose,
};
} catch (originalError) {
const originalStack = originalError?.stack;
const err = originalError instanceof ContainerError ? originalError : new ContainerError({
description: 'An error occurred running user commands in the container.',
originalError
});
if (originalStack) {
console.error(originalStack);
}
return {
outcome: 'error' as 'error',
message: err.message,
description: err.description,
dispose,
};
}
}
function buildOptions(y: Argv) {
return y.options({
'user-data-folder': { type: 'string', description: 'Host path to a directory that is intended to be persisted and share state between sessions.' },
'docker-path': { type: 'string', description: 'Docker CLI path.' },
'docker-compose-path': { type: 'string', description: 'Docker Compose CLI path.' },
'workspace-folder': { type: 'string', description: 'Workspace folder path. The devcontainer.json will be looked up relative to this path.', default: '.', defaultDescription: 'Current Working Directory' },
'config': { type: 'string', description: 'devcontainer.json path. The default is to use .devcontainer/devcontainer.json or, if that does not exist, .devcontainer.json in the workspace folder.' },
'log-level': { choices: ['info' as 'info', 'debug' as 'debug', 'trace' as 'trace'], default: 'info' as 'info', description: 'Log level.' },
'log-format': { choices: ['text' as 'text', 'json' as 'json'], default: 'text' as 'text', description: 'Log format.' },
'no-cache': { type: 'boolean', default: false, description: 'Builds the image with `--no-cache`.' },
'image-name': { type: 'string', description: 'Image name.' },
'cache-from': { type: 'string', description: 'Additional image to use as potential layer cache' },
'cache-to': { type: 'string', description: 'A destination of buildx cache' },
'buildkit': { choices: ['auto' as 'auto', 'never' as 'never'], default: 'auto' as 'auto', description: 'Control whether BuildKit should be used' },
'platform': { type: 'string', description: 'Set target platforms.' },
'push': { type: 'boolean', default: false, description: 'Push to a container registry.' },
'label': { type: 'string', description: 'Provide key and value configuration that adds metadata to an image' },
'output': { type: 'string', description: 'Overrides the default behavior to load built images into the local docker registry. Valid options are the same ones provided to the --output option of docker buildx build.' },
'additional-features': { type: 'string', description: 'Additional features to apply to the dev container (JSON as per "features" section in devcontainer.json)' },
'skip-feature-auto-mapping': { type: 'boolean', default: false, hidden: true, description: 'Temporary option for testing.' },
'skip-persisting-customizations-from-features': { type: 'boolean', default: false, hidden: true, description: 'Do not save customizations from referenced Features as image metadata' },
'experimental-lockfile': { type: 'boolean', default: false, hidden: true, description: 'Write lockfile' },
'experimental-frozen-lockfile': { type: 'boolean', default: false, hidden: true, description: 'Ensure lockfile remains unchanged' },
'omit-syntax-directive': { type: 'boolean', default: false, hidden: true, description: 'Omit Dockerfile syntax directives' },
});
}
type BuildArgs = UnpackArgv<ReturnType<typeof buildOptions>>;
function buildHandler(args: BuildArgs) {
runAsyncHandler(build.bind(null, args));
}
async function build(args: BuildArgs) {
const result = await doBuild(args);
const exitCode = result.outcome === 'error' ? 1 : 0;
await new Promise<void>((resolve, reject) => {
process.stdout.write(JSON.stringify(result) + '\n', err => err ? reject(err) : resolve());
});
await result.dispose();
process.exit(exitCode);
}
async function doBuild({
'user-data-folder': persistedFolder,
'docker-path': dockerPath,
'docker-compose-path': dockerComposePath,
'workspace-folder': workspaceFolderArg,
config: configParam,
'log-level': logLevel,
'log-format': logFormat,
'no-cache': buildNoCache,
'image-name': argImageName,
'cache-from': addCacheFrom,
'buildkit': buildkit,
'platform': buildxPlatform,
'push': buildxPush,
'label': buildxLabel,
'output': buildxOutput,
'cache-to': buildxCacheTo,
'additional-features': additionalFeaturesJson,
'skip-feature-auto-mapping': skipFeatureAutoMapping,
'skip-persisting-customizations-from-features': skipPersistingCustomizationsFromFeatures,
'experimental-lockfile': experimentalLockfile,
'experimental-frozen-lockfile': experimentalFrozenLockfile,
'omit-syntax-directive': omitSyntaxDirective,
}: BuildArgs) {
const disposables: (() => Promise<unknown> | undefined)[] = [];
const dispose = async () => {
await Promise.all(disposables.map(d => d()));
};
try {
const workspaceFolder = path.resolve(process.cwd(), workspaceFolderArg);
const configFile: URI | undefined = configParam ? URI.file(path.resolve(process.cwd(), configParam)) : undefined;
const overrideConfigFile: URI | undefined = /* overrideConfig ? URI.file(path.resolve(process.cwd(), overrideConfig)) : */ undefined;
const addCacheFroms = addCacheFrom ? (Array.isArray(addCacheFrom) ? addCacheFrom as string[] : [addCacheFrom]) : [];
const additionalFeatures = additionalFeaturesJson ? jsonc.parse(additionalFeaturesJson) as Record<string, string | boolean | Record<string, string | boolean>> : {};
const params = await createDockerParams({
dockerPath,
dockerComposePath,
containerDataFolder: undefined,
containerSystemDataFolder: undefined,
workspaceFolder,
mountWorkspaceGitRoot: false,
configFile,
overrideConfigFile,
logLevel: mapLogLevel(logLevel),
logFormat,
log: text => process.stderr.write(text),
terminalDimensions: /* terminalColumns && terminalRows ? { columns: terminalColumns, rows: terminalRows } : */ undefined, // TODO
defaultUserEnvProbe: 'loginInteractiveShell',
removeExistingContainer: false,
buildNoCache,
expectExistingContainer: false,
postCreateEnabled: false,
skipNonBlocking: false,
prebuild: false,
persistedFolder,
additionalMounts: [],
updateRemoteUserUIDDefault: 'never',
remoteEnv: {},
additionalCacheFroms: addCacheFroms,
useBuildKit: buildkit,
buildxPlatform,
buildxPush,
additionalLabels: [],
buildxOutput,
buildxCacheTo,
skipFeatureAutoMapping,
skipPostAttach: true,
skipPersistingCustomizationsFromFeatures: skipPersistingCustomizationsFromFeatures,
dotfiles: {},
experimentalLockfile,
experimentalFrozenLockfile,
omitSyntaxDirective,
}, disposables);
const { common, dockerComposeCLI } = params;
const { cliHost, env, output } = common;
const workspace = workspaceFromPath(cliHost.path, workspaceFolder);
const configPath = configFile ? configFile : workspace
? (await getDevContainerConfigPathIn(cliHost, workspace.configFolderPath)
|| (overrideConfigFile ? getDefaultDevContainerConfigPath(cliHost, workspace.configFolderPath) : undefined))
: overrideConfigFile;
const configs = configPath && await readDevContainerConfigFile(cliHost, workspace, configPath, params.mountWorkspaceGitRoot, output, undefined, overrideConfigFile) || undefined;
if (!configs) {
throw new ContainerError({ description: `Dev container config (${uriToFsPath(configFile || getDefaultDevContainerConfigPath(cliHost, workspace!.configFolderPath), cliHost.platform)}) not found.` });
}
const configWithRaw = configs.config;
const { config } = configWithRaw;
let imageNameResult: string[] = [''];
if (buildxOutput && buildxPush) {
throw new ContainerError({ description: '--push true cannot be used with --output.' });
}
const buildParams: DockerCLIParameters = { cliHost, dockerCLI: params.dockerCLI, dockerComposeCLI, env, output, platformInfo: params.platformInfo };
await ensureNoDisallowedFeatures(buildParams, config, additionalFeatures, undefined);
// Support multiple use of `--image-name`
const imageNames = (argImageName && (Array.isArray(argImageName) ? argImageName : [argImageName]) as string[]) || undefined;
// Support multiple use of `--label`
params.additionalLabels = (buildxLabel && (Array.isArray(buildxLabel) ? buildxLabel : [buildxLabel]) as string[]) || [];
if (isDockerFileConfig(config)) {
// Build the base image and extend with features etc.
let { updatedImageName } = await buildNamedImageAndExtend(params, configWithRaw as SubstitutedConfig<DevContainerFromDockerfileConfig>, additionalFeatures, false, imageNames);
if (imageNames) {
imageNameResult = imageNames;
} else {
imageNameResult = updatedImageName;
}
} else if ('dockerComposeFile' in config) {
if (buildxPlatform || buildxPush) {
throw new ContainerError({ description: '--platform or --push not supported.' });
}
if (buildxOutput) {
throw new ContainerError({ description: '--output not supported.' });
}
if (buildxCacheTo) {
throw new ContainerError({ description: '--cache-to not supported.' });
}
const cwdEnvFile = cliHost.path.join(cliHost.cwd, '.env');
const envFile = Array.isArray(config.dockerComposeFile) && config.dockerComposeFile.length === 0 && await cliHost.isFile(cwdEnvFile) ? cwdEnvFile : undefined;
const composeFiles = await getDockerComposeFilePaths(cliHost, config, cliHost.env, workspaceFolder);
// If dockerComposeFile is an array, add -f <file> in order. https://docs.docker.com/compose/extends/#multiple-compose-files
const composeGlobalArgs = ([] as string[]).concat(...composeFiles.map(composeFile => ['-f', composeFile]));
if (envFile) {
composeGlobalArgs.push('--env-file', envFile);
}
const composeConfig = await readDockerComposeConfig(buildParams, composeFiles, envFile);
const projectName = await getProjectName(params, workspace, composeFiles, composeConfig);
const services = Object.keys(composeConfig.services || {});
if (services.indexOf(config.service) === -1) {
throw new Error(`Service '${config.service}' configured in devcontainer.json not found in Docker Compose configuration.`);
}
const versionPrefix = await readVersionPrefix(cliHost, composeFiles);
const infoParams = { ...params, common: { ...params.common, output: makeLog(buildParams.output, LogLevel.Info) } };
const { overrideImageName } = await buildAndExtendDockerCompose(configWithRaw as SubstitutedConfig<DevContainerFromDockerComposeConfig>, projectName, infoParams, composeFiles, envFile, composeGlobalArgs, [config.service], params.buildNoCache || false, params.common.persistedFolder, 'docker-compose.devcontainer.build', versionPrefix, additionalFeatures, false, addCacheFroms);
const service = composeConfig.services[config.service];
const originalImageName = overrideImageName || service.image || getDefaultImageName(await buildParams.dockerComposeCLI(), projectName, config.service);
if (imageNames) {
// Future improvement: Compose 2.6.0 (released 2022-05-30) added `tags` to the compose file.
if (params.isTTY) {
await Promise.all(imageNames.map(imageName => dockerPtyCLI(params, 'tag', originalImageName, imageName)));
} else {
await Promise.all(imageNames.map(imageName => dockerCLI(params, 'tag', originalImageName, imageName)));
}
imageNameResult = imageNames;
} else {
imageNameResult = originalImageName;
}
} else {
if (!config.image) {
throw new ContainerError({ description: 'No image information specified in devcontainer.json.' });
}
await inspectDockerImage(params, config.image, true);
const { updatedImageName } = await extendImage(params, configWithRaw, config.image, imageNames || [], additionalFeatures, false);
if (imageNames) {
imageNameResult = imageNames;
} else {
imageNameResult = updatedImageName;
}
}
return {
outcome: 'success' as 'success',
imageName: imageNameResult,
dispose,
};
} catch (originalError) {
const originalStack = originalError?.stack;
const err = originalError instanceof ContainerError ? originalError : new ContainerError({
description: 'An error occurred building the container.',
originalError
});
if (originalStack) {
console.error(originalStack);
}
return {
outcome: 'error' as 'error',
message: err.message,
description: err.description,
dispose,
};
}
}
function runUserCommandsOptions(y: Argv) {
return y.options({
'user-data-folder': { type: 'string', description: 'Host path to a directory that is intended to be persisted and share state between sessions.' },
'docker-path': { type: 'string', description: 'Docker CLI path.' },
'docker-compose-path': { type: 'string', description: 'Docker Compose CLI path.' },
'container-data-folder': { type: 'string', description: 'Container data folder where user data inside the container will be stored.' },
'container-system-data-folder': { type: 'string', description: 'Container system data folder where system data inside the container will be stored.' },
'workspace-folder': { type: 'string', description: 'Workspace folder path. The devcontainer.json will be looked up relative to this path.', default: '.', defaultDescription: 'Current Working Directory' },
'mount-workspace-git-root': { type: 'boolean', default: true, description: 'Mount the workspace using its Git root.' },
'container-id': { type: 'string', description: 'Id of the container to run the user commands for.' },
'id-label': { type: 'string', description: 'Id label(s) of the format name=value. If no --container-id is given the id labels will be used to look up the container. If no --id-label is given, one will be inferred from the --workspace-folder path.' },
'config': { type: 'string', description: 'devcontainer.json path. The default is to use .devcontainer/devcontainer.json or, if that does not exist, .devcontainer.json in the workspace folder.' },
'override-config': { type: 'string', description: 'devcontainer.json path to override any devcontainer.json in the workspace folder (or built-in configuration). This is required when there is no devcontainer.json otherwise.' },
'log-level': { choices: ['info' as 'info', 'debug' as 'debug', 'trace' as 'trace'], default: 'info' as 'info', description: 'Log level for the --terminal-log-file. When set to trace, the log level for --log-file will also be set to trace.' },
'log-format': { choices: ['text' as 'text', 'json' as 'json'], default: 'text' as 'text', description: 'Log format.' },
'terminal-columns': { type: 'number', implies: ['terminal-rows'], description: 'Number of columns to render the output for. This is required for some of the subprocesses to correctly render their output.' },
'terminal-rows': { type: 'number', implies: ['terminal-columns'], description: 'Number of rows to render the output for. This is required for some of the subprocesses to correctly render their output.' },
'default-user-env-probe': { choices: ['none' as 'none', 'loginInteractiveShell' as 'loginInteractiveShell', 'interactiveShell' as 'interactiveShell', 'loginShell' as 'loginShell'], default: defaultDefaultUserEnvProbe, description: 'Default value for the devcontainer.json\'s "userEnvProbe".' },
'skip-non-blocking-commands': { type: 'boolean', default: false, description: 'Stop running user commands after running the command configured with waitFor or the updateContentCommand by default.' },
prebuild: { type: 'boolean', default: false, description: 'Stop after onCreateCommand and updateContentCommand, rerunning updateContentCommand if it has run before.' },
'stop-for-personalization': { type: 'boolean', default: false, description: 'Stop for personalization.' },
'remote-env': { type: 'string', description: 'Remote environment variables of the format name=value. These will be added when executing the user commands.' },
'skip-feature-auto-mapping': { type: 'boolean', default: false, hidden: true, description: 'Temporary option for testing.' },
'skip-post-attach': { type: 'boolean', default: false, description: 'Do not run postAttachCommand.' },
'dotfiles-repository': { type: 'string', description: 'URL of a dotfiles Git repository (e.g., https://github.com/owner/repository.git)' },
'dotfiles-install-command': { type: 'string', description: 'The command to run after cloning the dotfiles repository. Defaults to run the first file of `install.sh`, `install`, `bootstrap.sh`, `bootstrap`, `setup.sh` and `setup` found in the dotfiles repository`s root folder.' },
'dotfiles-target-path': { type: 'string', default: '~/dotfiles', description: 'The path to clone the dotfiles repository to. Defaults to `~/dotfiles`.' },
'container-session-data-folder': { type: 'string', description: 'Folder to cache CLI data, for example userEnvProbe results' },
'secrets-file': { type: 'string', description: 'Path to a json file containing secret environment variables as key-value pairs.' },
})
.check(argv => {
const idLabels = (argv['id-label'] && (Array.isArray(argv['id-label']) ? argv['id-label'] : [argv['id-label']])) as string[] | undefined;
if (idLabels?.some(idLabel => !/.+=.+/.test(idLabel))) {
throw new Error('Unmatched argument format: id-label must match <name>=<value>');
}
const remoteEnvs = (argv['remote-env'] && (Array.isArray(argv['remote-env']) ? argv['remote-env'] : [argv['remote-env']])) as string[] | undefined;
if (remoteEnvs?.some(remoteEnv => !/.+=.*/.test(remoteEnv))) {
throw new Error('Unmatched argument format: remote-env must match <name>=<value>');
}
return true;
});
}
type RunUserCommandsArgs = UnpackArgv<ReturnType<typeof runUserCommandsOptions>>;
function runUserCommandsHandler(args: RunUserCommandsArgs) {
runAsyncHandler(runUserCommands.bind(null, args));
}
async function runUserCommands(args: RunUserCommandsArgs) {
const result = await doRunUserCommands(args);
const exitCode = result.outcome === 'error' ? 1 : 0;
await new Promise<void>((resolve, reject) => {
process.stdout.write(JSON.stringify(result) + '\n', err => err ? reject(err) : resolve());
});
await result.dispose();
process.exit(exitCode);
}
async function doRunUserCommands({
'user-data-folder': persistedFolder,
'docker-path': dockerPath,
'docker-compose-path': dockerComposePath,
'container-data-folder': containerDataFolder,
'container-system-data-folder': containerSystemDataFolder,
'workspace-folder': workspaceFolderArg,
'mount-workspace-git-root': mountWorkspaceGitRoot,
'container-id': containerId,
'id-label': idLabel,
config: configParam,
'override-config': overrideConfig,
'log-level': logLevel,
'log-format': logFormat,
'terminal-rows': terminalRows,
'terminal-columns': terminalColumns,
'default-user-env-probe': defaultUserEnvProbe,
'skip-non-blocking-commands': skipNonBlocking,
prebuild,
'stop-for-personalization': stopForPersonalization,
'remote-env': addRemoteEnv,
'skip-feature-auto-mapping': skipFeatureAutoMapping,
'skip-post-attach': skipPostAttach,
'dotfiles-repository': dotfilesRepository,
'dotfiles-install-command': dotfilesInstallCommand,
'dotfiles-target-path': dotfilesTargetPath,
'container-session-data-folder': containerSessionDataFolder,
'secrets-file': secretsFile,
}: RunUserCommandsArgs) {
const disposables: (() => Promise<unknown> | undefined)[] = [];
const dispose = async () => {
await Promise.all(disposables.map(d => d()));
};
try {
const workspaceFolder = path.resolve(process.cwd(), workspaceFolderArg);
const providedIdLabels = idLabel ? Array.isArray(idLabel) ? idLabel as string[] : [idLabel] : undefined;
const addRemoteEnvs = addRemoteEnv ? (Array.isArray(addRemoteEnv) ? addRemoteEnv as string[] : [addRemoteEnv]) : [];
const configFile = configParam ? URI.file(path.resolve(process.cwd(), configParam)) : undefined;
const overrideConfigFile = overrideConfig ? URI.file(path.resolve(process.cwd(), overrideConfig)) : undefined;
const cwd = workspaceFolder || process.cwd();
const cliHost = await getCLIHost(cwd, loadNativeModule, logFormat === 'text');
const secretsP = readSecretsFromFile({ secretsFile, cliHost });
const params = await createDockerParams({
dockerPath,
dockerComposePath,
containerDataFolder,
containerSystemDataFolder,
workspaceFolder,
mountWorkspaceGitRoot,
configFile,
overrideConfigFile,
logLevel: mapLogLevel(logLevel),
logFormat,
log: text => process.stderr.write(text),
terminalDimensions: terminalColumns && terminalRows ? { columns: terminalColumns, rows: terminalRows } : undefined,
defaultUserEnvProbe,
removeExistingContainer: false,
buildNoCache: false,
expectExistingContainer: false,
postCreateEnabled: true,
skipNonBlocking,
prebuild,
persistedFolder,
additionalMounts: [],
updateRemoteUserUIDDefault: 'never',
remoteEnv: envListToObj(addRemoteEnvs),
additionalCacheFroms: [],
useBuildKit: 'auto',
buildxPlatform: undefined,
buildxPush: false,
additionalLabels: [],
buildxOutput: undefined,
buildxCacheTo: undefined,
skipFeatureAutoMapping,
skipPostAttach,
skipPersistingCustomizationsFromFeatures: false,
dotfiles: {
repository: dotfilesRepository,
installCommand: dotfilesInstallCommand,
targetPath: dotfilesTargetPath,
},
containerSessionDataFolder,
secretsP,
}, disposables);
const { common } = params;
const { output } = common;
const workspace = workspaceFolder ? workspaceFromPath(cliHost.path, workspaceFolder) : undefined;
const configPath = configFile ? configFile : workspace
? (await getDevContainerConfigPathIn(cliHost, workspace.configFolderPath)
|| (overrideConfigFile ? getDefaultDevContainerConfigPath(cliHost, workspace.configFolderPath) : undefined))
: overrideConfigFile;
const configs = configPath && await readDevContainerConfigFile(cliHost, workspace, configPath, params.mountWorkspaceGitRoot, output, undefined, overrideConfigFile) || undefined;
if ((configFile || workspaceFolder || overrideConfigFile) && !configs) {
throw new ContainerError({ description: `Dev container config (${uriToFsPath(configFile || getDefaultDevContainerConfigPath(cliHost, workspace!.configFolderPath), cliHost.platform)}) not found.` });
}
const config0 = configs?.config || {
raw: {},
config: {},
substitute: value => substitute({ platform: cliHost.platform, env: cliHost.env }, value)
};
const { container, idLabels } = await findContainerAndIdLabels(params, containerId, providedIdLabels, workspaceFolder, configPath?.fsPath);
if (!container) {
bailOut(common.output, 'Dev container not found.');
}
const config1 = addSubstitution(config0, config => beforeContainerSubstitute(envListToObj(idLabels), config));
const config = addSubstitution(config1, config => containerSubstitute(cliHost.platform, config1.config.configFilePath, envListToObj(container.Config.Env), config));
const imageMetadata = getImageMetadataFromContainer(container, config, undefined, idLabels, output).config;
const mergedConfig = mergeConfiguration(config.config, imageMetadata);
const containerProperties = await createContainerProperties(params, container.Id, configs?.workspaceConfig.workspaceFolder, mergedConfig.remoteUser);
const updatedConfig = containerSubstitute(cliHost.platform, config.config.configFilePath, containerProperties.env, mergedConfig);
const remoteEnvP = probeRemoteEnv(common, containerProperties, updatedConfig);
const result = await runLifecycleHooks(common, lifecycleCommandOriginMapFromMetadata(imageMetadata), containerProperties, updatedConfig, remoteEnvP, secretsP, stopForPersonalization);
return {
outcome: 'success' as 'success',
result,
dispose,
};
} catch (originalError) {
const originalStack = originalError?.stack;
const err = originalError instanceof ContainerError ? originalError : new ContainerError({
description: 'An error occurred running user commands in the container.',
originalError
});
if (originalStack) {
console.error(originalStack);
}
return {
outcome: 'error' as 'error',
message: err.message,
description: err.description,
dispose,
};
}
}
function readConfigurationOptions(y: Argv) {
return y.options({
'user-data-folder': { type: 'string', description: 'Host path to a directory that is intended to be persisted and share state between sessions.' },
'docker-path': { type: 'string', description: 'Docker CLI path.' },
'docker-compose-path': { type: 'string', description: 'Docker Compose CLI path.' },
'workspace-folder': { type: 'string', description: 'Workspace folder path. The devcontainer.json will be looked up relative to this path.', default: '.', defaultDescription: 'Current Working Directory' },
'mount-workspace-git-root': { type: 'boolean', default: true, description: 'Mount the workspace using its Git root.' },
'container-id': { type: 'string', description: 'Id of the container to run the user commands for.' },
'id-label': { type: 'string', description: 'Id label(s) of the format name=value. If no --container-id is given the id labels will be used to look up the container. If no --id-label is given, one will be inferred from the --workspace-folder path.' },
'config': { type: 'string', description: 'devcontainer.json path. The default is to use .devcontainer/devcontainer.json or, if that does not exist, .devcontainer.json in the workspace folder.' },
'override-config': { type: 'string', description: 'devcontainer.json path to override any devcontainer.json in the workspace folder (or built-in configuration). This is required when there is no devcontainer.json otherwise.' },
'log-level': { choices: ['info' as 'info', 'debug' as 'debug', 'trace' as 'trace'], default: 'info' as 'info', description: 'Log level for the --terminal-log-file. When set to trace, the log level for --log-file will also be set to trace.' },
'log-format': { choices: ['text' as 'text', 'json' as 'json'], default: 'text' as 'text', description: 'Log format.' },
'terminal-columns': { type: 'number', implies: ['terminal-rows'], description: 'Number of columns to render the output for. This is required for some of the subprocesses to correctly render their output.' },
'terminal-rows': { type: 'number', implies: ['terminal-columns'], description: 'Number of rows to render the output for. This is required for some of the subprocesses to correctly render their output.' },
'include-features-configuration': { type: 'boolean', default: false, description: 'Include features configuration.' },
'include-merged-configuration': { type: 'boolean', default: false, description: 'Include merged configuration.' },
'additional-features': { type: 'string', description: 'Additional features to apply to the dev container (JSON as per "features" section in devcontainer.json)' },
'skip-feature-auto-mapping': { type: 'boolean', default: false, hidden: true, description: 'Temporary option for testing.' },
})
.check(argv => {
const idLabels = (argv['id-label'] && (Array.isArray(argv['id-label']) ? argv['id-label'] : [argv['id-label']])) as string[] | undefined;
if (idLabels?.some(idLabel => !/.+=.+/.test(idLabel))) {
throw new Error('Unmatched argument format: id-label must match <name>=<value>');
}
return true;
});
}
type ReadConfigurationArgs = UnpackArgv<ReturnType<typeof readConfigurationOptions>>;
function readConfigurationHandler(args: ReadConfigurationArgs) {
runAsyncHandler(readConfiguration.bind(null, args));
}
async function readConfiguration({
// 'user-data-folder': persistedFolder,
'docker-path': dockerPath,
'docker-compose-path': dockerComposePath,
'workspace-folder': workspaceFolderArg,
'mount-workspace-git-root': mountWorkspaceGitRoot,
config: configParam,
'override-config': overrideConfig,
'container-id': containerId,
'id-label': idLabel,
'log-level': logLevel,
'log-format': logFormat,
'terminal-rows': terminalRows,
'terminal-columns': terminalColumns,
'include-features-configuration': includeFeaturesConfig,
'include-merged-configuration': includeMergedConfig,
'additional-features': additionalFeaturesJson,
'skip-feature-auto-mapping': skipFeatureAutoMapping,
}: ReadConfigurationArgs) {
const disposables: (() => Promise<unknown> | undefined)[] = [];
const dispose = async () => {
await Promise.all(disposables.map(d => d()));
};