mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-02 14:36:17 +00:00
2.x
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Traits\HasTimezoneTimestamps;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
@ -12,6 +13,8 @@
|
||||
*/
|
||||
abstract class AbstractModel extends Model
|
||||
{
|
||||
use HasTimezoneTimestamps;
|
||||
|
||||
public function jsonUpdate(string $field, string $key, mixed $value, bool $save = true): void
|
||||
{
|
||||
$current = $this->{$field};
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Traits\HasTimezoneTimestamps;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
@ -18,12 +19,14 @@
|
||||
* @property Carbon $updated_at
|
||||
* @property User $user
|
||||
* @property Collection<Server> $servers
|
||||
* @property Collection<User> $users
|
||||
* @property Collection<NotificationChannel> $notificationChannels
|
||||
* @property Collection<SourceControl> $sourceControls
|
||||
*/
|
||||
class Project extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use HasTimezoneTimestamps;
|
||||
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
|
@ -3,12 +3,14 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\Actions\Server\CheckConnection;
|
||||
use App\Enums\ServerStatus;
|
||||
use App\Enums\ServiceStatus;
|
||||
use App\Facades\SSH;
|
||||
use App\ServerTypes\ServerType;
|
||||
use App\SSH\Cron\Cron;
|
||||
use App\SSH\OS\OS;
|
||||
use App\SSH\Systemd\Systemd;
|
||||
use App\Support\Testing\SSHFake;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
@ -56,6 +58,7 @@
|
||||
* @property Backup[] $backups
|
||||
* @property Queue[] $daemons
|
||||
* @property SshKey[] $sshKeys
|
||||
* @property Tag[] $tags
|
||||
* @property string $hostname
|
||||
* @property int $updates
|
||||
* @property Carbon $last_update_check
|
||||
@ -138,6 +141,29 @@ public static function boot(): void
|
||||
});
|
||||
}
|
||||
|
||||
public static array $statusColors = [
|
||||
ServerStatus::READY => 'success',
|
||||
ServerStatus::INSTALLING => 'warning',
|
||||
ServerStatus::DISCONNECTED => 'gray',
|
||||
ServerStatus::INSTALLATION_FAILED => 'danger',
|
||||
ServerStatus::UPDATING => 'warning',
|
||||
];
|
||||
|
||||
public function isReady(): bool
|
||||
{
|
||||
return $this->status === ServerStatus::READY;
|
||||
}
|
||||
|
||||
public function isInstalling(): bool
|
||||
{
|
||||
return in_array($this->status, [ServerStatus::INSTALLING, ServerStatus::INSTALLATION_FAILED]);
|
||||
}
|
||||
|
||||
public function isInstallationFailed(): bool
|
||||
{
|
||||
return $this->status === ServerStatus::INSTALLATION_FAILED;
|
||||
}
|
||||
|
||||
public function project(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Project::class, 'project_id');
|
||||
@ -268,7 +294,7 @@ public function defaultService($type): ?Service
|
||||
return $service;
|
||||
}
|
||||
|
||||
public function ssh(?string $user = null): mixed
|
||||
public function ssh(?string $user = null): \App\Helpers\SSH|SSHFake
|
||||
{
|
||||
return SSH::init($this, $user);
|
||||
}
|
||||
@ -295,7 +321,7 @@ public function provider(): \App\ServerProviders\ServerProvider
|
||||
{
|
||||
$providerClass = config('core.server_providers_class')[$this->provider];
|
||||
|
||||
return new $providerClass($this);
|
||||
return new $providerClass($this->serverProvider, $this);
|
||||
}
|
||||
|
||||
public function webserver(?string $version = null): ?Service
|
||||
@ -404,4 +430,13 @@ public function checkForUpdates(): void
|
||||
$this->last_update_check = now();
|
||||
$this->save();
|
||||
}
|
||||
|
||||
public function getAvailableUpdatesAttribute(?int $value): int
|
||||
{
|
||||
if (! $value) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,7 @@
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
/**
|
||||
* @property int $user_id
|
||||
@ -15,6 +16,9 @@
|
||||
* @property bool $connected
|
||||
* @property User $user
|
||||
* @property ?int $project_id
|
||||
* @property Server[] $servers
|
||||
* @property Project $project
|
||||
* @property string $image_url
|
||||
*/
|
||||
class ServerProvider extends AbstractModel
|
||||
{
|
||||
@ -51,6 +55,13 @@ public function servers(): HasMany
|
||||
return $this->hasMany(Server::class, 'provider_id');
|
||||
}
|
||||
|
||||
public function provider(): \App\ServerProviders\ServerProvider
|
||||
{
|
||||
$providerClass = config('core.server_providers_class')[$this->provider];
|
||||
|
||||
return new $providerClass($this);
|
||||
}
|
||||
|
||||
public function project(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Project::class);
|
||||
@ -59,7 +70,54 @@ public function project(): BelongsTo
|
||||
public static function getByProjectId(int $projectId): Builder
|
||||
{
|
||||
return self::query()
|
||||
->where('project_id', $projectId)
|
||||
->orWhereNull('project_id');
|
||||
->where(function (Builder $query) use ($projectId) {
|
||||
$query->where('project_id', $projectId)
|
||||
->orWhereNull('project_id');
|
||||
});
|
||||
}
|
||||
|
||||
public function getImageUrlAttribute(): string
|
||||
{
|
||||
return url('/static/images/'.$this->provider.'.svg');
|
||||
}
|
||||
|
||||
public static function regions(?int $id): array
|
||||
{
|
||||
if (! $id) {
|
||||
return [];
|
||||
}
|
||||
$profile = self::find($id);
|
||||
if (! $profile) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (Cache::get('regions-'.$id)) {
|
||||
return Cache::get('regions-'.$id);
|
||||
}
|
||||
|
||||
$regions = $profile->provider()->regions();
|
||||
Cache::put('regions-'.$id, $regions, 600);
|
||||
|
||||
return $regions;
|
||||
}
|
||||
|
||||
public static function plans(?int $id, ?string $region): array
|
||||
{
|
||||
if (! $id) {
|
||||
return [];
|
||||
}
|
||||
$profile = self::find($id);
|
||||
if (! $profile) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (Cache::get('plans-'.$id.'-'.$region)) {
|
||||
return Cache::get('plans-'.$id.'-'.$region);
|
||||
}
|
||||
|
||||
$plans = $profile->provider()->plans($region);
|
||||
Cache::put('plans-'.$id.'-'.$region, $plans, 600);
|
||||
|
||||
return $plans;
|
||||
}
|
||||
}
|
||||
|
@ -2,10 +2,12 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\SiteStatus;
|
||||
use App\Exceptions\SourceControlIsNotConnected;
|
||||
use App\Exceptions\SSHError;
|
||||
use App\SiteTypes\SiteType;
|
||||
use App\SSH\Services\Webserver\Webserver;
|
||||
use App\Traits\HasProjectThroughServer;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
@ -44,6 +46,7 @@
|
||||
class Site extends AbstractModel
|
||||
{
|
||||
use HasFactory;
|
||||
use HasProjectThroughServer;
|
||||
|
||||
protected $fillable = [
|
||||
'server_id',
|
||||
@ -73,6 +76,13 @@ class Site extends AbstractModel
|
||||
'source_control_id' => 'integer',
|
||||
];
|
||||
|
||||
public static array $statusColors = [
|
||||
SiteStatus::READY => 'success',
|
||||
SiteStatus::INSTALLING => 'warning',
|
||||
SiteStatus::INSTALLATION_FAILED => 'danger',
|
||||
SiteStatus::DELETING => 'danger',
|
||||
];
|
||||
|
||||
public static function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
|
@ -3,8 +3,13 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\UserRole;
|
||||
use App\Traits\HasTimezoneTimestamps;
|
||||
use Carbon\Carbon;
|
||||
use Filament\Models\Contracts\HasTenants;
|
||||
use Filament\Panel;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
@ -34,10 +39,13 @@
|
||||
* @property Project $currentProject
|
||||
* @property Collection<Project> $projects
|
||||
* @property string $role
|
||||
* @property Carbon $created_at
|
||||
* @property Carbon $updated_at
|
||||
*/
|
||||
class User extends Authenticatable
|
||||
class User extends Authenticatable implements HasTenants
|
||||
{
|
||||
use HasFactory;
|
||||
use HasTimezoneTimestamps;
|
||||
use Notifiable;
|
||||
use TwoFactorAuthenticatable;
|
||||
|
||||
@ -111,6 +119,16 @@ public function projects(): BelongsToMany
|
||||
return $this->belongsToMany(Project::class, 'user_project')->withTimestamps();
|
||||
}
|
||||
|
||||
public function getTenants(Panel $panel): Collection
|
||||
{
|
||||
return $this->projects;
|
||||
}
|
||||
|
||||
public function canAccessTenant(Model $tenant): bool
|
||||
{
|
||||
return $this->projects()->whereKey($tenant)->exists();
|
||||
}
|
||||
|
||||
public function currentProject(): HasOne
|
||||
{
|
||||
return $this->HasOne(Project::class, 'id', 'current_project_id');
|
||||
@ -121,7 +139,7 @@ public function createDefaultProject(): Project
|
||||
$project = $this->projects()->first();
|
||||
|
||||
if (! $project) {
|
||||
$project = new Project();
|
||||
$project = new Project;
|
||||
$project->name = 'default';
|
||||
$project->save();
|
||||
|
||||
|
Reference in New Issue
Block a user