refactoring (#116)

- refactoring architecture
- fix incomplete ssh logs
- code editor for scripts in the app
- remove Jobs and SSHCommands
This commit is contained in:
Saeed Vaziry
2024-03-14 20:03:43 +01:00
committed by GitHub
parent cee4a70c3c
commit 428140b931
472 changed files with 24110 additions and 8159 deletions

View File

@ -2,10 +2,8 @@
namespace App\SiteTypes;
use App\Contracts\SiteType;
use App\Jobs\Site\DeleteSite;
use App\Exceptions\SourceControlIsNotConnected;
use App\Models\Site;
use Closure;
abstract class AbstractSiteType implements SiteType
{
@ -16,16 +14,25 @@ public function __construct(Site $site)
$this->site = $site;
}
public function delete(): void
protected function progress(int $percentage): void
{
dispatch(new DeleteSite($this->site))->onConnection('ssh');
$this->site->progress = $percentage;
$this->site->save();
}
protected function progress(int $percentage): Closure
/**
* @throws SourceControlIsNotConnected
*/
protected function deployKey(): void
{
return function () use ($percentage) {
$this->site->progress = $percentage;
$this->site->save();
};
$os = $this->site->server->os();
$os->generateSSHKey($this->site->getSshKeyName());
$this->site->ssh_key = $os->readSSHKey($this->site->getSshKeyName());
$this->site->save();
$this->site->sourceControl()->provider()->deployKey(
$this->site->domain.'-key-'.$this->site->id,
$this->site->repository,
$this->site->ssh_key
);
}
}

View File

