summaryrefslogtreecommitdiffstatshomepage
path: root/tools/local-env/scripts/start.js
blob: b0389b2fb0a7f8476d7e33441391f4d2a0d49186 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* jshint node:true */

const dotenv       = require( 'dotenv' );
const dotenvExpand = require( 'dotenv-expand' );
const { execSync, spawnSync } = require( 'child_process' );
const local_env_utils = require( './utils' );
const { constants, copyFile } = require( 'node:fs' );

// Copy the default .env file when one is not present.
copyFile( '.env.example', '.env', constants.COPYFILE_EXCL, () => {
	console.log( '.env file already exists. .env.example was not copied.' );
});

dotenvExpand.expand( dotenv.config() );

const composeFiles = local_env_utils.get_compose_files();

// Determine if a non-default database authentication plugin needs to be used.
local_env_utils.determine_auth_option();

// 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 = [ 'wordpress-develop', 'cli' ];
if ( process.env.LOCAL_PHP_MEMCACHED === 'true' ) {
	containers.push( 'memcached' );
}

spawnSync(
	'docker',
	[
		'compose',
		...composeFiles.map( ( composeFile ) => [ '-f', composeFile ] ).flat(),
		'up',
		'--quiet-pull',
		'-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 = spawnSync(
		vboxmanage,
		[
			'showvminfo',
			process.env.DOCKER_MACHINE_NAME,
			'--machinereadable'
		]
	).stdout;
	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 ) {
			spawnSync(
				vboxmanage,
				[
					'controlvm',
					process.env.DOCKER_MACHINE_NAME,
					'natpf1',
					'delete',
					rule[ 0 ]
				],
				{ stdio: 'inherit' }
			);
		}
	} );

	// Add our port forwarding rule.
	spawnSync(
		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' }
	);
}