From 7bdf8d378549fa3025a44e2b59ea3e0150d9b8c2 Mon Sep 17 00:00:00 2001 From: Noah Allen Date: Thu, 11 Jun 2020 16:19:57 -0700 Subject: [PATCH] Remove volumes and networks in wp-env destroy (#23101) --- packages/env/CHANGELOG.md | 4 ++++ packages/env/README.md | 3 ++- packages/env/lib/cli.js | 2 +- packages/env/lib/commands/destroy.js | 33 ++++++++++++++++++++++++---- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/packages/env/CHANGELOG.md b/packages/env/CHANGELOG.md index b9a0bc4aa83e62..b4980cf73c625a 100644 --- a/packages/env/CHANGELOG.md +++ b/packages/env/CHANGELOG.md @@ -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 diff --git a/packages/env/README.md b/packages/env/README.md index 2dd712d05ebb45..8966a61f42c2d8 100644 --- a/packages/env/README.md +++ b/packages/env/README.md @@ -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]` diff --git a/packages/env/lib/cli.js b/packages/env/lib/cli.js index d1408728b43fab..611ef56a641c9a 100644 --- a/packages/env/lib/cli.js +++ b/packages/env/lib/cli.js @@ -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 ) diff --git a/packages/env/lib/commands/destroy.js b/packages/env/lib/commands/destroy.js index 0f93a982bd2b08..2bda29e110c619 100644 --- a/packages/env/lib/commands/destroy.js +++ b/packages/env/lib/commands/destroy.js @@ -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' ); /** @@ -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( [ { @@ -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 );