2023-03-24 14:47:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2023-09-21 15:48:31 +00:00
|
|
|
use App\Enums\ProxyStatus;
|
|
|
|
use App\Enums\ProxyTypes;
|
2023-11-16 10:53:37 +00:00
|
|
|
use App\Notifications\Server\Revived;
|
|
|
|
use App\Notifications\Server\Unreachable;
|
2023-05-03 05:23:45 +00:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2023-10-04 08:57:44 +00:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
2023-11-16 10:53:37 +00:00
|
|
|
use Illuminate\Support\Sleep;
|
2023-05-03 05:23:45 +00:00
|
|
|
use Spatie\SchemalessAttributes\Casts\SchemalessAttributes;
|
2023-06-20 18:19:31 +00:00
|
|
|
use Spatie\SchemalessAttributes\SchemalessAttributesTrait;
|
2023-10-06 22:51:01 +00:00
|
|
|
use Illuminate\Support\Str;
|
2023-05-03 05:23:45 +00:00
|
|
|
|
2023-03-24 14:47:58 +00:00
|
|
|
class Server extends BaseModel
|
|
|
|
{
|
2023-06-20 18:19:31 +00:00
|
|
|
use SchemalessAttributesTrait;
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-08-23 08:14:39 +00:00
|
|
|
protected static function booted()
|
|
|
|
{
|
2023-10-09 09:00:18 +00:00
|
|
|
static::saving(function ($server) {
|
2023-10-09 18:12:03 +00: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-10-06 22:51:01 +00:00
|
|
|
});
|
|
|
|
|
2023-08-23 08:14:39 +00:00
|
|
|
static::created(function ($server) {
|
|
|
|
ServerSetting::create([
|
|
|
|
'server_id' => $server->id,
|
|
|
|
]);
|
2023-08-23 14:40:59 +00:00
|
|
|
if ($server->id === 0) {
|
|
|
|
StandaloneDocker::create([
|
|
|
|
'id' => 0,
|
|
|
|
'name' => 'coolify',
|
|
|
|
'network' => 'coolify',
|
|
|
|
'server_id' => $server->id,
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
StandaloneDocker::create([
|
|
|
|
'name' => 'coolify',
|
|
|
|
'network' => 'coolify',
|
|
|
|
'server_id' => $server->id,
|
|
|
|
]);
|
|
|
|
}
|
2023-08-23 08:14:39 +00:00
|
|
|
});
|
|
|
|
static::deleting(function ($server) {
|
2023-08-29 13:51:30 +00:00
|
|
|
$server->destinations()->each(function ($destination) {
|
|
|
|
$destination->delete();
|
|
|
|
});
|
2023-08-23 08:14:39 +00:00
|
|
|
$server->settings()->delete();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-08 09:51:36 +00:00
|
|
|
public $casts = [
|
|
|
|
'proxy' => SchemalessAttributes::class,
|
|
|
|
];
|
2023-06-20 18:19:31 +00:00
|
|
|
protected $schemalessAttributes = [
|
|
|
|
'proxy',
|
|
|
|
];
|
2023-08-23 08:14:39 +00:00
|
|
|
protected $guarded = [];
|
2023-08-08 09:51:36 +00:00
|
|
|
|
|
|
|
static public function isReachable()
|
|
|
|
{
|
|
|
|
return Server::ownedByCurrentTeam()->whereRelation('settings', 'is_reachable', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static public function ownedByCurrentTeam(array $select = ['*'])
|
|
|
|
{
|
2023-08-22 15:44:49 +00:00
|
|
|
$teamId = currentTeam()->id;
|
2023-08-08 09:51:36 +00:00
|
|
|
$selectArray = collect($select)->concat(['id']);
|
2023-08-21 08:18:11 +00:00
|
|
|
return Server::whereTeamId($teamId)->with('settings')->select($selectArray->all())->orderBy('name');
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static public function isUsable()
|
|
|
|
{
|
|
|
|
return Server::ownedByCurrentTeam()->whereRelation('settings', 'is_reachable', true)->whereRelation('settings', 'is_usable', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-20 18:19:31 +00:00
|
|
|
|
2023-09-25 07:17:42 +00:00
|
|
|
public function proxyType()
|
|
|
|
{
|
2023-10-12 06:51:32 +00:00
|
|
|
$proxyType = $this->proxy->get('type');
|
|
|
|
if ($proxyType === ProxyTypes::NONE->value) {
|
|
|
|
return $proxyType;
|
|
|
|
}
|
|
|
|
if (is_null($proxyType)) {
|
2023-09-21 15:48:31 +00:00
|
|
|
$this->proxy->type = ProxyTypes::TRAEFIK_V2->value;
|
|
|
|
$this->proxy->status = ProxyStatus::EXITED->value;
|
|
|
|
$this->save();
|
|
|
|
}
|
2023-09-20 13:42:41 +00:00
|
|
|
return $this->proxy->get('type');
|
|
|
|
}
|
2023-06-20 18:19:31 +00:00
|
|
|
public function scopeWithProxy(): Builder
|
2023-05-30 13:52:17 +00:00
|
|
|
{
|
2023-06-20 18:19:31 +00:00
|
|
|
return $this->proxy->modelScope();
|
2023-05-30 13:52:17 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-11-16 10:53:37 +00:00
|
|
|
public function checkServerRediness()
|
|
|
|
{
|
|
|
|
$serverUptimeCheckNumber = $this->unreachable_count;
|
|
|
|
$serverUptimeCheckNumberMax = 5;
|
|
|
|
while (true) {
|
|
|
|
if ($serverUptimeCheckNumber >= $serverUptimeCheckNumberMax) {
|
|
|
|
if ($this->unreachable_notification_sent === false) {
|
|
|
|
ray('Server unreachable, sending notification...');
|
|
|
|
$this->team->notify(new Unreachable($this));
|
|
|
|
$this->update(['unreachable_notification_sent' => true]);
|
|
|
|
}
|
|
|
|
$this->settings()->update([
|
|
|
|
'is_reachable' => false,
|
|
|
|
]);
|
|
|
|
$this->update([
|
|
|
|
'unreachable_count' => 0,
|
|
|
|
]);
|
|
|
|
foreach ($this->applications() as $application) {
|
|
|
|
$application->update(['status' => 'exited']);
|
|
|
|
}
|
|
|
|
foreach ($this->databases() as $database) {
|
|
|
|
$database->update(['status' => 'exited']);
|
|
|
|
}
|
|
|
|
foreach ($this->services() 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']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new \Exception('Server is not reachable.');
|
|
|
|
}
|
|
|
|
$result = $this->validateConnection();
|
|
|
|
ray('validateConnection: ' . $result);
|
|
|
|
if (!$result) {
|
|
|
|
$serverUptimeCheckNumber++;
|
|
|
|
$this->update([
|
|
|
|
'unreachable_count' => $serverUptimeCheckNumber,
|
|
|
|
]);
|
|
|
|
Sleep::for(5)->seconds();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->update([
|
|
|
|
'unreachable_count' => 0,
|
|
|
|
]);
|
|
|
|
if (data_get($this, 'unreachable_notification_sent') === true) {
|
|
|
|
ray('Server is reachable again, sending notification...');
|
|
|
|
$this->team->notify(new Revived($this));
|
|
|
|
$this->update(['unreachable_notification_sent' => false]);
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
data_get($this, 'settings.is_reachable') === false ||
|
|
|
|
data_get($this, 'settings.is_usable') === false
|
|
|
|
) {
|
|
|
|
$this->settings()->update([
|
|
|
|
'is_reachable' => true,
|
|
|
|
'is_usable' => true
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public function getDiskUsage()
|
|
|
|
{
|
|
|
|
return instant_remote_process(["df /| tail -1 | awk '{ print $5}' | sed 's/%//g'"], $this, false);
|
|
|
|
}
|
2023-11-06 17:04:18 +00:00
|
|
|
public function hasDefinedResources()
|
2023-06-15 13:15:27 +00:00
|
|
|
{
|
2023-11-14 14:06:03 +00:00
|
|
|
$applications = $this->applications()->count() > 0;
|
|
|
|
$databases = $this->databases()->count() > 0;
|
|
|
|
$services = $this->services()->count() > 0;
|
2023-11-06 17:04:18 +00:00
|
|
|
if ($applications || $databases || $services) {
|
2023-06-15 13:15:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-09-11 20:29:34 +00:00
|
|
|
public function databases()
|
|
|
|
{
|
2023-08-29 13:51:30 +00:00
|
|
|
return $this->destinations()->map(function ($standaloneDocker) {
|
2023-10-24 12:31:28 +00: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 13:51:30 +00:00
|
|
|
})->flatten();
|
|
|
|
}
|
2023-06-15 13:15:27 +00:00
|
|
|
public function applications()
|
|
|
|
{
|
|
|
|
return $this->destinations()->map(function ($standaloneDocker) {
|
|
|
|
return $standaloneDocker->applications;
|
|
|
|
})->flatten();
|
|
|
|
}
|
2023-09-25 07:17:42 +00:00
|
|
|
public function services()
|
|
|
|
{
|
2023-09-21 15:48:31 +00:00
|
|
|
return $this->hasMany(Service::class);
|
|
|
|
}
|
2023-10-04 08:57:44 +00:00
|
|
|
public function getIp(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::make(
|
|
|
|
get: function () {
|
|
|
|
if (isDev()) {
|
|
|
|
return '127.0.0.1';
|
|
|
|
}
|
|
|
|
if ($this->ip === 'host.docker.internal') {
|
|
|
|
return base_ip();
|
|
|
|
}
|
|
|
|
return $this->ip;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2023-09-25 07:17:42 +00:00
|
|
|
public function previews()
|
|
|
|
{
|
2023-09-14 13:52:04 +00:00
|
|
|
return $this->destinations()->map(function ($standaloneDocker) {
|
|
|
|
return $standaloneDocker->applications->map(function ($application) {
|
|
|
|
return $application->previews;
|
|
|
|
})->flatten();
|
|
|
|
})->flatten();
|
|
|
|
}
|
|
|
|
|
2023-06-13 08:02:58 +00: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-08-08 09:51:36 +00:00
|
|
|
|
2023-05-02 10:47:52 +00:00
|
|
|
public function standaloneDockers()
|
2023-04-25 12:43:35 +00:00
|
|
|
{
|
2023-05-02 10:47:52 +00:00
|
|
|
return $this->hasMany(StandaloneDocker::class);
|
|
|
|
}
|
2023-05-04 13:45:53 +00:00
|
|
|
|
2023-05-02 10:47:52 +00:00
|
|
|
public function swarmDockers()
|
|
|
|
{
|
|
|
|
return $this->hasMany(SwarmDocker::class);
|
2023-04-25 12:43:35 +00:00
|
|
|
}
|
2023-05-04 13:45:53 +00:00
|
|
|
|
2023-03-27 12:31:42 +00:00
|
|
|
public function privateKey()
|
2023-03-24 14:47:58 +00:00
|
|
|
{
|
2023-03-27 12:31:42 +00:00
|
|
|
return $this->belongsTo(PrivateKey::class);
|
2023-03-24 14:47:58 +00:00
|
|
|
}
|
2023-05-03 05:23:45 +00:00
|
|
|
|
2023-06-16 11:13:09 +00:00
|
|
|
public function muxFilename()
|
|
|
|
{
|
|
|
|
return "{$this->ip}_{$this->port}_{$this->user}";
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-07-07 19:35:29 +00:00
|
|
|
public function team()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Team::class);
|
|
|
|
}
|
2023-09-11 20:29:34 +00:00
|
|
|
public function isProxyShouldRun()
|
|
|
|
{
|
2023-09-25 07:17:42 +00:00
|
|
|
if ($this->proxyType() === ProxyTypes::NONE->value) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-10-17 13:40:47 +00:00
|
|
|
// 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 20:29:34 +00:00
|
|
|
}
|
2023-09-25 07:17:42 +00:00
|
|
|
public function isFunctional()
|
|
|
|
{
|
2023-09-12 11:14:01 +00:00
|
|
|
return $this->settings->is_reachable && $this->settings->is_usable;
|
|
|
|
}
|
2023-10-09 09:00:18 +00:00
|
|
|
public function validateConnection()
|
|
|
|
{
|
|
|
|
$uptime = instant_remote_process(['uptime'], $this, false);
|
|
|
|
if (!$uptime) {
|
|
|
|
$this->settings->is_reachable = false;
|
|
|
|
$this->settings->save();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->settings->is_reachable = true;
|
|
|
|
$this->settings->save();
|
|
|
|
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.');
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->settings->is_usable = true;
|
|
|
|
$this->settings->save();
|
|
|
|
$this->validateCoolifyNetwork();
|
|
|
|
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_usable = true;
|
|
|
|
$this->settings->save();
|
|
|
|
return true;
|
|
|
|
}
|
2023-10-24 12:31:28 +00:00
|
|
|
public function validateCoolifyNetwork()
|
|
|
|
{
|
2023-10-09 09:00:18 +00:00
|
|
|
return instant_remote_process(["docker network create coolify --attachable >/dev/null 2>&1 || true"], $this, false);
|
|
|
|
}
|
2023-07-14 11:38:24 +00:00
|
|
|
}
|