refactoring

This commit is contained in:
Saeed Vaziry
2023-08-04 18:28:04 +02:00
parent 8444323cf4
commit 643318fcfc
349 changed files with 3189 additions and 2729 deletions

View File

@ -15,7 +15,7 @@
* @property string $real_protocol
* @property int $port
* @property string $source
* @property string $mask
* @property ?string $mask
* @property string $note
* @property string $status
* @property Server $server

View File

@ -3,6 +3,7 @@
namespace App\Models;
use App\Contracts\ServerType;
use App\Enums\ServerStatus;
use App\Facades\SSH;
use App\Jobs\Installation\Upgrade;
use App\Jobs\Server\CheckConnection;
@ -232,9 +233,9 @@ public function install(): void
// $this->team->notify(new ServerInstallationStarted($this));
}
public function ssh(string $user = null, bool $defaultKeys = false): \App\Helpers\SSH|SSHFake
public function ssh(string $user = null): \App\Helpers\SSH|SSHFake
{
return SSH::init($this, $user, $defaultKeys);
return SSH::init($this, $user);
}
public function installedPHPVersions(): array
@ -323,7 +324,7 @@ public function getSshUserAttribute(string $value): string
return config('core.ssh_user');
}
public function sshKey(bool $default = false): array
public function sshKey(): array
{
if (app()->environment() == 'testing') {
return [
@ -333,14 +334,6 @@ public function sshKey(bool $default = false): array
];
}
if ($default) {
return [
'public_key' => Str::replace("\n", '', File::get(storage_path(config('core.ssh_public_key_name')))),
'public_key_path' => storage_path(config('core.ssh_public_key_name')),
'private_key_path' => storage_path(config('core.ssh_private_key_name')),
];
}
return [
'public_key' => Str::replace("\n", '', Storage::disk(config('core.key_pairs_disk'))->get($this->id.'.pub')),
'public_key_path' => Storage::disk(config('core.key_pairs_disk'))->path($this->id.'.pub'),
@ -385,4 +378,9 @@ public function getHostnameAttribute(): string
{
return Str::of($this->name)->slug();
}
public function isReady(): bool
{
return $this->status == ServerStatus::READY;
}
}

View File

@ -14,6 +14,7 @@
use App\Jobs\Site\UpdateBranch;
use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
@ -32,7 +33,9 @@
* @property string $path
* @property string $php_version
* @property string $source_control
* @property int $source_control_id
* @property string $repository
* @property string $ssh_key
* @property string $branch
* @property string $status
* @property int $port
@ -52,6 +55,7 @@
* @property string $aliases_string
* @property string $deployment_script_text
* @property string $env
* @property string $ssh_key_name
*/
class Site extends AbstractModel
{
@ -67,7 +71,9 @@ class Site extends AbstractModel
'path',
'php_version',
'source_control',
'source_control_id',
'repository',
'ssh_key',
'branch',
'status',
'port',
@ -81,6 +87,7 @@ class Site extends AbstractModel
'progress' => 'integer',
'auto_deployment' => 'boolean',
'aliases' => 'array',
'source_control_id' => 'integer',
];
protected $appends = [
@ -151,22 +158,21 @@ public function ssls(): HasMany
/**
* @throws SourceControlIsNotConnected
*/
public function sourceControl(): SourceControl|HasOne|null
public function sourceControl(): SourceControl|HasOne|null|Model
{
if (! $this->source_control) {
$sourceControl = null;
if (! $this->source_control && ! $this->source_control_id) {
return null;
}
if ($this->source_control == 'custom') {
return new SourceControl([
'user_id' => $this->id,
'provider' => 'custom',
'token' => '',
'connected' => true,
]);
if ($this->source_control) {
$sourceControl = SourceControl::query()->where('provider', $this->source_control)->first();
}
$sourceControl = SourceControl::query()->where('provider', $this->source_control)->first();
if ($this->source_control_id) {
$sourceControl = SourceControl::query()->find($this->source_control_id);
}
if (! $sourceControl) {
throw new SourceControlIsNotConnected($this->source_control);
@ -180,7 +186,7 @@ public function sourceControl(): SourceControl|HasOne|null
*/
public function getFullRepositoryUrlAttribute()
{
return $this->sourceControl()->provider()->fullRepoUrl($this->repository);
return $this->sourceControl()->provider()->fullRepoUrl($this->repository, $this->ssh_key_name);
}
public function getAliasesStringAttribute(): string
@ -394,4 +400,9 @@ public function updateBranch(string $branch): void
{
dispatch(new UpdateBranch($this, $branch))->onConnection('ssh');
}
public function getSshKeyNameAttribute(): string
{
return str('site_'.$this->id)->toString();
}
}

View File

@ -7,6 +7,8 @@
/**
* @property string $provider
* @property ?string $profile
* @property ?string $url
* @property string $access_token
*/
class SourceControl extends AbstractModel
@ -15,6 +17,8 @@ class SourceControl extends AbstractModel
protected $fillable = [
'provider',
'profile',
'url',
'access_token',
];