fix: a few inputs

This commit is contained in:
Andras Bacsai
2024-12-02 22:49:41 +01:00
parent 7f449c3b72
commit 58988d3686
10 changed files with 190 additions and 102 deletions

View File

@@ -179,7 +179,7 @@ class GetContainersStatus
})->first(); })->first();
if (! $foundTcpProxy) { if (! $foundTcpProxy) {
StartDatabaseProxy::run($database); StartDatabaseProxy::run($database);
// $this->server->team?->notify(new ContainerRestarted("TCP Proxy for {$database->name}", $this->server)); // $this->server->team?->notify(new ContainerRestarted("TCP Proxy for database", $this->server));
} }
} }
} else { } else {
@@ -260,7 +260,7 @@ class GetContainersStatus
$environmentName = data_get($service, 'environment.name'); $environmentName = data_get($service, 'environment.name');
if ($projectUuid && $serviceUuid && $environmentName) { if ($projectUuid && $serviceUuid && $environmentName) {
$url = base_url().'/project/'.$projectUuid.'/'.$environmentName.'/service/'.$serviceUuid; $url = base_url() . '/project/' . $projectUuid . '/' . $environmentName . '/service/' . $serviceUuid;
} else { } else {
$url = null; $url = null;
} }
@@ -286,7 +286,7 @@ class GetContainersStatus
$environment = data_get($application, 'environment.name'); $environment = data_get($application, 'environment.name');
if ($projectUuid && $applicationUuid && $environment) { if ($projectUuid && $applicationUuid && $environment) {
$url = base_url().'/project/'.$projectUuid.'/'.$environment.'/application/'.$applicationUuid; $url = base_url() . '/project/' . $projectUuid . '/' . $environment . '/application/' . $applicationUuid;
} else { } else {
$url = null; $url = null;
} }
@@ -311,7 +311,7 @@ class GetContainersStatus
$applicationUuid = data_get($preview, 'application.uuid'); $applicationUuid = data_get($preview, 'application.uuid');
if ($projectUuid && $applicationUuid && $environmentName) { if ($projectUuid && $applicationUuid && $environmentName) {
$url = base_url().'/project/'.$projectUuid.'/'.$environmentName.'/application/'.$applicationUuid; $url = base_url() . '/project/' . $projectUuid . '/' . $environmentName . '/application/' . $applicationUuid;
} else { } else {
$url = null; $url = null;
} }
@@ -336,7 +336,7 @@ class GetContainersStatus
$databaseUuid = data_get($database, 'uuid'); $databaseUuid = data_get($database, 'uuid');
if ($projectUuid && $databaseUuid && $environmentName) { if ($projectUuid && $databaseUuid && $environmentName) {
$url = base_url().'/project/'.$projectUuid.'/'.$environmentName.'/database/'.$databaseUuid; $url = base_url() . '/project/' . $projectUuid . '/' . $environmentName . '/database/' . $databaseUuid;
} else { } else {
$url = null; $url = null;
} }

View File

@@ -51,7 +51,6 @@ class ServerCheck
$containerReplicates = null; $containerReplicates = null;
$this->isSentinel = true; $this->isSentinel = true;
} else { } else {
['containers' => $this->containers, 'containerReplicates' => $containerReplicates] = $this->server->getContainers(); ['containers' => $this->containers, 'containerReplicates' => $containerReplicates] = $this->server->getContainers();
// ServerStorageCheckJob::dispatch($this->server); // ServerStorageCheckJob::dispatch($this->server);
@@ -148,7 +147,6 @@ class ServerCheck
} else { } else {
$labels = Arr::undot(data_get($container, 'Config.Labels')); $labels = Arr::undot(data_get($container, 'Config.Labels'));
} }
} }
$managed = data_get($labels, 'coolify.managed'); $managed = data_get($labels, 'coolify.managed');
if (! $managed) { if (! $managed) {
@@ -215,7 +213,7 @@ class ServerCheck
if ($isPublic) { if ($isPublic) {
$foundTcpProxy = $this->containers->filter(function ($value, $key) use ($uuid) { $foundTcpProxy = $this->containers->filter(function ($value, $key) use ($uuid) {
if ($this->isSentinel) { if ($this->isSentinel) {
return data_get($value, 'name') === $uuid.'-proxy'; return data_get($value, 'name') === $uuid . '-proxy';
} else { } else {
if ($this->server->isSwarm()) { if ($this->server->isSwarm()) {
@@ -247,7 +245,7 @@ class ServerCheck
if ($isPublic) { if ($isPublic) {
$foundTcpProxy = $this->containers->filter(function ($value, $key) use ($uuid) { $foundTcpProxy = $this->containers->filter(function ($value, $key) use ($uuid) {
if ($this->isSentinel) { if ($this->isSentinel) {
return data_get($value, 'name') === $uuid.'-proxy'; return data_get($value, 'name') === $uuid . '-proxy';
} else { } else {
if ($this->server->isSwarm()) { if ($this->server->isSwarm()) {
return data_get($value, 'Spec.Name') === "coolify-proxy_$uuid"; return data_get($value, 'Spec.Name') === "coolify-proxy_$uuid";
@@ -259,7 +257,7 @@ class ServerCheck
})->first(); })->first();
if (! $foundTcpProxy) { if (! $foundTcpProxy) {
StartDatabaseProxy::run($database); StartDatabaseProxy::run($database);
// $this->server->team?->notify(new ContainerRestarted("TCP Proxy for {$database->name}", $this->server)); // $this->server->team?->notify(new ContainerRestarted("TCP Proxy for database", $this->server));
} }
} }
} }

View File

@@ -1591,16 +1591,32 @@ class ApplicationsController extends Controller
} }
$domains = $request->domains; $domains = $request->domains;
if ($request->has('domains') && $server->isProxyShouldRun()) { if ($request->has('domains') && $server->isProxyShouldRun()) {
$errors = []; $uuid = $request->uuid;
$fqdn = $request->domains; $fqdn = $request->domains;
$fqdn = str($fqdn)->replaceEnd(',', '')->trim(); $fqdn = str($fqdn)->replaceEnd(',', '')->trim();
$fqdn = str($fqdn)->replaceStart(',', '')->trim(); $fqdn = str($fqdn)->replaceStart(',', '')->trim();
$application->fqdn = $fqdn; $errors = [];
if (! $application->settings->is_container_label_readonly_enabled) { $fqdn = str($fqdn)->trim()->explode(',')->map(function ($domain) use (&$errors) {
$customLabels = str(implode('|coolify|', generateLabelsApplication($application)))->replace('|coolify|', "\n"); $domain = trim($domain);
$application->custom_labels = base64_encode($customLabels); if (filter_var($domain, FILTER_VALIDATE_URL) === false || !preg_match('/^https?:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,}/', $domain)) {
$errors[] = 'Invalid domain: '.$domain;
}
return $domain;
});
if (count($errors) > 0) {
return response()->json([
'message' => 'Validation failed.',
'errors' => $errors,
], 422);
}
if (checkIfDomainIsAlreadyUsed($fqdn, $teamId, $uuid)) {
return response()->json([
'message' => 'Validation failed.',
'errors' => [
'domains' => 'One of the domain is already used.',
],
], 422);
} }
$request->offsetUnset('domains');
} }
$dockerComposeDomainsJson = collect(); $dockerComposeDomainsJson = collect();
@@ -2811,3 +2827,30 @@ class ApplicationsController extends Controller
} }
} }
} }
$fqdn = str($fqdn)->replaceStart(',', '')->trim();
$errors = [];
$fqdn = str($fqdn)->trim()->explode(',')->map(function ($domain) use (&$errors) {
if (filter_var($domain, FILTER_VALIDATE_URL) === false) {
$errors[] = 'Invalid domain: ' . $domain;
}
return str($domain)->trim()->lower();
});
if (count($errors) > 0) {
return response()->json([
'message' => 'Validation failed.',
'errors' => $errors,
], 422);
}
if (checkIfDomainIsAlreadyUsed($fqdn, $teamId, $uuid)) {
return response()->json([
'message' => 'Validation failed.',
'errors' => [
'domains' => 'One of the domain is already used.',
],
], 422);
}
}
}
}

View File

@@ -1557,7 +1557,8 @@ class DatabasesController extends Controller
] ]
) )
), ),
]), ]
),
new OA\Response( new OA\Response(
response: 401, response: 401,
ref: '#/components/responses/401', ref: '#/components/responses/401',
@@ -1632,9 +1633,11 @@ class DatabasesController extends Controller
type: 'object', type: 'object',
properties: [ properties: [
'message' => ['type' => 'string', 'example' => 'Database starting request queued.'], 'message' => ['type' => 'string', 'example' => 'Database starting request queued.'],
]) ]
)
), ),
]), ]
),
new OA\Response( new OA\Response(
response: 401, response: 401,
ref: '#/components/responses/401', ref: '#/components/responses/401',
@@ -1708,9 +1711,11 @@ class DatabasesController extends Controller
type: 'object', type: 'object',
properties: [ properties: [
'message' => ['type' => 'string', 'example' => 'Database stopping request queued.'], 'message' => ['type' => 'string', 'example' => 'Database stopping request queued.'],
]) ]
)
), ),
]), ]
),
new OA\Response( new OA\Response(
response: 401, response: 401,
ref: '#/components/responses/401', ref: '#/components/responses/401',
@@ -1784,9 +1789,11 @@ class DatabasesController extends Controller
type: 'object', type: 'object',
properties: [ properties: [
'message' => ['type' => 'string', 'example' => 'Database restaring request queued.'], 'message' => ['type' => 'string', 'example' => 'Database restaring request queued.'],
]) ]
)
), ),
]), ]
),
new OA\Response( new OA\Response(
response: 401, response: 401,
ref: '#/components/responses/401', ref: '#/components/responses/401',

View File

@@ -308,7 +308,7 @@ class ServicesController extends Controller
} }
if ($oneClickService) { if ($oneClickService) {
$service_payload = [ $service_payload = [
'name' => "$oneClickServiceName-".str()->random(10), 'name' => "$oneClickServiceName-" . str()->random(10),
'docker_compose_raw' => base64_decode($oneClickService), 'docker_compose_raw' => base64_decode($oneClickService),
'environment_id' => $environment->id, 'environment_id' => $environment->id,
'service_type' => $oneClickServiceName, 'service_type' => $oneClickServiceName,
@@ -320,7 +320,7 @@ class ServicesController extends Controller
data_set($service_payload, 'connect_to_docker_network', true); data_set($service_payload, 'connect_to_docker_network', true);
} }
$service = Service::create($service_payload); $service = Service::create($service_payload);
$service->name = "$oneClickServiceName-".$service->uuid; $service->name = "$oneClickServiceName-" . $service->uuid;
$service->save(); $service->save();
if ($oneClickDotEnvs?->count() > 0) { if ($oneClickDotEnvs?->count() > 0) {
$oneClickDotEnvs->each(function ($value) use ($service) { $oneClickDotEnvs->each(function ($value) use ($service) {

View File

@@ -280,7 +280,7 @@ class PushServerUpdateJob implements ShouldBeEncrypted, ShouldQueue
})->first(); })->first();
if (! $tcpProxyContainerFound) { if (! $tcpProxyContainerFound) {
StartDatabaseProxy::dispatch($database); StartDatabaseProxy::dispatch($database);
$this->server->team?->notify(new ContainerRestarted("TCP Proxy for {$database->name}", $this->server)); $this->server->team?->notify(new ContainerRestarted("TCP Proxy for database", $this->server));
} else { } else {
} }
} }

View File

@@ -2,6 +2,7 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Visus\Cuid2\Cuid2; use Visus\Cuid2\Cuid2;
@@ -18,4 +19,16 @@ abstract class BaseModel extends Model
} }
}); });
} }
public function name(): Attribute
{
return new Attribute(
get: fn() => sanitize_string($this->getRawOriginal('name')),
);
}
public function image(): Attribute
{
return new Attribute(
get: fn() => sanitize_string($this->getRawOriginal('image')),
);
}
} }

