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,47 @@
<?php
namespace App\Jobs\Backup;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\BackupFile;
use App\Models\Database;
class RestoreDatabase extends Job
{
protected BackupFile $backupFile;
protected Database $database;
public function __construct(BackupFile $backupFile, Database $database)
{
$this->backupFile = $backupFile;
$this->database = $database;
}
public function handle(): void
{
$this->database->server->database()->handler()->restoreBackup($this->backupFile, $this->database->name);
$this->backupFile->status = 'restored';
$this->backupFile->restored_at = now();
$this->backupFile->save();
event(
new Broadcast('backup-restore-finished', [
'file' => $this->backupFile,
])
);
}
public function failed(): void
{
$this->backupFile->status = 'restore_failed';
$this->backupFile->save();
event(
new Broadcast('backup-restore-failed', [
'file' => $this->backupFile,
])
);
}
}

View File

@ -0,0 +1,44 @@
<?php
namespace App\Jobs\Backup;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\BackupFile;
class RunBackup extends Job
{
protected BackupFile $backupFile;
public function __construct(BackupFile $backupFile)
{
$this->backupFile = $backupFile;
}
public function handle(): void
{
if ($this->backupFile->backup->type === 'database') {
$this->backupFile->backup->server->database()->handler()->runBackup($this->backupFile);
}
$this->backupFile->status = 'finished';
$this->backupFile->save();
event(
new Broadcast('run-backup-finished', [
'file' => $this->backupFile,
])
);
}
public function failed(): void
{
$this->backupFile->status = 'failed';
$this->backupFile->save();
event(
new Broadcast('run-backup-failed', [
'file' => $this->backupFile,
])
);
}
}

View File

@ -0,0 +1,48 @@
<?php
namespace App\Jobs\CronJob;
use App\Enums\CronjobStatus;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\CronJob;
use App\SSHCommands\UpdateCronJobsCommand;
use Throwable;
class AddToServer extends Job
{
protected CronJob $cronJob;
public function __construct(CronJob $cronJob)
{
$this->cronJob = $cronJob;
}
/**
* @throws Throwable
*/
public function handle(): void
{
$this->cronJob->server->ssh()->exec(
new UpdateCronJobsCommand($this->cronJob->user, $this->cronJob->crontab),
'update-crontab'
);
$this->cronJob->status = CronjobStatus::READY;
$this->cronJob->save();
event(
new Broadcast('add-cronjob-finished', [
'cronJob' => $this->cronJob,
])
);
}
public function failed(): void
{
$this->cronJob->delete();
event(
new Broadcast('add-cronjob-failed', [
'cronJob' => $this->cronJob,
])
);
}
}

View File

