From a100d0cf52cb55034dde982836c245e0837401db Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Fri, 16 Aug 2019 01:39:59 +0000 Subject: [PATCH] Build Tools: Improve `local-env` start behaviour under Docker Toolbox. Docker Toolbox requires port forwarding to be configured, but generates error when trying to forward a port that's already been taken. This change removes clashing port forwarding rules before adding our own. See #47767. git-svn-id: https://develop.svn.wordpress.org/trunk@45819 602fd350-edb4-49c9-b593-d223f7449a82 --- tools/local-env/scripts/start.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/tools/local-env/scripts/start.js b/tools/local-env/scripts/start.js index 3ea1c96ecc..ed065b021c 100644 --- a/tools/local-env/scripts/start.js +++ b/tools/local-env/scripts/start.js @@ -9,6 +9,27 @@ execSync( 'docker-compose up -d wordpress-develop', { stdio: 'inherit' } ); // If Docker Toolbox is being used, we need to manually forward LOCAL_PORT to the Docker VM. if ( process.env.DOCKER_TOOLBOX_INSTALL_PATH ) { // VBoxManage is added to the PATH on every platform except Windows. - const vboxmanage = process.env.VBOX_MSI_INSTALL_PATH ? `${process.env.VBOX_MSI_INSTALL_PATH}/VBoxManage` : 'VBoxManage' - execSync( `"${vboxmanage}" controlvm "${process.env.DOCKER_MACHINE_NAME}" natpf1 "tcp-port${process.env.LOCAL_PORT},tcp,127.0.0.1,${process.env.LOCAL_PORT},,${process.env.LOCAL_PORT}"`, { stdio: 'inherit' } ); + const vboxmanage = process.env.VBOX_MSI_INSTALL_PATH ? `${ process.env.VBOX_MSI_INSTALL_PATH }/VBoxManage` : 'VBoxManage' + + // Check if the port forwarding is already configured for this port. + const vminfoBuffer = execSync( `"${ vboxmanage }" showvminfo "${ process.env.DOCKER_MACHINE_NAME }" --machinereadable` ); + const vminfo = vminfoBuffer.toString().split( /[\r\n]+/ ); + + vminfo.forEach( ( info ) => { + if ( ! info.startsWith( 'Forwarding' ) ) { + return; + } + + // `info` is in the format: Forwarding(1)="tcp-port8889,tcp,127.0.0.1,8889,,8889" + // Parse it down so `rule` only contains the data inside quotes, split by ','. + const rule = info.replace( /(^.*?"|"$)/, '' ).split( ',' ); + + // Delete rules that are using the port we need. + if ( rule[ 3 ] === process.env.LOCAL_PORT || rule[ 5 ] === process.env.LOCAL_PORT ) { + execSync( `"${ vboxmanage }" controlvm "${ process.env.DOCKER_MACHINE_NAME }" natpf1 delete ${ rule[ 0 ] }`, { stdio: 'inherit' } ); + } + } ); + + // Add our port forwarding rule. + execSync( `"${ vboxmanage }" controlvm "${ process.env.DOCKER_MACHINE_NAME }" natpf1 "tcp-port${ process.env.LOCAL_PORT },tcp,127.0.0.1,${ process.env.LOCAL_PORT },,${ process.env.LOCAL_PORT }"`, { stdio: 'inherit' } ); }