Sergey Biryukov 4e795c423f Build/Test Tools: Check if the Docker is available when running npm run env:start.
This aims to display a more helpful error message if the Docker service is not running.

Follow-up to [45783], [45819].

Props mkox, cnspecialcolor, pento, hasanuzzamanshamim, bgoewert, robinwpdeveloper, costdev, mukesh27.
Fixes #51898.

git-svn-id: https://develop.svn.wordpress.org/trunk@55473 602fd350-edb4-49c9-b593-d223f7449a82
2023-03-07 09:07:25 +00:00

51 lines
2.1 KiB
JavaScript

const dotenv = require( 'dotenv' );
const dotenvExpand = require( 'dotenv-expand' );
const { execSync } = require( 'child_process' );
dotenvExpand.expand( dotenv.config() );
// Check if the Docker service is running.
try {
execSync( 'docker info' );
} catch ( e ) {
if ( e.message.startsWith( 'Command failed: docker info' ) ) {
throw new Error( 'Could not retrieve Docker system info. Is the Docker service running?' );
}
throw e;
}
// Start the local-env containers.
const containers = ( process.env.LOCAL_PHP_MEMCACHED === 'true' )
? 'wordpress-develop memcached'
: 'wordpress-develop';
execSync( `docker-compose up -d ${containers}`, { 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'
// 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' } );
}