Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve prune logic #1081

Merged
merged 1 commit into from
Jul 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 6 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"onCommand:vscode-docker.networks.prune",
"onCommand:vscode-docker.networks.refresh",
"onCommand:vscode-docker.networks.remove",
"onCommand:vscode-docker.pruneSystem",
"onCommand:vscode-docker.registries.azure.buildImage",
"onCommand:vscode-docker.registries.azure.createRegistry",
"onCommand:vscode-docker.registries.azure.deleteRegistry",
Expand All @@ -83,7 +84,6 @@
"onCommand:vscode-docker.registries.pullRepository",
"onCommand:vscode-docker.registries.refresh",
"onCommand:vscode-docker.registries.setAsDefault",
"onCommand:vscode-docker.system.prune",
"onCommand:vscode-docker.volumes.configureExplorer",
"onCommand:vscode-docker.volumes.inspect",
"onCommand:vscode-docker.volumes.prune",
Expand Down Expand Up @@ -1146,11 +1146,6 @@
"default": "powershell",
"description": "Attach command to use for Windows containers"
},
"docker.promptOnSystemPrune": {
"type": "boolean",
"default": true,
"description": "Prompt for confirmation when running System Prune command"
},
"docker.dockerComposeBuild": {
"type": "boolean",
"default": true,
Expand Down Expand Up @@ -1350,6 +1345,11 @@
"title": "Remove...",
"category": "Docker Networks"
},
{
"command": "vscode-docker.pruneSystem",
"title": "Prune System...",
"category": "Docker"
},
{
"command": "vscode-docker.registries.azure.buildImage",
"title": "Build Image in Azure...",
Expand Down Expand Up @@ -1486,15 +1486,6 @@
"title": "Set as Default",
"category": "Docker Registries"
},
{
"command": "vscode-docker.system.prune",
"title": "System Prune",
"category": "Docker",
"icon": {
"light": "resources/light/prune.svg",
"dark": "resources/dark/prune.svg"
}
},
{
"command": "vscode-docker.volumes.configureExplorer",
"title": "Configure Explorer...",
Expand Down
12 changes: 9 additions & 3 deletions src/commands/containers/pruneContainers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
* Licensed under the MIT License. See LICENSE.md in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { window } from 'vscode';
import { IActionContext } from 'vscode-azureextensionui';
import { ext } from '../../extensionVariables';
import { convertToMB } from '../../utils/convertToMB';

export async function pruneContainers(_context: IActionContext): Promise<void> {
const confirmPrune: string = "Are you sure you want to remove all stopped containers?";
// no need to check result - cancel will throw a UserCancelledError
await ext.ui.showWarningMessage(confirmPrune, { modal: true }, { title: 'Remove' });

const terminal = ext.terminalProvider.createTerminal("docker prune");
terminal.sendText('docker container prune -f');
terminal.show();
const result = await ext.dockerode.pruneContainers();

const numDeleted = (result.ContainersDeleted || []).length;
const mbReclaimed = convertToMB(result.SpaceReclaimed);
let message = `Removed ${numDeleted} container(s) and reclaimed ${mbReclaimed}MB of space.`;
// don't wait
window.showInformationMessage(message);
}
12 changes: 9 additions & 3 deletions src/commands/images/pruneImages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
* Licensed under the MIT License. See LICENSE.md in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { window } from 'vscode';
import { IActionContext } from 'vscode-azureextensionui';
import { ext } from '../../extensionVariables';
import { convertToMB } from '../../utils/convertToMB';

export async function pruneImages(_context: IActionContext): Promise<void> {
const confirmPrune: string = "Are you sure you want to remove all dangling images?";
// no need to check result - cancel will throw a UserCancelledError
await ext.ui.showWarningMessage(confirmPrune, { modal: true }, { title: 'Remove' });

const terminal = ext.terminalProvider.createTerminal("docker prune");
terminal.sendText('docker image prune -f');
terminal.show();
const result = await ext.dockerode.pruneImages();

const numDeleted = (result.ImagesDeleted || []).length;
const mbReclaimed = convertToMB(result.SpaceReclaimed);
let message = `Removed ${numDeleted} images(s) and reclaimed ${mbReclaimed}MB of space.`;
// don't wait
window.showInformationMessage(message);
}
10 changes: 7 additions & 3 deletions src/commands/networks/pruneNetworks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Licensed under the MIT License. See LICENSE.md in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { window } from 'vscode';
import { IActionContext } from 'vscode-azureextensionui';
import { ext } from '../../extensionVariables';

Expand All @@ -11,7 +12,10 @@ export async function pruneNetworks(_context: IActionContext): Promise<void> {
// no need to check result - cancel will throw a UserCancelledError
await ext.ui.showWarningMessage(confirmPrune, { modal: true }, { title: 'Remove' });

const terminal = ext.terminalProvider.createTerminal("docker prune");
terminal.sendText('docker network prune -f');
terminal.show();
const result = await ext.dockerode.pruneNetworks();

const numDeleted = (result.NetworksDeleted || []).length;
let message = `Removed ${numDeleted} networks(s).`;
// don't wait
window.showInformationMessage(message);
}
30 changes: 30 additions & 0 deletions src/commands/pruneSystem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE.md in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { window } from 'vscode';
import { IActionContext } from 'vscode-azureextensionui';
import { ext } from '../extensionVariables';
import { convertToMB } from '../utils/convertToMB';

export async function pruneSystem(context: IActionContext): Promise<void> {
const confirmPrune: string = "Are you sure you want to remove all stopped containers, dangling images, unused networks, and unused volumes?";
// no need to check result - cancel will throw a UserCancelledError
await ext.ui.showWarningMessage(confirmPrune, { modal: true }, { title: 'Remove' });

const containersResult = await ext.dockerode.pruneContainers();
const imagesResult = await ext.dockerode.pruneImages();
const networksResult = await ext.dockerode.pruneNetworks();
const volumesResult = await ext.dockerode.pruneVolumes();

const numContainers = (containersResult.ContainersDeleted || []).length;
const numImages = (imagesResult.ImagesDeleted || []).length;
const numNetworks = (networksResult.NetworksDeleted || []).length;
const numVolumes = (volumesResult.VolumesDeleted || []).length;

const mbReclaimed = convertToMB(containersResult.SpaceReclaimed + imagesResult.SpaceReclaimed + volumesResult.SpaceReclaimed);
let message = `Removed ${numContainers} container(s), ${numImages} image(s), ${numNetworks} network(s), ${numVolumes} volume(s) and reclaimed ${mbReclaimed}MB of space.`;
// don't wait
window.showInformationMessage(message);
}
4 changes: 2 additions & 2 deletions src/commands/registerCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { tagImage } from "./images/tagImage";
import { configureNetworksExplorer } from "./networks/configureNetworksExplorer";
import { pruneNetworks } from "./networks/pruneNetworks";
import { removeNetwork } from "./networks/removeNetwork";
import { pruneSystem } from "./pruneSystem";
import { createAzureRegistry } from "./registries/azure/createAzureRegistry";
import { deleteAzureRegistry } from "./registries/azure/deleteAzureRegistry";
import { deleteAzureRepository } from "./registries/azure/deleteAzureRepository";
Expand All @@ -49,7 +50,6 @@ import { connectPrivateRegistry } from "./registries/private/connectPrivateRegis
import { disconnectPrivateRegistry } from "./registries/private/disconnectPrivateRegistry";
import { pullImage, pullRepository } from "./registries/pullImages";
import { setRegistryAsDefault } from "./registries/registrySettings";
import { systemPrune } from "./systemPrune";
import { configureVolumesExplorer } from "./volumes/configureVolumesExplorer";
import { inspectVolume } from "./volumes/inspectVolume";
import { pruneVolumes } from "./volumes/pruneVolumes";
Expand All @@ -61,7 +61,7 @@ export function registerCommands(): void {
registerCommand('vscode-docker.compose.restart', composeRestart);
registerCommand('vscode-docker.compose.up', composeUp);
registerCommand('vscode-docker.configure', configure);
registerCommand('vscode-docker.system.prune', systemPrune);
registerCommand('vscode-docker.pruneSystem', pruneSystem);

registerCommand('vscode-docker.containers.attachShell', attachShellContainer);
registerCommand('vscode-docker.containers.inspect', inspectContainer);
Expand Down
36 changes: 0 additions & 36 deletions src/commands/systemPrune.ts

This file was deleted.

12 changes: 9 additions & 3 deletions src/commands/volumes/pruneVolumes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
* Licensed under the MIT License. See LICENSE.md in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { window } from 'vscode';
import { IActionContext } from 'vscode-azureextensionui';
import { ext } from '../../extensionVariables';
import { convertToMB } from '../../utils/convertToMB';

export async function pruneVolumes(_context: IActionContext): Promise<void> {
const confirmPrune: string = "Are you sure you want to remove all unused volumes?";
// no need to check result - cancel will throw a UserCancelledError
await ext.ui.showWarningMessage(confirmPrune, { modal: true }, { title: 'Remove' });

const terminal = ext.terminalProvider.createTerminal("docker prune");
terminal.sendText('docker volume prune -f');
terminal.show();
const result = await ext.dockerode.pruneVolumes();

const numDeleted = (result.VolumesDeleted || []).length;
const mbReclaimed = convertToMB(result.SpaceReclaimed);
let message = `Removed ${numDeleted} volumes(s) and reclaimed ${mbReclaimed}MB of space.`;
// don't wait
window.showInformationMessage(message);
}
3 changes: 2 additions & 1 deletion src/tree/containers/LocalContainerInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class LocalContainerInfo implements ILocalImageInfo {
}

public get treeId(): string {
return this.containerId;
// include state in treeId so that auto-refresh will detect and show a new icon when state changes
return this.containerId + this.state;
}
}
8 changes: 8 additions & 0 deletions src/utils/convertToMB.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE.md in the project root for license information.
*--------------------------------------------------------------------------------------------*/

export function convertToMB(bytes: number): number {
return Math.round(bytes / 1000000);
}