2023-03-20 12:04:22 +00:00
|
|
|
<?php
|
|
|
|
|
2023-03-21 09:56:49 +00:00
|
|
|
use App\Actions\RemoteProcess\DispatchRemoteProcess;
|
2023-03-21 10:32:38 +00:00
|
|
|
use App\Data\RemoteProcessArgs;
|
2023-03-29 10:52:22 +00:00
|
|
|
use App\Enums\ActivityTypes;
|
2023-03-24 14:48:57 +00:00
|
|
|
use App\Models\Server;
|
2023-03-29 10:27:02 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2023-03-30 13:52:19 +00:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2023-03-31 07:42:05 +00:00
|
|
|
use Illuminate\Support\Facades\Process;
|
2023-03-24 21:15:36 +00:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
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
|
|
|
/**
|
2023-03-24 21:15:36 +00:00
|
|
|
* Run a Remote Process, which SSH's asynchronously into a machine to run the command(s).
|
2023-03-21 10:32:38 +00:00
|
|
|
* @TODO Change 'root' to 'coolify' when it's able to run Docker commands without sudo
|
2023-03-20 12:04:22 +00:00
|
|
|
*
|
|
|
|
*/
|
2023-03-21 10:32:38 +00:00
|
|
|
function remoteProcess(
|
2023-03-29 10:27:02 +00:00
|
|
|
array $command,
|
|
|
|
Server $server,
|
2023-03-31 11:32:07 +00:00
|
|
|
?string $deployment_uuid = null,
|
|
|
|
?Model $model = null,
|
2023-03-24 14:48:57 +00:00
|
|
|
): Activity {
|
2023-03-31 16:54:55 +00:00
|
|
|
|
2023-03-29 10:27:02 +00:00
|
|
|
$command_string = implode("\n", $command);
|
2023-03-31 16:54:55 +00:00
|
|
|
|
2023-03-29 10:27:02 +00:00
|
|
|
// @TODO: Check if the user has access to this server
|
|
|
|
// checkTeam($server->team_id);
|
2023-03-24 21:15:36 +00:00
|
|
|
|
2023-04-04 12:11:53 +00:00
|
|
|
$private_key_location = savePrivateKeyForServer($server);
|
2023-03-29 10:27:02 +00:00
|
|
|
|
2023-03-21 09:56:49 +00:00
|
|
|
return resolve(DispatchRemoteProcess::class, [
|
2023-03-21 10:32:38 +00:00
|
|
|
'remoteProcessArgs' => new RemoteProcessArgs(
|
2023-03-29 10:27:02 +00:00
|
|
|
server_ip: $server->ip,
|
2023-03-24 21:15:36 +00:00
|
|
|
private_key_location: $private_key_location,
|
2023-03-31 11:32:07 +00:00
|
|
|
deployment_uuid: $deployment_uuid,
|
2023-03-28 13:47:37 +00:00
|
|
|
command: <<<EOT
|
2023-03-29 10:27:02 +00:00
|
|
|
{$command_string}
|
2023-03-28 13:47:37 +00:00
|
|
|
EOT,
|
2023-03-29 10:27:02 +00:00
|
|
|
port: $server->port,
|
|
|
|
user: $server->user,
|
2023-03-31 11:32:07 +00:00
|
|
|
type: $deployment_uuid ? ActivityTypes::DEPLOYMENT->value : ActivityTypes::REMOTE_PROCESS->value,
|
2023-03-31 16:54:55 +00:00
|
|
|
model: $model,
|
2023-03-21 10:32:38 +00:00
|
|
|
),
|
2023-03-21 09:31:16 +00:00
|
|
|
])();
|
2023-03-20 12:04:22 +00:00
|
|
|
}
|
2023-03-30 13:52:19 +00:00
|
|
|
}
|
|
|
|
|
2023-03-31 11:32:07 +00:00
|
|
|
// 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.');
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2023-04-04 12:11:53 +00:00
|
|
|
if (!function_exists('savePrivateKeyForServer')) {
|
|
|
|
function savePrivateKeyForServer(Server $server)
|
2023-03-30 13:52:19 +00:00
|
|
|
{
|
|
|
|
$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')) {
|
2023-03-31 10:26:56 +00:00
|
|
|
function generateSshCommand(string $private_key_location, string $server_ip, string $user, string $port, string $command, bool $isMux = false)
|
2023-03-30 13:52:19 +00:00
|
|
|
{
|
|
|
|
$delimiter = 'EOF-COOLIFY-SSH';
|
|
|
|
Storage::disk('local')->makeDirectory('.ssh');
|
2023-03-31 06:36:17 +00:00
|
|
|
$ssh_command = "ssh ";
|
|
|
|
if ($isMux) {
|
|
|
|
$ssh_command .= '-o ControlMaster=auto -o ControlPersist=1m -o ControlPath=/var/www/html/storage/app/.ssh/ssh_mux_%h_%p_%r ';
|
|
|
|
}
|
|
|
|
$ssh_command .= "-i {$private_key_location} "
|
2023-03-30 13:52:19 +00:00
|
|
|
. '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null '
|
|
|
|
. '-o PasswordAuthentication=no '
|
|
|
|
. '-o RequestTTY=no '
|
|
|
|
. '-o LogLevel=ERROR '
|
|
|
|
. "-p {$port} "
|
|
|
|
. "{$user}@{$server_ip} "
|
|
|
|
. " 'bash -se' << \\$delimiter" . PHP_EOL
|
|
|
|
. $command . PHP_EOL
|
|
|
|
. $delimiter;
|
2023-03-31 06:36:17 +00:00
|
|
|
|
2023-03-30 13:52:19 +00:00
|
|
|
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
|
2023-03-28 13:47:37 +00:00
|
|
|
{
|
2023-03-30 13:52:19 +00:00
|
|
|
$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];
|
2023-03-24 21:15:36 +00:00
|
|
|
}
|
2023-03-20 12:04:22 +00:00
|
|
|
}
|
2023-03-31 07:42:05 +00:00
|
|
|
if (!function_exists('runRemoteCommandSync')) {
|
2023-04-04 12:11:53 +00:00
|
|
|
function runRemoteCommandSync($server, array $command)
|
|
|
|
{
|
2023-03-31 07:42:05 +00:00
|
|
|
$command_string = implode("\n", $command);
|
2023-04-04 12:11:53 +00:00
|
|
|
$private_key_location = savePrivateKeyForServer($server);
|
2023-03-31 07:42:05 +00:00
|
|
|
$ssh_command = generateSshCommand($private_key_location, $server->ip, $server->user, $server->port, $command_string);
|
|
|
|
$process = Process::run($ssh_command);
|
|
|
|
$output = trim($process->output());
|
|
|
|
$exitCode = $process->exitCode();
|
|
|
|
if ($exitCode !== 0) {
|
|
|
|
Log::error($output);
|
|
|
|
throw new \RuntimeException('There was an error running the command.');
|
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
}
|