This commit is contained in:
Saeed Vaziry
2023-07-02 12:47:50 +02:00
commit 5c72f12490
825 changed files with 41659 additions and 0 deletions

View File

@ -0,0 +1,43 @@
<?php
namespace App\Jobs\Site;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\Site;
class ChangePHPVersion extends Job
{
protected Site $site;
protected string $version;
public function __construct(Site $site, $version)
{
$this->site = $site;
$this->version = $version;
}
public function handle(): void
{
$this->site->php_version = $this->version;
$this->site->server->webserver()->handler()->changePHPVersion($this->site, $this->version);
$this->site->save();
event(
new Broadcast('change-site-php-finished', [
'id' => $this->site->id,
'php_version' => $this->site->php_version,
])
);
}
public function failed(): void
{
event(
new Broadcast('change-site-php-failed', [
'message' => __('Failed to change PHP!'),
'id' => $this->site->id,
])
);
}
}

View File

@ -0,0 +1,34 @@
<?php
namespace App\Jobs\Site;
use App\Jobs\Job;
use App\Models\Site;
use App\SSHCommands\CloneRepositoryCommand;
use Throwable;
class CloneRepository extends Job
{
protected Site $site;
public function __construct(Site $site)
{
$this->site = $site;
}
/**
* @throws Throwable
*/
public function handle(): void
{
$this->site->server->ssh()->exec(
new CloneRepositoryCommand(
$this->site->full_repository_url,
$this->site->path,
$this->site->branch
),
'clone-repository',
$this->site->id
);
}
}

View File

@ -0,0 +1,34 @@
<?php
namespace App\Jobs\Site;
use App\Exceptions\ComposerInstallFailed;
use App\Jobs\Job;
use App\Models\Site;
use App\SSHCommands\ComposerInstallCommand;
use Throwable;
class ComposerInstall extends Job
{
protected Site $site;
public function __construct(Site $site)
{
$this->site = $site;
}
/**
* @throws ComposerInstallFailed
* @throws Throwable
*/
public function handle(): void
{
$this->site->server->ssh()->exec(
new ComposerInstallCommand(
$this->site->path
),
'composer-install',
$this->site->id
);
}
}

21
app/Jobs/Site/CreateVHost.php Executable file
View File

@ -0,0 +1,21 @@
<?php
namespace App\Jobs\Site;
use App\Jobs\Job;
use App\Models\Site;
class CreateVHost extends Job
{
protected Site $site;
public function __construct(Site $site)
{
$this->site = $site;
}
public function handle(): void
{
$this->site->server->webserver()->handler()->createVHost($this->site);
}
}

40
app/Jobs/Site/DeleteSite.php Executable file
View File

@ -0,0 +1,40 @@
<?php
namespace App\Jobs\Site;
use App\Enums\SiteStatus;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\Site;
class DeleteSite extends Job
{
protected Site $site;
public function __construct(Site $site)
{
$this->site = $site;
}
public function handle(): void
{
$this->site->server->webserver()->handler()->deleteSite($this->site);
$this->site->delete();
event(
new Broadcast('delete-site-finished', [
'site' => $this->site,
])
);
}
public function failed(): void
{
$this->site->status = SiteStatus::READY;
$this->site->save();
event(
new Broadcast('delete-site-failed', [
'site' => $this->site,
])
);
}
}

64
app/Jobs/Site/Deploy.php Normal file
View File

@ -0,0 +1,64 @@
<?php
namespace App\Jobs\Site;
use App\Enums\DeploymentStatus;
use App\Events\Broadcast;
use App\Helpers\SSH;
use App\Jobs\Job;
use App\Models\Deployment;
use App\SSHCommands\RunScript;
use Throwable;
class Deploy extends Job
{
protected Deployment $deployment;
protected string $path;
protected string $script;
protected SSH $ssh;
public function __construct(Deployment $deployment, string $path, string $script)
{
$this->deployment = $deployment;
$this->path = $path;
$this->script = $script;
}
/**
* @throws Throwable
*/
public function handle(): void
{
$this->ssh = $this->deployment->site->server->ssh();
$this->ssh->exec(
new RunScript($this->path, $this->script),
'deploy',
$this->deployment->site_id
);
$this->deployment->status = DeploymentStatus::FINISHED;
$this->deployment->log_id = $this->ssh->log->id;
$this->deployment->save();
event(
new Broadcast('deploy-site-finished', [
'deployment' => $this->deployment,
])
);
}
public function failed(): void
{
$this->deployment->status = DeploymentStatus::FAILED;
if ($this->ssh->log) {
$this->deployment->log_id = $this->ssh->log->id;
}
$this->deployment->save();
event(
new Broadcast('deploy-site-failed', [
'deployment' => $this->deployment,
])
);
}
}

View File

@ -0,0 +1,46 @@
<?php
namespace App\Jobs\Site;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\Site;
use App\SSHCommands\EditFileCommand;
use Throwable;
class DeployEnv extends Job
{
protected Site $site;
public function __construct(Site $site)
{
$this->site = $site;
}
/**
* @throws Throwable
*/
public function handle(): void
{
$this->site->server->ssh()->exec(
new EditFileCommand(
$this->site->path.'/.env',
$this->site->env
)
);
event(
new Broadcast('deploy-site-env-finished', [
'site' => $this->site,
])
);
}
public function failed(): void
{
event(
new Broadcast('deploy-site-env-failed', [
'site' => $this->site,
])
);
}
}

