Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wp-env: Fix chown cannot access 'wp-config.php' #30053

Merged
merged 3 commits into from
Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/env/lib/build-docker-compose-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ module.exports = function buildDockerComposeConfig( config ) {
},
},
volumes: {
...( ! config.coreSource && { wordpress: {} } ),
...( ! config.coreSource && { 'tests-wordpress': {} } ),
...( ! config.env.development.coreSource && { wordpress: {} } ),
...( ! config.env.tests.coreSource && { 'tests-wordpress': {} } ),
mysql: {},
'mysql-test': {},
'phpunit-uploads': {},
Expand Down
15 changes: 0 additions & 15 deletions packages/env/lib/commands/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ const initConfig = require( '../init-config' );
const downloadSources = require( '../download-sources' );
const {
checkDatabaseConnection,
makeContentDirectoriesWritable,
makeConfigWritable,
configureWordPress,
setupWordPressDirectories,
} = require( '../wordpress' );
Expand Down Expand Up @@ -141,23 +139,10 @@ module.exports = async function start( { spinner, debug, update, xdebug } ) {
: [],
} );

await Promise.all( [
makeConfigWritable( 'development', config ),
makeConfigWritable( 'tests', config ),
] );

// Only run WordPress install/configuration when config has changed.
if ( shouldConfigureWp ) {
spinner.text = 'Configuring WordPress.';

if ( config.coreSource === null ) {
noahtallen marked this conversation as resolved.
Show resolved Hide resolved
// Don't chown wp-content when it exists on the user's local filesystem.
await Promise.all( [
makeContentDirectoriesWritable( 'development', config ),
makeContentDirectoriesWritable( 'tests', config ),
] );
}

try {
await checkDatabaseConnection( config );
} catch ( error ) {
Expand Down
3 changes: 2 additions & 1 deletion packages/env/lib/init-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const buildDockerComposeConfig = require( './build-docker-compose-config' );

/**
* Initializes the local environment so that Docker commands can be run. Reads
* ./.wp-env.json, creates ~/.wp-env, and creates ~/.wp-env/docker-compose.yml.
* ./.wp-env.json, creates ~/.wp-env, ~/.wp-env/docker-compose.yml, and
* ~/.wp-env/Dockerfile.
*
* @param {Object} options
* @param {Object} options.spinner A CLI spinner which indicates progress.
Expand Down
52 changes: 0 additions & 52 deletions packages/env/lib/wordpress.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,56 +18,6 @@ const copyDir = util.promisify( require( 'copy-dir' ) );
* @typedef {'development'|'tests'|'all'} WPEnvironmentSelection
*/

/**
* Makes the WordPress content directories (wp-content, wp-content/plugins,
* wp-content/themes) owned by the www-data user. This ensures that WordPress
* can write to these directories.
*
* This is necessary when running wp-env with `"core": null` because Docker
* will automatically create these directories as the root user when binding
* volumes during `docker-compose up`, and `docker-compose up` doesn't support
* the `-u` option.
*
* See https://github.com/docker-library/wordpress/issues/436.
*
* @param {WPEnvironment} environment The environment to check. Either 'development' or 'tests'.
* @param {WPConfig} config The wp-env config object.
*/
async function makeContentDirectoriesWritable(
environment,
{ dockerComposeConfigPath, debug }
) {
await dockerCompose.exec(
environment === 'development' ? 'wordpress' : 'tests-wordpress',
'chown www-data:www-data wp-content wp-content/plugins wp-content/themes',
{
config: dockerComposeConfigPath,
log: debug,
}
);
}

/**
* Makes wp-config.php owned by www-data so that WordPress can work with the
* file.
*
* @param {WPEnvironment} environment The environment to check. Either 'development' or 'tests'.
* @param {WPConfig} config The wp-env config object.
*/
async function makeConfigWritable(
environment,
{ dockerComposeConfigPath, debug }
) {
await dockerCompose.exec(
environment === 'development' ? 'wordpress' : 'tests-wordpress',
'chown www-data:www-data wp-config.php',
{
config: dockerComposeConfigPath,
log: debug,
}
);
}

/**
* Checks a WordPress database connection. An error is thrown if the test is
* unsuccessful.
Expand Down Expand Up @@ -280,8 +230,6 @@ async function copyCoreFiles( fromPath, toPath ) {

module.exports = {
hasSameCoreSource,
makeContentDirectoriesWritable,
makeConfigWritable,
checkDatabaseConnection,
configureWordPress,
resetDatabase,
Expand Down
31 changes: 31 additions & 0 deletions packages/env/test/build-docker-compose-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,35 @@ describe( 'buildDockerComposeConfig', () => {
expectedVolumes
);
} );

it( 'should create "wordpress" and "tests-wordpress" volumes if they are needed by containers', () => {
// CONFIG has no coreSource entry, so there are no core sources on the
// local filesystem, so a volume should be created to contain core
// sources.
const dockerConfig = buildDockerComposeConfig( {
env: { development: CONFIG, tests: CONFIG },
} );

expect( dockerConfig.volumes.wordpress ).not.toBe( undefined );
expect( dockerConfig.volumes[ 'tests-wordpress' ] ).not.toBe(
undefined
);
} );

it( 'should NOT create "wordpress" and "tests-wordpress" volumes if they are not needed by containers', () => {
const envConfig = {
...CONFIG,
coreSource: {
path: '/some/random/path',
local: true,
},
};

const dockerConfig = buildDockerComposeConfig( {
env: { development: envConfig, tests: envConfig },
} );

expect( dockerConfig.volumes.wordpress ).toBe( undefined );
expect( dockerConfig.volumes[ 'tests-wordpress' ] ).toBe( undefined );
} );
} );