@ -0,0 +1,46 @@
<?php
namespace App\Jobs\CronJob;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\CronJob;
use App\SSHCommands\UpdateCronJobsCommand;
use Throwable;
class RemoveFromServer extends Job
{
protected CronJob $cronJob;
public function __construct(CronJob $cronJob)
{
$this->cronJob = $cronJob;
}
/**
* @throws Throwable
*/
public function handle(): void
{
$this->cronJob->server->ssh()->exec(
new UpdateCronJobsCommand($this->cronJob->user, $this->cronJob->crontab),
'update-crontab'
);
$this->cronJob->delete();
event(
new Broadcast('remove-cronjob-finished', [
'id' => $this->cronJob->id,
])
);
}
public function failed(): void
{
$this->cronJob->save();
event(
new Broadcast('remove-cronjob-failed', [
'cronJob' => $this->cronJob,
])
);
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace App\Jobs\Database;
use App\Enums\DatabaseStatus;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\Database;
class CreateOnServer extends Job
{
protected Database $database;
public function __construct(Database $database)
{
$this->database = $database;
}
public function handle(): void
{
$this->database->server->database()->handler()->create($this->database->name);
$this->database->status = DatabaseStatus::READY;
$this->database->save();
event(new Broadcast('create-database-finished', [
'id' => $this->database->id,
]));
}
public function failed(): void
{
event(new Broadcast('create-database-failed', [
'id' => $this->database->id,
]));
}
}

View File

@ -0,0 +1,37 @@
<?php
namespace App\Jobs\Database;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\Database;
class DeleteFromServer extends Job
{
protected Database $database;
public function __construct(Database $database)
{
$this->database = $database;
}
public function handle(): void
{
$this->database->server->database()->handler()->delete($this->database->name);
event(
new Broadcast('delete-database-finished', [
'id' => $this->database->id,
])
);
$this->database->delete();
}
public function failed(): void
{
event(
new Broadcast('delete-database-failed', [
'id' => $this->database->id,
])
);
}
}

View File

@ -0,0 +1,43 @@
<?php
namespace App\Jobs\DatabaseUser;
use App\Enums\DatabaseUserStatus;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\DatabaseUser;
class CreateOnServer extends Job
{
protected DatabaseUser $databaseUser;
public function __construct(DatabaseUser $databaseUser)
{
$this->databaseUser = $databaseUser;
}
public function handle(): void
{
$this->databaseUser->server->database()->handler()->createUser(
$this->databaseUser->username,
$this->databaseUser->password,
$this->databaseUser->host
);
$this->databaseUser->status = DatabaseUserStatus::READY;
$this->databaseUser->save();
event(
new Broadcast('create-database-user-finished', [
'id' => $this->databaseUser->id,
])
);
}
public function failed(): void
{
event(
new Broadcast('create-database-user-failed', [
'id' => $this->databaseUser->id,
])
);
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace App\Jobs\DatabaseUser;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\DatabaseUser;
class DeleteFromServer extends Job
{
protected DatabaseUser $databaseUser;
public function __construct(DatabaseUser $databaseUser)
{
$this->databaseUser = $databaseUser;
}
public function handle(): void
{
$this->databaseUser->server->database()->handler()->deleteUser(
$this->databaseUser->username,
$this->databaseUser->host
);
event(
new Broadcast('delete-database-user-finished', [
'id' => $this->databaseUser->id,
])
);
$this->databaseUser->delete();
}
public function failed(): void
{
event(
new Broadcast('delete-database-user-failed', [
'id' => $this->databaseUser->id,
])
);
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace App\Jobs\DatabaseUser;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\DatabaseUser;
class LinkUser extends Job
{
protected DatabaseUser $databaseUser;
public function __construct(DatabaseUser $databaseUser)
{
$this->databaseUser = $databaseUser;
}
public function handle(): void
{
$this->databaseUser->server->database()->handler()->link(
$this->databaseUser->username,
$this->databaseUser->host,
$this->databaseUser->databases
);
event(
new Broadcast('link-database-user-finished', [
'id' => $this->databaseUser->id,
])
);
}
public function failed(): void
{
event(
new Broadcast('link-database-user-failed', [
'id' => $this->databaseUser->id,
])
);
}
}

View File

@ -0,0 +1,39 @@
<?php
namespace App\Jobs\DatabaseUser;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\DatabaseUser;
class UnlinkUser extends Job
{
protected DatabaseUser $databaseUser;
public function __construct(DatabaseUser $databaseUser)
{
$this->databaseUser = $databaseUser;
}
public function handle(): void
{
$this->databaseUser->server->database()->handler()->unlink(
$this->databaseUser->username,
$this->databaseUser->host,
);
event(
new Broadcast('unlink-database-user-finished', [
'id' => $this->databaseUser->id,
])
);
}
public function failed(): void
{
event(
new Broadcast('unlink-database-user-failed', [
'id' => $this->databaseUser->id,
])
);
}
}

View File

@ -0,0 +1,48 @@
<?php
namespace App\Jobs\Firewall;
use App\Enums\FirewallRuleStatus;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\FirewallRule;
class AddToServer extends Job
{
protected FirewallRule $firewallRule;
public function __construct(FirewallRule $firewallRule)
{
$this->firewallRule = $firewallRule;
}
public function handle(): void
{
$this->firewallRule->server->firewall()
->handler()
->addRule(
$this->firewallRule->type,
$this->firewallRule->real_protocol,
$this->firewallRule->port,
$this->firewallRule->source,
$this->firewallRule->mask
);
$this->firewallRule->status = FirewallRuleStatus::READY;
$this->firewallRule->save();
event(
new Broadcast('create-firewall-rule-finished', [
'firewallRule' => $this->firewallRule,
])
);
}
public function failed(): void
{
$this->firewallRule->delete();
event(
new Broadcast('create-firewall-rule-failed', [
'firewallRule' => $this->firewallRule,
])
);
}
}

View File

@ -0,0 +1,48 @@
<?php
namespace App\Jobs\Firewall;
use App\Enums\FirewallRuleStatus;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\FirewallRule;
class RemoveFromServer extends Job
{
protected FirewallRule $firewallRule;
public function __construct(FirewallRule $firewallRule)
{
$this->firewallRule = $firewallRule;
}
public function handle(): void
{
$this->firewallRule->server->firewall()
->handler()
->removeRule(
$this->firewallRule->type,
$this->firewallRule->real_protocol,
$this->firewallRule->port,
$this->firewallRule->source,
$this->firewallRule->mask
);
$this->firewallRule->delete();
event(
new Broadcast('delete-firewall-rule-finished', [
'id' => $this->firewallRule->id,
])
);
}
public function failed(): void
{
$this->firewallRule->status = FirewallRuleStatus::READY;
$this->firewallRule->save();
event(
new Broadcast('delete-firewall-rule-failed', [
'firewallRule' => $this->firewallRule,
])
);
}
}

View File

@ -0,0 +1,34 @@
<?php
namespace App\Jobs\Installation;
use App\Events\Broadcast;
use App\Models\Server;
class ContinueInstallation extends InstallationJob
{
protected Server $server;
protected int $attempts;
public function __construct(Server $server)
{
$this->server = $server;
$this->attempts = 2;
}
public function handle(): void
{
if ($this->server->provider()->isRunning()) {
$this->server->install();
} else {
$this->attempts--;
if ($this->attempts > 0) {
sleep(120);
$this->handle();
} else {
event(new Broadcast('install-server-failed', $this->server->toArray()));
}
}
}
}

View File

@ -0,0 +1,75 @@
<?php
namespace App\Jobs\Installation;
use App\Models\Server;
use App\SSHCommands\CreateUserCommand;
use App\SSHCommands\GetPublicKeyCommand;
use Throwable;
class Initialize extends InstallationJob
{
protected Server $server;
protected ?string $asUser;
protected bool $defaultKeys;
public function __construct(Server $server, string $asUser = null, bool $defaultKeys = false)
{
$this->server = $server->refresh();
$this->asUser = $asUser;
$this->defaultKeys = $defaultKeys;
}
/**
* @throws Throwable
*/
public function handle(): void
{
$this->authentication();
$this->publicKey();
// $this->setHostname();
}
/**
* @throws Throwable
*/
protected function authentication(): void
{
$this->server
->ssh($this->asUser ?? $this->server->ssh_user, $this->defaultKeys)
->exec(
new CreateUserCommand(
$this->server->authentication['user'],
$this->server->authentication['pass'],
$this->server->sshKey()['public_key']
),
'create-user'
);
$this->server->ssh_user = config('core.ssh_user');
$this->server->save();
}
/**
* @throws Throwable
*/
protected function publicKey(): void
{
$publicKey = $this->server->ssh()->exec(new GetPublicKeyCommand());
$this->server->update([
'public_key' => $publicKey,
]);
}
/**
* @throws Throwable
*/
protected function setHostname(): void
{
$this->server
->ssh()
->exec('sudo hostnamectl set-hostname '.$this->server->hostname);
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace App\Jobs\Installation;
use App\Models\Server;
use App\SSHCommands\InstallCertbotCommand;
use Throwable;
class InstallCertbot extends InstallationJob
{
protected Server $server;
public function __construct(Server $server)
{
$this->server = $server;
}
/**
* @throws Throwable
*/
public function handle(): void
{
$this->server->ssh()->exec(new InstallCertbotCommand(), 'install-certbot');
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace App\Jobs\Installation;
use App\Models\Server;
use App\SSHCommands\InstallComposerCommand;
use Throwable;
class InstallComposer extends InstallationJob
{
protected Server $server;
public function __construct(Server $server)
{
$this->server = $server;
}
/**
* @throws Throwable
*/
public function handle(): void
{
$this->server->ssh()->exec(new InstallComposerCommand(), 'install-composer');
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace App\Jobs\Installation;
use App\Enums\ServiceStatus;
use App\Exceptions\InstallationFailed;
use App\Models\Service;
use App\SSHCommands\InstallMariadbCommand;
use App\SSHCommands\ServiceStatusCommand;
use Throwable;
class InstallMariadb extends InstallationJob
{
protected Service $service;
public function __construct(Service $service)
{
$this->service = $service;
}
/**
* @throws InstallationFailed
* @throws Throwable
*/
public function handle(): void
{
$ssh = $this->service->server->ssh();
$ssh->exec(new InstallMariadbCommand(), 'install-mariadb');
$status = $ssh->exec(new ServiceStatusCommand($this->service->unit), 'mariadb-status');
$this->service->validateInstall($status);
$this->service->update([
'status' => ServiceStatus::READY,
]);
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace App\Jobs\Installation;
use App\Enums\ServiceStatus;
use App\Exceptions\InstallationFailed;
use App\Models\Service;
use App\SSHCommands\InstallMysqlCommand;
use App\SSHCommands\ServiceStatusCommand;
use Throwable;
class InstallMysql extends InstallationJob
{
protected Service $service;
public function __construct(Service $service)
{
$this->service = $service;
}
/**
* @throws InstallationFailed
* @throws Throwable
*/
public function handle(): void
{
$ssh = $this->service->server->ssh();
$ssh->exec(new InstallMysqlCommand($this->service->version), 'install-mysql');
$status = $ssh->exec(new ServiceStatusCommand($this->service->unit), 'mysql-status');
$this->service->validateInstall($status);
$this->service->update([
'status' => ServiceStatus::READY,
]);
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace App\Jobs\Installation;
use App\Enums\ServiceStatus;
use App\Exceptions\InstallationFailed;
use App\Models\Service;
use App\SSHCommands\InstallNginxCommand;
use App\SSHCommands\ServiceStatusCommand;
use Throwable;
class InstallNginx extends InstallationJob
{
protected Service $service;
public function __construct(Service $service)
{
$this->service = $service;
}
/**
* @throws InstallationFailed
* @throws Throwable
*/
public function handle(): void
{
$ssh = $this->service->server->ssh();
$ssh->exec(new InstallNginxCommand(), 'install-nginx');
$status = $ssh->exec(new ServiceStatusCommand($this->service->unit), 'nginx-status');
$this->service->validateInstall($status);
$this->service->update([
'status' => ServiceStatus::READY,
]);
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace App\Jobs\Installation;
use App\Models\Server;
use App\SSHCommands\InstallNodejsCommand;
use Throwable;
class InstallNodejs extends InstallationJob
{
protected Server $server;
public function __construct(Server $server)
{
$this->server = $server;
}
/**
* @throws Throwable
*/
public function handle(): void
{
$this->server->ssh()->exec(new InstallNodejsCommand(), 'install-nodejs');
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace App\Jobs\Installation;
use App\Enums\ServiceStatus;
use App\Exceptions\InstallationFailed;
use App\Models\Service;
use App\SSHCommands\InstallPHPCommand;
use App\SSHCommands\ServiceStatusCommand;
use Throwable;
class InstallPHP extends InstallationJob
{
protected Service $service;
public function __construct(Service $service)
{
$this->service = $service;
}
/**
* @throws InstallationFailed
* @throws Throwable
*/
public function handle(): void
{
$ssh = $this->service->server->ssh();
$ssh->exec(new InstallPHPCommand($this->service->version), 'install-php');
$status = $ssh->exec(new ServiceStatusCommand($this->service->unit), 'php-status');
$this->service->validateInstall($status);
$this->service->update([
'status' => ServiceStatus::READY,
]);
}
}

View File

@ -0,0 +1,103 @@
<?php
namespace App\Jobs\Installation;
use App\Actions\FirewallRule\CreateRule;
use App\Jobs\Job;
use App\Models\FirewallRule;
use App\Models\Service;
use App\SSHCommands\CreateNginxPHPMyAdminVHostCommand;
use App\SSHCommands\DownloadPHPMyAdminCommand;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Throwable;
class InstallPHPMyAdmin extends Job
{
protected Service $service;
protected ?FirewallRule $firewallRule;
public function __construct(Service $service)
{
$this->service = $service;
}
/**
* @throws Throwable
*/
public function handle(): void
{
$this->setUpFirewall();
$this->downloadSource();
$this->setUpVHost();
$this->restartPHP();
}
/**
* @throws Throwable
*/
private function setUpFirewall(): void
{
$this->firewallRule = FirewallRule::query()
->where('server_id', $this->service->server_id)
->where('port', '54331')
->first();
if ($this->firewallRule) {
$this->firewallRule->source = $this->service->type_data['allowed_ip'];
$this->firewallRule->save();
} else {
$this->firewallRule = app(CreateRule::class)->create(
$this->service->server,
[
'type' => 'allow',
'protocol' => 'tcp',
'port' => '54331',
'source' => $this->service->type_data['allowed_ip'],
'mask' => '0',
]
);
}
}
/**
* @throws Throwable
*/
private function downloadSource(): void
{
$this->service->server->ssh()->exec(
new DownloadPHPMyAdminCommand(),
'download-phpmyadmin'
);
}
/**
* @throws Throwable
*/
private function setUpVHost(): void
{
$vhost = File::get(base_path('system/command-templates/nginx/phpmyadmin-vhost.conf'));
$vhost = Str::replace('__php_version__', $this->service->server->defaultService('php')->version, $vhost);
$this->service->server->ssh()->exec(
new CreateNginxPHPMyAdminVHostCommand($vhost),
'create-phpmyadmin-vhost'
);
}
private function restartPHP(): void
{
$this->service->server->service(
'php',
$this->service->type_data['php']
)?->restart();
}
/**
* @throws Throwable
*/
public function failed(Throwable $throwable): Throwable
{
$this->firewallRule?->removeFromServer();
throw $throwable;
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace App\Jobs\Installation;
use App\Enums\ServiceStatus;
use App\Exceptions\InstallationFailed;
use App\Models\Service;
use App\SSHCommands\InstallRedisCommand;
use App\SSHCommands\ServiceStatusCommand;
use Throwable;
class InstallRedis extends InstallationJob
{
protected Service $service;
public function __construct(Service $service)
{
$this->service = $service;
}
/**
* @throws InstallationFailed
* @throws Throwable
*/
public function handle(): void
{
$ssh = $this->service->server->ssh();
$ssh->exec(new InstallRedisCommand(), 'install-redis');
$status = $ssh->exec(new ServiceStatusCommand($this->service->unit), 'redis-status');
$this->service->validateInstall($status);
$this->service->update([
'status' => ServiceStatus::READY,
]);
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace App\Jobs\Installation;
use App\Models\Server;
use App\SSHCommands\InstallRequirementsCommand;
use Throwable;
class InstallRequirements extends InstallationJob
{
protected Server $server;
public function __construct(Server $server)
{
$this->server = $server;
}
/**
* @throws Throwable
*/
public function handle(): void
{
$this->server->ssh()->exec(new InstallRequirementsCommand(
$this->server->creator->email,
$this->server->creator->name,
), 'install-requirements');
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace App\Jobs\Installation;
use App\Enums\ServiceStatus;
use App\Exceptions\InstallationFailed;
use App\Models\Service;
use App\SSHCommands\InstallSupervisorCommand;
use App\SSHCommands\ServiceStatusCommand;
use Throwable;
class InstallSupervisor extends InstallationJob
{
protected Service $service;
public function __construct(Service $service)
{
$this->service = $service;
}
/**
* @throws InstallationFailed
* @throws Throwable
*/
public function handle(): void
{
$ssh = $this->service->server->ssh();
$ssh->exec(new InstallSupervisorCommand(), 'install-supervisor');
$status = $ssh->exec(new ServiceStatusCommand($this->service->unit), 'supervisor-status');
$this->service->validateInstall($status);
$this->service->update([
'status' => ServiceStatus::READY,
]);
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace App\Jobs\Installation;
use App\Enums\ServiceStatus;
use App\Exceptions\InstallationFailed;
use App\Models\Service;
use App\SSHCommands\InstallUfwCommand;
use App\SSHCommands\ServiceStatusCommand;
use Throwable;
class InstallUfw extends InstallationJob
{
protected Service $service;
public function __construct(Service $service)
{
$this->service = $service;
}
/**
* @throws InstallationFailed
* @throws Throwable
*/
public function handle(): void
{
$ssh = $this->service->server->ssh();
$ssh->exec(new InstallUfwCommand(), 'install-ufw');
$status = $ssh->exec(new ServiceStatusCommand($this->service->unit), 'ufw-status');
$this->service->validateInstall($status);
$this->service->update([
'status' => ServiceStatus::READY,
]);
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace App\Jobs\Installation;
use App\Jobs\LongJob;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
abstract class InstallationJob implements ShouldQueue, ShouldBeUnique
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
use LongJob;
}

View File

@ -0,0 +1,28 @@
<?php
namespace App\Jobs\Installation;
use App\Exceptions\InstallationFailed;
use App\Models\Service;
use App\SSHCommands\UninstallPHPCommand;
use Throwable;
class UninstallPHP extends InstallationJob
{
protected Service $service;
public function __construct(Service $service)
{
$this->service = $service;
}
/**
* @throws InstallationFailed
* @throws Throwable
*/
public function handle(): void
{
$ssh = $this->service->server->ssh();
$ssh->exec(new UninstallPHPCommand($this->service->version), 'uninstall-php');
}
}

View File

@ -0,0 +1,63 @@
<?php
namespace App\Jobs\Installation;
use App\Jobs\Job;
use App\Models\FirewallRule;
use App\Models\Service;
use App\SSHCommands\DeleteNginxPHPMyAdminVHost;
use Exception;
use Throwable;
class UninstallPHPMyAdmin extends Job
{
protected Service $service;
public function __construct(Service $service)
{
$this->service = $service;
}
/**
* @throws Exception
* @throws Throwable
*/
public function handle(): void
{
$this->removeFirewallRule();
$this->deleteVHost();
$this->restartPHP();
}
/**
* @throws Exception
*/
private function removeFirewallRule(): void
{
/** @var ?FirewallRule $rule */
$rule = FirewallRule::query()
->where('server_id', $this->service->server_id)
->where('port', '54331')
->first();
$rule?->removeFromServer();
}
/**
* @throws Throwable
*/
private function deleteVHost(): void
{
$this->service->server->ssh()->exec(
new DeleteNginxPHPMyAdminVHost('/home/vito/phpmyadmin'),
'delete-phpmyadmin-vhost'
);
}
private function restartPHP(): void
{
$this->service->server->service(
'php',
$this->service->type_data['php']
)?->restart();
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace App\Jobs\Installation;
use App\Models\Server;
use App\SSHCommands\UpgradeCommand;
use Throwable;
class Upgrade extends InstallationJob
{
protected Server $server;
public function __construct(Server $server)
{
$this->server = $server;
}
/**
* @throws Throwable
*/
public function handle(): void
{
$this->server->ssh()->exec(new UpgradeCommand(), 'upgrade');
}
}

15
app/Jobs/Job.php Executable file
View File

@ -0,0 +1,15 @@
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
abstract class Job implements ShouldQueue, ShouldBeUnique
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
}

8
app/Jobs/LongJob.php Executable file
View File

@ -0,0 +1,8 @@
<?php
namespace App\Jobs;
trait LongJob
{
public int $timeout = 600;
}

View File

@ -0,0 +1,58 @@
<?php
namespace App\Jobs\PHP;
use App\Events\Broadcast;
use App\Exceptions\ProcessFailed;
use App\Jobs\Job;
use App\Models\Service;
use App\SSHCommands\InstallPHPExtensionCommand;
use Illuminate\Support\Str;
use Throwable;
class InstallPHPExtension extends Job
{
protected Service $service;
protected string $name;
public function __construct(Service $service, string $name)
{
$this->service = $service;
$this->name = $name;
}
/**
* @throws ProcessFailed
* @throws Throwable
*/
public function handle(): void
{
$result = $this->service->server->ssh()->exec(
new InstallPHPExtensionCommand($this->service->version, $this->name),
'install-php-extension'
);
$result = Str::substr($result, strpos($result, '[PHP Modules]'));
if (! Str::contains($result, $this->name)) {
throw new ProcessFailed('Extension failed');
}
$typeData = $this->service->type_data;
$typeData['extensions'][] = $this->name;
$this->service->type_data = $typeData;
$this->service->save();
event(
new Broadcast('install-php-extension-finished', [
'service' => $this->service,
])
);
}
public function failed(): void
{
event(
new Broadcast('install-php-extension-failed', [
'service' => $this->service,
])
);
}
}

View File

@ -0,0 +1,46 @@
<?php
namespace App\Jobs\PHP;
use App\Enums\ServiceStatus;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\Service;
use App\SSHCommands\ChangeDefaultPHPCommand;
use Throwable;
class SetDefaultCli extends Job
{
protected Service $service;
public function __construct(Service $service)
{
$this->service = $service;
}
/**
* @throws Throwable
*/
public function handle(): void
{
$this->service->server->ssh()->exec(
new ChangeDefaultPHPCommand($this->service->version),
'change-default-php'
);
$this->service->server->defaultService('php')->update(['is_default' => 0]);
$this->service->update(['is_default' => 1]);
$this->service->update(['status' => ServiceStatus::READY]);
event(
new Broadcast('set-default-cli-finished', [
'defaultPHP' => $this->service->server->defaultService('php'),
])
);
}
public function failed(): void
{
event(new Broadcast('set-default-cli-failed', [
'defaultPHP' => $this->service->server->defaultService('php'),
]));
}
}

View File

@ -0,0 +1,58 @@
<?php
namespace App\Jobs\PHP;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\Service;
use App\SSHCommands\UpdatePHPSettingsCommand;
use Throwable;
class UpdatePHPSettings extends Job
{
protected Service $service;
protected array $settings;
public function __construct(Service $service, array $settings)
{
$this->service = $service;
$this->settings = $settings;
}
/**
* Execute the job.
*
* @throws Throwable
*/
public function handle(): void
{
$commands = [];
foreach ($this->settings as $key => $value) {
$commands[] = new UpdatePHPSettingsCommand(
$this->service->version,
$key,
$value.' '.config('core.php_settings_unit')[$key]
);
}
$this->service->server->ssh()->exec($commands, 'update-php-settings');
$typeData = $this->service->type_data;
$typeData['settings'] = $this->settings;
$this->service->type_data = $typeData;
$this->service->save();
event(
new Broadcast('update-php-settings-finished', [
'service' => $this->service,
])
);
}
public function failed(): void
{
event(
new Broadcast('update-php-settings-failed', [
'service' => $this->service,
])
);
}
}

49
app/Jobs/Queue/Deploy.php Normal file
View File

@ -0,0 +1,49 @@
<?php
namespace App\Jobs\Queue;
use App\Enums\QueueStatus;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\Queue;
class Deploy extends Job
{
protected Queue $worker;
public function __construct(Queue $worker)
{
$this->worker = $worker;
}
public function handle(): void
{
$this->worker->server->processManager()->handler()->create(
$this->worker->id,
$this->worker->command,
$this->worker->user,
$this->worker->auto_start,
$this->worker->auto_restart,
$this->worker->numprocs,
$this->worker->log_file,
$this->worker->site_id
);
$this->worker->status = QueueStatus::RUNNING;
$this->worker->save();
event(
new Broadcast('deploy-queue-finished', [
'queue' => $this->worker,
])
);
}
public function failed(): void
{
$this->worker->delete();
event(
new Broadcast('deploy-queue-failed', [
'queue' => $this->worker,
])
);
}
}

View File

@ -0,0 +1,37 @@
<?php
namespace App\Jobs\Queue;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\Queue;
class GetLogs extends Job
{
protected Queue $worker;
public function __construct(Queue $worker)
{
$this->worker = $worker;
}
public function handle(): void
{
$logs = $this->worker->server->processManager()->handler()->getLogs($this->worker->log_file);
event(
new Broadcast('get-logs-finished', [
'id' => $this->worker->id,
'logs' => $logs,
])
);
}
public function failed(): void
{
event(
new Broadcast('get-logs-failed', [
'message' => __('Failed to download the logs!'),
])
);
}
}

68
app/Jobs/Queue/Manage.php Normal file
View File

@ -0,0 +1,68 @@
<?php
namespace App\Jobs\Queue;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\Queue;
class Manage extends Job
{
protected Queue $worker;
protected string $action;
protected string $successStatus;
protected string $failStatus;
protected string $failMessage;
public function __construct(
Queue $worker,
string $action,
string $successStatus,
string $failStatus,
string $failMessage,
) {
$this->worker = $worker;
$this->action = $action;
$this->successStatus = $successStatus;
$this->failStatus = $failStatus;
$this->failMessage = $failMessage;
}
public function handle(): void
{
switch ($this->action) {
case 'start':
$this->worker->server->processManager()->handler()->start($this->worker->id, $this->worker->site_id);
break;
case 'stop':
$this->worker->server->processManager()->handler()->stop($this->worker->id, $this->worker->site_id);
break;
case 'restart':
$this->worker->server->processManager()->handler()->restart($this->worker->id, $this->worker->site_id);
break;
}
$this->worker->status = $this->successStatus;
$this->worker->save();
event(
new Broadcast('manage-queue-finished', [
'queue' => $this->worker,
])
);
}
public function failed(): void
{
$this->worker->status = $this->failStatus;
$this->worker->save();
event(
new Broadcast('manage-queue-failed', [
'message' => $this->failMessage,
'queue' => $this->worker,
])
);
}
}

41
app/Jobs/Queue/Remove.php Normal file
View File

@ -0,0 +1,41 @@
<?php
namespace App\Jobs\Queue;
use App\Enums\QueueStatus;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\Queue;
class Remove extends Job
{
protected Queue $worker;
public function __construct(Queue $worker)
{
$this->worker = $worker;
}
public function handle(): void
{
$this->worker->server->processManager()->handler()->delete($this->worker->id, $this->worker->site_id);
$this->worker->delete();
event(
new Broadcast('remove-queue-finished', [
'id' => $this->worker->id,
])
);
}
public function failed(): void
{
$this->worker->status = QueueStatus::FAILED;
$this->worker->save();
event(
new Broadcast('remove-queue-failed', [
'message' => __('Failed to delete worker!'),
'id' => $this->worker->id,
])
);
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace App\Jobs\Redirect;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\Redirect;
class AddToServer extends Job
{
protected Redirect $redirect;
public function __construct(Redirect $redirect)
{
$this->redirect = $redirect;
}
public function handle(): void
{
/** @var array $redirects */
$redirects = Redirect::query()->where('site_id', $this->redirect->site_id)->get();
$this->redirect->site->server->webserver()->handler()->updateRedirects($this->redirect->site, $redirects);
$this->redirect->status = 'ready';
$this->redirect->save();
event(
new Broadcast('create-redirect-finished', [
'redirect' => $this->redirect,
])
);
}
public function failed(): void
{
$this->redirect->status = 'failed';
$this->redirect->delete();
event(
new Broadcast('create-redirect-failed', [
'redirect' => $this->redirect,
])
);
}
}

View File

@ -0,0 +1,44 @@
<?php
namespace App\Jobs\Redirect;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\Redirect;
class DeleteFromServer extends Job
{
protected Redirect $redirect;
public function __construct(Redirect $redirect)
{
$this->redirect = $redirect;
}
public function handle(): void
{
/** @var array $redirects */
$redirects = Redirect::query()
->where('site_id', $this->redirect->site_id)
->where('id', '!=', $this->redirect->id)
->get();
$this->redirect->site->server->webserver()->handler()->updateRedirects($this->redirect->site, $redirects);
$this->redirect->delete();
event(
new Broadcast('delete-redirect-finished', [
'id' => $this->redirect->id,
])
);
}
public function failed(): void
{
$this->redirect->status = 'failed';
$this->redirect->save();
event(
new Broadcast('delete-redirect-failed', [
'redirect' => $this->redirect,
])
);
}
}

View File

@ -0,0 +1,61 @@
<?php
namespace App\Jobs\Script;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\Script;
use App\Models\ScriptExecution;
use App\Models\Server;
use Throwable;
class ExecuteOn extends Job
{
protected Script $script;
protected Server $server;
protected string $user;
protected ScriptExecution $scriptExecution;
public function __construct(Script $script, Server $server, string $user)
{
$this->script = $script;
$this->server = $server;
$this->user = $user;
}
/**
* @throws Throwable
*/
public function handle(): void
{
$this->scriptExecution = $this->script->executions()->create([
'server_id' => $this->server->id,
'user' => $this->user,
]);
$this->server->ssh($this->scriptExecution->user)->exec(
$this->script->content,
'execute-script'
);
$this->scriptExecution->finished_at = now();
$this->scriptExecution->save();
event(
new Broadcast('execute-script-finished', [
'execution' => $this->scriptExecution,
])
);
}
public function failed(): void
{
$this->scriptExecution->finished_at = now();
$this->scriptExecution->save();
event(
new Broadcast('execute-script-failed', [
'execution' => $this->scriptExecution,
])
);
}
}

View File

@ -0,0 +1,49 @@
<?php
namespace App\Jobs\Server;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\Server;
use Throwable;
class CheckConnection extends Job
{
protected Server $server;
public function __construct(Server $server)
{
$this->server = $server;
}
/**
* @throws Throwable
*/
public function handle(): void
{
$status = $this->server->status;
$this->server->ssh()->connect();
$this->server->refresh();
if ($status == 'disconnected') {
$this->server->status = 'ready';
$this->server->save();
}
event(
new Broadcast('server-status-finished', [
'server' => $this->server,
])
);
}
public function failed(): void
{
$this->server->status = 'disconnected';
$this->server->save();
/** @todo notify */
event(
new Broadcast('server-status-failed', [
'server' => $this->server,
])
);
}
}

View File

@ -0,0 +1,45 @@
<?php
namespace App\Jobs\Server;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\Server;
use App\SSHCommands\RebootCommand;
use Throwable;
class RebootServer extends Job
{
protected Server $server;
public function __construct(Server $server)
{
$this->server = $server;
}
/**
* @throws Throwable
*/
public function handle(): void
{
$this->server->ssh()->exec(new RebootCommand(), 'reboot');
event(
new Broadcast('reboot-server-finished', [
'message' => __('The server is being rebooted. It can take several minutes to boot up'),
'id' => $this->server->id,
])
);
}
public function failed(): void
{
$this->server->status = 'ready';
$this->server->save();
event(
new Broadcast('reboot-server-failed', [
'message' => __('Failed to reboot the server'),
'server' => $this->server,
])
);
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace App\Jobs\Service;
use App\Jobs\Job;
use App\Models\Service;
class Install extends Job
{
protected Service $service;
public function __construct(Service $service)
{
$this->service = $service;
}
public function handle()
{
}
public function failed(\Throwable $throwable)
{
}
}

View File

@ -0,0 +1,75 @@
<?php
namespace App\Jobs\Service;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\Service;
use App\SSHCommands\RestartServiceCommand;
use App\SSHCommands\StartServiceCommand;
use App\SSHCommands\StopServiceCommand;
use Exception;
use Throwable;
class Manage extends Job
{
protected Service $service;
protected string $action;
protected string $successStatus;
protected string $failStatus;
protected string $failMessage;
public function __construct(
Service $service,
string $action,
string $successStatus,
string $failStatus,
string $failMessage,
) {
$this->service = $service;
$this->action = $action;
$this->successStatus = $successStatus;
$this->failStatus = $failStatus;
$this->failMessage = $failMessage;
}
/**
* @throws Throwable
*/
public function handle(): void
{
$command = match ($this->action) {
'start' => new StartServiceCommand($this->service->unit),
'stop' => new StopServiceCommand($this->service->unit),
'restart' => new RestartServiceCommand($this->service->unit),
default => throw new Exception('Invalid action'),
};
$this->service->server->ssh()->exec(
$command,
$this->action.'-'.$this->service->name
);
$this->service->status = $this->successStatus;
$this->service->save();
event(
new Broadcast('update-service-finished', [
'service' => $this->service,
])
);
}
public function failed(): void
{
$this->service->status = $this->failStatus;
$this->service->save();
event(
new Broadcast('update-service-failed', [
'message' => $this->service->name.' '.$this->failMessage,
'service' => $this->service,
])
);
}
}

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);
}
}

View File

@ -0,0 +1,50 @@
<?php
namespace App\Jobs\SshKey;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\Server;
use App\Models\SshKey;
use App\SSHCommands\DeleteSshKeyCommand;
use Throwable;
class DeleteSshKeyFromServer extends Job
{
protected Server $server;
protected SshKey $sshKey;
public function __construct(Server $server, SshKey $sshKey)
{
$this->server = $server;
$this->sshKey = $sshKey;
}
/**
* @throws Throwable
*/
public function handle()
{
$this->server->ssh()->exec(
new DeleteSshKeyCommand($this->sshKey->public_key),
'delete-ssh-key'
);
$this->server->sshKeys()->detach($this->sshKey);
event(
new Broadcast('delete-ssh-key-finished', [
'sshKey' => $this->sshKey,
])
);
}
public function failed(): void
{
$this->server->sshKeys()->attach($this->sshKey);
event(
new Broadcast('delete-ssh-key-failed', [
'sshKey' => $this->sshKey,
])
);
}
}

View File

@ -0,0 +1,53 @@
<?php
namespace App\Jobs\SshKey;
use App\Enums\SshKeyStatus;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\Server;
use App\Models\SshKey;
use App\SSHCommands\DeploySshKeyCommand;
use Throwable;
class DeploySshKeyToServer extends Job
{
protected Server $server;
protected SshKey $sshKey;
public function __construct(Server $server, SshKey $sshKey)
{
$this->server = $server;
$this->sshKey = $sshKey;
}
/**
* @throws Throwable
*/
public function handle(): void
{
$this->server->ssh()->exec(
new DeploySshKeyCommand($this->sshKey->public_key),
'deploy-ssh-key'
);
$this->sshKey->servers()->updateExistingPivot($this->server->id, [
'status' => SshKeyStatus::ADDED,
]);
event(
new Broadcast('deploy-ssh-key-finished', [
'sshKey' => $this->sshKey,
])
);
}
public function failed(): void
{
$this->server->sshKeys()->detach($this->sshKey);
event(
new Broadcast('deploy-ssh-key-failed', [
'sshKey' => $this->sshKey,
])
);
}
}

48
app/Jobs/Ssl/Deploy.php Normal file
View File

@ -0,0 +1,48 @@
<?php
namespace App\Jobs\Ssl;
use App\Enums\SiteType;
use App\Enums\SslStatus;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\Ssl;
class Deploy extends Job
{
protected Ssl $ssl;
public function __construct(Ssl $ssl)
{
$this->ssl = $ssl;
}
public function handle(): void
{
$this->ssl->site->server->webserver()->handler()->setupSSL($this->ssl);
$this->ssl->status = SslStatus::CREATED;
$this->ssl->save();
event(
new Broadcast('deploy-ssl-finished', [
'ssl' => $this->ssl,
])
);
if ($this->ssl->site->type == SiteType::WORDPRESS) {
$typeData = $this->ssl->site->type_data;
$typeData['url'] = $this->ssl->site->url;
$this->ssl->site->type_data = $typeData;
$this->ssl->site->save();
$this->ssl->site->type()->edit();
}
}
public function failed(): void
{
event(
new Broadcast('deploy-ssl-failed', [
'ssl' => $this->ssl,
])
);
$this->ssl->delete();
}
}

39
app/Jobs/Ssl/Remove.php Normal file
View File

@ -0,0 +1,39 @@
<?php
namespace App\Jobs\Ssl;
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\Ssl;
class Remove extends Job
{
protected Ssl $ssl;
public function __construct(Ssl $ssl)
{
$this->ssl = $ssl;
}
public function handle(): void
{
$this->ssl->site->server->webserver()->handler()->removeSSL($this->ssl);
$this->ssl->delete();
event(
new Broadcast('remove-ssl-finished', [
'ssl' => $this->ssl,
])
);
}
public function failed(): void
{
$this->ssl->status = 'failed';
$this->ssl->save();
event(
new Broadcast('remove-ssl-failed', [
'ssl' => $this->ssl,
])
);
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace App\Jobs\StorageProvider;
use App\Jobs\Job;
use App\Models\StorageProvider;
class DeleteFile extends Job
{
protected StorageProvider $storageProvider;
protected array $paths;
public function __construct(StorageProvider $storageProvider, array $paths)
{
$this->storageProvider = $storageProvider;
$this->paths = $paths;
}
public function handle(): void
{
$this->storageProvider->provider()->delete($this->paths);
}
}