-
Notifications
You must be signed in to change notification settings - Fork 268
/
extension.ts
1241 lines (1094 loc) · 42.6 KB
/
extension.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) 2022 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/
import * as extensionApi from '@podman-desktop/api';
import * as path from 'node:path';
import * as os from 'node:os';
import * as http from 'node:http';
import * as fs from 'node:fs';
import { spawn } from 'node:child_process';
import { RegistrySetup } from './registry-setup';
import { getAssetsFolder, isLinux, isMac, isWindows, appHomeDir, LoggerDelegator } from './util';
import { PodmanInstall } from './podman-install';
import type { InstalledPodman } from './podman-cli';
import { getPodmanCli, getPodmanInstallation } from './podman-cli';
import { PodmanConfiguration } from './podman-configuration';
import { getDetectionChecks } from './detection-checks';
import { getDisguisedPodmanInformation, getSocketPath, isDisguisedPodman } from './warnings';
import { getSocketCompatibility } from './compatibility-mode';
import type { RunError } from '@podman-desktop/api';
import { compareVersions } from 'compare-versions';
type StatusHandler = (name: string, event: extensionApi.ProviderConnectionStatus) => void;
const listeners = new Set<StatusHandler>();
const podmanMachineSocketsDirectory = path.resolve(os.homedir(), appHomeDir(), 'machine');
const podmanMachineSocketsSymlinkDirectoryMac = path.resolve(os.homedir(), '.podman');
const MACOS_MAX_SOCKET_PATH_LENGTH = 104;
let storedExtensionContext;
let stopLoop = false;
let autoMachineStarted = false;
let autoMachineName;
// System default notifier
let defaultMachineNotify = true;
let defaultMachineMonitor = true;
// current status of machines
const podmanMachinesStatuses = new Map<string, extensionApi.ProviderConnectionStatus>();
let podmanProviderStatus: extensionApi.ProviderConnectionStatus = 'started';
const podmanMachinesInfo = new Map<string, MachineInfo>();
const currentConnections = new Map<string, extensionApi.Disposable>();
const containerProviderConnections = new Map<string, extensionApi.ContainerProviderConnection>();
// Warning to check to see if the socket is a disguised Podman socket,
// by default we assume it is until proven otherwise when we check
let isDisguisedPodmanSocket = true;
let disguisedPodmanSocketWatcher: extensionApi.FileSystemWatcher;
export type MachineJSON = {
Name: string;
CPUs: number;
Memory: string;
DiskSize: string;
Running: boolean;
Starting: boolean;
Default: boolean;
UserModeNetworking?: boolean;
};
export type ConnectionJSON = {
Name: string;
URI: string;
Identity: string;
IsMachine: boolean;
Default: boolean;
};
export type MachineInfo = {
name: string;
cpus: number;
memory: number;
diskSize: number;
userModeNetworking: boolean;
};
async function updateMachines(provider: extensionApi.Provider): Promise<void> {
// init machines available
const machineListOutput = await getJSONMachineList();
// parse output
const machines = JSON.parse(machineListOutput) as MachineJSON[];
extensionApi.context.setValue('podmanMachineExists', machines.length > 0, 'onboarding');
// update status of existing machines
for (const machine of machines) {
const running = machine?.Running === true;
let status: extensionApi.ProviderConnectionStatus = running ? 'started' : 'stopped';
// update the status to starting if the machine is starting but not yet running
const starting = machine?.Starting === true;
if (!running && starting) {
status = 'starting';
}
const previousStatus = podmanMachinesStatuses.get(machine.Name);
if (previousStatus !== status) {
// notify status change
listeners.forEach(listener => listener(machine.Name, status));
podmanMachinesStatuses.set(machine.Name, status);
}
const userModeNetworking = isWindows() ? machine.UserModeNetworking : true;
podmanMachinesInfo.set(machine.Name, {
name: machine.Name,
memory: parseInt(machine.Memory),
cpus: machine.CPUs,
diskSize: parseInt(machine.DiskSize),
userModeNetworking: userModeNetworking,
});
if (!podmanMachinesStatuses.has(machine.Name)) {
podmanMachinesStatuses.set(machine.Name, status);
}
if (containerProviderConnections.has(machine.Name)) {
const containerProviderConnection = containerProviderConnections.get(machine.Name);
await updateContainerConfiguration(containerProviderConnection, podmanMachinesInfo.get(machine.Name));
}
}
// remove machine no longer there
const machinesToRemove = Array.from(podmanMachinesStatuses.keys()).filter(
machine => !machines.find(m => m.Name === machine),
);
machinesToRemove.forEach(machine => {
podmanMachinesStatuses.delete(machine);
podmanMachinesInfo.delete(machine);
containerProviderConnections.delete(machine);
});
// create connections for new machines
const connectionsToCreate = Array.from(podmanMachinesStatuses.keys()).filter(
machineStatusKey => !currentConnections.has(machineStatusKey),
);
await Promise.all(
connectionsToCreate.map(async machineName => {
// podman.sock link
let socketPath;
try {
if (isWindows()) {
const { stdout: socket } = await extensionApi.process.exec(getPodmanCli(), [
'machine',
'inspect',
'--format',
'{{.ConnectionInfo.PodmanPipe.Path}}',
machineName,
]);
socketPath = socket;
} else {
const { stdout: socket } = await extensionApi.process.exec(getPodmanCli(), [
'machine',
'inspect',
'--format',
'{{.ConnectionInfo.PodmanSocket.Path}}',
machineName,
]);
socketPath = socket;
}
} catch (error) {
console.debug('Podman extension:', 'Failed to read socketPath from machine inspect');
}
if (!socketPath) {
if (isMac()) {
socketPath = calcMacosSocketPath(machineName);
} else if (isLinux()) {
socketPath = calcLinuxSocketPath(machineName);
} else if (isWindows()) {
socketPath = calcWinPipeName(machineName);
}
}
await registerProviderFor(provider, podmanMachinesInfo.get(machineName), socketPath);
}),
);
// delete connections for machines no longer there
machinesToRemove.forEach(machine => {
const disposable = currentConnections.get(machine);
if (disposable) {
disposable.dispose();
currentConnections.delete(machine);
}
});
// no machine, it's installed
if (machines.length === 0) {
if (provider.status !== 'configuring') {
provider.updateStatus('installed');
}
} else {
const atLeastOneMachineRunning = machines.some(machine => machine.Running);
const atLeastOneMachineStarting = machines.some(machine => machine.Starting);
// if a machine is running it's started else it is ready
if (atLeastOneMachineRunning) {
provider.updateStatus('ready');
} else if (atLeastOneMachineStarting) {
// update to starting
provider.updateStatus('starting');
} else {
// needs to start a machine
provider.updateStatus('configured');
}
}
// Finally, we check to see if the machine that is running is set by default or not on the CLI
// this will create a dialog that will ask the user if they wish to set the running machine as default.
await checkDefaultMachine(machines);
}
export async function checkDefaultMachine(machines: MachineJSON[]): Promise<void> {
// As a last check, let's see if the machine that is running is set by default or not on the CLI.
// if it isn't, we should prompt the user to set it as default, or else podman CLI commands will not work
const ROOTFUL_SUFFIX = '-root';
const runningMachine = machines.find(machine => machine.Running);
let defaultMachine = machines.find(machine => machine.Default);
// It may happen that the default machine has not been found because the rootful connection is set as default
// if so, we try to find the default system connection and use it to identify the default machine
if (!defaultMachine) {
const defaultConnection = await getDefaultConnection();
let defaultConnectionName = defaultConnection?.Name;
if (defaultConnectionName.endsWith(ROOTFUL_SUFFIX)) {
defaultConnectionName = defaultConnectionName.substring(0, defaultConnectionName.length - 5);
}
defaultMachine = machines.find(machine => machine.Name === defaultConnectionName);
if (runningMachine?.Name === defaultConnectionName) {
runningMachine.Default = true;
}
}
if (defaultMachineNotify && defaultMachineMonitor && runningMachine && !runningMachine.Default) {
// Make sure we do notifyDefault = false so we don't keep notifying the user when this dialog is open.
defaultMachineMonitor = false;
const defaultMachineText = defaultMachine ? `(default is '${defaultMachine.Name}')` : '';
// Create an information message to ask the user if they wish to set the running machine as default.
const result = await extensionApi.window.showInformationMessage(
`Podman Machine '${runningMachine.Name}' is running but not the default machine ${defaultMachineText}. This will cause podman CLI errors while trying to connect to '${runningMachine.Name}'. Do you want to set it as default?`,
'Yes',
'Ignore',
'Cancel',
);
if (result === 'Yes') {
try {
// make it the default to run the info command
await extensionApi.process.exec(getPodmanCli(), ['system', 'connection', 'default', runningMachine.Name]);
} catch (error) {
// eslint-disable-next-line quotes
console.error("Error running 'podman system connection default': ", error);
await extensionApi.window.showErrorMessage(`Error running 'podman system connection default': ${error}`);
return;
}
try {
// after updating the default connection using the rootless connection, we verify if the machine has been
// created as rootful. If so, the default connection must be set to the rootful connection
const { stdout: machineInfoJson } = await extensionApi.process.exec(getPodmanCli(), [
'machine',
'info',
'--format',
'json',
]);
const machineInfo = JSON.parse(machineInfoJson);
const filepath = path.join(machineInfo.Host.MachineConfigDir, `${runningMachine.Name}.json`);
if (fs.existsSync(filepath)) {
const machineConfigJson = await fs.promises.readFile(filepath, 'utf8');
if (machineConfigJson && machineConfigJson.length > 0) {
const machineConfig = JSON.parse(machineConfigJson);
// if it's rootful let's update the connection to the rootful one
if (machineConfig.Rootful) {
await extensionApi.process.exec(getPodmanCli(), [
'system',
'connection',
'default',
`${runningMachine.Name}${ROOTFUL_SUFFIX}`,
]);
}
}
}
} catch (error) {
console.error('Error when checking rootful machine: ', error);
}
await extensionApi.window.showInformationMessage(
`Podman Machine '${runningMachine.Name}' is now the default machine on the CLI.`,
'OK',
);
} else if (result === 'Ignore') {
// If the user chooses to ignore, we should not notify them again until Podman Desktop is restarted.
defaultMachineNotify = false;
}
defaultMachineMonitor = true;
}
}
async function getDefaultConnection(): Promise<ConnectionJSON | undefined> {
// init machines available
const { stdout: connectionListOutput } = await extensionApi.process.exec(getPodmanCli(), [
'system',
'connection',
'list',
'--format',
'json',
]);
// parse output
const connections = JSON.parse(connectionListOutput) as ConnectionJSON[];
return connections.find(connection => connection.Default);
}
async function updateContainerConfiguration(
containerProviderConnection: extensionApi.ContainerProviderConnection,
machineInfo: MachineInfo,
): Promise<void> {
// get configuration for this connection
const containerConfiguration = extensionApi.configuration.getConfiguration('podman', containerProviderConnection);
// Set values for the machine
await containerConfiguration.update('machine.cpus', machineInfo.cpus);
await containerConfiguration.update('machine.memory', machineInfo.memory);
await containerConfiguration.update('machine.diskSize', machineInfo.diskSize);
}
function calcMacosSocketPath(machineName: string): string {
// max length for the path of a socket in macos is 104 chars
let socketPath = path.resolve(podmanMachineSocketsDirectory, machineName, 'podman.sock');
if (socketPath.length > MACOS_MAX_SOCKET_PATH_LENGTH) {
socketPath = path.resolve(podmanMachineSocketsSymlinkDirectoryMac, machineName, 'podman.sock');
}
return socketPath;
}
function calcLinuxSocketPath(machineName: string): string {
return path.resolve(podmanMachineSocketsDirectory, machineName, 'podman.sock');
}
function calcWinPipeName(machineName: string): string {
const name = machineName.startsWith('podman') ? machineName : 'podman-' + machineName;
return `//./pipe/${name}`;
}
function getLinuxSocketPath(): string {
// grab user id of the user
const userInfo = os.userInfo();
const uid = userInfo.uid;
return `/run/user/${uid}/podman/podman.sock`;
}
// on linux, socket is started by the system service on a path like /run/user/1000/podman/podman.sock
async function initDefaultLinux(provider: extensionApi.Provider) {
const socketPath = getLinuxSocketPath();
if (!fs.existsSync(socketPath)) {
return;
}
const containerProviderConnection: extensionApi.ContainerProviderConnection = {
name: 'Podman',
type: 'podman',
status: () => podmanProviderStatus,
endpoint: {
socketPath,
},
};
monitorPodmanSocket(socketPath).catch((error: unknown) => {
console.error('Error monitoring podman socket', error);
});
const disposable = provider.registerContainerProviderConnection(containerProviderConnection);
currentConnections.set('podman', disposable);
storedExtensionContext.subscriptions.push(disposable);
}
async function isPodmanSocketAlive(socketPath: string): Promise<boolean> {
const pingUrl = {
path: '/_ping',
socketPath,
};
return new Promise<boolean>(resolve => {
const req = http.get(pingUrl, res => {
res.on('data', () => {
// do nothing
});
res.on('end', () => {
if (res.statusCode === 200) {
resolve(true);
} else {
resolve(false);
}
});
});
req.once('error', () => {
resolve(false);
});
});
}
async function monitorPodmanSocket(socketPath: string, machineName?: string) {
// call us again
if (!stopMonitoringPodmanSocket(machineName)) {
try {
const alive = await isPodmanSocketAlive(socketPath);
if (!alive) {
updateProviderStatus('stopped', machineName);
} else {
updateProviderStatus('started', machineName);
}
} catch (error) {
// ignore the update of machines
}
await timeout(5000);
monitorPodmanSocket(socketPath, machineName).catch((error: unknown) => {
console.error('Error monitoring podman socket', error);
});
}
}
function stopMonitoringPodmanSocket(machineName?: string) {
if (machineName) {
return stopLoop || !podmanMachinesStatuses.has(machineName);
}
return stopLoop;
}
function updateProviderStatus(status: extensionApi.ProviderConnectionStatus, machineName?: string) {
if (machineName) {
podmanMachinesStatuses.set(machineName, status);
} else {
podmanProviderStatus = status;
}
}
async function timeout(time: number): Promise<void> {
return new Promise<void>(resolve => {
setTimeout(resolve, time);
});
}
async function monitorMachines(provider: extensionApi.Provider) {
// call us again
if (!stopLoop) {
try {
await updateMachines(provider);
} catch (error) {
// ignore the update of machines
}
await timeout(5000);
monitorMachines(provider).catch((error: unknown) => {
console.error('Error monitoring podman machines', error);
});
}
}
async function monitorProvider(provider: extensionApi.Provider) {
// call us again
if (!stopLoop) {
try {
const installedPodman = await getPodmanInstallation();
provider.updateDetectionChecks(getDetectionChecks(installedPodman));
// update version
if (!installedPodman) {
provider.updateStatus('not-installed');
} else if (installedPodman.version) {
provider.updateVersion(installedPodman.version);
// update provider status if someone has installed podman externally
if (provider.status === 'not-installed') {
provider.updateStatus('installed');
}
}
} catch (error) {
// ignore the update
}
}
await timeout(8000);
monitorProvider(provider).catch((error: unknown) => {
console.error('Error monitoring podman provider', error);
});
}
function prettyMachineName(machineName: string): string {
let name;
if (machineName === 'podman-machine-default') {
name = 'Podman Machine';
} else if (machineName.startsWith('podman-machine-')) {
const sub = machineName.substring('podman-machine-'.length);
name = `Podman Machine ${sub}`;
} else {
name = `Podman Machine ${machineName}`;
}
return name;
}
async function registerProviderFor(provider: extensionApi.Provider, machineInfo: MachineInfo, socketPath: string) {
const lifecycle: extensionApi.ProviderConnectionLifecycle = {
start: async (context, logger): Promise<void> => {
await startMachine(provider, machineInfo, context, logger);
},
stop: async (context, logger): Promise<void> => {
await extensionApi.process.exec(getPodmanCli(), ['machine', 'stop', machineInfo.name], {
logger: new LoggerDelegator(context, logger),
});
provider.updateStatus('ready');
},
delete: async (logger): Promise<void> => {
await extensionApi.process.exec(getPodmanCli(), ['machine', 'rm', '-f', machineInfo.name], { logger });
},
};
const containerProviderConnection: extensionApi.ContainerProviderConnection = {
name: prettyMachineName(machineInfo.name),
type: 'podman',
status: () => podmanMachinesStatuses.get(machineInfo.name),
lifecycle,
endpoint: {
socketPath,
},
};
// Since Podman 4.5, machines are using the same path for all sockets of machines
// so a machine is not distinguishable from another one.
// monitorPodmanSocket(socketPath, machineInfo.name);
containerProviderConnections.set(machineInfo.name, containerProviderConnection);
const disposable = provider.registerContainerProviderConnection(containerProviderConnection);
provider.updateStatus('ready');
// get configuration for this connection
const containerConfiguration = extensionApi.configuration.getConfiguration('podman', containerProviderConnection);
// Set values for the machine
await containerConfiguration.update('machine.cpus', machineInfo.cpus);
await containerConfiguration.update('machine.memory', machineInfo.memory);
await containerConfiguration.update('machine.diskSize', machineInfo.diskSize);
currentConnections.set(machineInfo.name, disposable);
storedExtensionContext.subscriptions.push(disposable);
}
export async function startMachine(
provider: extensionApi.Provider,
machineInfo: MachineInfo,
context?: extensionApi.LifecycleContext,
logger?: extensionApi.Logger,
skipHandleError?: boolean,
): Promise<void> {
try {
// start the machine
await extensionApi.process.exec(getPodmanCli(), ['machine', 'start', machineInfo.name], {
logger: new LoggerDelegator(context, logger),
});
provider.updateStatus('started');
} catch (err) {
if (skipHandleError) {
console.error(err);
// propagate the error
throw err;
}
await doHandleError(provider, machineInfo, err);
}
}
async function doHandleError(
provider: extensionApi.Provider,
machineInfo: MachineInfo,
error: string | RunError,
): Promise<void> {
let errText: string;
if (typeof error === 'object' && 'message' in error) {
errText = error.message.toString();
} else if (typeof error === 'string') {
errText = error;
}
if (errText.toLowerCase().includes('wsl bootstrap script failed: exit status 0xffffffff')) {
const handled = await doHandleWSLDistroNotFoundError(provider, machineInfo);
if (handled) {
return;
}
}
console.error(error);
// propagate the error
throw error;
}
async function doHandleWSLDistroNotFoundError(
provider: extensionApi.Provider,
machineInfo: MachineInfo,
): Promise<boolean> {
const result = await extensionApi.window.showInformationMessage(
`Error while starting Podman Machine '${machineInfo.name}'. The WSL bootstrap script failed: exist status 0xffffffff. The machine is probably broken and should be deleted and reinitialized. Do you want to recreate it?`,
'Yes',
'Cancel',
);
if (result === 'Yes') {
return await extensionApi.window.withProgress(
{ location: extensionApi.ProgressLocation.TASK_WIDGET, title: `Initializing ${machineInfo.name}` },
async progress => {
progress.report({ increment: 5 });
try {
provider.updateStatus('configuring');
await extensionApi.process.exec(getPodmanCli(), ['machine', 'rm', '-f', machineInfo.name]);
progress.report({ increment: 40 });
await createMachine(
{
'podman.factory.machine.name': machineInfo.name,
'podman.factory.machine.cpus': machineInfo.cpus,
'podman.factory.machine.memory': machineInfo.memory,
'podman.factory.machine.diskSize': machineInfo.diskSize,
},
undefined,
undefined,
);
} catch (error) {
console.error(error);
} finally {
progress.report({ increment: -1 });
}
return true;
},
);
}
return false;
}
async function registerUpdatesIfAny(
provider: extensionApi.Provider,
installedPodman: InstalledPodman,
podmanInstall: PodmanInstall,
): Promise<void> {
const updateInfo = await podmanInstall.checkForUpdate(installedPodman);
if (updateInfo.hasUpdate) {
provider.registerUpdate({
version: updateInfo.bundledVersion,
update: () => podmanInstall.performUpdate(provider, installedPodman),
preflightChecks: () => podmanInstall.getUpdatePreflightChecks(),
});
}
}
export const ROOTFUL_MACHINE_INIT_SUPPORTED_KEY = 'podman.isRootfulMachineInitSupported';
export const USER_MODE_NETWORKING_SUPPORTED_KEY = 'podman.isUserModeNetworkingSupported';
export async function activate(extensionContext: extensionApi.ExtensionContext): Promise<void> {
storedExtensionContext = extensionContext;
const podmanInstall = new PodmanInstall(extensionContext.storagePath);
const installedPodman = await getPodmanInstallation();
const version: string | undefined = installedPodman?.version;
if (version) {
extensionApi.context.setValue(ROOTFUL_MACHINE_INIT_SUPPORTED_KEY, isRootfulMachineInitSupported(version));
extensionApi.context.setValue(USER_MODE_NETWORKING_SUPPORTED_KEY, isUserModeNetworkingSupported(version));
}
const detectionChecks: extensionApi.ProviderDetectionCheck[] = [];
let status: extensionApi.ProviderStatus = 'not-installed';
if (version) {
status = 'installed';
}
// update detection checks
detectionChecks.push(...getDetectionChecks(installedPodman));
const providerOptions: extensionApi.ProviderOptions = {
name: 'Podman',
id: 'podman',
detectionChecks,
status,
version,
};
// add images
providerOptions.images = {
icon: './icon.png',
logo: './logo.png',
};
// Empty connection descriptive message
providerOptions.emptyConnectionMarkdownDescription =
'Podman is a lightweight, open-source container runtime and image management tool that enables users to run and manage containers without the need for a separate daemon.\n\nMore information: [podman.io](https://podman.io/)';
const corePodmanEngineLinkGroup = 'Core Podman Engine';
// add links
providerOptions.links = [
{
title: 'Website',
url: 'https://podman.io/',
},
{
title: 'Installation guide',
url: 'https://podman.io/getting-started/installation',
},
{
title: 'Docker compatibility guide',
url: 'https://podman-desktop.io/docs/troubleshooting#warning-about-docker-compatibility-mode',
},
{
title: 'Join the community',
url: 'https://podman.io/community/',
},
{
title: 'Getting started with containers',
url: 'https://podman.io/getting-started/',
group: corePodmanEngineLinkGroup,
},
{
title: 'View podman commands',
url: 'https://docs.podman.io/en/latest/Commands.html',
group: corePodmanEngineLinkGroup,
},
{
title: 'Set up podman',
url: 'https://podman.io/getting-started/installation',
group: corePodmanEngineLinkGroup,
},
{
title: 'View all tutorials',
url: 'https://docs.podman.io/en/latest/Tutorials.html',
group: corePodmanEngineLinkGroup,
},
];
const provider = extensionApi.provider.createProvider(providerOptions);
// Check on initial setup
await checkDisguisedPodmanSocket(provider);
// Update the status of the provider if the socket is changed, created or deleted
disguisedPodmanSocketWatcher = setupDisguisedPodmanSocketWatcher(provider, getSocketPath());
extensionContext.subscriptions.push(disguisedPodmanSocketWatcher);
// Compatibility mode status bar item
// only available for macOS or Linux (for now).
if (isMac() || isLinux()) {
// Get the socketCompatibilityClass for the current OS.
const socketCompatibilityMode = getSocketCompatibility();
// Create a status bar item to show the status of compatibility mode as well as
// create a command so when you can disable / enable compatibility mode
const statusBarItem = extensionApi.window.createStatusBarItem();
statusBarItem.text = 'Docker Compatibility';
statusBarItem.command = 'podman.socketCompatibilityMode';
// Use tooltip text from class
statusBarItem.tooltip = socketCompatibilityMode.tooltipText();
statusBarItem.iconClass = 'fa fa-plug';
statusBarItem.show();
extensionContext.subscriptions.push(statusBarItem);
// Create a modal dialog to ask the user if they want to enable or disable compatibility mode
const command = extensionApi.commands.registerCommand('podman.socketCompatibilityMode', async () => {
// Manually check to see if the socket is disguised (this will be called when pressing the status bar item)
const isDisguisedPodmanSocket = await isDisguisedPodman();
// We use isEnabled() as we do not want to "renable" again if the user has already enabled it.
if (!isDisguisedPodmanSocket && !socketCompatibilityMode.isEnabled()) {
const result = await extensionApi.window.showInformationMessage(
`Do you want to enable Docker socket compatibility mode for Podman?\n\n${socketCompatibilityMode.details}`,
'Enable',
'Cancel',
);
if (result === 'Enable') {
await socketCompatibilityMode.enable();
}
} else {
const result = await extensionApi.window.showInformationMessage(
`Do you want to disable Docker socket compatibility mode for Podman?\n\n${socketCompatibilityMode.details}`,
'Disable',
'Cancel',
);
if (result === 'Disable') {
await socketCompatibilityMode.disable();
}
}
// Use tooltip text from class
statusBarItem.tooltip = socketCompatibilityMode.tooltipText();
});
// Push the results of the command so we can unload it later
extensionContext.subscriptions.push(command);
}
// provide an installation path ?
if (podmanInstall.isAbleToInstall()) {
provider.registerInstallation({
install: () => podmanInstall.doInstallPodman(provider),
preflightChecks: () => podmanInstall.getInstallChecks(),
});
}
// provide an installation path ?
// add update information asynchronously
await registerUpdatesIfAny(provider, installedPodman, podmanInstall);
// register autostart if enabled
if (isMac() || isWindows()) {
try {
await updateMachines(provider);
} catch (error) {
// ignore the update of machines
}
provider.registerAutostart({
start: async (logger: extensionApi.Logger) => {
// do we have a running machine ?
const isRunningMachine = Array.from(podmanMachinesStatuses.values()).find(
connectionStatus => connectionStatus === 'started' || connectionStatus === 'starting',
);
if (isRunningMachine) {
console.log('Podman extension:', 'Do not start a machine as there is already one starting or started');
return;
}
// start the first machine if any
const machines = Array.from(podmanMachinesStatuses.entries());
if (machines.length > 0) {
const [machineName] = machines[0];
if (!podmanMachinesInfo.has(machineName)) {
console.error('Unable to retrieve machine infos to be autostarted', machineName);
} else {
console.log('Podman extension:', 'Autostarting machine', machineName);
const machineInfo = podmanMachinesInfo.get(machineName);
const containerProviderConnection = containerProviderConnections.get(machineName);
const context: extensionApi.LifecycleContext = extensionApi.provider.getProviderLifecycleContext(
provider.id,
containerProviderConnection,
);
await startMachine(provider, machineInfo, context, logger);
autoMachineStarted = true;
autoMachineName = machineName;
}
}
},
});
}
extensionContext.subscriptions.push(provider);
// allows to create machines
if (isMac() || isWindows()) {
provider.setContainerProviderConnectionFactory({
initialize: () => createMachine({}, undefined),
create: createMachine,
creationDisplayName: 'Podman machine',
});
}
// no podman for now, skip
if (isMac()) {
if (!fs.existsSync(podmanMachineSocketsDirectory)) {
return;
}
monitorMachines(provider).catch((error: unknown) => {
console.error('Error while monitoring machines', error);
});
} else if (isLinux()) {
// on Linux, need to run the system service for unlimited time
let command = 'podman';
let args = ['system', 'service', '--time=0'];
const env = process.env;
if (env.FLATPAK_ID) {
// need to execute the command on the host
command = 'flatpak-spawn';
args = ['--host', 'podman', ...args];
}
const podmanProcess = spawn(command, args);
podmanProcess.on('error', err => {
console.error('Failed to spawn process.', err);
});
// check for up to 5s to see if the socket is being made available
const socketPath = getLinuxSocketPath();
let socketFound = false;
for (let i = 0; i < 50; i++) {
if (fs.existsSync(socketPath)) {
socketFound = true;
break;
}
await timeout(100);
}
if (!socketFound) {
console.error(
'Podman extension:',
`Could not find the socket at ${socketPath} after 5s. The command podman system service --time=0 did not work to start the podman socket.`,
);
}
provider.updateStatus('ready');
const disposable = extensionApi.Disposable.create(() => {
podmanProcess.kill();
});
extensionContext.subscriptions.push(disposable);
initDefaultLinux(provider).catch((error: unknown) => {
console.error('Error while initializing default linux', error);
});
} else if (isWindows()) {
monitorMachines(provider).catch((error: unknown) => {
console.error('Error while monitoring machines', error);
});
}
// monitor provider
// like version, checks, warnings
monitorProvider(provider).catch((error: unknown) => {
console.error('Error while monitoring provider', error);
});
const onboardingCheckInstallationCommand = extensionApi.commands.registerCommand(
'podman.onboarding.checkPodmanInstalled',
async () => {
const installation = await getPodmanInstallation();
const installed = installation ? true : false;
extensionApi.context.setValue('podmanIsNotInstalled', !installed, 'onboarding');
},
);
const onboardingCheckReqsCommand = extensionApi.commands.registerCommand(
'podman.onboarding.checkPodmanRequirements',
async () => {
const checks = podmanInstall.getInstallChecks();
const result = [];
let successful = true;
for (const check of checks) {
try {
const checkResult = await check.execute();
result.push({
name: check.title,
successful: checkResult.successful,
description: checkResult.description,
docLinks: checkResult.docLinks,
});
if (!checkResult.successful) {
successful = false;
}
} catch (err) {
result.push({
name: check.title,
successful: false,
description:
err instanceof Error ? err.message : typeof err === 'object' ? err?.toString() : 'unknown error',
});
successful = false;
}
}
let warningsMarkdown = '';
for (const res of result) {
warningsMarkdown += `* ${res.successful ? '✅' : '❌'} ${res.name} \n`;
if (res.description) {
warningsMarkdown += res.description;
if (res.docLinks) {
warningsMarkdown += ' See: ';
for (const link of res.docLinks) {
warningsMarkdown += `[${link.title}](${link.url}) `;
}
warningsMarkdown += '\n';
}
}
}
extensionApi.context.setValue('requirementsStatus', successful ? 'ok' : 'failed', 'onboarding');
extensionApi.context.setValue('warningsMarkdown', warningsMarkdown, 'onboarding');
},
);
const onboardingInstallPodmanCommand = extensionApi.commands.registerCommand(
'podman.onboarding.installPodman',
async () => {
try {
await podmanInstall.doInstallPodman(provider);
const installation = await getPodmanInstallation();
const installed = installation ? true : false;