fix: boarding

fix: error handling
fix: restarting state
This commit is contained in:
Andras Bacsai
2023-09-15 15:34:25 +02:00
parent fcf7c5ddd5
commit da4c2ee60f
71 changed files with 326 additions and 290 deletions

View File

@@ -59,6 +59,18 @@ function format_docker_envs_to_json($rawOutput)
return collect([]);
}
}
function checkMinimumDockerEngineVersion($dockerVersion) {
$majorDockerVersion = Str::of($dockerVersion)->before('.')->value();
if ($majorDockerVersion <= 22) {
$dockerVersion = null;
}
return $dockerVersion;
}
function executeInDocker(string $containerId, string $command)
{
return "docker exec {$containerId} bash -c '{$command}'";
// return "docker exec {$this->deployment_uuid} bash -c '{$command} |& tee -a /proc/1/fd/1; [ \$PIPESTATUS -eq 0 ] || exit \$PIPESTATUS'";
}
function getApplicationContainerStatus(Application $application) {
$server = data_get($application,'destination.server');

View File

@@ -36,12 +36,10 @@ function remote_process(
return resolve(PrepareCoolifyTask::class, [
'remoteProcessArgs' => new CoolifyTaskArgs(
server_ip: $server->ip,
server_uuid: $server->uuid,
command: <<<EOT
{$command_string}
EOT,
port: $server->port,
user: $server->user,
type: $type,
type_uuid: $type_uuid,
model: $model,
@@ -66,15 +64,14 @@ function addPrivateKeyToSshAgent(Server $server)
Storage::disk('ssh-keys')->makeDirectory('.');
Storage::disk('ssh-mux')->makeDirectory('.');
Storage::disk('ssh-keys')->put($sshKeyFileLocation, $server->privateKey->private_key);
return '/var/www/html/storage/app/ssh/keys/' . $sshKeyFileLocation;
$location = '/var/www/html/storage/app/ssh/keys/' . $sshKeyFileLocation;
return $location;
}
function generateSshCommand(string $server_ip, string $user, string $port, string $command, bool $isMux = true)
function generateSshCommand(Server $server, string $command, bool $isMux = true)
{
$server = Server::where('ip', $server_ip)->first();
if (!$server) {
throw new \Exception("Server with ip {$server_ip} not found");
}
$user = $server->user;
$port = $server->port;
$privateKeyLocation = addPrivateKeyToSshAgent($server);
$timeout = config('constants.ssh.command_timeout');
$connectionTimeout = config('constants.ssh.connection_timeout');
@@ -95,27 +92,21 @@ function generateSshCommand(string $server_ip, string $user, string $port, strin
. '-o RequestTTY=no '
. '-o LogLevel=ERROR '
. "-p {$port} "
. "{$user}@{$server_ip} "
. "{$user}@{$server->ip} "
. " 'bash -se' << \\$delimiter" . PHP_EOL
. $command . PHP_EOL
. $delimiter;
// ray($ssh_command);
return $ssh_command;
}
function instant_remote_process(array $command, Server $server, $throwError = true, $repeat = 1)
function instant_remote_process(array $command, Server $server, $throwError = true)
{
$command_string = implode("\n", $command);
$ssh_command = generateSshCommand($server->ip, $server->user, $server->port, $command_string);
$ssh_command = generateSshCommand($server, $command_string);
$process = Process::run($ssh_command);
$output = trim($process->output());
$exitCode = $process->exitCode();
if ($exitCode !== 0) {
if ($repeat > 1) {
ray("repeat: ", $repeat);
Sleep::for(200)->milliseconds();
return instant_remote_process($command, $server, $throwError, $repeat - 1);
}
// ray('ERROR OCCURED: ' . $process->errorOutput());
if (!$throwError) {
return null;
}
@@ -161,11 +152,10 @@ function refresh_server_connection(PrivateKey $private_key)
}
}
function validateServer(Server $server)
function validateServer(Server $server, bool $throwError = false)
{
try {
refresh_server_connection($server->privateKey);
$uptime = instant_remote_process(['uptime'], $server, false);
$uptime = instant_remote_process(['uptime'], $server, $throwError);
if (!$uptime) {
$server->settings->is_reachable = false;
return [
@@ -175,7 +165,7 @@ function validateServer(Server $server)
}
$server->settings->is_reachable = true;
$dockerVersion = instant_remote_process(["docker version|head -2|grep -i version| awk '{print $2}'"], $server, false);
$dockerVersion = instant_remote_process(["docker version|head -2|grep -i version| awk '{print $2}'"], $server, $throwError);
if (!$dockerVersion) {
$dockerVersion = null;
return [
@@ -183,9 +173,8 @@ function validateServer(Server $server)
"dockerVersion" => null,
];
}
$majorDockerVersion = Str::of($dockerVersion)->before('.')->value();
if ($majorDockerVersion <= 22) {
$dockerVersion = null;
$dockerVersion = checkMinimumDockerEngineVersion($dockerVersion);
if (is_null($dockerVersion)) {
$server->settings->is_usable = false;
} else {
$server->settings->is_usable = true;

View File

@@ -69,12 +69,34 @@ function refreshSession(?Team $team = null): void
}
}
Cache::forget('team:' . auth()->user()->id);
Cache::remember('team:' . auth()->user()->id, 3600, function() use ($team) {
Cache::remember('team:' . auth()->user()->id, 3600, function () use ($team) {
return $team;
});
session(['currentTeam' => $team]);
}
function general_error_handler(Throwable | null $err = null, $that = null, $isJson = false, $customErrorMessage = null): mixed
function handleError(?Throwable $error = null, ?Livewire\Component $livewire = null, ?string $customErrorMessage = null)
{
ray('handleError');
if ($error instanceof Throwable) {
$message = $error->getMessage();
} else {
$message = null;
}
if ($customErrorMessage) {
$message = $customErrorMessage . ' ' . $message;
}
if ($error instanceof TooManyRequestsException) {
if (isset($livewire)) {
return $livewire->emit('error', "Too many requests. Please try again in {$error->secondsUntilAvailable} seconds.");
}
return "Too many requests. Please try again in {$error->secondsUntilAvailable} seconds.";
}
if (isset($livewire)) {
return $livewire->emit('error', $message);
}
throw new RuntimeException($message);
}
function general_error_handler(Throwable $err, Livewire\Component $that = null, $isJson = false, $customErrorMessage = null): mixed
{
try {
ray($err);
@@ -95,7 +117,7 @@ function general_error_handler(Throwable | null $err = null, $that = null, $isJs
}
throw new Exception($customErrorMessage ?? $err->getMessage());
}
} catch (Throwable $e) {
} catch (\Throwable $e) {
if ($that) {
return $that->emit('error', $customErrorMessage ?? $e->getMessage());
} elseif ($isJson) {
@@ -122,7 +144,7 @@ function get_latest_version_of_coolify(): string
$response = Http::get('https://cdn.coollabs.io/coolify/versions.json');
$versions = $response->json();
return data_get($versions, 'coolify.v4.version');
} catch (Throwable $e) {
} catch (\Throwable $e) {
//throw $e;
ray($e->getMessage());
return '0.0.0';
@@ -321,7 +343,8 @@ function setNotificationChannels($notifiable, $event)
}
return $channels;
}
function parseEnvFormatToArray($env_file_contents) {
function parseEnvFormatToArray($env_file_contents)
{
$env_array = array();
$lines = explode("\n", $env_file_contents);
foreach ($lines as $line) {
@@ -334,8 +357,7 @@ function parseEnvFormatToArray($env_file_contents) {
$value = substr($line, $equals_pos + 1);
if (substr($value, 0, 1) === '"' && substr($value, -1) === '"') {
$value = substr($value, 1, -1);
}
elseif (substr($value, 0, 1) === "'" && substr($value, -1) === "'") {
} elseif (substr($value, 0, 1) === "'" && substr($value, -1) === "'") {
$value = substr($value, 1, -1);
}
$env_array[$key] = $value;