Skip to content

Commit

Permalink
Add 'Restart' command for containers. (#152)
Browse files Browse the repository at this point in the history
Currently user can stop container and remove stopped container from the context menu. To start existing one they need to use console command.
  • Loading branch information
orfevr authored and chrisdias committed Nov 8, 2017
1 parent 8a044fc commit f7f033d
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
64 changes: 64 additions & 0 deletions commands/restart-container.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { docker } from './utils/docker-endpoint';
import { ContainerItem, quickPickContainer } from './utils/quick-pick-container';
import { reporter } from '../telemetry/telemetry';
import { ContainerNode } from '../explorer/models/containerNode';
import { dockerExplorerProvider } from '../dockerExtension';

import vscode = require('vscode');

const teleCmdId: string = 'vscode-docker.container.restart';


export async function restartContainer(context?: ContainerNode) {

let containersToRestart: Docker.ContainerDesc[];

if (context && context.containerDesc) {
containersToRestart = [context.containerDesc];
}
else {
const opts = {
"filters": {
"status": ["running", "paused", "exited"]
}
};
const selectedItem: ContainerItem = await quickPickContainer(true, opts);
if (selectedItem) {
if (selectedItem.label.toLocaleLowerCase().includes("all containers")) {
containersToRestart = await docker.getContainerDescriptors(opts);
}
else {
containersToRestart = [selectedItem.containerDesc];
}
}
}

if (containersToRestart) {

const numContainers: number = containersToRestart.length;
let containerCounter: number = 0;

vscode.window.setStatusBarMessage("Docker: Restarting Container(s)...", new Promise((resolve, reject) => {
containersToRestart.forEach((container) => {
docker.getContainer(container.Id).restart((err: Error, data: any) => {
containerCounter++;
if (err) {
vscode.window.showErrorMessage(err.message);
dockerExplorerProvider.refreshContainers();
reject();
}
if (containerCounter === numContainers) {
dockerExplorerProvider.refreshContainers();
resolve();
}
});
});
}));

if (reporter) {
reporter.sendTelemetryEvent('command', {
command: teleCmdId
});
}
}
}
2 changes: 2 additions & 0 deletions dockerExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { removeImage } from './commands/remove-image';
import { pushImage } from './commands/push-image';
import { startContainer, startContainerInteractive, startAzureCLI } from './commands/start-container';
import { stopContainer } from './commands/stop-container';
import { restartContainer } from './commands/restart-container';
import { showLogsContainer } from './commands/showlogs-container';
import { openShellContainer } from './commands/open-shell-container';
import { tagImage } from './commands/tag-image';
Expand Down Expand Up @@ -92,6 +93,7 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
ctx.subscriptions.push(vscode.commands.registerCommand('vscode-docker.container.start.interactive', startContainerInteractive));
ctx.subscriptions.push(vscode.commands.registerCommand('vscode-docker.container.start.azurecli', startAzureCLI));
ctx.subscriptions.push(vscode.commands.registerCommand('vscode-docker.container.stop', stopContainer));
ctx.subscriptions.push(vscode.commands.registerCommand('vscode-docker.container.restart', restartContainer));
ctx.subscriptions.push(vscode.commands.registerCommand('vscode-docker.container.show-logs', showLogsContainer));
ctx.subscriptions.push(vscode.commands.registerCommand('vscode-docker.container.open-shell', openShellContainer));
ctx.subscriptions.push(vscode.commands.registerCommand('vscode-docker.container.remove', removeContainer));
Expand Down
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"onCommand:vscode-docker.container.start.interactive",
"onCommand:vscode-docker.container.start.azurecli",
"onCommand:vscode-docker.container.stop",
"onCommand:vscode-docker.container.restart",
"onCommand:vscode-docker.container.show-logs",
"onCommand:vscode-docker.container.open-shell",
"onCommand:vscode-docker.compose.up",
Expand Down Expand Up @@ -146,6 +147,18 @@
"command": "vscode-docker.container.stop",
"when": "view == dockerExplorer && viewItem == containersRootNode"
},
{
"command": "vscode-docker.container.restart",
"when": "view == dockerExplorer && viewItem == runningLocalContainerNode"
},
{
"command": "vscode-docker.container.restart",
"when": "view == dockerExplorer && viewItem == stoppedLocalContainerNode"
},
{
"command": "vscode-docker.container.restart",
"when": "view == dockerExplorer && viewItem == containersRootNode"
},
{
"command": "vscode-docker.container.show-logs",
"when": "view == dockerExplorer && viewItem == runningLocalContainerNode"
Expand Down Expand Up @@ -385,6 +398,12 @@
"description": "Stop a running container",
"category": "Docker"
},
{
"command": "vscode-docker.container.restart",
"title": "Restart Container",
"description": "Restart one or more containers",
"category": "Docker"
},
{
"command": "vscode-docker.container.remove",
"title": "Remove Container",
Expand Down

0 comments on commit f7f033d

Please sign in to comment.