Skip to content

Commit

Permalink
Remove volumes and networks in wp-env destroy (#23101)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahtallen committed Jun 11, 2020
1 parent 7075e3a commit 7bdf8d3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
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 @@ -11,11 +11,11 @@ const inquirer = require( 'inquirer' );
* Promisified dependencies
*/
const rimraf = util.promisify( require( 'rimraf' ) );
const exec = util.promisify( require( 'child_process' ).exec );

/**
* 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

0 comments on commit 7bdf8d3

Please sign in to comment.