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

Remove volumes and networks in wp-env destroy #23101

Merged
merged 3 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/env/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Bug Fixes

- `wp-env destroy` now removes dangling docker volumes and networks associated with the WordPress environment.

## 1.4.0 (2020-05-28)

### New Feature
Expand Down
3 changes: 2 additions & 1 deletion packages/env/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ wp> ^C
```sh
wp-env destroy
Destroy the WordPress environment. Delete docker containers and remove local files.
Destroy the WordPress environment. Deletes docker containers, volumes, and
networks associated with the WordPress environment and removes local files.
```
### `wp-env logs [environment]`
Expand Down
2 changes: 1 addition & 1 deletion packages/env/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ module.exports = function cli() {
yargs.command(
'destroy',
wpRed(
'Destroy the WordPress environment. Delete docker containers and remove local files.'
'Destroy the WordPress environment. Deletes docker containers, volumes, and networks associated with the WordPress environment and removes local files.'
),
() => {},
withSpinner( env.destroy )
Expand Down
33 changes: 29 additions & 4 deletions packages/env/lib/commands/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const util = require( 'util' );
const fs = require( 'fs' ).promises;
const path = require( 'path' );
const inquirer = require( 'inquirer' );
const exec = util.promisify( require( 'child_process' ).exec );
noahtallen marked this conversation as resolved.
Show resolved Hide resolved

/**
* Promisified dependencies
Expand All @@ -15,7 +16,6 @@ const rimraf = util.promisify( require( 'rimraf' ) );
/**
* Internal dependencies
*/
const stop = require( './stop' );
const { readConfig } = require( '../../lib/config' );

/**
Expand All @@ -38,7 +38,9 @@ module.exports = async function destroy( { spinner, debug } ) {
return;
}

spinner.info( 'WARNING! This will remove local volumes and containers.' );
spinner.info(
'WARNING! This will remove Docker containers, volumes, and networks associated with the WordPress instance.'
);

const { yesDelete } = await inquirer.prompt( [
{
Expand All @@ -56,15 +58,38 @@ module.exports = async function destroy( { spinner, debug } ) {
return;
}

await stop( { spinner, debug } );

spinner.text = 'Removing WordPress docker containers.';

await dockerCompose.rm( {
config: dockerComposeConfigPath,
commandOptions: [ '--stop', '-v' ],
log: debug,
} );

const directoryHash = path.basename( workDirectoryPath );

spinner.text = 'Removing docker networks and volumes.';
const getVolumes = `docker volume ls | grep "${ directoryHash }" | awk '/ / { print $2 }'`;
const removeVolumes = `docker volume rm $(${ getVolumes })`;

const getNetworks = `docker network ls | grep "${ directoryHash }" | awk '/ / { print $1 }'`;
const removeNetworks = `docker network rm $(${ getNetworks })`;

const command = `${ removeVolumes } && ${ removeNetworks }`;

if ( debug ) {
spinner.info(
`Running command to remove volumes and networks:\n${ command }\n`
);
}

const { stdout } = await exec( command );
if ( debug && stdout ) {
// Disable reason: Logging information in debug mode.
// eslint-disable-next-line no-console
console.log( `Removed volumes and networks:\n${ stdout }` );
}

spinner.text = 'Removing local files.';

await rimraf( workDirectoryPath );
Expand Down