Skip to content

Commit

Permalink
Core as volume mount, plugins and themes as volume binds
Browse files Browse the repository at this point in the history
  • Loading branch information
Luc45 committed Mar 26, 2024
1 parent 90e7bfe commit d7fa041
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 122 deletions.
Binary file modified qit
Binary file not shown.
3 changes: 0 additions & 3 deletions src/src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,6 @@ public function init_cache(): void {
* @throws \RuntimeException When could not write to the cache file.
*/
protected function save(): void {
if ( getenv( 'QIT_INTERNAL' ) ) {
return;
}
$written = file_put_contents( $this->cache_file_path, json_encode( $this->cache ) );

if ( ! $written ) {
Expand Down
51 changes: 0 additions & 51 deletions src/src/Commands/CustomTests/ExtensionDownloaderCommand.php

This file was deleted.

4 changes: 0 additions & 4 deletions src/src/Environment/EnvironmentDanglingCleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ protected function debug_output( string $message ): void {
}

public function cleanup_dangling(): void {
if ( getenv( 'QIT_INTERNAL' ) ) {
return;
}

$this->remove_dangling_environments();
$this->detect_dangling_containers_exited();
$this->detect_dangling_containers_running();
Expand Down
46 changes: 42 additions & 4 deletions src/src/Environment/Environments/E2E/E2EEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected function post_up(): void {
}

// Copy mu-plugins.
$this->docker->run_inside_docker( $this->env_info, [ '/bin/bash', '-c', 'mkdir -p /var/www/html/wp-content/mu-plugins && cp /qit/mu-plugins/* /var/www/html/wp-content/mu-plugins 2>&1' ], [], '82:82' );
$this->docker->run_inside_docker( $this->env_info, [ '/bin/bash', '-c', 'mkdir -p /var/www/html/wp-content/mu-plugins && cp /qit/mu-plugins/* /var/www/html/wp-content/mu-plugins 2>&1' ] );

// Setup WordPress.
$this->output->writeln( '<info>Setting up WordPress...</info>' );
Expand All @@ -83,7 +83,7 @@ protected function post_up(): void {
'THEMES_TO_INSTALL' => json_encode( $this->env_info->themes ),
'SITE_URL' => $this->env_info->site_url,
'QIT_DOCKER_REDIS' => $this->env_info->object_cache ? 'yes' : 'no',
],'82:82' );
] );

// Activate plugins.
if ( ! $this->skip_activating_plugins ) {
Expand Down Expand Up @@ -177,8 +177,46 @@ protected function get_generate_docker_compose_envs(): array {
protected function additional_default_volumes( array $default_volumes ): array {
// Create a named docker volume.
$named_volume = sprintf( 'qit_env_volume_%s', $this->env_info->env_id );
$process = new Process( [ App::make( Docker::class )->find_docker(), 'volume', 'create', $named_volume ] );
$process->run();
$process = new Process( [
App::make( Docker::class )->find_docker(),
'volume',
'create',
'--driver',
'local',
$named_volume,
] );
if ( $this->output->isVerbose() ) {
$this->output->writeln( $process->getCommandLine() );
}
$process->mustRun( function ( $type, $buffer ) {
if ( $this->output->isVerbose() ) {
$this->output->write( $buffer );
}
} );

$args = [
App::make( Docker::class )->find_docker(),
'run',
'--rm',
'--mount',
'src=' . $named_volume . ',dst=/var/www/html',
'busybox',
'sh',
'-c',
'mkdir -p /var/www/html/wp-content/plugins && mkdir -p /var/www/html/wp-content/themes && chown -R 82:82 /var/www/html',
];

/*
* Create "wp-content/plugins" and "wp-content/themes" directories mount binds have correct parent directory permissions.
* We make them owned by 82:82, which is the UID of "www-data" in our alpine PHP images.
* Once the container starts and the entrypoint is triggered, FixUID will map these to the runtime UID.
*/
$dirs_process = new Process( $args );
$dirs_process->mustRun( function ( $type, $buffer ) {
if ( $this->output->isVerbose() ) {
$this->output->write( $buffer );
}
} );

$default_volumes['/var/www/html'] = $named_volume;

Expand Down
63 changes: 16 additions & 47 deletions src/src/Environment/Environments/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,63 +128,19 @@ public function up( string $type = 'up' ): void {
$this->copy_environment();
$this->environment_monitor->environment_added_or_updated( $this->env_info );

$this->generate_docker_compose();
$this->post_generate_docker_compose();

if ( ! empty( $this->env_info->plugins ) || ! empty( $this->env_info->themes ) ) {
$this->output->writeln( '<info>Downloading plugins and themes...</info>' );
}

if ( ! file_exists( Config::get_qit_dir() . 'ext-cache' ) ) {
if ( ! mkdir( Config::get_qit_dir() . 'ext-cache', 0755 ) ) {
throw new \RuntimeException( 'Failed to create ext-cache directory on ' . Config::get_qit_dir() . 'ext-cache' );
}
}

$extension_downloader = new Process(
[
$this->docker->find_docker(),
'run',
'--rm',
'-v',
sprintf( "%s:/app/qit", \Phar::running() ? \Phar::running( false ) : QIT_ABSPATH ),
'-v',
"qit_env_volume_{$this->env_info->env_id}:/var/www/html",
'-v',
sprintf( '%s:/qit/home', Config::get_qit_dir() ),
'-v',
sprintf( '%s:/qit/home/ext-cache', $this->cache_dir ),
'-e',
'QIT_INTERNAL=1',
'-e',
'QIT_HOME=/qit/home',
'--network',
'host',
'automattic/qit-runner-php-7.4-fpm-alpine:latest',
'sh',
'-c',
sprintf( 'php %s internal:ext-download "%s" --json', \Phar::running() ? '/app/qit': '/app/qit/qit-cli.php', base64_encode( json_encode( $this->env_info ) ) ),
]
);

$extension_downloader->setTimeout( 600 );
$extension_downloader->setIdleTimeout( 600 );
$extension_downloader->setPty( false );
$extension_downloader->run( function ( $type, $buffer ) {
if ( $this->output->isVerbose() ) {
$this->output->write( $buffer );
}
} );

if ( ! $extension_downloader->isSuccessful() ) {
throw new \RuntimeException( "Failed to download extensions. Output: \n" . $extension_downloader->getOutput() . $extension_downloader->getErrorOutput() );
}
$this->extension_downloader->download( $this->env_info, $this->cache_dir, $this->env_info->plugins, $this->env_info->themes );

if ( $type === 'up_and_test' ) {
$this->custom_tests_downloader->download( $this->env_info, $this->cache_dir, $this->env_info->plugins, $this->env_info->themes );
}

$this->output->writeln( '<info>Setting up Docker...</info>' );
$this->generate_docker_compose();
$this->post_generate_docker_compose();
$this->up_docker_compose();
$this->post_up();

Expand Down Expand Up @@ -366,6 +322,19 @@ public static function down( EnvInfo $env_info, ?OutputInterface $output = null
$output->writeln( 'Failed to remove temporary environment: ' . $env_info->temporary_env );
}

// Remove volume.
$process = new Process( [
App::make( Docker::class )->find_docker(),
'volume',
'remove',
sprintf( 'qit_env_volume_%s', $env_info->env_id ),
] );
$process->run( function ( $type, $buffer ) use ( $output ) {
if ( $output->isVerbose() ) {
$output->write( $buffer );
}
} );

$environment_monitor->environment_stopped( $env_info );
}

Expand Down
12 changes: 3 additions & 9 deletions src/src/Environment/ExtensionDownload/ExtensionDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ public function download( EnvInfo $env_info, string $cache_dir, array $plugins =
App::make( $handler_type )->maybe_download_extensions( $e, $cache_dir );
}

$is_docker = file_exists( '/.dockerenv' );

foreach ( $extensions as $e ) {
if ( ! file_exists( $e->path ) ) {
throw new \RuntimeException( 'Download failed.' );
Expand All @@ -77,13 +75,9 @@ public function download( EnvInfo $env_info, string $cache_dir, array $plugins =

if ( is_file( $e->path ) ) {
// Extract zip to temp environment.
if ($is_docker) {
$this->extension_zip->extract_zip( $e->path, "/var/www/html/wp-content/{$e->type}s" );
} else {
$this->extension_zip->extract_zip( $e->path, "$env_info->temporary_env/html/wp-content/{$e->type}s" );
// Add a volume bind.
$env_info->volumes[ "/var/www/html/wp-content/{$e->type}s/{$e->extension_identifier}" ] = "$env_info->temporary_env/html/wp-content/{$e->type}s/{$e->extension_identifier}";
}
$this->extension_zip->extract_zip( $e->path, "$env_info->temporary_env/html/wp-content/{$e->type}s" );
// Add a volume bind.
$env_info->volumes[ "/var/www/html/wp-content/{$e->type}s/{$e->extension_identifier}" ] = "$env_info->temporary_env/html/wp-content/{$e->type}s/{$e->extension_identifier}";
} elseif ( is_dir( $e->path ) ) {
if ( ! getenv( 'QIT_ALLOW_WRITE' ) ) {
// Set it as read-only to prevent dev messing up their local copy inadvertently (default behavior).
Expand Down
4 changes: 0 additions & 4 deletions src/src/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,6 @@ public function filter( $in, $out, &$consumed, $closing ): int {
$application->add( $container->make( ShowReportCommand::class ) );
$application->add( $container->make( ScaffoldE2ECommand::class ) );

if ( getenv( 'QIT_INTERNAL' ) ) {
$application->add( $container->make( ExtensionDownloaderCommand::class ) );
}

if ( Config::is_development_mode() ) {
// Dynamically crete Mass Test run command.
$container->make( CreateMassTestCommands::class )->register_commands( $application );
Expand Down

0 comments on commit d7fa041

Please sign in to comment.