add lifecycle hook support for install events

This commit is contained in:
robert dennis
2026-06-05 12:25:30 -04:00
parent c227a20f32
commit 2f01a047df
4 changed files with 206 additions and 0 deletions
@@ -0,0 +1,68 @@
<?php
namespace Pterodactyl\Console\Commands\Environment\Addons;
use Illuminate\Support\Str;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Process;
class RunHooksCommand extends Command
{
protected $signature = 'p:environment:addons:run-hooks
{event : The lifecycle event to run hooks for (e.g. post-install).}';
protected $description = 'Execute addon lifecycle hook scripts for the given event.';
/**
* Runs every executable "addons/<name>/hooks/<event>" script for the given lifecycle event when addon hooks are enabled.
*/
public function handle(): int
{
if (!config('addons.hooks_enabled')) {
return self::SUCCESS;
}
$event = $this->argument('event');
if (!Str::isMatch('/^[a-z0-9-]+$/', $event)) {
$this->components->error("Invalid hook event name: {$event}");
return self::INVALID;
}
$hooks = Collection::make(File::glob(base_path("addons/*/hooks/{$event}")) ?: [])
->filter(fn (string $hook) => is_executable($hook))
->values();
if ($hooks->isEmpty()) {
return self::SUCCESS;
}
if ($this->input->isInteractive() && !$this->confirm(
sprintf('Execute %d addon hook script(s) for the "%s" event? They run with the privileges of this process.', $hooks->count(), $event)
)) {
return self::SUCCESS;
}
$hooks->each($this->runHook(...));
return self::SUCCESS;
}
/**
* Streams a single hook's output, reporting a non-zero exit without aborting the remaining hooks.
*/
private function runHook(string $hook): void
{
$this->components->info("Running addon hook: {$hook}");
$result = Process::path(base_path())
->forever()
->run([$hook], fn (string $type, string $output) => $this->output->write($output));
if ($result->failed()) {
$this->components->warn("Addon hook exited with an error: {$hook}");
}
}
}
+3
View File
@@ -87,6 +87,9 @@
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi || true"
],
"post-install-cmd": [
"@php artisan p:environment:addons:run-hooks post-install || true"
],
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
+17
View File
@@ -0,0 +1,17 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| Addon Lifecycle Hooks
|--------------------------------------------------------------------------
|
| When enabled, the Panel executes the hook scripts that addons place under
| "addons/<name>/hooks/<event>" during lifecycle events such as post-install
| (see the p:environment:addons:run-hooks command). These scripts run with
| the privileges of the invoking process — often root during an upgrade — so
| only enable this if you trust every installed addon.
|
*/
'hooks_enabled' => env('ADDONS_HOOKS_ENABLED', false),
];
@@ -0,0 +1,118 @@
<?php
namespace Pterodactyl\Tests\Unit\Console\Commands\Environment\Addons;
use Illuminate\Console\Command;
use Pterodactyl\Tests\TestCase;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Process;
class RunHooksCommandTest extends TestCase
{
protected function tearDown(): void
{
File::deleteDirectory(base_path('addons'));
parent::tearDown();
}
public function testHooksAreSkippedWhenDisabled(): void
{
config(['addons.hooks_enabled' => false]);
Process::fake();
$this->makeHook('example');
$this->runHooks()->assertExitCode(Command::SUCCESS);
Process::assertNothingRan();
}
public function testExecutableHooksRunForEvent(): void
{
config(['addons.hooks_enabled' => true]);
Process::fake();
$first = $this->makeHook('alpha');
$second = $this->makeHook('beta');
$this->runHooks()->assertExitCode(Command::SUCCESS);
Process::assertRan(fn ($process) => $process->command === [$first]);
Process::assertRan(fn ($process) => $process->command === [$second]);
}
public function testNonExecutableFilesAreIgnored(): void
{
config(['addons.hooks_enabled' => true]);
Process::fake();
$executable = $this->makeHook('alpha');
$ignored = $this->makeHook('beta', executable: false);
$this->runHooks()->assertExitCode(Command::SUCCESS);
Process::assertRan(fn ($process) => $process->command === [$executable]);
Process::assertDidntRun(fn ($process) => $process->command === [$ignored]);
}
public function testInvalidEventNameIsRejected(): void
{
config(['addons.hooks_enabled' => true]);
Process::fake();
$this->runHooks('BadEvent')
->expectsOutputToContain('Invalid hook event name')
->assertExitCode(Command::INVALID);
Process::assertNothingRan();
}
public function testFailingHookDoesNotAbortRemainingHooks(): void
{
config(['addons.hooks_enabled' => true]);
$failing = $this->makeHook('broken');
$passing = $this->makeHook('healthy');
Process::fake([
'*broken*' => Process::result(exitCode: 3),
'*' => Process::result(),
]);
$this->runHooks()
->expectsOutputToContain('exited with an error')
->assertExitCode(Command::SUCCESS);
Process::assertRan(fn ($process) => $process->command === [$failing]);
Process::assertRan(fn ($process) => $process->command === [$passing]);
}
public function testDeclinedConfirmationSkipsHooks(): void
{
config(['addons.hooks_enabled' => true]);
Process::fake();
$this->makeHook('example');
$this->artisan('p:environment:addons:run-hooks', ['event' => 'post-install'])
->expectsConfirmation('Execute 1 addon hook script(s) for the "post-install" event? They run with the privileges of this process.', 'no')
->assertExitCode(Command::SUCCESS);
Process::assertNothingRan();
}
private function runHooks(string $event = 'post-install'): \Illuminate\Testing\PendingCommand
{
return $this->artisan('p:environment:addons:run-hooks', ['event' => $event, '--no-interaction' => true]);
}
private function makeHook(string $addon, string $event = 'post-install', bool $executable = true): string
{
$path = base_path("addons/{$addon}/hooks/{$event}");
File::ensureDirectoryExists(dirname($path));
File::put($path, "#!/usr/bin/env bash\nexit 0\n");
if ($executable) {
chmod($path, 0o755);
}
return $path;
}
}