2023-03-24 14:47:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2024-02-05 13:40:54 +00:00
|
|
|
use App\Actions\Server\InstallDocker;
|
2024-05-08 12:22:35 +00:00
|
|
|
use App\Actions\Server\StartSentinel;
|
2023-09-21 15:48:31 +00:00
|
|
|
use App\Enums\ProxyTypes;
|
2024-05-08 18:59:58 +00:00
|
|
|
use App\Jobs\PullSentinelImageJob;
|
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;
|
2024-05-08 18:59:58 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2024-02-06 14:42:31 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2024-02-26 09:25:21 +00:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
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-11-28 13:05:55 +00:00
|
|
|
use Illuminate\Support\Stringable;
|
2024-03-11 16:17:34 +00:00
|
|
|
use Spatie\Url\Url;
|
|
|
|
use Symfony\Component\Yaml\Yaml;
|
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-11-22 14:18:49 +00:00
|
|
|
public static $batch_counter = 0;
|
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,
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
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-11-17 09:21:19 +00:00
|
|
|
'logdrain_axiom_api_key' => 'encrypted',
|
|
|
|
'logdrain_newrelic_license_key' => 'encrypted',
|
2023-08-08 09:51:36 +00:00
|
|
|
];
|
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-12-04 14:08:24 +00:00
|
|
|
return Server::whereTeamId($teamId)->with('settings', 'swarmDockers', 'standaloneDockers')->select($selectArray->all())->orderBy('name');
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static public function isUsable()
|
|
|
|
{
|
2024-02-26 09:25:21 +00:00
|
|
|
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)->whereRelation('settings', 'force_disabled', false);
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 14:08:24 +00:00
|
|
|
public function addInitialNetwork()
|
|
|
|
{
|
2023-11-28 14:49:24 +00: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 17:31:04 +00:00
|
|
|
'name' => 'coolify-overlay',
|
2023-11-28 14:49:24 +00:00
|
|
|
'network' => 'coolify-overlay',
|
|
|
|
'server_id' => $this->id,
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
StandaloneDocker::create([
|
2024-01-17 13:11:46 +00:00
|
|
|
'name' => 'coolify',
|
2023-11-28 14:49:24 +00:00
|
|
|
'network' => 'coolify',
|
|
|
|
'server_id' => $this->id,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-12 09:42:56 +00:00
|
|
|
public function setupDefault404Redirect()
|
|
|
|
{
|
|
|
|
$dynamic_conf_path = $this->proxyPath() . "/dynamic";
|
|
|
|
$proxy_type = $this->proxyType();
|
|
|
|
$redirect_url = $this->proxy->redirect_url;
|
|
|
|
if ($proxy_type === 'TRAEFIK_V2') {
|
|
|
|
$default_redirect_file = "$dynamic_conf_path/default_redirect_404.yaml";
|
|
|
|
} else if ($proxy_type === 'CADDY') {
|
|
|
|
$default_redirect_file = "$dynamic_conf_path/default_redirect_404.caddy";
|
|
|
|
}
|
|
|
|
if (empty($redirect_url)) {
|
2024-03-13 10:25:34 +00:00
|
|
|
if ($proxy_type === 'CADDY') {
|
|
|
|
$conf = ":80, :443 {
|
|
|
|
respond 404
|
|
|
|
}";
|
|
|
|
$conf =
|
|
|
|
"# This file is automatically generated by Coolify.\n" .
|
|
|
|
"# Do not edit it manually (only if you know what are you doing).\n\n" .
|
|
|
|
$conf;
|
|
|
|
$base64 = base64_encode($conf);
|
|
|
|
instant_remote_process([
|
|
|
|
"mkdir -p $dynamic_conf_path",
|
2024-04-17 08:49:34 +00:00
|
|
|
"echo '$base64' | base64 -d | tee $default_redirect_file > /dev/null",
|
2024-03-13 10:25:34 +00:00
|
|
|
], $this);
|
|
|
|
$this->reloadCaddy();
|
|
|
|
return;
|
|
|
|
}
|
2024-03-12 09:42:56 +00:00
|
|
|
instant_remote_process([
|
|
|
|
"mkdir -p $dynamic_conf_path",
|
|
|
|
"rm -f $default_redirect_file",
|
|
|
|
], $this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($proxy_type === 'TRAEFIK_V2') {
|
|
|
|
$dynamic_conf = [
|
|
|
|
'http' =>
|
|
|
|
[
|
|
|
|
'routers' =>
|
|
|
|
[
|
|
|
|
'catchall' =>
|
|
|
|
[
|
|
|
|
'entryPoints' => [
|
|
|
|
0 => 'http',
|
|
|
|
1 => 'https',
|
|
|
|
],
|
|
|
|
'service' => 'noop',
|
|
|
|
'rule' => "HostRegexp(`{catchall:.*}`)",
|
|
|
|
'priority' => 1,
|
|
|
|
'middlewares' => [
|
|
|
|
0 => 'redirect-regexp@file',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'services' =>
|
|
|
|
[
|
|
|
|
'noop' =>
|
|
|
|
[
|
|
|
|
'loadBalancer' =>
|
|
|
|
[
|
|
|
|
'servers' =>
|
|
|
|
[
|
|
|
|
0 =>
|
|
|
|
[
|
|
|
|
'url' => '',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'middlewares' =>
|
|
|
|
[
|
|
|
|
'redirect-regexp' =>
|
|
|
|
[
|
|
|
|
'redirectRegex' =>
|
|
|
|
[
|
|
|
|
'regex' => '(.*)',
|
|
|
|
'replacement' => $redirect_url,
|
|
|
|
'permanent' => false,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
$conf = Yaml::dump($dynamic_conf, 12, 2);
|
|
|
|
$conf =
|
|
|
|
"# This file is automatically generated by Coolify.\n" .
|
|
|
|
"# Do not edit it manually (only if you know what are you doing).\n\n" .
|
|
|
|
$conf;
|
|
|
|
|
|
|
|
$base64 = base64_encode($conf);
|
|
|
|
} else if ($proxy_type === 'CADDY') {
|
|
|
|
$conf = ":80, :443 {
|
|
|
|
redir $redirect_url
|
|
|
|
}";
|
|
|
|
$conf =
|
|
|
|
"# This file is automatically generated by Coolify.\n" .
|
|
|
|
"# Do not edit it manually (only if you know what are you doing).\n\n" .
|
|
|
|
$conf;
|
|
|
|
$base64 = base64_encode($conf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
instant_remote_process([
|
|
|
|
"mkdir -p $dynamic_conf_path",
|
2024-04-17 08:49:34 +00:00
|
|
|
"echo '$base64' | base64 -d | tee $default_redirect_file > /dev/null",
|
2024-03-12 09:42:56 +00:00
|
|
|
], $this);
|
|
|
|
|
|
|
|
if (config('app.env') == 'local') {
|
|
|
|
ray($conf);
|
|
|
|
}
|
|
|
|
if ($proxy_type === 'CADDY') {
|
|
|
|
$this->reloadCaddy();
|
|
|
|
}
|
|
|
|
}
|
2024-03-11 16:17:34 +00:00
|
|
|
public function setupDynamicProxyConfiguration()
|
|
|
|
{
|
|
|
|
$settings = InstanceSettings::get();
|
|
|
|
$dynamic_config_path = $this->proxyPath() . "/dynamic";
|
2024-03-12 11:30:40 +00:00
|
|
|
if ($this->proxyType() === 'TRAEFIK_V2') {
|
|
|
|
$file = "$dynamic_config_path/coolify.yaml";
|
2024-05-03 08:32:04 +00:00
|
|
|
if (empty($settings->fqdn) || (isCloud() && $this->id !== 0)) {
|
2024-03-12 11:30:40 +00:00
|
|
|
instant_remote_process([
|
|
|
|
"rm -f $file",
|
|
|
|
], $this);
|
|
|
|
} else {
|
|
|
|
$url = Url::fromString($settings->fqdn);
|
|
|
|
$host = $url->getHost();
|
|
|
|
$schema = $url->getScheme();
|
|
|
|
$traefik_dynamic_conf = [
|
|
|
|
'http' =>
|
|
|
|
[
|
|
|
|
'middlewares' => [
|
|
|
|
'redirect-to-https' => [
|
|
|
|
'redirectscheme' => [
|
|
|
|
'scheme' => 'https',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'gzip' => [
|
|
|
|
'compress' => true,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'routers' =>
|
2024-03-11 16:17:34 +00:00
|
|
|
[
|
2024-03-12 11:30:40 +00:00
|
|
|
'coolify-http' =>
|
|
|
|
[
|
|
|
|
'middlewares' => [
|
|
|
|
0 => 'gzip',
|
2024-03-11 16:17:34 +00:00
|
|
|
],
|
2024-03-12 11:30:40 +00:00
|
|
|
'entryPoints' => [
|
|
|
|
0 => 'http',
|
2024-03-11 16:17:34 +00:00
|
|
|
],
|
2024-03-12 11:30:40 +00:00
|
|
|
'service' => 'coolify',
|
|
|
|
'rule' => "Host(`{$host}`)",
|
2024-03-11 16:17:34 +00:00
|
|
|
],
|
2024-03-12 11:30:40 +00:00
|
|
|
'coolify-realtime-ws' =>
|
2024-03-11 16:17:34 +00:00
|
|
|
[
|
2024-03-12 11:30:40 +00:00
|
|
|
'entryPoints' => [
|
|
|
|
0 => 'http',
|
2024-03-11 16:17:34 +00:00
|
|
|
],
|
2024-03-12 11:30:40 +00:00
|
|
|
'service' => 'coolify-realtime',
|
|
|
|
'rule' => "Host(`{$host}`) && PathPrefix(`/app`)",
|
2024-03-11 16:17:34 +00:00
|
|
|
],
|
2024-03-12 11:30:40 +00:00
|
|
|
],
|
|
|
|
'services' =>
|
|
|
|
[
|
|
|
|
'coolify' =>
|
2024-03-11 16:17:34 +00:00
|
|
|
[
|
2024-03-12 11:30:40 +00:00
|
|
|
'loadBalancer' =>
|
2024-03-11 16:17:34 +00:00
|
|
|
[
|
2024-03-12 11:30:40 +00:00
|
|
|
'servers' =>
|
2024-03-11 16:17:34 +00:00
|
|
|
[
|
2024-03-12 11:30:40 +00:00
|
|
|
0 =>
|
2024-03-11 16:17:34 +00:00
|
|
|
[
|
2024-03-12 11:30:40 +00:00
|
|
|
'url' => 'http://coolify:80',
|
2024-03-11 16:17:34 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
2024-03-12 11:30:40 +00:00
|
|
|
],
|
|
|
|
'coolify-realtime' =>
|
|
|
|
[
|
|
|
|
'loadBalancer' =>
|
2024-03-11 16:17:34 +00:00
|
|
|
[
|
2024-03-12 11:30:40 +00:00
|
|
|
'servers' =>
|
2024-03-11 16:17:34 +00:00
|
|
|
[
|
2024-03-12 11:30:40 +00:00
|
|
|
0 =>
|
2024-03-11 16:17:34 +00:00
|
|
|
[
|
2024-03-12 11:30:40 +00:00
|
|
|
'url' => 'http://coolify-realtime:6001',
|
2024-03-11 16:17:34 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
2024-03-12 11:30:40 +00:00
|
|
|
],
|
|
|
|
];
|
2024-03-11 16:17:34 +00:00
|
|
|
|
2024-03-12 11:30:40 +00:00
|
|
|
if ($schema === 'https') {
|
|
|
|
$traefik_dynamic_conf['http']['routers']['coolify-http']['middlewares'] = [
|
|
|
|
0 => 'redirect-to-https',
|
|
|
|
];
|
2024-03-11 16:17:34 +00:00
|
|
|
|
2024-03-12 11:30:40 +00:00
|
|
|
$traefik_dynamic_conf['http']['routers']['coolify-https'] = [
|
|
|
|
'entryPoints' => [
|
|
|
|
0 => 'https',
|
|
|
|
],
|
|
|
|
'service' => 'coolify',
|
|
|
|
'rule' => "Host(`{$host}`)",
|
|
|
|
'tls' => [
|
|
|
|
'certresolver' => 'letsencrypt',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
$traefik_dynamic_conf['http']['routers']['coolify-realtime-wss'] = [
|
|
|
|
'entryPoints' => [
|
|
|
|
0 => 'https',
|
|
|
|
],
|
|
|
|
'service' => 'coolify-realtime',
|
|
|
|
'rule' => "Host(`{$host}`) && PathPrefix(`/app`)",
|
|
|
|
'tls' => [
|
|
|
|
'certresolver' => 'letsencrypt',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
$yaml = Yaml::dump($traefik_dynamic_conf, 12, 2);
|
|
|
|
$yaml =
|
|
|
|
"# This file is automatically generated by Coolify.\n" .
|
|
|
|
"# Do not edit it manually (only if you know what are you doing).\n\n" .
|
|
|
|
$yaml;
|
2024-03-11 16:17:34 +00:00
|
|
|
|
2024-03-12 11:30:40 +00:00
|
|
|
$base64 = base64_encode($yaml);
|
|
|
|
instant_remote_process([
|
|
|
|
"mkdir -p $dynamic_config_path",
|
2024-04-17 08:49:34 +00:00
|
|
|
"echo '$base64' | base64 -d | tee $file > /dev/null",
|
2024-03-12 11:30:40 +00:00
|
|
|
], $this);
|
2024-03-11 16:17:34 +00:00
|
|
|
|
2024-03-12 11:30:40 +00:00
|
|
|
if (config('app.env') == 'local') {
|
|
|
|
// ray($yaml);
|
2024-03-11 16:17:34 +00:00
|
|
|
}
|
2024-03-12 11:30:40 +00:00
|
|
|
}
|
|
|
|
} else if ($this->proxyType() === 'CADDY') {
|
|
|
|
$file = "$dynamic_config_path/coolify.caddy";
|
2024-05-03 08:32:04 +00:00
|
|
|
if (empty($settings->fqdn) || (isCloud() && $this->id !== 0)) {
|
2024-03-12 11:30:40 +00:00
|
|
|
instant_remote_process([
|
|
|
|
"rm -f $file",
|
|
|
|
], $this);
|
|
|
|
$this->reloadCaddy();
|
|
|
|
} else {
|
|
|
|
$url = Url::fromString($settings->fqdn);
|
|
|
|
$host = $url->getHost();
|
|
|
|
$schema = $url->getScheme();
|
|
|
|
$caddy_file = "
|
2024-03-11 16:17:34 +00:00
|
|
|
$schema://$host {
|
2024-03-12 10:34:57 +00:00
|
|
|
handle /app/* {
|
|
|
|
reverse_proxy coolify-realtime:6001
|
|
|
|
}
|
2024-03-11 16:17:34 +00:00
|
|
|
reverse_proxy coolify:80
|
|
|
|
}";
|
2024-03-12 11:30:40 +00:00
|
|
|
$base64 = base64_encode($caddy_file);
|
|
|
|
instant_remote_process([
|
2024-04-17 08:49:34 +00:00
|
|
|
"echo '$base64' | base64 -d | tee $file > /dev/null",
|
2024-03-12 11:30:40 +00:00
|
|
|
], $this);
|
|
|
|
$this->reloadCaddy();
|
2024-03-11 16:17:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-12 09:42:56 +00:00
|
|
|
public function reloadCaddy()
|
|
|
|
{
|
2024-03-11 16:31:28 +00:00
|
|
|
return instant_remote_process([
|
2024-03-12 09:42:56 +00:00
|
|
|
"docker exec coolify-proxy caddy reload --config /config/caddy/Caddyfile.autosave",
|
2024-03-11 16:31:28 +00:00
|
|
|
], $this);
|
|
|
|
}
|
2024-03-11 16:17:34 +00:00
|
|
|
public function proxyPath()
|
|
|
|
{
|
2024-03-11 14:08:05 +00:00
|
|
|
$base_path = config('coolify.base_config_path');
|
|
|
|
$proxyType = $this->proxyType();
|
|
|
|
$proxy_path = "$base_path/proxy";
|
2024-03-11 19:17:37 +00:00
|
|
|
// TODO: should use /traefik for already exisiting configurations?
|
|
|
|
// Should move everything except /caddy and /nginx to /traefik
|
|
|
|
// The code needs to be modified as well, so maybe it does not worth it
|
2024-03-11 14:08:05 +00:00
|
|
|
if ($proxyType === ProxyTypes::TRAEFIK_V2->value) {
|
|
|
|
$proxy_path = $proxy_path;
|
|
|
|
} else if ($proxyType === ProxyTypes::CADDY->value) {
|
|
|
|
$proxy_path = $proxy_path . '/caddy';
|
|
|
|
} else if ($proxyType === ProxyTypes::NGINX->value) {
|
|
|
|
$proxy_path = $proxy_path . '/nginx';
|
|
|
|
}
|
|
|
|
return $proxy_path;
|
|
|
|
}
|
2023-09-25 07:17:42 +00:00
|
|
|
public function proxyType()
|
|
|
|
{
|
2024-03-12 11:30:40 +00:00
|
|
|
// $proxyType = $this->proxy->get('type');
|
|
|
|
// if ($proxyType === ProxyTypes::NONE->value) {
|
|
|
|
// return $proxyType;
|
|
|
|
// }
|
2024-03-11 14:08:05 +00:00
|
|
|
// if (is_null($proxyType)) {
|
|
|
|
// $this->proxy->type = ProxyTypes::TRAEFIK_V2->value;
|
|
|
|
// $this->proxy->status = ProxyStatus::EXITED->value;
|
|
|
|
// $this->save();
|
|
|
|
// }
|
2024-03-12 11:45:55 +00:00
|
|
|
return data_get($this->proxy, 'type');
|
2023-09-20 13:42:41 +00:00
|
|
|
}
|
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 13:29:01 +00:00
|
|
|
public function isLocalhost()
|
|
|
|
{
|
|
|
|
return $this->ip === 'host.docker.internal' || $this->id === 0;
|
2023-11-16 13:28:26 +00:00
|
|
|
}
|
2024-01-17 10:52:56 +00:00
|
|
|
static public function buildServers($teamId)
|
|
|
|
{
|
|
|
|
return Server::whereTeamId($teamId)->whereRelation('settings', 'is_reachable', true)->whereRelation('settings', 'is_build_server', true);
|
|
|
|
}
|
2023-11-17 11:47:15 +00:00
|
|
|
public function skipServer()
|
|
|
|
{
|
|
|
|
if ($this->ip === '1.2.3.4') {
|
2024-02-26 13:22:24 +00:00
|
|
|
// ray('skipping 1.2.3.4');
|
2023-11-17 11:47:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
2024-02-26 09:25:21 +00:00
|
|
|
if ($this->settings->force_disabled === true) {
|
2024-02-26 13:22:24 +00:00
|
|
|
// ray('force_disabled');
|
2024-02-26 09:25:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
2023-11-17 11:47:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
2024-02-26 09:25:21 +00:00
|
|
|
public function isForceDisabled()
|
|
|
|
{
|
|
|
|
return $this->settings->force_disabled;
|
|
|
|
}
|
|
|
|
public function forceEnableServer()
|
|
|
|
{
|
|
|
|
$this->settings->update([
|
|
|
|
'force_disabled' => false,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
public function forceDisableServer()
|
|
|
|
{
|
2024-02-25 22:34:01 +00:00
|
|
|
$this->settings->update([
|
2024-02-26 09:25:21 +00:00
|
|
|
'force_disabled' => true,
|
2024-02-25 22:34:01 +00:00
|
|
|
]);
|
2024-02-26 09:25:21 +00:00
|
|
|
$sshKeyFileLocation = "id.root@{$this->uuid}";
|
|
|
|
Storage::disk('ssh-keys')->delete($sshKeyFileLocation);
|
|
|
|
Storage::disk('ssh-mux')->delete($this->muxFilename());
|
2024-02-25 22:34:01 +00:00
|
|
|
}
|
2024-05-08 18:59:58 +00:00
|
|
|
public function checkSentinel()
|
|
|
|
{
|
2024-05-08 12:22:35 +00:00
|
|
|
ray("Checking sentinel on server: {$this->name}");
|
|
|
|
if ($this->is_metrics_enabled) {
|
|
|
|
$sentinel_found = instant_remote_process(["docker inspect coolify-sentinel"], $this, false);
|
|
|
|
$sentinel_found = json_decode($sentinel_found, true);
|
|
|
|
$status = data_get($sentinel_found, '0.State.Status', 'exited');
|
|
|
|
if ($status !== 'running') {
|
|
|
|
ray('Sentinel is not running, starting it...');
|
2024-05-08 18:59:58 +00:00
|
|
|
PullSentinelImageJob::dispatch($this);
|
2024-05-08 12:22:35 +00:00
|
|
|
} else {
|
|
|
|
ray('Sentinel is running');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-05-09 11:25:18 +00:00
|
|
|
public function getMetrics()
|
|
|
|
{
|
|
|
|
if ($this->is_metrics_enabled) {
|
2024-05-09 13:44:32 +00:00
|
|
|
$from = now()->subMinutes(5)->toIso8601ZuluString();
|
|
|
|
$cpu = instant_remote_process(["docker exec coolify-sentinel sh -c 'curl http://localhost:8888/api/cpu/history?from=$from'"], $this, false);
|
|
|
|
$cpu = str($cpu)->explode("\n")->skip(1)->all();
|
2024-05-09 11:25:18 +00:00
|
|
|
$parsedCollection = collect($cpu)->flatMap(function ($item) {
|
|
|
|
return collect(explode("\n", trim($item)))->map(function ($line) {
|
|
|
|
list($time, $value) = explode(',', trim($line));
|
|
|
|
return [(int) $time, (float) $value];
|
|
|
|
});
|
|
|
|
})->toArray();
|
|
|
|
return $parsedCollection;
|
|
|
|
}
|
|
|
|
}
|
2023-12-19 14:36:59 +00:00
|
|
|
public function isServerReady(int $tries = 3)
|
2023-11-16 10:53:37 +00:00
|
|
|
{
|
2023-12-20 10:21:17 +00:00
|
|
|
if ($this->skipServer()) {
|
|
|
|
return false;
|
|
|
|
}
|
2024-05-03 11:31:35 +00:00
|
|
|
$serverUptimeCheckNumber = $this->unreachable_count;
|
|
|
|
if ($this->unreachable_count < $tries) {
|
|
|
|
$serverUptimeCheckNumber = $this->unreachable_count + 1;
|
2024-05-03 08:22:28 +00:00
|
|
|
}
|
2024-05-03 11:31:35 +00:00
|
|
|
if ($this->unreachable_count > $tries) {
|
|
|
|
$serverUptimeCheckNumber = $tries;
|
2024-05-03 08:22:28 +00:00
|
|
|
}
|
2024-05-03 11:31:35 +00:00
|
|
|
|
|
|
|
$serverUptimeCheckNumberMax = $tries;
|
|
|
|
|
|
|
|
// ray('server: ' . $this->name);
|
|
|
|
// ray('serverUptimeCheckNumber: ' . $serverUptimeCheckNumber);
|
|
|
|
// ray('serverUptimeCheckNumberMax: ' . $serverUptimeCheckNumberMax);
|
|
|
|
|
|
|
|
['uptime' => $uptime] = $this->validateConnection();
|
|
|
|
if ($uptime) {
|
|
|
|
if ($this->unreachable_notification_sent === true) {
|
|
|
|
$this->update(['unreachable_notification_sent' => false]);
|
2023-12-19 14:16:08 +00:00
|
|
|
}
|
2024-05-03 11:31:35 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
if ($serverUptimeCheckNumber >= $serverUptimeCheckNumberMax) {
|
|
|
|
// Reached max number of retries
|
|
|
|
if ($this->unreachable_notification_sent === false) {
|
|
|
|
ray('Server unreachable, sending notification...');
|
|
|
|
$this->team?->notify(new Unreachable($this));
|
|
|
|
$this->update(['unreachable_notification_sent' => true]);
|
|
|
|
}
|
|
|
|
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']);
|
|
|
|
}
|
|
|
|
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']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->update([
|
|
|
|
'unreachable_count' => $this->unreachable_count + 1,
|
|
|
|
]);
|
2023-11-16 10:53:37 +00:00
|
|
|
}
|
2024-05-03 11:31:35 +00:00
|
|
|
return false;
|
2023-11-16 10:53:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
public function getDiskUsage()
|
|
|
|
{
|
|
|
|
return instant_remote_process(["df /| tail -1 | awk '{ print $5}' | sed 's/%//g'"], $this, false);
|
|
|
|
}
|
2023-11-28 12:12:25 +00:00
|
|
|
public function definedResources()
|
|
|
|
{
|
2023-11-27 08:39:43 +00:00
|
|
|
$applications = $this->applications();
|
|
|
|
$databases = $this->databases();
|
|
|
|
$services = $this->services();
|
|
|
|
return $applications->concat($databases)->concat($services->get());
|
|
|
|
}
|
2024-02-19 12:28:14 +00:00
|
|
|
public function stopUnmanaged($id)
|
|
|
|
{
|
2024-02-16 22:09:35 +00:00
|
|
|
return instant_remote_process(["docker stop -t 0 $id"], $this);
|
|
|
|
}
|
2024-02-19 12:28:14 +00:00
|
|
|
public function restartUnmanaged($id)
|
|
|
|
{
|
2024-02-16 22:09:35 +00:00
|
|
|
return instant_remote_process(["docker restart $id"], $this);
|
|
|
|
}
|
2024-02-19 12:28:14 +00:00
|
|
|
public function startUnmanaged($id)
|
|
|
|
{
|
2024-02-16 22:09:35 +00:00
|
|
|
return instant_remote_process(["docker start $id"], $this);
|
|
|
|
}
|
2024-05-08 18:59:58 +00:00
|
|
|
public function getContainers(): Collection
|
|
|
|
{
|
|
|
|
$sentinel_found = instant_remote_process(["docker inspect coolify-sentinel"], $this, false);
|
|
|
|
$sentinel_found = json_decode($sentinel_found, true);
|
|
|
|
$status = data_get($sentinel_found, '0.State.Status', 'exited');
|
|
|
|
if ($status === 'running') {
|
|
|
|
$containers = instant_remote_process(['docker exec coolify-sentinel sh -c "curl http://127.0.0.1:8888/api/containers"'], $this, false);
|
|
|
|
if (is_null($containers)) {
|
|
|
|
return collect([]);
|
|
|
|
}
|
|
|
|
$containers = data_get(json_decode($containers, true), 'containers', []);
|
|
|
|
return collect($containers);
|
|
|
|
} else {
|
|
|
|
if ($this->isSwarm()) {
|
|
|
|
$containers = instant_remote_process(["docker service inspect $(docker service ls -q) --format '{{json .}}'"], $this, false);
|
|
|
|
} else {
|
|
|
|
$containers = instant_remote_process(["docker container ls -q"], $this, false);
|
|
|
|
if (!$containers) {
|
|
|
|
return collect([]);
|
|
|
|
}
|
|
|
|
$containers = instant_remote_process(["docker container inspect $(docker container ls -q) --format '{{json .}}'"], $this, false);
|
|
|
|
}
|
|
|
|
if (is_null($containers)) {
|
|
|
|
return collect([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return format_docker_command_output_to_json($containers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public function loadUnmanagedContainers(): Collection
|
2024-02-16 22:09:35 +00:00
|
|
|
{
|
2024-04-09 06:46:00 +00:00
|
|
|
if ($this->isFunctional()) {
|
2024-04-16 13:42:38 +00:00
|
|
|
$containers = instant_remote_process(["docker ps -a --format '{{json .}}'"], $this);
|
2024-04-09 06:46:00 +00:00
|
|
|
$containers = format_docker_command_output_to_json($containers);
|
|
|
|
$containers = $containers->map(function ($container) {
|
|
|
|
$labels = data_get($container, 'Labels');
|
|
|
|
if (!str($labels)->contains("coolify.managed")) {
|
|
|
|
return $container;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
$containers = $containers->filter();
|
|
|
|
return collect($containers);
|
|
|
|
} else {
|
|
|
|
return collect([]);
|
|
|
|
}
|
2024-02-16 22:09:35 +00:00
|
|
|
}
|
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([]));
|
2024-04-10 13:00:46 +00:00
|
|
|
$keydbs = data_get($standaloneDocker, 'keydbs', collect([]));
|
|
|
|
$dragonflies = data_get($standaloneDocker, 'dragonflies', collect([]));
|
|
|
|
$clickhouses = data_get($standaloneDocker, 'clickhouses', collect([]));
|
|
|
|
return $postgresqls->concat($redis)->concat($mongodbs)->concat($mysqls)->concat($mariadbs)->concat($keydbs)->concat($dragonflies)->concat($clickhouses);
|
2024-02-16 20:56:38 +00:00
|
|
|
})->filter(function ($item) {
|
2024-02-19 12:28:14 +00:00
|
|
|
return data_get($item, 'name') !== 'coolify-db';
|
2023-08-29 13:51:30 +00:00
|
|
|
})->flatten();
|
|
|
|
}
|
2023-06-15 13:15:27 +00:00
|
|
|
public function applications()
|
|
|
|
{
|
2024-02-06 14:42:31 +00:00
|
|
|
$applications = $this->destinations()->map(function ($standaloneDocker) {
|
2023-06-15 13:15:27 +00:00
|
|
|
return $standaloneDocker->applications;
|
|
|
|
})->flatten();
|
2024-02-06 14:42:31 +00:00
|
|
|
$additionalApplicationIds = DB::table('additional_destinations')->where('server_id', $this->id)->get('application_id');
|
|
|
|
$additionalApplicationIds = collect($additionalApplicationIds)->map(function ($item) {
|
|
|
|
return $item->application_id;
|
|
|
|
});
|
|
|
|
Application::whereIn('id', $additionalApplicationIds)->get()->each(function ($application) use ($applications) {
|
|
|
|
$applications->push($application);
|
|
|
|
});
|
|
|
|
return $applications;
|
2023-06-15 13:15:27 +00:00
|
|
|
}
|
2023-11-28 11:48:55 +00:00
|
|
|
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 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';
|
|
|
|
}
|
2023-11-16 13:29:01 +00:00
|
|
|
if ($this->isLocalhost()) {
|
2023-10-04 08:57:44 +00:00
|
|
|
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();
|
2024-03-02 14:58:02 +00:00
|
|
|
// $additional_dockers = $this->belongsToMany(StandaloneDocker::class, 'additional_destinations')->withPivot('server_id')->get();
|
|
|
|
// return $standalone_docker->concat($swarm_docker)->concat($additional_dockers);
|
|
|
|
return $standalone_docker->concat($swarm_docker);
|
2023-06-13 08:02:58 +00:00
|
|
|
}
|
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()
|
|
|
|
{
|
2024-01-16 14:19:14 +00:00
|
|
|
if ($this->proxyType() === ProxyTypes::NONE->value || $this->settings->is_build_server) {
|
2023-09-25 07:17:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
2023-10-17 13:40:47 +00:00
|
|
|
return true;
|
2023-09-11 20:29:34 +00:00
|
|
|
}
|
2023-09-25 07:17:42 +00:00
|
|
|
public function isFunctional()
|
|
|
|
{
|
2024-04-09 06:46:00 +00:00
|
|
|
$isFunctional = $this->settings->is_reachable && $this->settings->is_usable && !$this->settings->force_disabled;
|
|
|
|
['private_key_filename' => $private_key_filename, 'mux_filename' => $mux_filename] = server_ssh_configuration($this);
|
|
|
|
if (!$isFunctional) {
|
|
|
|
Storage::disk('ssh-keys')->delete($private_key_filename);
|
|
|
|
Storage::disk('ssh-mux')->delete($mux_filename);
|
|
|
|
}
|
|
|
|
return $isFunctional;
|
2023-09-12 11:14:01 +00:00
|
|
|
}
|
2024-03-04 10:01:14 +00:00
|
|
|
public function isLogDrainEnabled()
|
2023-11-17 11:22:45 +00:00
|
|
|
{
|
2023-12-01 10:13:58 +00: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;
|
2023-11-17 10:13:16 +00:00
|
|
|
}
|
2023-11-28 12:21:32 +00:00
|
|
|
public function validateOS(): bool | Stringable
|
2023-11-21 10:39:19 +00:00
|
|
|
{
|
|
|
|
$os_release = instant_remote_process(['cat /etc/os-release'], $this);
|
2024-01-08 20:59:26 +00:00
|
|
|
$releaseLines = collect(explode("\n", $os_release));
|
2023-11-21 10:39:19 +00:00
|
|
|
$collectedData = collect([]);
|
2024-01-08 20:59:26 +00:00
|
|
|
foreach ($releaseLines as $line) {
|
|
|
|
$item = Str::of($line)->trim();
|
2023-11-21 10:39:19 +00:00
|
|
|
$collectedData->put($item->before('=')->value(), $item->after('=')->lower()->replace('"', '')->value());
|
|
|
|
}
|
|
|
|
$ID = data_get($collectedData, 'ID');
|
2023-11-28 12:12:25 +00:00
|
|
|
// $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) {
|
2024-02-22 10:28:45 +00:00
|
|
|
// ray('supported');
|
2023-11-28 12:17:59 +00:00
|
|
|
return str($supported->first());
|
2023-11-21 10:39:19 +00:00
|
|
|
} else {
|
2024-02-22 10:28:45 +00:00
|
|
|
// ray('not supported');
|
2023-11-21 10:39:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-11-28 14:49:24 +00:00
|
|
|
public function isSwarm()
|
|
|
|
{
|
2023-11-29 09:06:52 +00:00
|
|
|
return data_get($this, 'settings.is_swarm_manager') || data_get($this, 'settings.is_swarm_worker');
|
2023-11-28 14:49:24 +00:00
|
|
|
}
|
2023-12-18 13:01:25 +00: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 09:00:18 +00:00
|
|
|
public function validateConnection()
|
|
|
|
{
|
2024-01-26 07:54:56 +00:00
|
|
|
config()->set('coolify.mux_enabled', false);
|
|
|
|
|
2023-11-28 14:49:24 +00:00
|
|
|
$server = Server::find($this->id);
|
2023-12-19 11:24:43 +00:00
|
|
|
if (!$server) {
|
2024-04-16 13:42:38 +00:00
|
|
|
return ['uptime' => false, 'error' => 'Server not found.'];
|
2023-12-19 11:24:43 +00:00
|
|
|
}
|
2023-12-04 19:47:32 +00:00
|
|
|
if ($server->skipServer()) {
|
2024-04-16 13:42:38 +00:00
|
|
|
return ['uptime' => false, 'error' => 'Server skipped.'];
|
2023-11-17 11:47:15 +00:00
|
|
|
}
|
2024-04-16 13:42:38 +00:00
|
|
|
try {
|
|
|
|
// EC2 does not have `uptime` command, lol
|
|
|
|
instant_remote_process(['ls /'], $server);
|
2023-12-04 19:47:32 +00:00
|
|
|
$server->settings()->update([
|
2023-11-21 10:39:19 +00:00
|
|
|
'is_reachable' => true,
|
|
|
|
]);
|
2023-11-28 14:49:24 +00:00
|
|
|
$server->update([
|
2023-11-21 10:39:19 +00:00
|
|
|
'unreachable_count' => 0,
|
|
|
|
]);
|
2024-04-16 13:42:38 +00:00
|
|
|
if (data_get($server, 'unreachable_notification_sent') === true) {
|
|
|
|
$server->team?->notify(new Revived($server));
|
|
|
|
$server->update(['unreachable_notification_sent' => false]);
|
|
|
|
}
|
|
|
|
return ['uptime' => true, 'error' => null];
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
$server->settings()->update([
|
|
|
|
'is_reachable' => false,
|
|
|
|
]);
|
|
|
|
return ['uptime' => false, 'error' => $e->getMessage()];
|
2023-10-09 09:00:18 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-05 13:40:54 +00:00
|
|
|
public function installDocker()
|
|
|
|
{
|
|
|
|
$activity = InstallDocker::run($this);
|
|
|
|
return $activity;
|
|
|
|
}
|
2023-10-09 09:00:18 +00:00
|
|
|
public function validateDockerEngine($throwError = false)
|
|
|
|
{
|
2024-04-16 13:42:38 +00:00
|
|
|
$dockerBinary = instant_remote_process(["command -v docker"], $this, false, no_sudo: true);
|
2023-10-09 09:00:18 +00:00
|
|
|
if (is_null($dockerBinary)) {
|
|
|
|
$this->settings->is_usable = false;
|
|
|
|
$this->settings->save();
|
|
|
|
if ($throwError) {
|
2023-11-21 10:39:19 +00:00
|
|
|
throw new \Exception('Server is not usable. Docker Engine is not installed.');
|
2023-10-09 09:00:18 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2024-02-22 10:28:45 +00:00
|
|
|
try {
|
2024-02-22 13:46:11 +00:00
|
|
|
instant_remote_process(["docker version"], $this);
|
2024-02-22 10:28:45 +00:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
$this->settings->is_usable = false;
|
|
|
|
$this->settings->save();
|
|
|
|
if ($throwError) {
|
|
|
|
throw new \Exception('Server is not usable. Docker Engine is not running.');
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2023-10-09 09:00:18 +00:00
|
|
|
$this->settings->is_usable = true;
|
|
|
|
$this->settings->save();
|
2024-01-16 14:19:14 +00:00
|
|
|
$this->validateCoolifyNetwork(isSwarm: false, isBuildServer: $this->settings->is_build_server);
|
2023-11-28 14:49:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
2024-02-11 14:32:58 +00:00
|
|
|
public function validateDockerCompose($throwError = false)
|
|
|
|
{
|
|
|
|
$dockerCompose = instant_remote_process(["docker compose version"], $this, false);
|
|
|
|
if (is_null($dockerCompose)) {
|
|
|
|
$this->settings->is_usable = false;
|
|
|
|
$this->settings->save();
|
|
|
|
if ($throwError) {
|
|
|
|
throw new \Exception('Server is not usable. Docker Compose is not installed.');
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->settings->is_usable = true;
|
|
|
|
$this->settings->save();
|
|
|
|
return true;
|
|
|
|
}
|
2023-11-28 14:49:24 +00:00
|
|
|
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 09:00:18 +00: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;
|
|
|
|
}
|
2023-11-21 10:39:19 +00:00
|
|
|
$this->settings->is_reachable = true;
|
2023-10-09 09:00:18 +00:00
|
|
|
$this->settings->is_usable = true;
|
|
|
|
$this->settings->save();
|
|
|
|
return true;
|
|
|
|
}
|
2024-01-16 14:19:14 +00:00
|
|
|
public function validateCoolifyNetwork($isSwarm = false, $isBuildServer = false)
|
2023-10-24 12:31:28 +00:00
|
|
|
{
|
2024-01-16 14:19:14 +00:00
|
|
|
if ($isBuildServer) {
|
|
|
|
return;
|
|
|
|
}
|
2023-11-28 14:49:24 +00:00
|
|
|
if ($isSwarm) {
|
2023-11-28 17:31:04 +00:00
|
|
|
return instant_remote_process(["docker network create --attachable --driver overlay coolify-overlay >/dev/null 2>&1 || true"], $this, false);
|
2023-11-28 14:49:24 +00:00
|
|
|
} else {
|
|
|
|
return instant_remote_process(["docker network create coolify --attachable >/dev/null 2>&1 || true"], $this, false);
|
|
|
|
}
|
2023-10-09 09:00:18 +00:00
|
|
|
}
|
2024-04-16 13:42:38 +00:00
|
|
|
public function isNonRoot()
|
|
|
|
{
|
2024-04-18 09:48:10 +00:00
|
|
|
if ($this->user instanceof Stringable) {
|
|
|
|
return $this->user->value() !== 'root';
|
|
|
|
}
|
2024-04-16 13:42:38 +00:00
|
|
|
return $this->user !== 'root';
|
|
|
|
}
|
2023-07-14 11:38:24 +00:00
|
|
|
}
|