mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-01 05:56:16 +00:00
- 2.x - scripts
This commit is contained in:
@ -7,30 +7,28 @@
|
||||
use App\Models\ScriptExecution;
|
||||
use App\Models\Server;
|
||||
use App\Models\ServerLog;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class ExecuteScript
|
||||
{
|
||||
public function execute(Script $script, Server $server, array $input): ScriptExecution
|
||||
public function execute(Script $script, array $input): ScriptExecution
|
||||
{
|
||||
$this->validate($server, $input);
|
||||
|
||||
$execution = new ScriptExecution([
|
||||
'script_id' => $script->id,
|
||||
'server_id' => $input['server'],
|
||||
'user' => $input['user'],
|
||||
'variables' => $input['variables'] ?? [],
|
||||
'status' => ScriptExecutionStatus::EXECUTING,
|
||||
]);
|
||||
$execution->save();
|
||||
|
||||
dispatch(function () use ($execution, $server, $script) {
|
||||
dispatch(function () use ($execution, $script) {
|
||||
$content = $execution->getContent();
|
||||
$log = ServerLog::make($server, 'script-'.$script->id.'-'.strtotime('now'));
|
||||
$log = ServerLog::make($execution->server, 'script-'.$script->id.'-'.strtotime('now'));
|
||||
$log->save();
|
||||
$execution->server_log_id = $log->id;
|
||||
$execution->save();
|
||||
$server->os()->runScript('~/', $content, $log, $execution->user);
|
||||
$execution->server->os()->runScript('~/', $content, $log, $execution->user);
|
||||
$execution->status = ScriptExecutionStatus::COMPLETED;
|
||||
$execution->save();
|
||||
})->catch(function () use ($execution) {
|
||||
@ -41,14 +39,23 @@ public function execute(Script $script, Server $server, array $input): ScriptExe
|
||||
return $execution;
|
||||
}
|
||||
|
||||
private function validate(Server $server, array $input): void
|
||||
public static function rules(array $input): array
|
||||
{
|
||||
Validator::make($input, [
|
||||
if (isset($input['server'])) {
|
||||
/** @var ?Server $server */
|
||||
$server = Server::query()->find($input['server']);
|
||||
}
|
||||
|
||||
return [
|
||||
'server' => [
|
||||
'required',
|
||||
Rule::exists('servers', 'id'),
|
||||
],
|
||||
'user' => [
|
||||
'required',
|
||||
Rule::in([
|
||||
'root',
|
||||
$server->ssh_user,
|
||||
isset($server) ? $server?->ssh_user : null,
|
||||
]),
|
||||
],
|
||||
'variables' => 'array',
|
||||
@ -57,6 +64,6 @@ private function validate(Server $server, array $input): void
|
||||
'string',
|
||||
'max:255',
|
||||
],
|
||||
])->validate();
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user