Skip to content

Commit

Permalink
feat: add dialog to set currently running podman machine to default
Browse files Browse the repository at this point in the history
### What does this PR do?

* Creates a dialog / notification that informs the user that the
  currently running podman machine is not set as the default and CLI
  commands will not work with Podman.
* Add buttons to ignore / cancel the notification as well.

### Screenshot/screencast of this PR

<!-- Please include a screenshot or a screencast explaining what is doing this PR -->

### What issues does this PR fix or reference?

<!-- Please include any related issue from Podman Desktop repository (or from another issue tracker).
-->

Closes containers#1322

### How to test this PR?

See video. Otherwise, you can do:

```
podman machine create test-vm
podman system connection default test-vm
# you should see pop up on podman desktop
# to set the default
# use list to see the changes
podman system connection list
```

<!-- Please explain steps to reproduce -->

Signed-off-by: Charlie Drage <charlie@charliedrage.com>
  • Loading branch information
cdrage committed May 3, 2023
1 parent b0f1cdf commit 7fc16f9
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions extensions/podman/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ const MACOS_MAX_SOCKET_PATH_LENGTH = 104;
let storedExtensionContext;
let stopLoop = false;

// 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';
Expand All @@ -61,6 +65,7 @@ type MachineJSON = {
DiskSize: string;
Running: boolean;
Starting: boolean;
Default: boolean;
};

type MachineInfo = {
Expand Down Expand Up @@ -186,6 +191,47 @@ async function updateMachines(provider: extensionApi.Provider): Promise<void> {
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);
}

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 runningMachine = machines.find(machine => machine.Running);
const defaultMachine = machines.find(machine => machine.Default);
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;

// 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 (default is '${defaultMachine.Name}'). 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 {
await execPromise(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}`);
}
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;
}
}

function updateContainerConfiguration(
Expand Down

0 comments on commit 7fc16f9

Please sign in to comment.