@wordpress/env 10.38.0 → 10.39.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +33 -1
- package/lib/cli.js +21 -17
- package/lib/commands/clean.js +6 -37
- package/lib/commands/destroy.js +5 -25
- package/lib/commands/install-path.js +21 -9
- package/lib/commands/logs.js +6 -57
- package/lib/commands/run.js +5 -110
- package/lib/commands/start.js +35 -240
- package/lib/commands/stop.js +6 -15
- package/lib/download-sources.js +9 -53
- package/lib/{build-docker-compose-config.js → runtime/docker/build-docker-compose-config.js} +3 -3
- package/lib/runtime/docker/download-sources.js +59 -0
- package/lib/runtime/docker/index.js +673 -0
- package/lib/{init-config.js → runtime/docker/init-config.js} +2 -2
- package/lib/runtime/docker/wordpress.js +324 -0
- package/lib/runtime/errors.js +17 -0
- package/lib/runtime/index.js +69 -0
- package/lib/runtime/playground/blueprint-builder.js +166 -0
- package/lib/runtime/playground/index.js +452 -0
- package/lib/test/build-docker-compose-config.js +3 -3
- package/lib/wordpress.js +1 -297
- package/package.json +6 -3
- package/lib/{download-wp-phpunit.js → runtime/docker/download-wp-phpunit.js} +1 -1
- /package/lib/{get-host-user.js → runtime/docker/get-host-user.js} +0 -0
- /package/lib/{validate-run-container.js → runtime/docker/validate-run-container.js} +0 -0
|
@@ -0,0 +1,673 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* External dependencies
|
|
4
|
+
*/
|
|
5
|
+
const { spawn, execSync } = require( 'child_process' );
|
|
6
|
+
const path = require( 'path' );
|
|
7
|
+
const util = require( 'util' );
|
|
8
|
+
const { v2: dockerCompose } = require( 'docker-compose' );
|
|
9
|
+
const { rimraf } = require( 'rimraf' );
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Promisified dependencies
|
|
13
|
+
*/
|
|
14
|
+
const sleep = util.promisify( setTimeout );
|
|
15
|
+
const exec = util.promisify( require( 'child_process' ).exec );
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Internal dependencies
|
|
19
|
+
*/
|
|
20
|
+
const initConfig = require( './init-config' );
|
|
21
|
+
const getHostUser = require( './get-host-user' );
|
|
22
|
+
const downloadSources = require( './download-sources' );
|
|
23
|
+
const downloadWPPHPUnit = require( './download-wp-phpunit' );
|
|
24
|
+
const {
|
|
25
|
+
RUN_CONTAINERS,
|
|
26
|
+
validateRunContainer,
|
|
27
|
+
} = require( './validate-run-container' );
|
|
28
|
+
const {
|
|
29
|
+
checkDatabaseConnection,
|
|
30
|
+
configureWordPress,
|
|
31
|
+
resetDatabase,
|
|
32
|
+
setupWordPressDirectories,
|
|
33
|
+
} = require( './wordpress' );
|
|
34
|
+
const { readWordPressVersion, canAccessWPORG } = require( '../../wordpress' );
|
|
35
|
+
const { didCacheChange, setCache } = require( '../../cache' );
|
|
36
|
+
const md5 = require( '../../md5' );
|
|
37
|
+
const retry = require( '../../retry' );
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @typedef {import('../../config').WPConfig} WPConfig
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
const CONFIG_CACHE_KEY = 'config_checksum';
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Docker runtime implementation for wp-env.
|
|
47
|
+
*
|
|
48
|
+
* This runtime uses Docker Compose for container orchestration.
|
|
49
|
+
*/
|
|
50
|
+
class DockerRuntime {
|
|
51
|
+
/**
|
|
52
|
+
* Get the name of this runtime.
|
|
53
|
+
*
|
|
54
|
+
* @return {string} Runtime name.
|
|
55
|
+
*/
|
|
56
|
+
getName() {
|
|
57
|
+
return 'docker';
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Get supported features for this runtime.
|
|
62
|
+
*
|
|
63
|
+
* @return {Object} Feature flags.
|
|
64
|
+
*/
|
|
65
|
+
getFeatures() {
|
|
66
|
+
return {
|
|
67
|
+
testsEnvironment: true,
|
|
68
|
+
xdebug: true,
|
|
69
|
+
spx: true,
|
|
70
|
+
phpMyAdmin: true,
|
|
71
|
+
multisite: true,
|
|
72
|
+
customPhpVersion: true,
|
|
73
|
+
persistentDatabase: true,
|
|
74
|
+
wpCli: true,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Check if Docker is available.
|
|
80
|
+
*
|
|
81
|
+
* @return {Promise<boolean>} True if Docker is available.
|
|
82
|
+
*/
|
|
83
|
+
async isAvailable() {
|
|
84
|
+
try {
|
|
85
|
+
execSync( 'docker info', { stdio: 'ignore' } );
|
|
86
|
+
return true;
|
|
87
|
+
} catch {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Start the Docker containers and configure WordPress.
|
|
94
|
+
*
|
|
95
|
+
* @param {WPConfig} config The wp-env config object.
|
|
96
|
+
* @param {Object} options Start options.
|
|
97
|
+
* @param {Object} options.spinner A CLI spinner which indicates progress.
|
|
98
|
+
* @param {boolean} options.update If true, update sources.
|
|
99
|
+
* @param {string} options.xdebug The Xdebug mode to set.
|
|
100
|
+
* @param {string} options.spx The SPX mode to set.
|
|
101
|
+
* @param {boolean} options.debug True if debug mode is enabled.
|
|
102
|
+
* @return {Promise<Object>} Result object with message and siteUrl.
|
|
103
|
+
*/
|
|
104
|
+
async start( config, { spinner, update, xdebug, spx, debug } ) {
|
|
105
|
+
// Initialize Docker-specific files (docker-compose.yml, Dockerfiles)
|
|
106
|
+
const fullConfig = await initConfig( {
|
|
107
|
+
spinner,
|
|
108
|
+
debug,
|
|
109
|
+
xdebug,
|
|
110
|
+
spx,
|
|
111
|
+
writeChanges: true,
|
|
112
|
+
} );
|
|
113
|
+
|
|
114
|
+
// Check if the hash of the config has changed. If so, run configuration.
|
|
115
|
+
const configHash = md5( fullConfig );
|
|
116
|
+
const { workDirectoryPath, dockerComposeConfigPath } = fullConfig;
|
|
117
|
+
const shouldConfigureWp =
|
|
118
|
+
( update ||
|
|
119
|
+
( await didCacheChange( CONFIG_CACHE_KEY, configHash, {
|
|
120
|
+
workDirectoryPath,
|
|
121
|
+
} ) ) ) &&
|
|
122
|
+
// Don't reconfigure everything when we can't connect to the internet because
|
|
123
|
+
// the majority of update tasks involve connecting to the internet. (Such
|
|
124
|
+
// as downloading sources and pulling docker images.)
|
|
125
|
+
( await canAccessWPORG() );
|
|
126
|
+
|
|
127
|
+
const dockerComposeConfig = {
|
|
128
|
+
config: dockerComposeConfigPath,
|
|
129
|
+
log: fullConfig.debug,
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
if ( ! ( await canAccessWPORG() ) ) {
|
|
133
|
+
spinner.info( 'wp-env is offline' );
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* If the Docker image is already running and the `wp-env` files have been
|
|
138
|
+
* deleted, the start command will not complete successfully. Stopping
|
|
139
|
+
* the container before continuing allows the docker entrypoint script,
|
|
140
|
+
* which restores the files, to run again when we start the containers.
|
|
141
|
+
*
|
|
142
|
+
* Additionally, this serves as a way to restart the container entirely
|
|
143
|
+
* should the need arise.
|
|
144
|
+
*
|
|
145
|
+
* @see https://github.com/WordPress/gutenberg/pull/20253#issuecomment-587228440
|
|
146
|
+
*/
|
|
147
|
+
if ( shouldConfigureWp ) {
|
|
148
|
+
await this.stop( fullConfig, { spinner, debug } );
|
|
149
|
+
// Update the images before starting the services again.
|
|
150
|
+
spinner.text = 'Updating docker images.';
|
|
151
|
+
|
|
152
|
+
const directoryHash = path.basename( workDirectoryPath );
|
|
153
|
+
|
|
154
|
+
// Note: when the base docker image is updated, we want that operation to
|
|
155
|
+
// also update WordPress. Since we store wordpress/tests-wordpress files
|
|
156
|
+
// as docker volumes, simply updating the image will not change those
|
|
157
|
+
// files. Thus, we need to remove those volumes in order for the files
|
|
158
|
+
// to be updated when pulling the new images.
|
|
159
|
+
const volumesToRemove = `${ directoryHash }_wordpress ${ directoryHash }_tests-wordpress`;
|
|
160
|
+
|
|
161
|
+
try {
|
|
162
|
+
if ( fullConfig.debug ) {
|
|
163
|
+
spinner.text = `Removing the WordPress volumes: ${ volumesToRemove }`;
|
|
164
|
+
}
|
|
165
|
+
await exec( `docker volume rm ${ volumesToRemove }` );
|
|
166
|
+
} catch {
|
|
167
|
+
// Note: we do not care about this error condition because it will
|
|
168
|
+
// mostly happen when the volume already exists. This error would not
|
|
169
|
+
// stop wp-env from working correctly.
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
await dockerCompose.pullAll( dockerComposeConfig );
|
|
173
|
+
spinner.text = 'Downloading sources.';
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
await Promise.all( [
|
|
177
|
+
dockerCompose.upOne( 'mysql', {
|
|
178
|
+
...dockerComposeConfig,
|
|
179
|
+
commandOptions: shouldConfigureWp
|
|
180
|
+
? [ '--build', '--force-recreate' ]
|
|
181
|
+
: [],
|
|
182
|
+
} ),
|
|
183
|
+
shouldConfigureWp && downloadSources( fullConfig, spinner ),
|
|
184
|
+
] );
|
|
185
|
+
|
|
186
|
+
if ( shouldConfigureWp ) {
|
|
187
|
+
spinner.text = 'Setting up WordPress directories';
|
|
188
|
+
|
|
189
|
+
await setupWordPressDirectories( fullConfig );
|
|
190
|
+
|
|
191
|
+
// Use the WordPress versions to download the PHPUnit suite.
|
|
192
|
+
const wpVersions = await Promise.all( [
|
|
193
|
+
readWordPressVersion(
|
|
194
|
+
fullConfig.env.development.coreSource,
|
|
195
|
+
spinner,
|
|
196
|
+
debug
|
|
197
|
+
),
|
|
198
|
+
readWordPressVersion(
|
|
199
|
+
fullConfig.env.tests.coreSource,
|
|
200
|
+
spinner,
|
|
201
|
+
debug
|
|
202
|
+
),
|
|
203
|
+
] );
|
|
204
|
+
await downloadWPPHPUnit(
|
|
205
|
+
fullConfig,
|
|
206
|
+
{ development: wpVersions[ 0 ], tests: wpVersions[ 1 ] },
|
|
207
|
+
spinner,
|
|
208
|
+
debug
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
spinner.text = 'Starting WordPress.';
|
|
213
|
+
|
|
214
|
+
await dockerCompose.upMany(
|
|
215
|
+
[ 'wordpress', 'tests-wordpress', 'cli', 'tests-cli' ],
|
|
216
|
+
{
|
|
217
|
+
...dockerComposeConfig,
|
|
218
|
+
commandOptions: shouldConfigureWp
|
|
219
|
+
? [ '--build', '--force-recreate' ]
|
|
220
|
+
: [],
|
|
221
|
+
}
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
if ( fullConfig.env.development.phpmyadminPort ) {
|
|
225
|
+
await dockerCompose.upOne( 'phpmyadmin', {
|
|
226
|
+
...dockerComposeConfig,
|
|
227
|
+
commandOptions: shouldConfigureWp
|
|
228
|
+
? [ '--build', '--force-recreate' ]
|
|
229
|
+
: [],
|
|
230
|
+
} );
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if ( fullConfig.env.tests.phpmyadminPort ) {
|
|
234
|
+
await dockerCompose.upOne( 'tests-phpmyadmin', {
|
|
235
|
+
...dockerComposeConfig,
|
|
236
|
+
commandOptions: shouldConfigureWp
|
|
237
|
+
? [ '--build', '--force-recreate' ]
|
|
238
|
+
: [],
|
|
239
|
+
} );
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Make sure we've consumed the custom CLI dockerfile.
|
|
243
|
+
if ( shouldConfigureWp ) {
|
|
244
|
+
await dockerCompose.buildOne( [ 'cli' ], {
|
|
245
|
+
...dockerComposeConfig,
|
|
246
|
+
} );
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// Only run WordPress install/configuration when config has changed.
|
|
250
|
+
if ( shouldConfigureWp ) {
|
|
251
|
+
spinner.text = 'Configuring WordPress.';
|
|
252
|
+
|
|
253
|
+
try {
|
|
254
|
+
await checkDatabaseConnection( fullConfig );
|
|
255
|
+
} catch ( error ) {
|
|
256
|
+
// Wait 30 seconds for MySQL to accept connections.
|
|
257
|
+
await retry( () => checkDatabaseConnection( fullConfig ), {
|
|
258
|
+
times: 30,
|
|
259
|
+
delay: 1000,
|
|
260
|
+
} );
|
|
261
|
+
|
|
262
|
+
// It takes 3-4 seconds for MySQL to be ready after it starts accepting connections.
|
|
263
|
+
await sleep( 4000 );
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// Retry WordPress installation in case MySQL *still* wasn't ready.
|
|
267
|
+
await Promise.all( [
|
|
268
|
+
retry(
|
|
269
|
+
() =>
|
|
270
|
+
configureWordPress(
|
|
271
|
+
'development',
|
|
272
|
+
fullConfig,
|
|
273
|
+
spinner
|
|
274
|
+
),
|
|
275
|
+
{
|
|
276
|
+
times: 2,
|
|
277
|
+
}
|
|
278
|
+
),
|
|
279
|
+
retry(
|
|
280
|
+
() => configureWordPress( 'tests', fullConfig, spinner ),
|
|
281
|
+
{
|
|
282
|
+
times: 2,
|
|
283
|
+
}
|
|
284
|
+
),
|
|
285
|
+
] );
|
|
286
|
+
|
|
287
|
+
// Set the cache key once everything has been configured.
|
|
288
|
+
await setCache( CONFIG_CACHE_KEY, configHash, {
|
|
289
|
+
workDirectoryPath,
|
|
290
|
+
} );
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// Get port information for the result message
|
|
294
|
+
const siteUrl = fullConfig.env.development.config.WP_SITEURL;
|
|
295
|
+
const testsSiteUrl = fullConfig.env.tests.config.WP_SITEURL;
|
|
296
|
+
|
|
297
|
+
const mySQLPort = await this._getPublicDockerPort(
|
|
298
|
+
'mysql',
|
|
299
|
+
3306,
|
|
300
|
+
dockerComposeConfig
|
|
301
|
+
);
|
|
302
|
+
|
|
303
|
+
const testsMySQLPort = await this._getPublicDockerPort(
|
|
304
|
+
'tests-mysql',
|
|
305
|
+
3306,
|
|
306
|
+
dockerComposeConfig
|
|
307
|
+
);
|
|
308
|
+
|
|
309
|
+
const phpmyadminPort = fullConfig.env.development.phpmyadminPort
|
|
310
|
+
? await this._getPublicDockerPort(
|
|
311
|
+
'phpmyadmin',
|
|
312
|
+
80,
|
|
313
|
+
dockerComposeConfig
|
|
314
|
+
)
|
|
315
|
+
: null;
|
|
316
|
+
|
|
317
|
+
const testsPhpmyadminPort = fullConfig.env.tests.phpmyadminPort
|
|
318
|
+
? await this._getPublicDockerPort(
|
|
319
|
+
'tests-phpmyadmin',
|
|
320
|
+
80,
|
|
321
|
+
dockerComposeConfig
|
|
322
|
+
)
|
|
323
|
+
: null;
|
|
324
|
+
|
|
325
|
+
const message = [
|
|
326
|
+
'WordPress development site started' +
|
|
327
|
+
( siteUrl ? ` at ${ siteUrl }` : '.' ),
|
|
328
|
+
'WordPress test site started' +
|
|
329
|
+
( testsSiteUrl ? ` at ${ testsSiteUrl }` : '.' ),
|
|
330
|
+
`MySQL is listening on port ${ mySQLPort }`,
|
|
331
|
+
`MySQL for automated testing is listening on port ${ testsMySQLPort }`,
|
|
332
|
+
phpmyadminPort &&
|
|
333
|
+
`phpMyAdmin started at http://localhost:${ phpmyadminPort }`,
|
|
334
|
+
testsPhpmyadminPort &&
|
|
335
|
+
`phpMyAdmin for automated testing started at http://localhost:${ testsPhpmyadminPort }`,
|
|
336
|
+
]
|
|
337
|
+
.filter( Boolean )
|
|
338
|
+
.join( '\n' );
|
|
339
|
+
|
|
340
|
+
return {
|
|
341
|
+
message,
|
|
342
|
+
siteUrl,
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Get the public port for a Docker service.
|
|
348
|
+
*
|
|
349
|
+
* @param {string} service The service name.
|
|
350
|
+
* @param {number} containerPort The container port.
|
|
351
|
+
* @param {Object} dockerComposeConfig The docker-compose config.
|
|
352
|
+
* @return {Promise<string>} The public port.
|
|
353
|
+
*/
|
|
354
|
+
async _getPublicDockerPort( service, containerPort, dockerComposeConfig ) {
|
|
355
|
+
const { out: address } = await dockerCompose.port(
|
|
356
|
+
service,
|
|
357
|
+
containerPort,
|
|
358
|
+
dockerComposeConfig
|
|
359
|
+
);
|
|
360
|
+
return address.split( ':' ).pop().trim();
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Get the warning message for destroy confirmation.
|
|
365
|
+
*
|
|
366
|
+
* @return {string} Warning message.
|
|
367
|
+
*/
|
|
368
|
+
getDestroyWarningMessage() {
|
|
369
|
+
return 'WARNING! This will remove Docker containers, volumes, networks, and images associated with the WordPress instance.';
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Stop the Docker containers.
|
|
374
|
+
*
|
|
375
|
+
* @param {WPConfig} config The wp-env config object.
|
|
376
|
+
* @param {Object} options Stop options.
|
|
377
|
+
* @param {Object} options.spinner A CLI spinner which indicates progress.
|
|
378
|
+
* @param {boolean} options.debug True if debug mode is enabled.
|
|
379
|
+
*/
|
|
380
|
+
async stop( config, { spinner, debug } ) {
|
|
381
|
+
const { dockerComposeConfigPath } = await initConfig( {
|
|
382
|
+
spinner,
|
|
383
|
+
debug,
|
|
384
|
+
} );
|
|
385
|
+
|
|
386
|
+
spinner.text = 'Stopping WordPress.';
|
|
387
|
+
|
|
388
|
+
await dockerCompose.down( {
|
|
389
|
+
config: dockerComposeConfigPath,
|
|
390
|
+
log: debug,
|
|
391
|
+
} );
|
|
392
|
+
|
|
393
|
+
spinner.text = 'Stopped WordPress.';
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* Destroy the Docker containers and remove local files.
|
|
398
|
+
*
|
|
399
|
+
* @param {WPConfig} config The wp-env config object.
|
|
400
|
+
* @param {Object} options Destroy options.
|
|
401
|
+
* @param {Object} options.spinner A CLI spinner which indicates progress.
|
|
402
|
+
* @param {boolean} options.debug True if debug mode is enabled.
|
|
403
|
+
*/
|
|
404
|
+
async destroy( config, { spinner, debug } ) {
|
|
405
|
+
spinner.text = 'Removing docker images, volumes, and networks.';
|
|
406
|
+
|
|
407
|
+
await dockerCompose.down( {
|
|
408
|
+
config: config.dockerComposeConfigPath,
|
|
409
|
+
commandOptions: [ '--volumes', '--remove-orphans', '--rmi', 'all' ],
|
|
410
|
+
log: debug,
|
|
411
|
+
} );
|
|
412
|
+
|
|
413
|
+
spinner.text = 'Removing local files.';
|
|
414
|
+
// Note: there is a race condition where docker compose actually hasn't finished
|
|
415
|
+
// by this point, which causes rimraf to fail. We need to wait at least 2.5-5s,
|
|
416
|
+
// but using 10s in case it's dependant on the machine.
|
|
417
|
+
await new Promise( ( resolve ) => setTimeout( resolve, 10000 ) );
|
|
418
|
+
await rimraf( config.workDirectoryPath );
|
|
419
|
+
|
|
420
|
+
spinner.text = 'Removed WordPress environment.';
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* Clean/reset the WordPress database.
|
|
425
|
+
*
|
|
426
|
+
* @param {WPConfig} config The wp-env config object.
|
|
427
|
+
* @param {Object} options Clean options.
|
|
428
|
+
* @param {string} options.environment The environment to clean.
|
|
429
|
+
* @param {Object} options.spinner A CLI spinner which indicates progress.
|
|
430
|
+
* @param {boolean} options.debug True if debug mode is enabled.
|
|
431
|
+
*/
|
|
432
|
+
async clean( config, { environment, spinner, debug } ) {
|
|
433
|
+
const fullConfig = await initConfig( { spinner, debug } );
|
|
434
|
+
|
|
435
|
+
const description = `${ environment } environment${
|
|
436
|
+
environment === 'all' ? 's' : ''
|
|
437
|
+
}`;
|
|
438
|
+
spinner.text = `Cleaning ${ description }.`;
|
|
439
|
+
|
|
440
|
+
const tasks = [];
|
|
441
|
+
|
|
442
|
+
// Start the database first to avoid race conditions where all tasks create
|
|
443
|
+
// different docker networks with the same name.
|
|
444
|
+
await dockerCompose.upOne( 'mysql', {
|
|
445
|
+
config: fullConfig.dockerComposeConfigPath,
|
|
446
|
+
log: fullConfig.debug,
|
|
447
|
+
} );
|
|
448
|
+
|
|
449
|
+
if ( environment === 'all' || environment === 'development' ) {
|
|
450
|
+
tasks.push(
|
|
451
|
+
resetDatabase( 'development', fullConfig )
|
|
452
|
+
.then( () =>
|
|
453
|
+
configureWordPress( 'development', fullConfig )
|
|
454
|
+
)
|
|
455
|
+
.catch( () => {} )
|
|
456
|
+
);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
if ( environment === 'all' || environment === 'tests' ) {
|
|
460
|
+
tasks.push(
|
|
461
|
+
resetDatabase( 'tests', fullConfig )
|
|
462
|
+
.then( () => configureWordPress( 'tests', fullConfig ) )
|
|
463
|
+
.catch( () => {} )
|
|
464
|
+
);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
await Promise.all( tasks );
|
|
468
|
+
|
|
469
|
+
spinner.text = `Cleaned ${ description }.`;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* Get the list of valid container names for the run command.
|
|
474
|
+
*
|
|
475
|
+
* @return {string[]} Array of valid container names.
|
|
476
|
+
*/
|
|
477
|
+
getRunContainers() {
|
|
478
|
+
return RUN_CONTAINERS;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/**
|
|
482
|
+
* Run a command in a Docker container.
|
|
483
|
+
*
|
|
484
|
+
* @param {WPConfig} config The wp-env config object.
|
|
485
|
+
* @param {Object} options Run options.
|
|
486
|
+
* @param {string} options.container The container to run the command in.
|
|
487
|
+
* @param {string[]} options.command The command to run.
|
|
488
|
+
* @param {string} options.envCwd The working directory.
|
|
489
|
+
* @param {Object} options.spinner A CLI spinner which indicates progress.
|
|
490
|
+
* @param {boolean} options.debug True if debug mode is enabled.
|
|
491
|
+
*/
|
|
492
|
+
async run( config, { container, command, envCwd, spinner, debug } ) {
|
|
493
|
+
// Validate the container name (throws for deprecated containers)
|
|
494
|
+
validateRunContainer( container );
|
|
495
|
+
|
|
496
|
+
const fullConfig = await initConfig( { spinner, debug } );
|
|
497
|
+
|
|
498
|
+
// Shows a contextual tip for the given command.
|
|
499
|
+
const joinedCommand = command.join( ' ' );
|
|
500
|
+
this._showCommandTips( joinedCommand, container, spinner );
|
|
501
|
+
|
|
502
|
+
await this._spawnCommandDirectly(
|
|
503
|
+
fullConfig,
|
|
504
|
+
container,
|
|
505
|
+
command,
|
|
506
|
+
envCwd
|
|
507
|
+
);
|
|
508
|
+
|
|
509
|
+
spinner.text = `Ran \`${ joinedCommand }\` in '${ container }'.`;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
* Show logs from Docker containers.
|
|
514
|
+
*
|
|
515
|
+
* @param {WPConfig} config The wp-env config object.
|
|
516
|
+
* @param {Object} options Logs options.
|
|
517
|
+
* @param {string} options.environment The environment to show logs for.
|
|
518
|
+
* @param {boolean} options.watch If true, follow along with log output.
|
|
519
|
+
* @param {Object} options.spinner A CLI spinner which indicates progress.
|
|
520
|
+
* @param {boolean} options.debug True if debug mode is enabled.
|
|
521
|
+
*/
|
|
522
|
+
async logs( config, { environment, watch, spinner, debug } ) {
|
|
523
|
+
const fullConfig = await initConfig( { spinner, debug } );
|
|
524
|
+
|
|
525
|
+
// If we show text while watching the logs, it will continue showing up every
|
|
526
|
+
// few lines in the logs as they happen, which isn't a good look. So only
|
|
527
|
+
// show the message if we are not watching the logs.
|
|
528
|
+
if ( ! watch ) {
|
|
529
|
+
spinner.text = `Showing logs for the ${ environment } environment.`;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
const servicesToWatch =
|
|
533
|
+
environment === 'all'
|
|
534
|
+
? [ 'tests-wordpress', 'wordpress' ]
|
|
535
|
+
: [ environment === 'tests' ? 'tests-wordpress' : 'wordpress' ];
|
|
536
|
+
|
|
537
|
+
const output = await Promise.all( [
|
|
538
|
+
...servicesToWatch.map( ( service ) =>
|
|
539
|
+
dockerCompose.logs( service, {
|
|
540
|
+
config: fullConfig.dockerComposeConfigPath,
|
|
541
|
+
log: watch, // Must log inline if we are watching the log output.
|
|
542
|
+
commandOptions: watch ? [ '--follow' ] : [],
|
|
543
|
+
} )
|
|
544
|
+
),
|
|
545
|
+
] );
|
|
546
|
+
|
|
547
|
+
// Combine the results from each docker output.
|
|
548
|
+
const result = output.reduce(
|
|
549
|
+
( acc, current ) => {
|
|
550
|
+
if ( current.out ) {
|
|
551
|
+
acc.out = acc.out.concat( current.out );
|
|
552
|
+
}
|
|
553
|
+
if ( current.err ) {
|
|
554
|
+
acc.err = acc.err.concat( current.err );
|
|
555
|
+
}
|
|
556
|
+
if ( current.exitCode !== 0 ) {
|
|
557
|
+
acc.hasNon0ExitCode = true;
|
|
558
|
+
}
|
|
559
|
+
return acc;
|
|
560
|
+
},
|
|
561
|
+
{ out: '', err: '', hasNon0ExitCode: false }
|
|
562
|
+
);
|
|
563
|
+
|
|
564
|
+
if ( result.out.length ) {
|
|
565
|
+
console.log(
|
|
566
|
+
process.stdout.isTTY ? `\n\n${ result.out }\n\n` : result.out
|
|
567
|
+
);
|
|
568
|
+
} else if ( result.err.length ) {
|
|
569
|
+
console.error(
|
|
570
|
+
process.stdout.isTTY ? `\n\n${ result.err }\n\n` : result.err
|
|
571
|
+
);
|
|
572
|
+
if ( result.hasNon0ExitCode ) {
|
|
573
|
+
throw result.err;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
spinner.text = 'Finished showing logs.';
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
/**
|
|
581
|
+
* Runs an arbitrary command on the given Docker container.
|
|
582
|
+
*
|
|
583
|
+
* @param {WPConfig} config The wp-env configuration.
|
|
584
|
+
* @param {string} container The Docker container to run the command on.
|
|
585
|
+
* @param {string[]} command The command to run.
|
|
586
|
+
* @param {string} envCwd The working directory for the command.
|
|
587
|
+
* @return {Promise} Promise that resolves when the command completes.
|
|
588
|
+
*/
|
|
589
|
+
_spawnCommandDirectly( config, container, command, envCwd ) {
|
|
590
|
+
// Both the `wordpress` and `tests-wordpress` containers have the host's
|
|
591
|
+
// user so that they can maintain ownership parity with the host OS.
|
|
592
|
+
// We should run any commands as that user so that they are able
|
|
593
|
+
// to interact with the files mounted from the host.
|
|
594
|
+
const hostUser = getHostUser();
|
|
595
|
+
|
|
596
|
+
// Since Docker requires absolute paths, we should resolve the input to a POSIX path.
|
|
597
|
+
// This is needed because Windows resolves relative paths from the C: drive.
|
|
598
|
+
envCwd = path.posix.resolve(
|
|
599
|
+
// Not all containers have the same starting working directory.
|
|
600
|
+
container === 'mysql' || container === 'tests-mysql'
|
|
601
|
+
? '/'
|
|
602
|
+
: '/var/www/html',
|
|
603
|
+
// Remove spaces and single quotes from both ends of the path.
|
|
604
|
+
// This is needed because Windows treats single quotes as a literal character.
|
|
605
|
+
envCwd.trim().replace( /^'|'$/g, '' )
|
|
606
|
+
);
|
|
607
|
+
|
|
608
|
+
const composeCommand = [
|
|
609
|
+
'compose',
|
|
610
|
+
'-f',
|
|
611
|
+
config.dockerComposeConfigPath,
|
|
612
|
+
'exec',
|
|
613
|
+
'-w',
|
|
614
|
+
envCwd,
|
|
615
|
+
'--user',
|
|
616
|
+
hostUser.fullUser,
|
|
617
|
+
];
|
|
618
|
+
|
|
619
|
+
if ( ! process.stdout.isTTY ) {
|
|
620
|
+
composeCommand.push( '-T' );
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
composeCommand.push( container, ...command );
|
|
624
|
+
|
|
625
|
+
return new Promise( ( resolve, reject ) => {
|
|
626
|
+
// Note: since the npm docker-compose package uses the -T option, we
|
|
627
|
+
// cannot use it to spawn an interactive command. Thus, we run docker-
|
|
628
|
+
// compose on the CLI directly.
|
|
629
|
+
const childProc = spawn( 'docker', composeCommand, {
|
|
630
|
+
stdio: 'inherit',
|
|
631
|
+
} );
|
|
632
|
+
childProc.on( 'error', reject );
|
|
633
|
+
childProc.on( 'exit', ( code ) => {
|
|
634
|
+
// Code 130 is set if the user tries to exit with ctrl-c before using
|
|
635
|
+
// ctrl-d (so it is not an error which should fail the script.)
|
|
636
|
+
if ( code === 0 || code === 130 ) {
|
|
637
|
+
resolve();
|
|
638
|
+
} else {
|
|
639
|
+
reject( `Command failed with exit code ${ code }` );
|
|
640
|
+
}
|
|
641
|
+
} );
|
|
642
|
+
} );
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
/**
|
|
646
|
+
* This shows a contextual tip for the command being run. Certain commands (like
|
|
647
|
+
* bash) may have weird behavior (exit with ctrl-d instead of ctrl-c or ctrl-z),
|
|
648
|
+
* so we want the user to have that information without having to ask someone.
|
|
649
|
+
*
|
|
650
|
+
* @param {string} joinedCommand The command joined by spaces.
|
|
651
|
+
* @param {string} container The container the command will be run on.
|
|
652
|
+
* @param {Object} spinner A spinner object to show progress.
|
|
653
|
+
*/
|
|
654
|
+
_showCommandTips( joinedCommand, container, spinner ) {
|
|
655
|
+
if ( ! joinedCommand.length ) {
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
const tip = `Starting '${ joinedCommand }' on the ${ container } container. ${ ( () => {
|
|
660
|
+
switch ( joinedCommand ) {
|
|
661
|
+
case 'bash':
|
|
662
|
+
return 'Exit bash with ctrl-d.';
|
|
663
|
+
case 'wp shell':
|
|
664
|
+
return 'Exit the WordPress shell with ctrl-c.';
|
|
665
|
+
default:
|
|
666
|
+
return '';
|
|
667
|
+
}
|
|
668
|
+
} )() }\n`;
|
|
669
|
+
spinner.info( tip );
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
module.exports = DockerRuntime;
|
|
@@ -10,11 +10,11 @@ const yaml = require( 'js-yaml' );
|
|
|
10
10
|
/**
|
|
11
11
|
* Internal dependencies
|
|
12
12
|
*/
|
|
13
|
-
const { loadConfig, ValidationError } = require( '
|
|
13
|
+
const { loadConfig, ValidationError } = require( '../../config' );
|
|
14
14
|
const buildDockerComposeConfig = require( './build-docker-compose-config' );
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
|
-
* @typedef {import('
|
|
17
|
+
* @typedef {import('../../config').WPConfig} WPConfig
|
|
18
18
|
*/
|
|
19
19
|
|
|
20
20
|
/**
|