@ -3,10 +3,7 @@
namespace App\SiteTypes;
use App\Enums\SiteFeature;
use App\Jobs\Site\CreateVHost;
use Illuminate\Support\Facades\Bus;
use Illuminate\Validation\Rule;
use Throwable;
class PHPBlank extends PHPSite
{
@ -20,7 +17,7 @@ public function supportedFeatures(): array
];
}
public function createValidationRules(array $input): array
public function createRules(array $input): array
{
return [
'php_version' => [
@ -45,23 +42,8 @@ public function data(array $input): array
public function install(): void
{
$chain = [
new CreateVHost($this->site),
$this->progress(65),
function () {
$this->site->php()?->restart();
},
];
$chain[] = function () {
$this->site->installationFinished();
};
Bus::chain($chain)
->catch(function (Throwable $e) {
$this->site->installationFailed($e);
})
->onConnection('ssh-long')
->dispatch();
$this->site->server->webserver()->handler()->createVHost($this->site);
$this->progress(65);
$this->site->php()?->restart();
}
}

View File

@ -3,13 +3,10 @@
namespace App\SiteTypes;
use App\Enums\SiteFeature;
use App\Jobs\Site\CloneRepository;
use App\Jobs\Site\ComposerInstall;
use App\Jobs\Site\CreateVHost;
use App\Jobs\Site\DeployKey;
use Illuminate\Support\Facades\Bus;
use App\Exceptions\SourceControlIsNotConnected;
use App\SSH\Composer\Composer;
use App\SSH\Git\Git;
use Illuminate\Validation\Rule;
use Throwable;
class PHPSite extends AbstractSiteType
{
@ -28,7 +25,7 @@ public function supportedFeatures(): array
];
}
public function createValidationRules(array $input): array
public function createRules(array $input): array
{
return [
'php_version' => [
@ -66,37 +63,24 @@ public function data(array $input): array
];
}
/**
* @throws SourceControlIsNotConnected
*/
public function install(): void
{
$chain = [
new CreateVHost($this->site),
$this->progress(15),
new DeployKey($this->site),
$this->progress(30),
new CloneRepository($this->site),
$this->progress(65),
function () {
$this->site->php()?->restart();
},
];
$this->site->server->webserver()->handler()->createVHost($this->site);
$this->progress(15);
$this->deployKey();
$this->progress(30);
app(Git::class)->clone($this->site);
$this->progress(65);
$this->site->php()?->restart();
if ($this->site->type_data['composer']) {
$chain[] = new ComposerInstall($this->site);
app(Composer::class)->installDependencies($this->site);
}
$chain[] = function () {
$this->site->installationFinished();
};
Bus::chain($chain)
->catch(function (Throwable $e) {
$this->site->installationFailed($e);
})
->onConnection('ssh-long')
->dispatch();
}
public function editValidationRules(array $input): array
public function editRules(array $input): array
{
return [];
}

22
app/SiteTypes/SiteType.php Executable file
View File

@ -0,0 +1,22 @@
<?php
namespace App\SiteTypes;
interface SiteType
{
public function language(): string;
public function supportedFeatures(): array;
public function createRules(array $input): array;
public function createFields(array $input): array;
public function data(array $input): array;
public function install(): void;
public function editRules(array $input): array;
public function edit(): void;
}

View File

@ -6,15 +6,10 @@
use App\Actions\Database\CreateDatabaseUser;
use App\Actions\Database\LinkUser;
use App\Enums\SiteFeature;
use App\Jobs\Site\CreateVHost;
use App\Jobs\Site\InstallWordpress;
use App\Models\Database;
use App\Models\DatabaseUser;
use App\SSHCommands\Wordpress\UpdateWordpressCommand;
use Closure;
use Illuminate\Support\Facades\Bus;
use Illuminate\Validation\Rule;
use Throwable;
class Wordpress extends AbstractSiteType
{
@ -30,7 +25,7 @@ public function supportedFeatures(): array
];
}
public function createValidationRules(array $input): array
public function createRules(array $input): array
{
return [
'php_version' => [
@ -73,7 +68,7 @@ public function createFields(array $input): array
public function data(array $input): array
{
return [
'url' => $this->site->url,
'url' => $this->site->getUrl(),
'title' => $input['title'],
'username' => $input['username'],
'email' => $input['email'],
@ -86,79 +81,37 @@ public function data(array $input): array
public function install(): void
{
$chain = [
new CreateVHost($this->site),
$this->progress(15),
function () {
/** @var Database $database */
$database = app(CreateDatabase::class)->create($this->site->server, [
'name' => $this->site->type_data['database'],
]);
/** @var DatabaseUser $databaseUser */
$databaseUser = app(CreateDatabaseUser::class)->create($this->site->server, [
'username' => $this->site->type_data['database_user'],
'password' => $this->site->type_data['database_password'],
'remote' => false,
'host' => 'localhost',
], [$database->name]);
app(LinkUser::class)->link($databaseUser, [
'databases' => [$database->name],
]);
},
$this->progress(50),
new InstallWordpress($this->site),
$this->progress(75),
function () {
$this->site->php()?->restart();
$this->site->installationFinished();
},
];
Bus::chain($chain)
->catch(function (Throwable $e) {
$this->site->installationFailed($e);
})
->onConnection('ssh-long')
->dispatch();
$this->site->server->webserver()->handler()->createVHost($this->site);
$this->progress(30);
/** @var Database $database */
$database = app(CreateDatabase::class)->create($this->site->server, [
'name' => $this->site->type_data['database'],
]);
/** @var DatabaseUser $databaseUser */
$databaseUser = app(CreateDatabaseUser::class)->create($this->site->server, [
'username' => $this->site->type_data['database_user'],
'password' => $this->site->type_data['database_password'],
'remote' => false,
'host' => 'localhost',
], [$database->name]);
app(LinkUser::class)->link($databaseUser, [
'databases' => [$database->name],
]);
$this->site->php()?->restart();
$this->progress(60);
app(\App\SSH\Wordpress\Wordpress::class)->install($this->site);
}
public function editValidationRules(array $input): array
public function editRules(array $input): array
{
return [
'title' => 'required',
'url' => 'required',
// 'email' => 'required|email',
];
}
public function edit(): void
{
$this->site->status = 'installing';
$this->site->progress = 90;
$this->site->save();
$chain = [
function () {
$this->site->server->ssh()->exec(
new UpdateWordpressCommand(
$this->site->path,
$this->site->type_data['url'],
$this->site->type_data['username'] ?? '',
$this->site->type_data['password'] ?? '',
$this->site->type_data['email'] ?? '',
$this->site->type_data['title'] ?? '',
),
'update-wordpress',
$this->site->id
);
$this->site->installationFinished();
},
];
Bus::chain($chain)
->catch(function (Throwable $e) {
$this->site->installationFailed($e);
})
->onConnection('ssh')
->dispatch();
//
}
}