lasthourcloud/bootstrap/helpers.php

36 lines
1.2 KiB
PHP
Raw Normal View History

2023-03-20 12:04:22 +00:00
<?php
use App\Actions\RemoteProcess\DispatchRemoteProcess;
use App\Data\RemoteProcessArgs;
2023-03-24 14:48:57 +00:00
use App\Models\Server;
2023-03-20 12:04:22 +00:00
use Spatie\Activitylog\Contracts\Activity;
2023-03-24 14:48:57 +00:00
if (!function_exists('remoteProcess')) {
2023-03-20 12:04:22 +00:00
/**
* Run a Coolify Process, which SSH's asynchronously into a machine to run the command(s).
* @TODO Change 'root' to 'coolify' when it's able to run Docker commands without sudo
2023-03-20 12:04:22 +00:00
*
*/
function remoteProcess(
string $command,
2023-03-24 14:48:57 +00:00
string $destination
): Activity {
$found_server = Server::where('name', $destination)->first();
if (!$found_server) {
throw new \RuntimeException('Server not found.');
}
$found_team = auth()->user()->teams->pluck('id')->contains($found_server->team_id);
if (!$found_team) {
throw new \RuntimeException('You do not have access to this server.');
}
return resolve(DispatchRemoteProcess::class, [
'remoteProcessArgs' => new RemoteProcessArgs(
2023-03-24 14:48:57 +00:00
destination: $found_server->ip,
command: $command,
2023-03-24 14:48:57 +00:00
port: $found_server->port,
user: $found_server->user,
),
])();
2023-03-20 12:04:22 +00:00
}
}