WordPress.org

Make WordPress Core

Changeset 45819


Ignore:
Timestamp:
08/16/2019 01:39:59 AM (5 weeks ago)
Author:
pento
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/local-env/scripts/start.js

    r45783 r45819  
    1010if ( process.env.DOCKER_TOOLBOX_INSTALL_PATH ) {
    1111    // VBoxManage is added to the PATH on every platform except Windows.
    12     const vboxmanage = process.env.VBOX_MSI_INSTALL_PATH ? `${process.env.VBOX_MSI_INSTALL_PATH}/VBoxManage` : 'VBoxManage'
    13     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' } );
     12    const vboxmanage = process.env.VBOX_MSI_INSTALL_PATH ? `${ process.env.VBOX_MSI_INSTALL_PATH }/VBoxManage` : 'VBoxManage'
     13
     14    // Check if the port forwarding is already configured for this port.
     15    const vminfoBuffer = execSync( `"${ vboxmanage }" showvminfo "${ process.env.DOCKER_MACHINE_NAME }" --machinereadable` );
     16    const vminfo = vminfoBuffer.toString().split( /[\r\n]+/ );
     17
     18    vminfo.forEach( ( info ) => {
     19        if ( ! info.startsWith( 'Forwarding' ) ) {
     20            return;
     21        }
     22
     23        // `info` is in the format: Forwarding(1)="tcp-port8889,tcp,127.0.0.1,8889,,8889"
     24        // Parse it down so `rule` only contains the data inside quotes, split by ','.
     25        const rule = info.replace( /(^.*?"|"$)/, '' ).split( ',' );
     26
     27        // Delete rules that are using the port we need.
     28        if ( rule[ 3 ] === process.env.LOCAL_PORT || rule[ 5 ] === process.env.LOCAL_PORT ) {
     29            execSync( `"${ vboxmanage }" controlvm "${ process.env.DOCKER_MACHINE_NAME }" natpf1 delete ${ rule[ 0 ] }`, { stdio: 'inherit' } );
     30        }
     31    } );
     32
     33    // Add our port forwarding rule.
     34    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' } );
    1435}
Note: See TracChangeset for help on using the changeset viewer.