coolify/app/Models/Application.php

311 lines
10 KiB
PHP
Raw Normal View History

<?php
namespace App\Models;
2023-03-30 09:47:04 +02:00
use Illuminate\Database\Eloquent\Casts\Attribute;
2023-05-04 22:29:14 +02:00
use Illuminate\Database\Eloquent\Relations\HasMany;
use Spatie\Activitylog\Models\Activity;
2023-10-09 11:10:04 +02:00
use Illuminate\Support\Str;
class Application extends BaseModel
{
2023-08-11 16:13:53 +02:00
protected $guarded = [];
2023-09-21 17:48:31 +02:00
protected static function booted()
{
2023-10-09 11:10:04 +02:00
static::saving(function ($application) {
if ($application->fqdn == '') {
$application->fqdn = null;
}
$application->forceFill([
'fqdn' => $application->fqdn,
'install_command' => Str::of($application->install_command)->trim(),
'build_command' => Str::of($application->build_command)->trim(),
'start_command' => Str::of($application->start_command)->trim(),
'base_directory' => Str::of($application->base_directory)->trim(),
'publish_directory' => Str::of($application->publish_directory)->trim(),
]);
});
static::created(function ($application) {
ApplicationSetting::create([
'application_id' => $application->id,
]);
});
static::deleting(function ($application) {
$application->settings()->delete();
$storages = $application->persistentStorages()->get();
2023-11-06 18:04:18 +01:00
$server = data_get($application, 'destination.server');
if ($server) {
foreach ($storages as $storage) {
instant_remote_process(["docker volume rm -f $storage->name"], $server, false);
}
}
$application->persistentStorages()->delete();
$application->environment_variables()->delete();
$application->environment_variables_preview()->delete();
});
}
public function settings()
{
return $this->hasOne(ApplicationSetting::class);
}
public function persistentStorages()
{
return $this->morphMany(LocalPersistentVolume::class, 'resource');
}
public function fileStorages()
{
return $this->morphMany(LocalFileVolume::class, 'resource');
}
public function type()
{
2023-08-07 22:14:21 +02:00
return 'application';
}
2023-04-26 13:01:09 +02:00
public function publishDirectory(): Attribute
{
return Attribute::make(
2023-08-11 20:48:52 +02:00
set: fn ($value) => $value ? '/' . ltrim($value, '/') : null,
2023-04-26 13:01:09 +02:00
);
}
2023-05-10 12:22:27 +02:00
public function gitBranchLocation(): Attribute
2023-05-08 12:22:45 +02:00
{
return Attribute::make(
2023-05-10 11:06:54 +02:00
get: function () {
if (!is_null($this->source?->html_url) && !is_null($this->git_repository) && !is_null($this->git_branch)) {
2023-05-10 12:22:27 +02:00
return "{$this->source->html_url}/{$this->git_repository}/tree/{$this->git_branch}";
}
return $this->git_repository;
2023-05-10 12:22:27 +02:00
}
);
}
2023-05-10 12:22:27 +02:00
public function gitCommits(): Attribute
{
return Attribute::make(
get: function () {
if (!is_null($this->source?->html_url) && !is_null($this->git_repository) && !is_null($this->git_branch)) {
return "{$this->source->html_url}/{$this->git_repository}/commits/{$this->git_branch}";
2023-05-10 11:06:54 +02:00
}
return $this->git_repository;
2023-05-10 11:06:54 +02:00
}
2023-05-08 12:22:45 +02:00
);
}
2023-10-10 14:02:43 +02:00
public function dockerfileLocation(): Attribute
{
return Attribute::make(
set: function ($value) {
if (is_null($value) || $value === '') {
return '/Dockerfile';
} else {
if ($value !== '/') {
return Str::start(Str::replaceEnd('/', '', $value), '/');
}
return Str::start($value, '/');
}
}
);
}
2023-04-26 13:01:09 +02:00
public function baseDirectory(): Attribute
{
return Attribute::make(
2023-08-11 20:48:52 +02:00
set: fn ($value) => '/' . ltrim($value, '/'),
2023-04-26 13:01:09 +02:00
);
}
public function portsMappings(): Attribute
{
return Attribute::make(
2023-08-11 20:48:52 +02:00
set: fn ($value) => $value === "" ? null : $value,
);
}
// Normal Deployments
2023-04-24 13:25:02 +02:00
public function portsMappingsArray(): Attribute
2023-03-30 09:47:04 +02:00
{
return Attribute::make(
2023-08-11 20:48:52 +02:00
get: fn () => is_null($this->ports_mappings)
2023-03-30 09:47:04 +02:00
? []
: explode(',', $this->ports_mappings),
2023-03-30 09:47:04 +02:00
);
}
2023-04-24 13:25:02 +02:00
public function portsExposesArray(): Attribute
2023-03-30 09:47:04 +02:00
{
return Attribute::make(
2023-08-11 20:48:52 +02:00
get: fn () => is_null($this->ports_exposes)
2023-03-30 09:47:04 +02:00
? []
2023-04-24 13:25:02 +02:00
: explode(',', $this->ports_exposes)
2023-03-30 09:47:04 +02:00
);
}
2023-05-04 22:29:14 +02:00
public function environment_variables(): HasMany
{
2023-09-08 16:16:59 +02:00
return $this->hasMany(EnvironmentVariable::class)->where('is_preview', false)->orderBy('key', 'asc');
2023-05-04 22:29:14 +02:00
}
public function runtime_environment_variables(): HasMany
{
2023-06-05 12:07:55 +02:00
return $this->hasMany(EnvironmentVariable::class)->where('is_preview', false)->where('key', 'not like', 'NIXPACKS_%');
}
// Preview Deployments
2023-05-05 09:02:50 +02:00
public function build_environment_variables(): HasMany
{
2023-06-05 12:07:55 +02:00
return $this->hasMany(EnvironmentVariable::class)->where('is_preview', false)->where('is_build_time', true)->where('key', 'not like', 'NIXPACKS_%');
2023-05-05 09:02:50 +02:00
}
public function nixpacks_environment_variables(): HasMany
{
2023-06-05 12:07:55 +02:00
return $this->hasMany(EnvironmentVariable::class)->where('is_preview', false)->where('key', 'like', 'NIXPACKS_%');
}
2023-06-05 12:07:55 +02:00
public function environment_variables_preview(): HasMany
{
2023-09-08 16:16:59 +02:00
return $this->hasMany(EnvironmentVariable::class)->where('is_preview', true)->orderBy('key', 'asc');
2023-06-05 12:07:55 +02:00
}
2023-06-05 12:07:55 +02:00
public function runtime_environment_variables_preview(): HasMany
{
return $this->hasMany(EnvironmentVariable::class)->where('is_preview', true)->where('key', 'not like', 'NIXPACKS_%');
}
2023-06-05 12:07:55 +02:00
public function build_environment_variables_preview(): HasMany
{
return $this->hasMany(EnvironmentVariable::class)->where('is_preview', true)->where('is_build_time', true)->where('key', 'not like', 'NIXPACKS_%');
}
2023-06-05 12:07:55 +02:00
public function nixpacks_environment_variables_preview(): HasMany
{
return $this->hasMany(EnvironmentVariable::class)->where('is_preview', true)->where('key', 'like', 'NIXPACKS_%');
}
2023-05-10 13:05:32 +02:00
public function private_key()
{
return $this->belongsTo(PrivateKey::class);
}
public function environment()
{
return $this->belongsTo(Environment::class);
}
2023-05-30 15:52:17 +02:00
public function previews()
{
return $this->hasMany(ApplicationPreview::class);
}
public function destination()
{
return $this->morphTo();
}
public function source()
{
return $this->morphTo();
}
2023-05-31 14:24:20 +02:00
public function deployments(int $skip = 0, int $take = 10)
{
2023-05-31 14:57:42 +02:00
$deployments = ApplicationDeploymentQueue::where('application_id', $this->id)->orderBy('created_at', 'desc');
$count = $deployments->count();
$deployments = $deployments->skip($skip)->take($take)->get();
return [
'count' => $count,
'deployments' => $deployments
];
}
public function get_deployment(string $deployment_uuid)
2023-03-28 15:47:37 +02:00
{
2023-05-03 08:51:03 +02:00
return Activity::where('subject_id', $this->id)->where('properties->type_uuid', '=', $deployment_uuid)->first();
2023-03-28 15:47:37 +02:00
}
2023-05-10 11:43:49 +02:00
public function isDeployable(): bool
{
2023-05-31 11:24:02 +02:00
if ($this->settings->is_auto_deploy_enabled) {
return true;
}
return false;
}
2023-05-31 11:24:02 +02:00
public function isPRDeployable(): bool
{
if ($this->settings->is_preview_deployments_enabled) {
2023-05-10 11:43:49 +02:00
return true;
}
return false;
}
2023-05-10 13:05:32 +02:00
public function deploymentType()
{
2023-07-05 22:10:10 +02:00
if (data_get($this, 'private_key_id')) {
return 'deploy_key';
2023-09-28 21:56:34 +02:00
} else if (data_get($this, 'source')) {
2023-05-10 13:05:32 +02:00
return 'source';
} else {
return 'other';
2023-05-10 13:05:32 +02:00
}
throw new \Exception('No deployment type found');
}
2023-08-11 22:41:47 +02:00
public function could_set_build_commands(): bool
{
if ($this->build_pack === 'nixpacks') {
return true;
}
return false;
}
public function git_based(): bool
{
if ($this->dockerfile) {
2023-08-11 22:41:47 +02:00
return false;
}
2023-10-10 14:02:43 +02:00
if ($this->build_pack === 'dockerimage') {
2023-10-10 11:16:38 +02:00
return false;
}
2023-08-11 22:41:47 +02:00
return true;
}
public function isHealthcheckDisabled(): bool
{
2023-10-10 14:02:43 +02:00
if (data_get($this, 'health_check_enabled') === false) {
return true;
}
return false;
}
2023-10-18 11:20:40 +02:00
public function isConfigurationChanged($save = false)
{
$newConfigHash = $this->fqdn . $this->git_repository . $this->git_branch . $this->git_commit_sha . $this->build_pack . $this->static_image . $this->install_command . $this->build_command . $this->start_command . $this->port_exposes . $this->port_mappings . $this->base_directory . $this->publish_directory . $this->health_check_path . $this->health_check_port . $this->health_check_host . $this->health_check_method . $this->health_check_return_code . $this->health_check_scheme . $this->health_check_response_text . $this->health_check_interval . $this->health_check_timeout . $this->health_check_retries . $this->health_check_start_period . $this->health_check_enabled . $this->limits_memory . $this->limits_swap . $this->limits_swappiness . $this->limits_reservation . $this->limits_cpus . $this->limits_cpuset . $this->limits_cpu_shares . $this->dockerfile . $this->dockerfile_location . $this->custom_labels;
if ($this->pull_request_id === 0) {
$newConfigHash .= json_encode($this->environment_variables->all());
} else {
$newConfigHash .= json_encode($this->environment_variables_preview->all());
}
$newConfigHash = md5($newConfigHash);
$oldConfigHash = data_get($this, 'config_hash');
if ($oldConfigHash === null) {
if ($save) {
$this->config_hash = $newConfigHash;
$this->save();
}
return true;
}
if ($oldConfigHash === $newConfigHash) {
return false;
} else {
if ($save) {
$this->config_hash = $newConfigHash;
$this->save();
}
return true;
}
}
2023-08-07 22:14:21 +02:00
}