-
Notifications
You must be signed in to change notification settings - Fork 0
feat(server): add server:optimize command with hardware-based PHP tuning #78
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Bigpixelrocket\DeployerPHP\Console\Server; | ||
|
|
||
| use Bigpixelrocket\DeployerPHP\Contracts\BaseCommand; | ||
| use Bigpixelrocket\DeployerPHP\Traits\PlaybooksTrait; | ||
| use Bigpixelrocket\DeployerPHP\Traits\ServersTrait; | ||
| use Symfony\Component\Console\Attribute\AsCommand; | ||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Input\InputOption; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
|
|
||
| #[AsCommand( | ||
| name: 'server:optimize', | ||
| description: 'Optimize PHP-FPM and OPcache settings based on server hardware' | ||
| )] | ||
| class ServerOptimizeCommand extends BaseCommand | ||
| { | ||
| use PlaybooksTrait; | ||
| use ServersTrait; | ||
|
|
||
| // ---- | ||
| // Configuration | ||
| // ---- | ||
|
|
||
| protected function configure(): void | ||
| { | ||
| parent::configure(); | ||
|
|
||
| $this->addOption('server', null, InputOption::VALUE_REQUIRED, 'Server name'); | ||
| } | ||
|
|
||
| // ---- | ||
| // Execution | ||
| // ---- | ||
|
|
||
| protected function execute(InputInterface $input, OutputInterface $output): int | ||
| { | ||
| parent::execute($input, $output); | ||
|
|
||
| $this->heading('Optimize Server'); | ||
|
|
||
| // | ||
| // Select server & display details | ||
| // ---- | ||
|
|
||
| $server = $this->selectServer(); | ||
|
|
||
| if (is_int($server)) { | ||
| return $server; | ||
| } | ||
|
|
||
| $this->displayServerDeets($server); | ||
|
|
||
| // | ||
| // Get server info (includes hardware detection) | ||
| // ---- | ||
|
|
||
| $info = $this->getServerInfo($server); | ||
|
|
||
| if (is_int($info)) { | ||
| return $info; | ||
| } | ||
|
|
||
| // Extract hardware info | ||
| if (!isset($info['hardware']) || !is_array($info['hardware'])) { | ||
| $this->io->error('Server hardware information not available. Run server:install first.'); | ||
|
|
||
| return Command::FAILURE; | ||
| } | ||
|
|
||
| $hardware = $info['hardware']; | ||
| $cpuCores = $hardware['cpu_cores'] ?? '1'; | ||
| $ramMb = $hardware['ram_mb'] ?? '512'; | ||
| $diskType = $hardware['disk_type'] ?? 'hdd'; | ||
|
|
||
| /** @var string $cpuCores */ | ||
| /** @var string $ramMb */ | ||
| /** @var string $diskType */ | ||
|
|
||
| $permissions = $info['permissions'] ?? 'none'; | ||
| /** @var string $permissions */ | ||
|
|
||
| // | ||
| // Display hardware summary | ||
| // ---- | ||
|
|
||
| $this->io->writeln([ | ||
| '', | ||
| '<fg=cyan>Hardware Configuration:</>', | ||
| " CPU Cores: <fg=yellow>{$cpuCores}</>", | ||
| " RAM: <fg=yellow>{$ramMb}MB</>", | ||
| " Disk: <fg=yellow>{$diskType}</>", | ||
| '', | ||
| ]); | ||
|
|
||
| // | ||
| // Confirm optimization | ||
| // ---- | ||
|
|
||
| $confirmed = $this->io->promptConfirm( | ||
| 'Apply hardware-optimized settings to PHP-FPM and OPcache?', | ||
| default: true | ||
| ); | ||
|
|
||
| if (!$confirmed) { | ||
| $this->io->info('Optimization cancelled'); | ||
|
|
||
| return Command::SUCCESS; | ||
| } | ||
|
|
||
| // | ||
| // Execute optimization playbook | ||
| // ---- | ||
|
|
||
| $result = $this->executePlaybook( | ||
| $server, | ||
| 'server-optimize', | ||
| 'Optimizing server...', | ||
| [ | ||
| 'DEPLOYER_PERMS' => $permissions, | ||
| 'DEPLOYER_CPU_CORES' => $cpuCores, | ||
| 'DEPLOYER_RAM_MB' => $ramMb, | ||
| 'DEPLOYER_DISK_TYPE' => $diskType, | ||
| ], | ||
| true | ||
| ); | ||
|
|
||
| if (is_int($result)) { | ||
| $this->io->error('Server optimization failed'); | ||
|
|
||
| return $result; | ||
| } | ||
|
|
||
| $this->yay('Server optimization completed successfully'); | ||
|
|
||
| // | ||
| // Display applied settings | ||
| // ---- | ||
|
|
||
| if (isset($result['php_fpm_settings']) && is_string($result['php_fpm_settings'])) { | ||
| $this->io->writeln([ | ||
| '', | ||
| '<fg=cyan>Applied Settings:</>', | ||
| " PHP-FPM: <fg=yellow>{$result['php_fpm_settings']}</>", | ||
| ]); | ||
| } | ||
|
|
||
| if (isset($result['opcache_settings']) && is_string($result['opcache_settings'])) { | ||
| $this->io->writeln([ | ||
| " OPcache: <fg=yellow>{$result['opcache_settings']}</>", | ||
| '', | ||
| ]); | ||
| } | ||
|
|
||
| // | ||
| // Show command replay | ||
| // ---- | ||
|
|
||
| $this->showCommandReplay('server:optimize', [ | ||
| 'server' => $server->name, | ||
| ]); | ||
|
|
||
| return Command::SUCCESS; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove duplicate hardware display.
The hardware information is already displayed by
getServerInfo()at line 62, which callsdisplayServerInfo()in ServersTrait. The second display at lines 91-98 shows the same information in a different format, creating redundant console output.Apply this diff to remove the duplicate display:
📝 Committable suggestion
🤖 Prompt for AI Agents