View File

@ -0,0 +1,109 @@
<?php
namespace App\Jobs\Site;
use App\Exceptions\FailedToInstallWordpress;
use App\Jobs\Job;
use App\Models\Database;
use App\Models\DatabaseUser;
use App\Models\Site;
use App\SSHCommands\InstallWordpressCommand;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Throwable;
class InstallWordpress extends Job
{
protected Site $site;
protected ?Database $database;
protected ?DatabaseUser $databaseUser;
public function __construct(Site $site)
{
$this->site = $site;
}
/**
* @throws ValidationException
* @throws FailedToInstallWordpress
* @throws Throwable
*/
public function handle(): void
{
$this->setupDatabase();
$result = $this->site->server->ssh()->exec(
new InstallWordpressCommand(
$this->site->path,
$this->site->domain,
$this->database->name,
$this->databaseUser->username,
$this->databaseUser->password,
'localhost',
'wp_',
$this->site->type_data['username'],
$this->site->type_data['password'],
$this->site->type_data['email'],
$this->site->type_data['title'],
),
'install-wordpress',
$this->site->id
);
if (! Str::contains($result, 'Wordpress installed!')) {
throw new FailedToInstallWordpress($result);
}
}
/**
* @throws ValidationException
*/
private function setupDatabase()
{
// create database
$this->database = $this->site->server->databases()->where('name', $this->site->type_data['database'])->first();
if (! $this->database) {
$this->database = new Database([
'server_id' => $this->site->server_id,
'name' => $this->site->type_data['database'],
]);
$this->database->server->database()->handler()->create($this->database->name);
$this->database->is_created = true;
$this->database->save();
}
// create database user
$this->databaseUser = $this->site->server->databaseUsers()->where('username', $this->site->type_data['database_user'])->first();
if (! $this->databaseUser) {
$this->databaseUser = new DatabaseUser([
'server_id' => $this->site->server_id,
'username' => $this->site->type_data['database_user'],
'password' => Str::random(10),
'host' => 'localhost',
]);
$this->databaseUser->save();
$this->databaseUser->server->database()->handler()->createUser($this->databaseUser->username, $this->databaseUser->password, $this->databaseUser->host);
$this->databaseUser->is_created = true;
$this->databaseUser->save();
}
// link database user
$linkedDatabases = $this->databaseUser->databases ?? [];
if (! in_array($this->database->name, $linkedDatabases)) {
$linkedDatabases[] = $this->database->name;
$this->databaseUser->databases = $linkedDatabases;
$this->databaseUser->server->database()->handler()->unlink(
$this->databaseUser->username,
$this->databaseUser->host,
);
$this->databaseUser->server->database()->handler()->link(
$this->databaseUser->username,
$this->databaseUser->host,
$this->databaseUser->databases
);
$this->databaseUser->save();
}
}
}

View File

@ -0,0 +1,53 @@
<?php
namespace App\Jobs\Site;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\Site;
use App\SSHCommands\UpdateBranchCommand;
use Throwable;
class UpdateBranch extends Job
{
protected Site $site;
protected string $branch;
public function __construct(Site $site, string $branch)
{
$this->site = $site;
$this->branch = $branch;
}
/**
* @throws Throwable
*/
public function handle(): void
{
$this->site->server->ssh()->exec(
new UpdateBranchCommand(
$this->site->path,
$this->branch
),
'update-branch',
$this->site->id
);
$this->site->branch = $this->branch;
$this->site->save();
event(
new Broadcast('update-branch-finished', [
'site' => $this->site,
])
);
}
public function failed(): void
{
event(
new Broadcast('update-branch-failed', [
'site' => $this->site,
])
);
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace App\Jobs\Site;
use App\Jobs\Job;
use App\Models\Site;
use App\Models\SourceControl;
use Throwable;
class UpdateSourceControlsRemote extends Job
{
protected SourceControl $sourceControl;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(SourceControl $sourceControl)
{
$this->sourceControl = $sourceControl;
}
/**
* Execute the job.
*
* @throws Throwable
*/
public function handle(): void
{
$sites = Site::query()
->where('user_id', $this->sourceControl->user_id)
->where('source_control', $this->sourceControl->provider)
->get();
foreach ($sites as $site) {
$site->server->ssh()->exec(
'cd '.$site->path.' && git remote set-url origin '.$site->full_repository_url
);
}
}
}

21
app/Jobs/Site/UpdateVHost.php Executable file
View File

@ -0,0 +1,21 @@
<?php
namespace App\Jobs\Site;
use App\Jobs\Job;
use App\Models\Site;
class UpdateVHost extends Job
{
protected Site $site;
public function __construct(Site $site)
{
$this->site = $site;
}
public function handle(): void
{
$this->site->server->webserver()->handler()->updateVHost($this->site);
}
}