coolify/app/Models/Server.php

634 lines
23 KiB
PHP
Raw Normal View History

<?php
namespace App\Models;
2023-11-22 15:18:49 +01:00
use App\Enums\ApplicationDeploymentStatus;
2023-09-21 17:48:31 +02:00
use App\Enums\ProxyStatus;
use App\Enums\ProxyTypes;
use App\Notifications\Server\Revived;
use App\Notifications\Server\Unreachable;
2023-05-03 07:23:45 +02:00
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
2023-11-22 15:18:49 +01:00
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Process;
2023-05-03 07:23:45 +02:00
use Spatie\SchemalessAttributes\Casts\SchemalessAttributes;
2023-06-20 20:19:31 +02:00
use Spatie\SchemalessAttributes\SchemalessAttributesTrait;
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
2023-05-03 07:23:45 +02:00
class Server extends BaseModel
{
2023-06-20 20:19:31 +02:00
use SchemalessAttributesTrait;
2023-11-22 15:18:49 +01:00
public static $batch_counter = 0;
2023-08-23 10:14:39 +02:00
protected static function booted()
{
2023-10-09 11:00:18 +02:00
static::saving(function ($server) {
2023-10-09 20:12:03 +02:00
$payload = [];
if ($server->user) {
$payload['user'] = Str::of($server->user)->trim();
}
if ($server->ip) {
$payload['ip'] = Str::of($server->ip)->trim();
}
$server->forceFill($payload);
});
2023-08-23 10:14:39 +02:00
static::created(function ($server) {
ServerSetting::create([
'server_id' => $server->id,
]);
});
static::deleting(function ($server) {
2023-08-29 15:51:30 +02:00
$server->destinations()->each(function ($destination) {
$destination->delete();
});
2023-08-23 10:14:39 +02:00
$server->settings()->delete();
});
}
public $casts = [
'proxy' => SchemalessAttributes::class,
'logdrain_axiom_api_key' => 'encrypted',
'logdrain_newrelic_license_key' => 'encrypted',
];
2023-06-20 20:19:31 +02:00
protected $schemalessAttributes = [
'proxy',
];
2023-08-23 10:14:39 +02:00
protected $guarded = [];
static public function isReachable()
{
return Server::ownedByCurrentTeam()->whereRelation('settings', 'is_reachable', true);
}
static public function ownedByCurrentTeam(array $select = ['*'])
{
2023-08-22 17:44:49 +02:00
$teamId = currentTeam()->id;
$selectArray = collect($select)->concat(['id']);
2023-12-04 15:08:24 +01:00
return Server::whereTeamId($teamId)->with('settings', 'swarmDockers', 'standaloneDockers')->select($selectArray->all())->orderBy('name');
}
static public function isUsable()
{
return Server::ownedByCurrentTeam()->whereRelation('settings', 'is_reachable', true)->whereRelation('settings', 'is_usable', true)->whereRelation('settings', 'is_swarm_worker', false)->whereRelation('settings', 'is_build_server', false);
}
static public function destinationsByServer(string $server_id)
{
$server = Server::ownedByCurrentTeam()->get()->where('id', $server_id)->firstOrFail();
$standaloneDocker = collect($server->standaloneDockers->all());
$swarmDocker = collect($server->swarmDockers->all());
return $standaloneDocker->concat($swarmDocker);
}
public function settings()
{
return $this->hasOne(ServerSetting::class);
}
2023-12-04 15:08:24 +01:00
public function addInitialNetwork()
{
2023-11-28 15:49:24 +01:00
if ($this->id === 0) {
if ($this->isSwarm()) {
SwarmDocker::create([
'id' => 0,
'name' => 'coolify',
'network' => 'coolify-overlay',
'server_id' => $this->id,
]);
} else {
StandaloneDocker::create([
'id' => 0,
'name' => 'coolify',
'network' => 'coolify',
'server_id' => $this->id,
]);
}
} else {
if ($this->isSwarm()) {
SwarmDocker::create([
2023-11-28 18:31:04 +01:00
'name' => 'coolify-overlay',
2023-11-28 15:49:24 +01:00
'network' => 'coolify-overlay',
'server_id' => $this->id,
]);
} else {
StandaloneDocker::create([
'name' => 'coolify',
2023-11-28 15:49:24 +01:00
'network' => 'coolify',
'server_id' => $this->id,
]);
}
}
}
2023-09-25 09:17:42 +02:00
public function proxyType()
{
$proxyType = $this->proxy->get('type');
if ($proxyType === ProxyTypes::NONE->value) {
return $proxyType;
}
if (is_null($proxyType)) {
2023-09-21 17:48:31 +02:00
$this->proxy->type = ProxyTypes::TRAEFIK_V2->value;
$this->proxy->status = ProxyStatus::EXITED->value;
$this->save();
}
2023-09-20 15:42:41 +02:00
return $this->proxy->get('type');
}
2023-06-20 20:19:31 +02:00
public function scopeWithProxy(): Builder
2023-05-30 15:52:17 +02:00
{
2023-06-20 20:19:31 +02:00
return $this->proxy->modelScope();
2023-05-30 15:52:17 +02:00
}
public function isLocalhost()
{
return $this->ip === 'host.docker.internal' || $this->id === 0;
}
2024-01-17 11:52:56 +01:00
static public function buildServers($teamId)
{
return Server::whereTeamId($teamId)->whereRelation('settings', 'is_reachable', true)->whereRelation('settings', 'is_build_server', true);
}
public function skipServer()
{
if ($this->ip === '1.2.3.4') {
ray('skipping 1.2.3.4');
return true;
}
return false;
}
2023-12-19 15:36:59 +01:00
public function isServerReady(int $tries = 3)
{
2023-12-20 11:21:17 +01:00
if ($this->skipServer()) {
return false;
}
2023-12-21 09:57:39 +01:00
$serverUptimeCheckNumber = $this->unreachable_count;
2023-12-20 11:21:17 +01:00
if ($this->unreachable_count < $tries) {
$serverUptimeCheckNumber = $this->unreachable_count + 1;
}
2023-12-21 10:00:41 +01:00
if ($this->unreachable_count > $tries) {
$serverUptimeCheckNumber = $tries;
}
2023-12-21 09:57:39 +01:00
2023-12-19 15:36:59 +01:00
$serverUptimeCheckNumberMax = $tries;
2023-12-21 10:28:02 +01:00
// ray('server: ' . $this->name);
// ray('serverUptimeCheckNumber: ' . $serverUptimeCheckNumber);
// ray('serverUptimeCheckNumberMax: ' . $serverUptimeCheckNumberMax);
2023-12-19 15:16:08 +01:00
$result = $this->validateConnection();
if ($result) {
if ($this->unreachable_notification_sent === true) {
$this->update(['unreachable_notification_sent' => false]);
}
return true;
} else {
if ($serverUptimeCheckNumber >= $serverUptimeCheckNumberMax) {
2023-12-19 15:16:08 +01:00
// Reached max number of retries
if ($this->unreachable_notification_sent === false) {
ray('Server unreachable, sending notification...');
2023-12-13 12:01:27 +01:00
$this->team?->notify(new Unreachable($this));
$this->update(['unreachable_notification_sent' => true]);
}
2023-12-19 15:16:08 +01:00
if ($this->settings->is_reachable === true) {
$this->settings()->update([
'is_reachable' => false,
]);
}
foreach ($this->applications() as $application) {
$application->update(['status' => 'exited']);
}
foreach ($this->databases() as $database) {
$database->update(['status' => 'exited']);
}
2024-01-17 15:48:01 +01:00
foreach ($this->services()->get() as $service) {
$apps = $service->applications()->get();
$dbs = $service->databases()->get();
foreach ($apps as $app) {
$app->update(['status' => 'exited']);
}
foreach ($dbs as $db) {
$db->update(['status' => 'exited']);
}
}
2023-12-19 15:16:08 +01:00
} else {
$this->update([
2023-12-19 15:16:08 +01:00
'unreachable_count' => $this->unreachable_count + 1,
]);
}
2023-12-19 15:16:08 +01:00
return false;
}
}
public function getDiskUsage()
{
return instant_remote_process(["df /| tail -1 | awk '{ print $5}' | sed 's/%//g'"], $this, false);
}
public function definedResources()
{
$applications = $this->applications();
$databases = $this->databases();
$services = $this->services();
return $applications->concat($databases)->concat($services->get());
}
2023-11-06 18:04:18 +01:00
public function hasDefinedResources()
2023-06-15 15:15:27 +02:00
{
$applications = $this->applications()->count() > 0;
$databases = $this->databases()->count() > 0;
$services = $this->services()->count() > 0;
2023-11-06 18:04:18 +01:00
if ($applications || $databases || $services) {
2023-06-15 15:15:27 +02:00
return true;
}
return false;
}
2023-09-11 22:29:34 +02:00
public function databases()
{
2023-08-29 15:51:30 +02:00
return $this->destinations()->map(function ($standaloneDocker) {
2023-10-24 14:31:28 +02:00
$postgresqls = data_get($standaloneDocker, 'postgresqls', collect([]));
$redis = data_get($standaloneDocker, 'redis', collect([]));
$mongodbs = data_get($standaloneDocker, 'mongodbs', collect([]));
$mysqls = data_get($standaloneDocker, 'mysqls', collect([]));
$mariadbs = data_get($standaloneDocker, 'mariadbs', collect([]));
return $postgresqls->concat($redis)->concat($mongodbs)->concat($mysqls)->concat($mariadbs);
2023-08-29 15:51:30 +02:00
})->flatten();
}
2023-06-15 15:15:27 +02:00
public function applications()
{
return $this->destinations()->map(function ($standaloneDocker) {
return $standaloneDocker->applications;
})->flatten();
}
public function dockerComposeBasedApplications()
{
return $this->applications()->filter(function ($application) {
return data_get($application, 'build_pack') === 'dockercompose';
});
}
public function dockerComposeBasedPreviewDeployments()
{
return $this->previews()->filter(function ($preview) {
$applicationId = data_get($preview, 'application_id');
$application = Application::find($applicationId);
if (!$application) {
return false;
}
return data_get($application, 'build_pack') === 'dockercompose';
});
}
2023-09-25 09:17:42 +02:00
public function services()
{
2023-09-21 17:48:31 +02:00
return $this->hasMany(Service::class);
}
public function getIp(): Attribute
{
return Attribute::make(
get: function () {
if (isDev()) {
return '127.0.0.1';
}
if ($this->isLocalhost()) {
return base_ip();
}
return $this->ip;
}
);
}
2023-09-25 09:17:42 +02:00
public function previews()
{
2023-09-14 15:52:04 +02:00
return $this->destinations()->map(function ($standaloneDocker) {
return $standaloneDocker->applications->map(function ($application) {
return $application->previews;
})->flatten();
})->flatten();
}
2023-06-13 10:02:58 +02:00
public function destinations()
{
$standalone_docker = $this->hasMany(StandaloneDocker::class)->get();
$swarm_docker = $this->hasMany(SwarmDocker::class)->get();
return $standalone_docker->concat($swarm_docker);
}
2023-05-02 12:47:52 +02:00
public function standaloneDockers()
2023-04-25 14:43:35 +02:00
{
2023-05-02 12:47:52 +02:00
return $this->hasMany(StandaloneDocker::class);
}
2023-05-04 15:45:53 +02:00
2023-05-02 12:47:52 +02:00
public function swarmDockers()
{
return $this->hasMany(SwarmDocker::class);
2023-04-25 14:43:35 +02:00
}
2023-05-04 15:45:53 +02:00
2023-03-27 14:31:42 +02:00
public function privateKey()
{
2023-03-27 14:31:42 +02:00
return $this->belongsTo(PrivateKey::class);
}
2023-05-03 07:23:45 +02:00
2023-06-16 13:13:09 +02:00
public function muxFilename()
{
return "{$this->ip}_{$this->port}_{$this->user}";
}
2023-07-07 21:35:29 +02:00
public function team()
{
return $this->belongsTo(Team::class);
}
2023-09-11 22:29:34 +02:00
public function isProxyShouldRun()
{
if ($this->proxyType() === ProxyTypes::NONE->value || $this->settings->is_build_server) {
2023-09-25 09:17:42 +02:00
return false;
}
// foreach ($this->applications() as $application) {
// if (data_get($application, 'fqdn')) {
// $shouldRun = true;
// break;
// }
// }
// ray($this->services()->get());
// if ($this->id === 0) {
// $settings = InstanceSettings::get();
// if (data_get($settings, 'fqdn')) {
// $shouldRun = true;
// }
// }
return true;
2023-09-11 22:29:34 +02:00
}
2023-09-25 09:17:42 +02:00
public function isFunctional()
{
2023-09-12 13:14:01 +02:00
return $this->settings->is_reachable && $this->settings->is_usable;
}
public function isLogDrainEnabled()
{
2023-12-01 11:13:58 +01:00
return $this->settings->is_logdrain_newrelic_enabled || $this->settings->is_logdrain_highlight_enabled || $this->settings->is_logdrain_axiom_enabled || $this->settings->is_logdrain_custom_enabled;
}
public function validateOS(): bool | Stringable
{
$os_release = instant_remote_process(['cat /etc/os-release'], $this);
2024-01-08 21:59:26 +01:00
$releaseLines = collect(explode("\n", $os_release));
$collectedData = collect([]);
2024-01-08 21:59:26 +01:00
foreach ($releaseLines as $line) {
$item = Str::of($line)->trim();
$collectedData->put($item->before('=')->value(), $item->after('=')->lower()->replace('"', '')->value());
}
$ID = data_get($collectedData, 'ID');
// $ID_LIKE = data_get($collectedData, 'ID_LIKE');
// $VERSION_ID = data_get($collectedData, 'VERSION_ID');
$supported = collect(SUPPORTED_OS)->filter(function ($supportedOs) use ($ID) {
if (str($supportedOs)->contains($ID)) {
return str($ID);
}
});
if ($supported->count() === 1) {
ray('supported');
2023-11-28 13:17:59 +01:00
return str($supported->first());
} else {
ray('not supported');
return false;
}
}
2023-11-28 15:49:24 +01:00
public function isSwarm()
{
2023-11-29 10:06:52 +01:00
return data_get($this, 'settings.is_swarm_manager') || data_get($this, 'settings.is_swarm_worker');
2023-11-28 15:49:24 +01:00
}
2023-12-18 14:01:25 +01:00
public function isSwarmManager()
{
return data_get($this, 'settings.is_swarm_manager');
}
public function isSwarmWorker()
{
return data_get($this, 'settings.is_swarm_worker');
}
2023-10-09 11:00:18 +02:00
public function validateConnection()
{
2023-11-28 15:49:24 +01:00
$server = Server::find($this->id);
2023-12-19 12:24:43 +01:00
if (!$server) {
return false;
}
2023-12-04 20:47:32 +01:00
if ($server->skipServer()) {
return false;
}
2023-12-04 20:47:32 +01:00
$uptime = instant_remote_process(['uptime'], $server, false);
2023-10-09 11:00:18 +02:00
if (!$uptime) {
2023-12-04 20:47:32 +01:00
$server->settings()->update([
'is_reachable' => false,
]);
2023-10-09 11:00:18 +02:00
return false;
} else {
2023-12-04 20:47:32 +01:00
$server->settings()->update([
'is_reachable' => true,
]);
2023-11-28 15:49:24 +01:00
$server->update([
'unreachable_count' => 0,
]);
2023-10-09 11:00:18 +02:00
}
2023-12-04 20:47:32 +01:00
if (data_get($server, 'unreachable_notification_sent') === true) {
2023-12-13 12:01:27 +01:00
$server->team?->notify(new Revived($server));
2023-11-28 15:49:24 +01:00
$server->update(['unreachable_notification_sent' => false]);
}
2023-10-09 11:00:18 +02:00
return true;
}
public function validateDockerEngine($throwError = false)
{
$dockerBinary = instant_remote_process(["command -v docker"], $this, false);
if (is_null($dockerBinary)) {
$this->settings->is_usable = false;
$this->settings->save();
if ($throwError) {
throw new \Exception('Server is not usable. Docker Engine is not installed.');
2023-10-09 11:00:18 +02:00
}
return false;
}
$this->settings->is_usable = true;
$this->settings->save();
$this->validateCoolifyNetwork(isSwarm: false, isBuildServer: $this->settings->is_build_server);
2023-11-28 15:49:24 +01:00
return true;
}
public function validateDockerSwarm()
{
$swarmStatus = instant_remote_process(["docker info|grep -i swarm"], $this, false);
$swarmStatus = str($swarmStatus)->trim()->after(':')->trim();
if ($swarmStatus === 'inactive') {
throw new \Exception('Docker Swarm is not initiated. Please join the server to a swarm before continuing.');
return false;
}
$this->settings->is_usable = true;
$this->settings->save();
$this->validateCoolifyNetwork(isSwarm: true);
2023-10-09 11:00:18 +02:00
return true;
}
public function validateDockerEngineVersion()
{
$dockerVersion = instant_remote_process(["docker version|head -2|grep -i version| awk '{print $2}'"], $this, false);
$dockerVersion = checkMinimumDockerEngineVersion($dockerVersion);
if (is_null($dockerVersion)) {
$this->settings->is_usable = false;
$this->settings->save();
return false;
}
$this->settings->is_reachable = true;
2023-10-09 11:00:18 +02:00
$this->settings->is_usable = true;
$this->settings->save();
return true;
}
public function validateCoolifyNetwork($isSwarm = false, $isBuildServer = false)
2023-10-24 14:31:28 +02:00
{
if ($isBuildServer) {
return;
}
2023-11-28 15:49:24 +01:00
if ($isSwarm) {
2023-11-28 18:31:04 +01:00
return instant_remote_process(["docker network create --attachable --driver overlay coolify-overlay >/dev/null 2>&1 || true"], $this, false);
2023-11-28 15:49:24 +01:00
} else {
return instant_remote_process(["docker network create coolify --attachable >/dev/null 2>&1 || true"], $this, false);
}
2023-10-09 11:00:18 +02:00
}
2023-11-24 15:48:23 +01:00
public function executeRemoteCommand(Collection $commands, ?ApplicationDeploymentQueue $loggingModel = null)
2023-11-22 15:18:49 +01:00
{
static::$batch_counter++;
foreach ($commands as $command) {
2023-11-23 21:02:30 +01:00
$realCommand = data_get($command, 'command');
if (is_null($realCommand)) {
throw new \RuntimeException('Command is not set');
2023-11-22 15:18:49 +01:00
}
$hidden = data_get($command, 'hidden', false);
$ignoreErrors = data_get($command, 'ignoreErrors', false);
$customOutputType = data_get($command, 'customOutputType');
2023-11-23 21:02:30 +01:00
$name = data_get($command, 'name');
$remoteCommand = generateSshCommand($this, $realCommand);
2023-11-22 15:18:49 +01:00
2023-11-23 21:02:30 +01:00
$process = Process::timeout(3600)->idleTimeout(3600)->start($remoteCommand, function (string $type, string $output) use ($realCommand, $hidden, $customOutputType, $loggingModel, $name) {
2023-11-22 15:18:49 +01:00
$output = str($output)->trim();
if ($output->startsWith('╔')) {
$output = "\n" . $output;
}
$newLogEntry = [
2023-11-23 21:02:30 +01:00
'command' => remove_iip($realCommand),
2023-11-22 15:18:49 +01:00
'output' => remove_iip($output),
'type' => $customOutputType ?? $type === 'err' ? 'stderr' : 'stdout',
'timestamp' => Carbon::now('UTC'),
'hidden' => $hidden,
'batch' => static::$batch_counter,
];
2023-11-24 15:48:23 +01:00
if ($loggingModel) {
if (!$loggingModel->logs) {
$newLogEntry['order'] = 1;
} else {
$previousLogs = json_decode($loggingModel->logs, associative: true, flags: JSON_THROW_ON_ERROR);
$newLogEntry['order'] = count($previousLogs) + 1;
}
if ($name) {
$newLogEntry['name'] = $name;
}
2023-11-23 21:02:30 +01:00
2023-11-24 15:48:23 +01:00
$previousLogs[] = $newLogEntry;
$loggingModel->logs = json_encode($previousLogs, flags: JSON_THROW_ON_ERROR);
$loggingModel->save();
}
2023-11-22 15:18:49 +01:00
});
2023-11-24 15:48:23 +01:00
if ($loggingModel) {
$loggingModel->update([
'current_process_id' => $process->id(),
]);
}
2023-11-22 15:18:49 +01:00
$processResult = $process->wait();
if ($processResult->exitCode() !== 0) {
if (!$ignoreErrors) {
2023-11-24 15:48:23 +01:00
if ($loggingModel) {
$status = ApplicationDeploymentStatus::FAILED->value;
$loggingModel->status = $status;
$loggingModel->save();
}
2023-11-22 15:18:49 +01:00
throw new \RuntimeException($processResult->errorOutput());
}
}
}
}
2023-11-23 21:02:30 +01:00
public function stopApplicationRelatedRunningContainers(string $applicationId, string $containerName)
{
$containers = getCurrentApplicationContainerStatus($this, $applicationId, 0);
$containers = $containers->filter(function ($container) use ($containerName) {
return data_get($container, 'Names') !== $containerName;
});
$containers->each(function ($container) {
$removableContainer = data_get($container, 'Names');
$this->server->executeRemoteCommand(
commands: collect([
'command' => "docker rm -f $removableContainer >/dev/null 2>&1",
'hidden' => true,
'ignoreErrors' => true
]),
loggingModel: $this->deploymentQueueEntry
);
});
}
2023-12-04 15:08:24 +01:00
public function getHostIPMappings($network)
{
$addHosts = null;
$allContainers = instant_remote_process(["docker network inspect {$network} -f '{{json .Containers}}' "], $this);
if (!is_null($allContainers)) {
$allContainers = format_docker_command_output_to_json($allContainers);
$ips = collect([]);
if (count($allContainers) > 0) {
$allContainers = $allContainers[0];
foreach ($allContainers as $container) {
$containerName = data_get($container, 'Name');
if ($containerName === 'coolify-proxy') {
continue;
}
$containerIp = data_get($container, 'IPv4Address');
if ($containerName && $containerIp) {
$containerIp = str($containerIp)->before('/');
$ips->put($containerName, $containerIp->value());
}
}
}
$addHosts = $ips->map(function ($ip, $name) {
return "--add-host $name:$ip";
})->implode(' ');
}
return $addHosts;
}
public function checkIfDockerImageExists(string $imageName, ApplicationDeploymentQueue $deployment)
{
$this->executeRemoteCommand(
commands: collect([
[
"name" => "local_image_found",
"command" => "docker images -q {$imageName} 2>/dev/null",
"hidden" => true,
]
]),
loggingModel: $deployment
);
if (str($deployment->getOutput('local_image_found'))->isEmpty()) {
$this->executeRemoteCommand(
commands: collect([
[
"command" => "docker pull {$imageName} 2>/dev/null",
"ignoreErrors" => true,
"hidden" => true
],
[
"name" => "local_image_found",
"command" => "docker images -q {$imageName} 2>/dev/null",
"hidden" => true,
]
]),
loggingModel: $deployment
);
}
}
public function createWorkDirForDeployment(string $workdir, ApplicationDeploymentQueue $deployment)
{
$this->executeRemoteCommand(
commands: collect([
[
"command" => executeInDocker($deployment->deployment_uuid, "mkdir -p {$workdir}"),
"ignoreErrors" => true,
"hidden" => true
],
]),
loggingModel: $deployment
);
}
2023-07-14 13:38:24 +02:00
}