-
Notifications
You must be signed in to change notification settings - Fork 0
feat: server info command #62
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c5ffaf4
feat(playbooks): add reusable playbook execution infrastructure
loadinglucian 917a209
feat(server): add server info command
loadinglucian 0f6d7d4
style(console): add spacing after server/site details display
loadinglucian 8b332b3
docs(playbooks): document YAML output and error handling patterns
loadinglucian 9d2281d
chore(deps): add bash formatting scripts to composer and npm
loadinglucian 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,3 +16,7 @@ indent_size = 2 | |
|
|
||
| [docker-compose.yml] | ||
| indent_size = 4 | ||
|
|
||
| [*.sh] | ||
| indent_style = tab | ||
| indent_size = 0 | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Bigpixelrocket\DeployerPHP\Console\Server; | ||
|
|
||
| use Bigpixelrocket\DeployerPHP\Contracts\BaseCommand; | ||
| use Bigpixelrocket\DeployerPHP\Traits\ServerHelpersTrait; | ||
| use Bigpixelrocket\DeployerPHP\Traits\ServerInfoTrait; | ||
| 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:info', description: 'Display server information')] | ||
| class ServerInfoCommand extends BaseCommand | ||
| { | ||
| use ServerHelpersTrait; | ||
| use ServerInfoTrait; | ||
|
|
||
| // | ||
| // 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->io->hr(); | ||
| $this->io->h1('Server Information'); | ||
|
|
||
| // | ||
| // Select server | ||
|
|
||
| $server = $this->selectServer(); | ||
|
|
||
| if (is_int($server)) { | ||
| return $server; | ||
| } | ||
|
|
||
| // Get sites for this server | ||
| $serverSites = $this->sites->findByServer($server->name); | ||
|
|
||
| // | ||
| // Display server details | ||
|
|
||
| $this->io->hr(); | ||
|
|
||
| $this->displayServerDeets($server, $serverSites); | ||
|
|
||
| // | ||
| // Display server information | ||
|
|
||
| $info = $this->getServerInfo($server); | ||
|
|
||
| if (is_int($info)) { | ||
| return $info; | ||
| } | ||
|
|
||
| $this->displayServerInfo($info); | ||
|
|
||
| // | ||
| // Show command hint | ||
|
|
||
| $this->io->showCommandHint('server:info', [ | ||
| '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
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,123 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Bigpixelrocket\DeployerPHP\Traits; | ||
|
|
||
| use Bigpixelrocket\DeployerPHP\Container; | ||
| use Bigpixelrocket\DeployerPHP\DTOs\ServerDTO; | ||
| use Bigpixelrocket\DeployerPHP\Services\FilesystemService; | ||
| use Bigpixelrocket\DeployerPHP\Services\IOService; | ||
| use Bigpixelrocket\DeployerPHP\Services\SSHService; | ||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Yaml\Yaml; | ||
|
|
||
| /** | ||
| * Reusable helpers for executing playbooks on remote servers. | ||
| * | ||
| * Requires classes using this trait to have Container, IOService, SSHService, and FilesystemService properties. | ||
| * | ||
| * @property Container $container | ||
| * @property FilesystemService $fs | ||
| * @property IOService $io | ||
| * @property SSHService $ssh | ||
| */ | ||
| trait PlaybookHelpersTrait | ||
| { | ||
| use KeyHelpersTrait; | ||
|
|
||
| /** | ||
| * Execute a playbook on a server. | ||
| * | ||
| * Handles SSH execution, error display, and YAML parsing. | ||
| * Displays errors via IOService and returns Command::FAILURE on any error. | ||
| * | ||
| * @param string $playbookName Playbook name without .sh extension (e.g., 'server-info', 'install-php', etc) | ||
| * @param array<string, string> $playbookVars Playbook variables to pass to the playbook (don't pass sensitive data) | ||
| * @return array<string, mixed>|int Returns parsed YAML on success or Command::FAILURE on error | ||
| */ | ||
| protected function executePlaybook( | ||
| ServerDTO $server, | ||
| string $playbookName, | ||
| string $spinnerMessage, | ||
| array $playbookVars = [] | ||
| ): array|int { | ||
| $projectRoot = dirname(__DIR__, 2); | ||
| $playbookPath = $projectRoot . '/playbooks/' . $playbookName . '.sh'; | ||
| $scriptContents = $this->fs->readFile($playbookPath); | ||
|
|
||
| // Build variable prefix | ||
| $varsPrefix = ''; | ||
| foreach ($playbookVars as $key => $value) { | ||
| $varsPrefix .= sprintf('%s=%s ', $key, escapeshellarg((string) $value)); | ||
| } | ||
|
|
||
| // Wrap script with environment and heredoc | ||
| $scriptWithVars = sprintf( | ||
| "%sbash <<'DEPLOYER_SCRIPT_EOF'\n%s\nDEPLOYER_SCRIPT_EOF", | ||
| $varsPrefix, | ||
| $scriptContents | ||
| ); | ||
|
|
||
| // Resolve SSH key path | ||
| $privateKeyPath = $this->resolvePrivateKeyPath($server->privateKeyPath); | ||
|
|
||
| if ($privateKeyPath === null) { | ||
| throw new \RuntimeException('No valid SSH private key found'); | ||
| } | ||
|
|
||
| // Execute command | ||
| try { | ||
| $result = $this->io->promptSpin( | ||
| callback: fn () => $this->ssh->executeCommand( | ||
| $server->host, | ||
| $server->port, | ||
| $server->username, | ||
| $scriptWithVars, | ||
| $privateKeyPath | ||
| ), | ||
| message: $spinnerMessage | ||
| ); | ||
| } catch (\RuntimeException $e) { | ||
| $this->io->error($e->getMessage()); | ||
|
|
||
| return Command::FAILURE; | ||
| } | ||
|
|
||
| // Check exit code | ||
| if ($result['exit_code'] !== 0) { | ||
| $this->io->error('Playbook execution failed:'); | ||
| $this->io->writeln([ | ||
| '', | ||
| '<fg=red>'.$result['output'].'</>', | ||
| '', | ||
| ]); | ||
|
|
||
| return Command::FAILURE; | ||
| } | ||
|
|
||
| // Parse YAML output | ||
| try { | ||
| $parsed = Yaml::parse($result['output']); | ||
|
|
||
| if (!is_array($parsed)) { | ||
| throw new \RuntimeException('Expected playbook output to be YAML array'); | ||
| } | ||
|
|
||
| /** @var array<string, mixed> $parsed */ | ||
| return $parsed; | ||
| } catch (\Throwable $e) { | ||
| $this->io->error('Failed to parse YAML output: ' . $e->getMessage()); | ||
| $this->io->writeln([ | ||
| '', | ||
| '<fg=yellow>Raw output:</>', | ||
| '', | ||
| $result['output'], | ||
| '', | ||
| ]); | ||
|
|
||
| return Command::FAILURE; | ||
| } | ||
| } | ||
|
|
||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.