lasthourcloud/app/Models/Server.php

106 lines
2.8 KiB
PHP
Raw Normal View History

<?php
namespace App\Models;
2023-05-03 05:23:45 +00:00
use Illuminate\Database\Eloquent\Builder;
use Spatie\SchemalessAttributes\Casts\SchemalessAttributes;
2023-06-20 18:19:31 +00:00
use Spatie\SchemalessAttributes\SchemalessAttributesTrait;
2023-05-03 05:23:45 +00:00
class Server extends BaseModel
{
2023-06-20 18:19:31 +00:00
use SchemalessAttributesTrait;
protected $schemalessAttributes = [
'proxy',
];
public $casts = [
'proxy' => SchemalessAttributes::class,
];
2023-03-30 13:52:19 +00:00
protected static function booted()
{
static::created(function ($server) {
ServerSetting::create([
'server_id' => $server->id,
]);
});
2023-06-15 13:15:27 +00:00
static::deleting(function ($server) {
$server->settings()->delete();
});
2023-03-30 13:52:19 +00:00
}
protected $fillable = [
'name',
'ip',
'user',
'port',
'team_id',
'private_key_id',
2023-06-20 18:19:31 +00:00
'proxy',
];
2023-05-04 13:45:53 +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-06-15 13:15:27 +00:00
public function isEmpty()
{
if ($this->applications()->count() === 0) {
return true;
}
return false;
}
public function applications()
{
return $this->destinations()->map(function ($standaloneDocker) {
return $standaloneDocker->applications;
})->flatten();
}
2023-06-13 08:02:58 +00:00
public function destinations()
{
$standalone_docker = $this->hasMany(StandaloneDocker::class)->get();
$swarm_docker = $this->hasMany(SwarmDocker::class)->get();
return $standalone_docker->concat($swarm_docker);
}
2023-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-27 12:31:42 +00:00
return $this->belongsTo(PrivateKey::class);
}
2023-05-03 05:23:45 +00:00
2023-03-30 13:52:19 +00:00
public function settings()
{
return $this->hasOne(ServerSetting::class);
}
2023-06-16 11:13:09 +00:00
public function muxFilename()
{
return "{$this->ip}_{$this->port}_{$this->user}";
}
2023-06-16 10:00:36 +00:00
static public function ownedByCurrentTeam(array $select = ['*'])
2023-06-07 13:08:35 +00:00
{
2023-06-16 10:00:36 +00:00
$selectArray = collect($select)->concat(['id']);
2023-06-22 12:31:23 +00:00
return Server::whereTeamId(session('currentTeam')->id)->with('settings')->select($selectArray->all())->orderBy('name');
2023-06-07 13:08:35 +00:00
}
2023-05-08 07:16:50 +00:00
static public function validated()
{
2023-06-15 11:51:31 +00:00
return Server::ownedByCurrentTeam()->whereRelation('settings', 'is_reachable', true);
2023-05-08 07:16:50 +00:00
}
2023-06-02 13:15:12 +00:00
2023-06-07 13:08:35 +00:00
static public function destinationsByServer(string $server_id)
2023-05-11 13:20:02 +00:00
{
2023-06-07 13:08:35 +00:00
$server = Server::ownedByCurrentTeam()->get()->where('id', $server_id)->firstOrFail();
$standaloneDocker = collect($server->standaloneDockers->all());
$swarmDocker = collect($server->swarmDockers->all());
return $standaloneDocker->concat($swarmDocker);
2023-05-11 13:20:02 +00:00
}
}