wip
This commit is contained in:
@@ -5,6 +5,8 @@ use App\Data\RemoteProcessArgs;
|
||||
use App\Enums\ActivityTypes;
|
||||
use App\Models\Server;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Spatie\Activitylog\Contracts\Activity;
|
||||
|
||||
@@ -24,9 +26,7 @@ if (!function_exists('remoteProcess')) {
|
||||
// @TODO: Check if the user has access to this server
|
||||
// checkTeam($server->team_id);
|
||||
|
||||
$temp_file = 'id.rsa_' . 'root' . '@' . $server->ip;
|
||||
Storage::disk('local')->put($temp_file, $server->privateKey->private_key, 'private');
|
||||
$private_key_location = '/var/www/html/storage/app/' . $temp_file;
|
||||
$private_key_location = savePrivateKey($server);
|
||||
|
||||
return resolve(DispatchRemoteProcess::class, [
|
||||
'remoteProcessArgs' => new RemoteProcessArgs(
|
||||
@@ -43,11 +43,72 @@ if (!function_exists('remoteProcess')) {
|
||||
),
|
||||
])();
|
||||
}
|
||||
function checkTeam(string $team_id)
|
||||
// function checkTeam(string $team_id)
|
||||
// {
|
||||
// $found_team = auth()->user()->teams->pluck('id')->contains($team_id);
|
||||
// if (!$found_team) {
|
||||
// throw new \RuntimeException('You do not have access to this server.');
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
if (!function_exists('savePrivateKey')) {
|
||||
function savePrivateKey(Server $server)
|
||||
{
|
||||
$found_team = auth()->user()->teams->pluck('id')->contains($team_id);
|
||||
if (!$found_team) {
|
||||
throw new \RuntimeException('You do not have access to this server.');
|
||||
}
|
||||
$temp_file = 'id.rsa_' . 'root' . '@' . $server->ip;
|
||||
Storage::disk('local')->put($temp_file, $server->privateKey->private_key, 'private');
|
||||
return '/var/www/html/storage/app/' . $temp_file;
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('generateSshCommand')) {
|
||||
function generateSshCommand(string $private_key_location, string $server_ip, string $user, string $port, $command)
|
||||
{
|
||||
$delimiter = 'EOF-COOLIFY-SSH';
|
||||
Storage::disk('local')->makeDirectory('.ssh');
|
||||
$ssh_command = "ssh "
|
||||
. "-i {$private_key_location} "
|
||||
. '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null '
|
||||
. '-o PasswordAuthentication=no '
|
||||
. '-o RequestTTY=no '
|
||||
. '-o LogLevel=ERROR '
|
||||
. '-o ControlMaster=auto -o ControlPersist=1m -o ControlPath=/var/www/html/storage/app/.ssh/ssh_mux_%h_%p_%r '
|
||||
. "-p {$port} "
|
||||
. "{$user}@{$server_ip} "
|
||||
. " 'bash -se' << \\$delimiter" . PHP_EOL
|
||||
. $command . PHP_EOL
|
||||
. $delimiter;
|
||||
return $ssh_command;
|
||||
}
|
||||
}
|
||||
if (!function_exists('formatDockerCmdOutputToJson')) {
|
||||
function formatDockerCmdOutputToJson($rawOutput): Collection
|
||||
{
|
||||
$outputLines = explode(PHP_EOL, $rawOutput);
|
||||
|
||||
return collect($outputLines)
|
||||
->reject(fn ($line) => empty($line))
|
||||
->map(fn ($outputLine) => json_decode($outputLine, true, flags: JSON_THROW_ON_ERROR));
|
||||
}
|
||||
}
|
||||
if (!function_exists('formatDockerLabelsToJson')) {
|
||||
function formatDockerLabelsToJson($rawOutput): Collection
|
||||
{
|
||||
$outputLines = explode(PHP_EOL, $rawOutput);
|
||||
|
||||
return collect($outputLines)
|
||||
->reject(fn ($line) => empty($line))
|
||||
->map(function ($outputLine) {
|
||||
$outputArray = explode(',', $outputLine);
|
||||
return collect($outputArray)
|
||||
->map(function ($outputLine) {
|
||||
return explode('=', $outputLine);
|
||||
})
|
||||
->mapWithKeys(function ($outputLine) {
|
||||
return [$outputLine[0] => $outputLine[1]];
|
||||
});
|
||||
})[0];
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user