vito/app/Actions/Site/ExecuteCommand.php
Dimitar Yanakiev 97e20206e8
ite Commands (#298) (#519)
* feat: Implement Site Commands (#298)

- Introduced a Commands widget/table for Site view, allowing users to create, edit, delete, and execute commands.
- Each Site Type now has a predefined set of commands inserted upon site creation.
- A migration script ensures commands are created for existing sites.
- Implemented necessary policies for command management.
- Added feature tests to validate functionality.

* I'm trying to fix the tests, but it seems like it might not work. I'm having trouble running the tests locally for some reason.

* I'm trying to fix the tests, but it seems like it might not work. I'm having trouble running the tests locally for some reason.

* I'm trying to fix the tests, but it seems like it might not work. I'm having trouble running the tests locally for some reason.

* I'm trying to fix the tests, but it seems like it might not work. I'm having trouble running the tests locally for some reason.

* Remove feature tests for commands due to inconsistencies for now

* fixes

* add tests for commands

* ui fix and add to wordpress

---------

Co-authored-by: Saeed Vaziry <mr.saeedvaziry@gmail.com>
2025-03-02 10:43:26 +01:00

59 lines
1.7 KiB
PHP

<?php
namespace App\Actions\Site;
use App\Enums\CommandExecutionStatus;
use App\Models\Command;
use App\Models\CommandExecution;
use App\Models\ServerLog;
use App\Models\User;
class ExecuteCommand
{
public function execute(Command $command, User $user, array $input): CommandExecution
{
$execution = new CommandExecution([
'command_id' => $command->id,
'server_id' => $command->site->server_id,
'user_id' => $user->id,
'variables' => $input['variables'] ?? [],
'status' => CommandExecutionStatus::EXECUTING,
]);
$execution->save();
dispatch(function () use ($execution, $command) {
$content = $execution->getContent();
$log = ServerLog::make($execution->server, 'command-'.$command->id.'-'.strtotime('now'));
$log->save();
$execution->server_log_id = $log->id;
$execution->save();
$execution->server->os()->runScript(
path: $command->site->path,
script: $content,
user: $command->site->user,
serverLog: $log,
variables: $execution->variables
);
$execution->status = CommandExecutionStatus::COMPLETED;
$execution->save();
})->catch(function () use ($execution) {
$execution->status = CommandExecutionStatus::FAILED;
$execution->save();
})->onConnection('ssh');
return $execution;
}
public static function rules(array $input): array
{
return [
'variables' => 'array',
'variables.*' => [
'required',
'string',
'max:255',
],
];
}
}