View File

@@ -25,7 +25,7 @@ class StandalonePostgresql extends BaseModel
{ {
static::created(function ($database) { static::created(function ($database) {
LocalPersistentVolume::create([ LocalPersistentVolume::create([
'name' => 'postgres-data-'.$database->uuid, 'name' => 'postgres-data-' . $database->uuid,
'mount_path' => '/var/lib/postgresql/data', 'mount_path' => '/var/lib/postgresql/data',
'host_path' => null, 'host_path' => null,
'resource_id' => $database->id, 'resource_id' => $database->id,
@@ -48,7 +48,7 @@ class StandalonePostgresql extends BaseModel
public function workdir() public function workdir()
{ {
return database_configuration_dir()."/{$this->uuid}"; return database_configuration_dir() . "/{$this->uuid}";
} }
protected function serverStatus(): Attribute protected function serverStatus(): Attribute
@@ -65,7 +65,7 @@ class StandalonePostgresql extends BaseModel
$server = data_get($this, 'destination.server'); $server = data_get($this, 'destination.server');
$workdir = $this->workdir(); $workdir = $this->workdir();
if (str($workdir)->endsWith($this->uuid)) { if (str($workdir)->endsWith($this->uuid)) {
instant_remote_process(['rm -rf '.$this->workdir()], $server, false); instant_remote_process(['rm -rf ' . $this->workdir()], $server, false);
} }
} }
@@ -82,7 +82,7 @@ class StandalonePostgresql extends BaseModel
public function isConfigurationChanged(bool $save = false) public function isConfigurationChanged(bool $save = false)
{ {
$newConfigHash = $this->image.$this->ports_mappings.$this->postgres_initdb_args.$this->postgres_host_auth_method; $newConfigHash = $this->image . $this->ports_mappings . $this->postgres_initdb_args . $this->postgres_host_auth_method;
$newConfigHash .= json_encode($this->environment_variables()->get('value')->sort()); $newConfigHash .= json_encode($this->environment_variables()->get('value')->sort());
$newConfigHash = md5($newConfigHash); $newConfigHash = md5($newConfigHash);
$oldConfigHash = data_get($this, 'config_hash'); $oldConfigHash = data_get($this, 'config_hash');
@@ -186,14 +186,14 @@ class StandalonePostgresql extends BaseModel
public function portsMappings(): Attribute public function portsMappings(): Attribute
{ {
return Attribute::make( return Attribute::make(
set: fn ($value) => $value === '' ? null : $value, set: fn($value) => $value === '' ? null : $value,
); );
} }
public function portsMappingsArray(): Attribute public function portsMappingsArray(): Attribute
{ {
return Attribute::make( return Attribute::make(
get: fn () => is_null($this->ports_mappings) get: fn() => is_null($this->ports_mappings)
? [] ? []
: explode(',', $this->ports_mappings), : explode(',', $this->ports_mappings),
@@ -208,7 +208,7 @@ class StandalonePostgresql extends BaseModel
public function databaseType(): Attribute public function databaseType(): Attribute
{ {
return new Attribute( return new Attribute(
get: fn () => $this->type(), get: fn() => $this->type(),
); );
} }
@@ -220,7 +220,7 @@ class StandalonePostgresql extends BaseModel
protected function internalDbUrl(): Attribute protected function internalDbUrl(): Attribute
{ {
return new Attribute( return new Attribute(
get: fn () => "postgres://{$this->postgres_user}:{$this->postgres_password}@{$this->uuid}:5432/{$this->postgres_db}", get: fn() => "postgres://{$this->postgres_user}:{$this->postgres_password}@{$this->uuid}:5432/{$this->postgres_db}",
); );
} }

View File

@@ -127,6 +127,13 @@ class Team extends Model implements SendsDiscord, SendsEmail
]; ];
} }
public function name(): Attribute
{
return new Attribute(
get: fn() => sanitize_string($this->getRawOriginal('name')),
);
}
public function getRecepients($notification) public function getRecepients($notification)
{ {
$recipients = data_get($notification, 'emails', null); $recipients = data_get($notification, 'emails', null);

View File

@@ -67,31 +67,51 @@ function base_configuration_dir(): string
} }
function application_configuration_dir(): string function application_configuration_dir(): string
{ {
return base_configuration_dir().'/applications'; return base_configuration_dir() . '/applications';
} }
function service_configuration_dir(): string function service_configuration_dir(): string
{ {
return base_configuration_dir().'/services'; return base_configuration_dir() . '/services';
} }
function database_configuration_dir(): string function database_configuration_dir(): string
{ {
return base_configuration_dir().'/databases'; return base_configuration_dir() . '/databases';
} }
function database_proxy_dir($uuid): string function database_proxy_dir($uuid): string
{ {
return base_configuration_dir()."/databases/$uuid/proxy"; return base_configuration_dir() . "/databases/$uuid/proxy";
} }
function backup_dir(): string function backup_dir(): string
{ {
return base_configuration_dir().'/backups'; return base_configuration_dir() . '/backups';
} }
function metrics_dir(): string function metrics_dir(): string
{ {
return base_configuration_dir().'/metrics'; return base_configuration_dir() . '/metrics';
}
function sanitize_string(string $input): string
{
// Remove any HTML/PHP tags
$sanitized = strip_tags($input);
// Convert special characters to HTML entities
$sanitized = htmlspecialchars($sanitized, ENT_QUOTES | ENT_HTML5, 'UTF-8');
// Remove any control characters
$sanitized = preg_replace('/[\x00-\x1F\x7F]/u', '', $sanitized);
// Trim whitespace
$sanitized = trim($sanitized);
return $sanitized;
} }
function generate_readme_file(string $name, string $updated_at): string function generate_readme_file(string $name, string $updated_at): string
{ {
$name = sanitize_string($name);
$updated_at = sanitize_string($updated_at);
return "Resource name: $name\nLatest Deployment Date: $updated_at"; return "Resource name: $name\nLatest Deployment Date: $updated_at";
} }
@@ -122,8 +142,8 @@ function refreshSession(?Team $team = null): void
$team = User::find(Auth::id())->teams->first(); $team = User::find(Auth::id())->teams->first();
} }
} }
Cache::forget('team:'.Auth::id()); Cache::forget('team:' . Auth::id());
Cache::remember('team:'.Auth::id(), 3600, function () use ($team) { Cache::remember('team:' . Auth::id(), 3600, function () use ($team) {
return $team; return $team;
}); });
session(['currentTeam' => $team]); session(['currentTeam' => $team]);
@@ -155,7 +175,7 @@ function handleError(?Throwable $error = null, ?Livewire\Component $livewire = n
$message = null; $message = null;
} }
if ($customErrorMessage) { if ($customErrorMessage) {
$message = $customErrorMessage.' '.$message; $message = $customErrorMessage . ' ' . $message;
} }
if (isset($livewire)) { if (isset($livewire)) {
@@ -410,7 +430,7 @@ function send_user_an_email(MailMessage $mail, string $email, ?string $cc = null
Mail::send( Mail::send(
[], [],
[], [],
fn (Message $message) => $message fn(Message $message) => $message
->to($email) ->to($email)
->replyTo($email) ->replyTo($email)
->cc($cc) ->cc($cc)
@@ -421,7 +441,7 @@ function send_user_an_email(MailMessage $mail, string $email, ?string $cc = null
Mail::send( Mail::send(
[], [],
[], [],
fn (Message $message) => $message fn(Message $message) => $message
->to($email) ->to($email)
->subject($mail->subject) ->subject($mail->subject)
->html((string) $mail->render()) ->html((string) $mail->render())
@@ -657,19 +677,19 @@ function queryResourcesByUuid(string $uuid)
function generateTagDeployWebhook($tag_name) function generateTagDeployWebhook($tag_name)
{ {
$baseUrl = base_url(); $baseUrl = base_url();
$api = Url::fromString($baseUrl).'/api/v1'; $api = Url::fromString($baseUrl) . '/api/v1';
$endpoint = "/deploy?tag=$tag_name"; $endpoint = "/deploy?tag=$tag_name";
return $api.$endpoint; return $api . $endpoint;
} }
function generateDeployWebhook($resource) function generateDeployWebhook($resource)
{ {
$baseUrl = base_url(); $baseUrl = base_url();
$api = Url::fromString($baseUrl).'/api/v1'; $api = Url::fromString($baseUrl) . '/api/v1';
$endpoint = '/deploy'; $endpoint = '/deploy';
$uuid = data_get($resource, 'uuid'); $uuid = data_get($resource, 'uuid');
return $api.$endpoint."?uuid=$uuid&force=false"; return $api . $endpoint . "?uuid=$uuid&force=false";
} }
function generateGitManualWebhook($resource, $type) function generateGitManualWebhook($resource, $type)
{ {
@@ -679,7 +699,7 @@ function generateGitManualWebhook($resource, $type)
if ($resource->getMorphClass() === \App\Models\Application::class) { if ($resource->getMorphClass() === \App\Models\Application::class) {
$baseUrl = base_url(); $baseUrl = base_url();
return Url::fromString($baseUrl)."/webhooks/source/$type/events/manual"; return Url::fromString($baseUrl) . "/webhooks/source/$type/events/manual";
} }
return null; return null;
@@ -1047,7 +1067,7 @@ function validate_dns_entry(string $fqdn, Server $server)
$query = new DNSQuery($dns_server); $query = new DNSQuery($dns_server);
$results = $query->query($host, $type); $results = $query->query($host, $type);
if ($results === false || $query->hasError()) { if ($results === false || $query->hasError()) {
ray('Error: '.$query->getLasterror()); ray('Error: ' . $query->getLasterror());
} else { } else {
foreach ($results as $result) { foreach ($results as $result) {
if ($result->getType() == $type) { if ($result->getType() == $type) {
@@ -1057,7 +1077,7 @@ function validate_dns_entry(string $fqdn, Server $server)
break; break;
} }
if ($result->getData() === $ip) { if ($result->getData() === $ip) {
ray($host.' has IP address '.$result->getData()); ray($host . ' has IP address ' . $result->getData());
ray($result->getString()); ray($result->getString());
$found_matching_ip = true; $found_matching_ip = true;
break; break;
@@ -1105,15 +1125,15 @@ function checkIfDomainIsAlreadyUsed(Collection|array $domains, ?string $teamId =
$applications = Application::ownedByCurrentTeamAPI($teamId)->get(['fqdn', 'uuid']); $applications = Application::ownedByCurrentTeamAPI($teamId)->get(['fqdn', 'uuid']);
$serviceApplications = ServiceApplication::ownedByCurrentTeamAPI($teamId)->get(['fqdn', 'uuid']); $serviceApplications = ServiceApplication::ownedByCurrentTeamAPI($teamId)->get(['fqdn', 'uuid']);
if ($uuid) { if ($uuid) {
$applications = $applications->filter(fn ($app) => $app->uuid !== $uuid); $applications = $applications->filter(fn($app) => $app->uuid !== $uuid);
$serviceApplications = $serviceApplications->filter(fn ($app) => $app->uuid !== $uuid); $serviceApplications = $serviceApplications->filter(fn($app) => $app->uuid !== $uuid);
} }
$domainFound = false; $domainFound = false;
foreach ($applications as $app) { foreach ($applications as $app) {
if (is_null($app->fqdn)) { if (is_null($app->fqdn)) {
continue; continue;
} }
$list_of_domains = collect(explode(',', $app->fqdn))->filter(fn ($fqdn) => $fqdn !== ''); $list_of_domains = collect(explode(',', $app->fqdn))->filter(fn($fqdn) => $fqdn !== '');
foreach ($list_of_domains as $domain) { foreach ($list_of_domains as $domain) {
if (str($domain)->endsWith('/')) { if (str($domain)->endsWith('/')) {
$domain = str($domain)->beforeLast('/'); $domain = str($domain)->beforeLast('/');
@@ -1132,7 +1152,7 @@ function checkIfDomainIsAlreadyUsed(Collection|array $domains, ?string $teamId =
if (str($app->fqdn)->isEmpty()) { if (str($app->fqdn)->isEmpty()) {
continue; continue;
} }
$list_of_domains = collect(explode(',', $app->fqdn))->filter(fn ($fqdn) => $fqdn !== ''); $list_of_domains = collect(explode(',', $app->fqdn))->filter(fn($fqdn) => $fqdn !== '');
foreach ($list_of_domains as $domain) { foreach ($list_of_domains as $domain) {
if (str($domain)->endsWith('/')) { if (str($domain)->endsWith('/')) {
$domain = str($domain)->beforeLast('/'); $domain = str($domain)->beforeLast('/');
@@ -1182,7 +1202,7 @@ function check_domain_usage(ServiceApplication|Application|null $resource = null
}); });
$apps = Application::all(); $apps = Application::all();
foreach ($apps as $app) { foreach ($apps as $app) {
$list_of_domains = collect(explode(',', $app->fqdn))->filter(fn ($fqdn) => $fqdn !== ''); $list_of_domains = collect(explode(',', $app->fqdn))->filter(fn($fqdn) => $fqdn !== '');
foreach ($list_of_domains as $domain) { foreach ($list_of_domains as $domain) {
if (str($domain)->endsWith('/')) { if (str($domain)->endsWith('/')) {
$domain = str($domain)->beforeLast('/'); $domain = str($domain)->beforeLast('/');
@@ -1201,7 +1221,7 @@ function check_domain_usage(ServiceApplication|Application|null $resource = null
} }
$apps = ServiceApplication::all(); $apps = ServiceApplication::all();
foreach ($apps as $app) { foreach ($apps as $app) {
$list_of_domains = collect(explode(',', $app->fqdn))->filter(fn ($fqdn) => $fqdn !== ''); $list_of_domains = collect(explode(',', $app->fqdn))->filter(fn($fqdn) => $fqdn !== '');
foreach ($list_of_domains as $domain) { foreach ($list_of_domains as $domain) {
if (str($domain)->endsWith('/')) { if (str($domain)->endsWith('/')) {
$domain = str($domain)->beforeLast('/'); $domain = str($domain)->beforeLast('/');
@@ -1258,7 +1278,7 @@ function parseCommandsByLineForSudo(Collection $commands, Server $server): array
$commands = $commands->map(function ($line) use ($server) { $commands = $commands->map(function ($line) use ($server) {
if (Str::startsWith($line, 'sudo mkdir -p')) { if (Str::startsWith($line, 'sudo mkdir -p')) {
return "$line && sudo chown -R $server->user:$server->user ".Str::after($line, 'sudo mkdir -p').' && sudo chmod -R o-rwx '.Str::after($line, 'sudo mkdir -p'); return "$line && sudo chown -R $server->user:$server->user " . Str::after($line, 'sudo mkdir -p') . ' && sudo chmod -R o-rwx ' . Str::after($line, 'sudo mkdir -p');
} }
return $line; return $line;
@@ -1290,7 +1310,7 @@ function parseLineForSudo(string $command, Server $server): string
$command = "sudo $command"; $command = "sudo $command";
} }
if (Str::startsWith($command, 'sudo mkdir -p')) { if (Str::startsWith($command, 'sudo mkdir -p')) {
$command = "$command && sudo chown -R $server->user:$server->user ".Str::after($command, 'sudo mkdir -p').' && sudo chmod -R o-rwx '.Str::after($command, 'sudo mkdir -p'); $command = "$command && sudo chown -R $server->user:$server->user " . Str::after($command, 'sudo mkdir -p') . ' && sudo chmod -R o-rwx ' . Str::after($command, 'sudo mkdir -p');
} }
if (str($command)->contains('$(') || str($command)->contains('`')) { if (str($command)->contains('$(') || str($command)->contains('`')) {
$command = str($command)->replace('$(', '$(sudo ')->replace('`', '`sudo ')->value(); $command = str($command)->replace('$(', '$(sudo ')->replace('`', '`sudo ')->value();
@@ -1427,9 +1447,9 @@ function parseServiceVolumes($serviceVolumes, $resource, $topLevelVolumes, $pull
return $volume; return $volume;
} }
if (get_class($resource) === \App\Models\Application::class) { if (get_class($resource) === \App\Models\Application::class) {
$dir = base_configuration_dir().'/applications/'.$resource->uuid; $dir = base_configuration_dir() . '/applications/' . $resource->uuid;
} else { } else {
$dir = base_configuration_dir().'/services/'.$resource->service->uuid; $dir = base_configuration_dir() . '/services/' . $resource->service->uuid;
} }
if ($source->startsWith('.')) { if ($source->startsWith('.')) {
@@ -1439,7 +1459,7 @@ function parseServiceVolumes($serviceVolumes, $resource, $topLevelVolumes, $pull
$source = $source->replaceFirst('~', $dir); $source = $source->replaceFirst('~', $dir);
} }
if ($pull_request_id !== 0) { if ($pull_request_id !== 0) {
$source = $source."-pr-$pull_request_id"; $source = $source . "-pr-$pull_request_id";
} }
if (! $resource?->settings?->is_preserve_repository_enabled || $foundConfig?->is_based_on_git) { if (! $resource?->settings?->is_preserve_repository_enabled || $foundConfig?->is_based_on_git) {
LocalFileVolume::updateOrCreate( LocalFileVolume::updateOrCreate(
@@ -1897,7 +1917,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
if (! $isDatabase) { if (! $isDatabase) {
if ($savedService->fqdn) { if ($savedService->fqdn) {
data_set($savedService, 'fqdn', $savedService->fqdn.','.$fqdn); data_set($savedService, 'fqdn', $savedService->fqdn . ',' . $fqdn);
} else { } else {
data_set($savedService, 'fqdn', $fqdn); data_set($savedService, 'fqdn', $fqdn);
} }
@@ -1957,7 +1977,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
if (Str::lower($forService) === $serviceName) { if (Str::lower($forService) === $serviceName) {
$fqdn = generateFqdn($resource->server, $containerName); $fqdn = generateFqdn($resource->server, $containerName);
} else { } else {
$fqdn = generateFqdn($resource->server, Str::lower($forService).'-'.$resource->uuid); $fqdn = generateFqdn($resource->server, Str::lower($forService) . '-' . $resource->uuid);
} }
if ($port) { if ($port) {
$fqdn = "$fqdn:$port"; $fqdn = "$fqdn:$port";
@@ -2299,7 +2319,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
$name = $volume->before(':'); $name = $volume->before(':');
$mount = $volume->after(':'); $mount = $volume->after(':');
if ($name->startsWith('.') || $name->startsWith('~')) { if ($name->startsWith('.') || $name->startsWith('~')) {
$dir = base_configuration_dir().'/applications/'.$resource->uuid; $dir = base_configuration_dir() . '/applications/' . $resource->uuid;
if ($name->startsWith('.')) { if ($name->startsWith('.')) {
$name = $name->replaceFirst('.', $dir); $name = $name->replaceFirst('.', $dir);
} }
@@ -2307,12 +2327,12 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
$name = $name->replaceFirst('~', $dir); $name = $name->replaceFirst('~', $dir);
} }
if ($pull_request_id !== 0) { if ($pull_request_id !== 0) {
$name = $name."-pr-$pull_request_id"; $name = $name . "-pr-$pull_request_id";
} }
$volume = str("$name:$mount"); $volume = str("$name:$mount");
} else { } else {
if ($pull_request_id !== 0) { if ($pull_request_id !== 0) {
$name = $name."-pr-$pull_request_id"; $name = $name . "-pr-$pull_request_id";
$volume = str("$name:$mount"); $volume = str("$name:$mount");
if ($topLevelVolumes->has($name)) { if ($topLevelVolumes->has($name)) {
$v = $topLevelVolumes->get($name); $v = $topLevelVolumes->get($name);
@@ -2351,7 +2371,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
$name = $volume->before(':'); $name = $volume->before(':');
$mount = $volume->after(':'); $mount = $volume->after(':');
if ($pull_request_id !== 0) { if ($pull_request_id !== 0) {
$name = $name."-pr-$pull_request_id"; $name = $name . "-pr-$pull_request_id";
} }
$volume = str("$name:$mount"); $volume = str("$name:$mount");
} }
@@ -2362,7 +2382,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
$read_only = data_get($volume, 'read_only'); $read_only = data_get($volume, 'read_only');
if ($source && $target) { if ($source && $target) {
if ((str($source)->startsWith('.') || str($source)->startsWith('~'))) { if ((str($source)->startsWith('.') || str($source)->startsWith('~'))) {
$dir = base_configuration_dir().'/applications/'.$resource->uuid; $dir = base_configuration_dir() . '/applications/' . $resource->uuid;
if (str($source, '.')) { if (str($source, '.')) {
$source = str($source)->replaceFirst('.', $dir); $source = str($source)->replaceFirst('.', $dir);
} }
@@ -2370,21 +2390,21 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
$source = str($source)->replaceFirst('~', $dir); $source = str($source)->replaceFirst('~', $dir);
} }
if ($pull_request_id !== 0) { if ($pull_request_id !== 0) {
$source = $source."-pr-$pull_request_id"; $source = $source . "-pr-$pull_request_id";
} }
if ($read_only) { if ($read_only) {
data_set($volume, 'source', $source.':'.$target.':ro'); data_set($volume, 'source', $source . ':' . $target . ':ro');
} else { } else {
data_set($volume, 'source', $source.':'.$target); data_set($volume, 'source', $source . ':' . $target);
} }
} else { } else {
if ($pull_request_id !== 0) { if ($pull_request_id !== 0) {
$source = $source."-pr-$pull_request_id"; $source = $source . "-pr-$pull_request_id";
} }
if ($read_only) { if ($read_only) {
data_set($volume, 'source', $source.':'.$target.':ro'); data_set($volume, 'source', $source . ':' . $target . ':ro');
} else { } else {
data_set($volume, 'source', $source.':'.$target); data_set($volume, 'source', $source . ':' . $target);
} }
if (! str($source)->startsWith('/')) { if (! str($source)->startsWith('/')) {
if ($topLevelVolumes->has($source)) { if ($topLevelVolumes->has($source)) {
@@ -2423,7 +2443,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
$name = $volume->before(':'); $name = $volume->before(':');
$mount = $volume->after(':'); $mount = $volume->after(':');
if ($name->startsWith('.') || $name->startsWith('~')) { if ($name->startsWith('.') || $name->startsWith('~')) {
$dir = base_configuration_dir().'/applications/'.$resource->uuid; $dir = base_configuration_dir() . '/applications/' . $resource->uuid;
if ($name->startsWith('.')) { if ($name->startsWith('.')) {
$name = $name->replaceFirst('.', $dir); $name = $name->replaceFirst('.', $dir);
} }
@@ -2431,13 +2451,13 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
$name = $name->replaceFirst('~', $dir); $name = $name->replaceFirst('~', $dir);
} }
if ($pull_request_id !== 0) { if ($pull_request_id !== 0) {
$name = $name."-pr-$pull_request_id"; $name = $name . "-pr-$pull_request_id";
} }
$volume = str("$name:$mount"); $volume = str("$name:$mount");
} else { } else {
if ($pull_request_id !== 0) { if ($pull_request_id !== 0) {
$uuid = $resource->uuid; $uuid = $resource->uuid;
$name = $uuid."-$name-pr-$pull_request_id"; $name = $uuid . "-$name-pr-$pull_request_id";
$volume = str("$name:$mount"); $volume = str("$name:$mount");
if ($topLevelVolumes->has($name)) { if ($topLevelVolumes->has($name)) {
$v = $topLevelVolumes->get($name); $v = $topLevelVolumes->get($name);
@@ -2456,7 +2476,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
} }
} else { } else {
$uuid = $resource->uuid; $uuid = $resource->uuid;
$name = str($uuid."-$name"); $name = str($uuid . "-$name");
$volume = str("$name:$mount"); $volume = str("$name:$mount");
if ($topLevelVolumes->has($name->value())) { if ($topLevelVolumes->has($name->value())) {
$v = $topLevelVolumes->get($name->value()); $v = $topLevelVolumes->get($name->value());
@@ -2479,7 +2499,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
$name = $volume->before(':'); $name = $volume->before(':');
$mount = $volume->after(':'); $mount = $volume->after(':');
if ($pull_request_id !== 0) { if ($pull_request_id !== 0) {
$name = $name."-pr-$pull_request_id"; $name = $name . "-pr-$pull_request_id";
} }
$volume = str("$name:$mount"); $volume = str("$name:$mount");
} }
@@ -2491,7 +2511,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
if ($source && $target) { if ($source && $target) {
$uuid = $resource->uuid; $uuid = $resource->uuid;
if ((str($source)->startsWith('.') || str($source)->startsWith('~') || str($source)->startsWith('/'))) { if ((str($source)->startsWith('.') || str($source)->startsWith('~') || str($source)->startsWith('/'))) {
$dir = base_configuration_dir().'/applications/'.$resource->uuid; $dir = base_configuration_dir() . '/applications/' . $resource->uuid;
if (str($source, '.')) { if (str($source, '.')) {
$source = str($source)->replaceFirst('.', $dir); $source = str($source)->replaceFirst('.', $dir);
} }
@@ -2499,20 +2519,20 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
$source = str($source)->replaceFirst('~', $dir); $source = str($source)->replaceFirst('~', $dir);
} }
if ($read_only) { if ($read_only) {
data_set($volume, 'source', $source.':'.$target.':ro'); data_set($volume, 'source', $source . ':' . $target . ':ro');
} else { } else {
data_set($volume, 'source', $source.':'.$target); data_set($volume, 'source', $source . ':' . $target);
} }
} else { } else {
if ($pull_request_id === 0) { if ($pull_request_id === 0) {
$source = $uuid."-$source"; $source = $uuid . "-$source";
} else { } else {
$source = $uuid."-$source-pr-$pull_request_id"; $source = $uuid . "-$source-pr-$pull_request_id";
} }
if ($read_only) { if ($read_only) {
data_set($volume, 'source', $source.':'.$target.':ro'); data_set($volume, 'source', $source . ':' . $target . ':ro');
} else { } else {
data_set($volume, 'source', $source.':'.$target); data_set($volume, 'source', $source . ':' . $target);
} }
if (! str($source)->startsWith('/')) { if (! str($source)->startsWith('/')) {
if ($topLevelVolumes->has($source)) { if ($topLevelVolumes->has($source)) {
@@ -2547,7 +2567,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
if ($pull_request_id !== 0 && count($serviceDependencies) > 0) { if ($pull_request_id !== 0 && count($serviceDependencies) > 0) {
$serviceDependencies = $serviceDependencies->map(function ($dependency) use ($pull_request_id) { $serviceDependencies = $serviceDependencies->map(function ($dependency) use ($pull_request_id) {
return $dependency."-pr-$pull_request_id"; return $dependency . "-pr-$pull_request_id";
}); });
data_set($service, 'depends_on', $serviceDependencies->toArray()); data_set($service, 'depends_on', $serviceDependencies->toArray());
} }
@@ -2720,7 +2740,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
if (Str::lower($forService) === $serviceName) { if (Str::lower($forService) === $serviceName) {
$fqdn = generateFqdn($server, $containerName); $fqdn = generateFqdn($server, $containerName);
} else { } else {
$fqdn = generateFqdn($server, Str::lower($forService).'-'.$resource->uuid); $fqdn = generateFqdn($server, Str::lower($forService) . '-' . $resource->uuid);
} }
if ($port) { if ($port) {
$fqdn = "$fqdn:$port"; $fqdn = "$fqdn:$port";
@@ -2924,7 +2944,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
}); });
if ($pull_request_id !== 0) { if ($pull_request_id !== 0) {
$services->each(function ($service, $serviceName) use ($pull_request_id, $services) { $services->each(function ($service, $serviceName) use ($pull_request_id, $services) {
$services[$serviceName."-pr-$pull_request_id"] = $service; $services[$serviceName . "-pr-$pull_request_id"] = $service;
data_forget($services, $serviceName); data_forget($services, $serviceName);
}); });
} }
@@ -3352,22 +3372,22 @@ function newParser(Application|Service $resource, int $pull_request_id = 0, ?int
} }
if ($type->value() === 'bind') { if ($type->value() === 'bind') {
if ($source->value() === '/var/run/docker.sock') { if ($source->value() === '/var/run/docker.sock') {
$volume = $source->value().':'.$target->value(); $volume = $source->value() . ':' . $target->value();
} elseif ($source->value() === '/tmp' || $source->value() === '/tmp/') { } elseif ($source->value() === '/tmp' || $source->value() === '/tmp/') {
$volume = $source->value().':'.$target->value(); $volume = $source->value() . ':' . $target->value();
} else { } else {
if ((int) $resource->compose_parsing_version >= 4) { if ((int) $resource->compose_parsing_version >= 4) {
if ($isApplication) { if ($isApplication) {
$mainDirectory = str(base_configuration_dir().'/applications/'.$uuid); $mainDirectory = str(base_configuration_dir() . '/applications/' . $uuid);
} elseif ($isService) { } elseif ($isService) {
$mainDirectory = str(base_configuration_dir().'/services/'.$uuid); $mainDirectory = str(base_configuration_dir() . '/services/' . $uuid);
} }
} else { } else {
$mainDirectory = str(base_configuration_dir().'/applications/'.$uuid); $mainDirectory = str(base_configuration_dir() . '/applications/' . $uuid);
} }
$source = replaceLocalSource($source, $mainDirectory); $source = replaceLocalSource($source, $mainDirectory);
if ($isApplication && $isPullRequest) { if ($isApplication && $isPullRequest) {
$source = $source."-pr-$pullRequestId"; $source = $source . "-pr-$pullRequestId";
} }
LocalFileVolume::updateOrCreate( LocalFileVolume::updateOrCreate(
[ [
@@ -3387,12 +3407,12 @@ function newParser(Application|Service $resource, int $pull_request_id = 0, ?int
if (isDev()) { if (isDev()) {
if ((int) $resource->compose_parsing_version >= 4) { if ((int) $resource->compose_parsing_version >= 4) {
if ($isApplication) { if ($isApplication) {
$source = $source->replace($mainDirectory, '/var/lib/docker/volumes/coolify_dev_coolify_data/_data/applications/'.$uuid); $source = $source->replace($mainDirectory, '/var/lib/docker/volumes/coolify_dev_coolify_data/_data/applications/' . $uuid);
} elseif ($isService) { } elseif ($isService) {
$source = $source->replace($mainDirectory, '/var/lib/docker/volumes/coolify_dev_coolify_data/_data/services/'.$uuid); $source = $source->replace($mainDirectory, '/var/lib/docker/volumes/coolify_dev_coolify_data/_data/services/' . $uuid);
} }
} else { } else {
$source = $source->replace($mainDirectory, '/var/lib/docker/volumes/coolify_dev_coolify_data/_data/applications/'.$uuid); $source = $source->replace($mainDirectory, '/var/lib/docker/volumes/coolify_dev_coolify_data/_data/applications/' . $uuid);
} }
} }
$volume = "$source:$target"; $volume = "$source:$target";
@@ -4061,7 +4081,7 @@ function isEmailRateLimited(string $limiterKey, int $decaySeconds = 3600, ?calla
$limiterKey, $limiterKey,
$maxAttempts = 0, $maxAttempts = 0,
function () use (&$rateLimited, &$limiterKey, $callbackOnSuccess) { function () use (&$rateLimited, &$limiterKey, $callbackOnSuccess) {
isDev() && loggy('Rate limit not reached for '.$limiterKey); isDev() && loggy('Rate limit not reached for ' . $limiterKey);
$rateLimited = false; $rateLimited = false;
if ($callbackOnSuccess) { if ($callbackOnSuccess) {
@@ -4071,7 +4091,7 @@ function isEmailRateLimited(string $limiterKey, int $decaySeconds = 3600, ?calla
$decaySeconds, $decaySeconds,
); );
if (! $executed) { if (! $executed) {
isDev() && loggy('Rate limit reached for '.$limiterKey.'. Rate limiter will be disabled for '.$decaySeconds.' seconds.'); isDev() && loggy('Rate limit reached for ' . $limiterKey . '. Rate limiter will be disabled for ' . $decaySeconds . ' seconds.');
$rateLimited = true; $rateLimited = true;
} }