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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
472 changed files with 24110 additions and 8159 deletions

2
.gitignore vendored
View File

@ -4,6 +4,8 @@
/public/storage
/storage/*.key
/storage/*.pem
/storage/test-key
/storage/test-key.pub
/vendor
.env
.env.backup

View File

@ -8,7 +8,8 @@
"printWidth": 120,
"htmlWhitespaceSensitivity": "ignore",
"tabWidth": 4,
"quoteProps": "consistent"
"quoteProps": "consistent",
"trailingComma": "none"
}
}
]

View File

@ -5,17 +5,12 @@
use App\Enums\CronjobStatus;
use App\Models\CronJob;
use App\Models\Server;
use App\SSHCommands\CronJob\UpdateCronJobsCommand;
use App\ValidationRules\CronRule;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use Throwable;
class CreateCronJob
{
/**
* @throws Throwable
*/
public function create(Server $server, array $input): void
{
$this->validate($input);
@ -29,10 +24,7 @@ public function create(Server $server, array $input): void
]);
$cronJob->save();
$server->ssh()->exec(
new UpdateCronJobsCommand($cronJob->user, CronJob::crontab($server, $cronJob->user)),
'update-crontab'
);
$server->cron()->update($cronJob->user, CronJob::crontab($server, $cronJob->user));
$cronJob->status = CronjobStatus::READY;
$cronJob->save();
}

View File

@ -4,21 +4,13 @@
use App\Models\CronJob;
use App\Models\Server;
use App\SSHCommands\CronJob\UpdateCronJobsCommand;
use Throwable;
class DeleteCronJob
{
/**
* @throws Throwable
*/
public function delete(Server $server, CronJob $cronJob): void
{
$user = $cronJob->user;
$cronJob->delete();
$server->ssh()->exec(
new UpdateCronJobsCommand($user, CronJob::crontab($server, $user)),
'update-crontab'
);
$server->cron()->update($cronJob->user, CronJob::crontab($server, $user));
}
}

View File

@ -32,7 +32,7 @@ public function create($type, Server $server, array $input): Backup
]);
$backup->save();
$backup->run();
app(RunBackup::class)->run($backup);
return $backup;
}

View File

@ -22,8 +22,6 @@ public function create(Server $server, array $input): Database
'server_id' => $server->id,
'name' => $input['name'],
]);
$database->save();
$server->database()->handler()->create($database->name);
$database->status = DatabaseStatus::READY;
$database->save();

View File

@ -25,8 +25,6 @@ public function create(Server $server, array $input, array $links = []): Databas
'host' => isset($input['remote']) && $input['remote'] ? $input['host'] : 'localhost',
'databases' => $links,
]);
$databaseUser->save();
$server->database()->handler()->createUser(
$databaseUser->username,
$databaseUser->password,
@ -35,8 +33,8 @@ public function create(Server $server, array $input, array $links = []): Databas
$databaseUser->status = DatabaseUserStatus::READY;
$databaseUser->save();
if (count($databaseUser->databases) > 0) {
app(LinkUser::class)->link($databaseUser, $databaseUser->databases);
if (count($links) > 0) {
app(LinkUser::class)->link($databaseUser, ['databases' => $links]);
}
return $databaseUser;

View File

@ -0,0 +1,39 @@
<?php
namespace App\Actions\Database;
use App\Enums\BackupFileStatus;
use App\Models\BackupFile;
use App\Models\Database;
use Illuminate\Support\Facades\Validator;
class RestoreBackup
{
public function restore(BackupFile $backupFile, array $input): void
{
$this->validate($input);
/** @var Database $database */
$database = Database::query()->findOrFail($input['database']);
$backupFile->status = BackupFileStatus::RESTORING;
$backupFile->restored_to = $database->name;
$backupFile->save();
dispatch(function () use ($backupFile, $database) {
$database->server->database()->handler()->restoreBackup($backupFile, $database->name);
$backupFile->status = BackupFileStatus::RESTORED;
$backupFile->restored_at = now();
$backupFile->save();
})->catch(function () use ($backupFile) {
$backupFile->status = BackupFileStatus::RESTORE_FAILED;
$backupFile->save();
})->onConnection('ssh');
}
private function validate(array $input): void
{
Validator::make($input, [
'database' => 'required|exists:databases,id',
])->validate();
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace App\Actions\Database;
use App\Enums\BackupFileStatus;
use App\Models\Backup;
use App\Models\BackupFile;
use Illuminate\Support\Str;
class RunBackup
{
public function run(Backup $backup): void
{
$file = new BackupFile([
'backup_id' => $backup->id,
'name' => Str::of($backup->database->name)->slug().'-'.now()->format('YmdHis'),
'status' => BackupFileStatus::CREATING,
]);
$file->save();
dispatch(function () use ($file) {
$file->backup->server->database()->handler()->runBackup($file);
$file->status = BackupFileStatus::CREATED;
$file->save();
})->catch(function () use ($file) {
$file->status = BackupFileStatus::FAILED;
$file->save();
})->onConnection('ssh');
}
}

View File

@ -21,15 +21,13 @@ public function create(Server $server, array $input): FirewallRule
'port' => $input['port'],
'source' => $input['source'],
'mask' => $input['mask'] ?? null,
'status' => FirewallRuleStatus::CREATING,
]);
$rule->save();
$server->firewall()
->handler()
->addRule(
$rule->type,
$rule->real_protocol,
$rule->getRealProtocol(),
$rule->port,
$rule->source,
$rule->mask

View File

@ -17,7 +17,7 @@ public function delete(Server $server, FirewallRule $rule): void
->handler()
->removeRule(
$rule->type,
$rule->real_protocol,
$rule->getRealProtocol(),
$rule->port,
$rule->source,
$rule->mask

View File

@ -3,7 +3,6 @@
namespace App\Actions\PHP;
use App\Models\Server;
use App\SSHCommands\PHP\GetPHPIniCommand;
use Illuminate\Validation\ValidationException;
class GetPHPIni
@ -12,8 +11,10 @@ public function getIni(Server $server, array $input): string
{
$this->validate($server, $input);
$php = $server->php($input['version']);
try {
return $server->ssh()->exec(new GetPHPIniCommand($input['version']));
return $php->handler()->getPHPIni();
} catch (\Throwable $e) {
throw ValidationException::withMessages(
['ini' => $e->getMessage()]

View File

@ -28,7 +28,14 @@ public function install(Server $server, array $input): void
'is_default' => false,
]);
$php->save();
$php->install();
dispatch(function () use ($php) {
$php->handler()->install();
$php->status = ServiceStatus::READY;
$php->save();
})->catch(function () use ($php) {
$php->delete();
})->onConnection('ssh');
}
/**

View File

@ -10,9 +10,6 @@
class InstallPHPExtension
{
/**
* @throws ValidationException
*/
public function install(Server $server, array $input): Service
{
$this->validate($server, $input);
@ -21,10 +18,19 @@ public function install(Server $server, array $input): Service
$service = $server->php($input['version']);
$typeData = $service->type_data;
$typeData['extensions'] = $typeData['extensions'] ?? [];
$typeData['extensions'][] = $input['extension'];
$service->type_data = $typeData;
$service->save();
$service->handler()->installExtension($input['extension']);
dispatch(function () use ($service, $input) {
$service->handler()->installExtension($input['extension']);
})->catch(function () use ($service, $input) {
$service->refresh();
$typeData = $service->type_data;
$typeData['extensions'] = array_values(array_diff($typeData['extensions'], [$input['extension']]));
$service->type_data = $typeData;
$service->save();
})->onConnection('ssh');
return $service;
}

View File

@ -2,6 +2,7 @@
namespace App\Actions\PHP;
use App\Enums\ServiceStatus;
use App\Models\Server;
use App\Models\Service;
use Illuminate\Support\Facades\Validator;
@ -15,8 +16,16 @@ public function uninstall(Server $server, array $input): void
/** @var Service $php */
$php = $server->php($input['version']);
$php->status = ServiceStatus::UNINSTALLING;
$php->save();
$php->uninstall();
dispatch(function () use ($php) {
$php->handler()->uninstall();
$php->delete();
})->catch(function () use ($php) {
$php->status = ServiceStatus::FAILED;
$php->save();
})->onConnection('ssh');
}
/**

View File

@ -29,7 +29,23 @@ public function create(mixed $queueable, array $input): void
'status' => QueueStatus::CREATING,
]);
$queue->save();
$queue->deploy();
dispatch(function () use ($queue) {
$queue->server->processManager()->handler()->create(
$queue->id,
$queue->command,
$queue->user,
$queue->auto_start,
$queue->auto_restart,
$queue->numprocs,
$queue->getLogFile(),
$queue->site_id
);
$queue->status = QueueStatus::RUNNING;
$queue->save();
})->catch(function () use ($queue) {
$queue->delete();
})->onConnection('ssh');
}
/**

View File

@ -0,0 +1,14 @@
<?php
namespace App\Actions\Queue;
use App\Models\Queue;
class DeleteQueue
{
public function delete(Queue $queue): void
{
$queue->server->processManager()->handler()->delete($queue->id, $queue->site_id);
$queue->delete();
}
}

View File

@ -0,0 +1,52 @@
<?php
namespace App\Actions\Queue;
use App\Enums\QueueStatus;
use App\Enums\ServiceStatus;
use App\Models\Queue;
class ManageQueue
{
public function start(Queue $queue): void
{
$queue->status = QueueStatus::STARTING;
$queue->save();
dispatch(function () use ($queue) {
$queue->server->processManager()->handler()->start($queue->id, $queue->site_id);
$queue->status = ServiceStatus::READY;
$queue->save();
})->catch(function () use ($queue) {
$queue->status = ServiceStatus::FAILED;
$queue->save();
})->onConnection('ssh');
}
public function stop(Queue $queue): void
{
$queue->status = QueueStatus::STOPPING;
$queue->save();
dispatch(function () use ($queue) {
$queue->server->processManager()->handler()->stop($queue->id, $queue->site_id);
$queue->status = ServiceStatus::STOPPED;
$queue->save();
})->catch(function () use ($queue) {
$queue->status = ServiceStatus::FAILED;
$queue->save();
})->onConnection('ssh');
}
public function restart(Queue $queue): void
{
$queue->status = QueueStatus::RESTARTING;
$queue->save();
dispatch(function () use ($queue) {
$queue->server->processManager()->handler()->restart($queue->id, $queue->site_id);
$queue->status = ServiceStatus::READY;
$queue->save();
})->catch(function () use ($queue) {
$queue->status = ServiceStatus::FAILED;
$queue->save();
})->onConnection('ssh');
}
}

View File

@ -2,8 +2,10 @@
namespace App\Actions\SSL;
use App\Enums\SslStatus;
use App\Enums\SslType;
use App\Models\Site;
use App\Models\Ssl;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
@ -17,13 +19,24 @@ public function create(Site $site, array $input): void
{
$this->validate($input);
if ($input['type'] == SslType::LETSENCRYPT) {
$site->createFreeSsl();
}
$ssl = new Ssl([
'site_id' => $site->id,
'type' => $input['type'],
'certificate' => $input['certificate'] ?? null,
'pk' => $input['private'] ?? null,
'expires_at' => $input['type'] === SslType::LETSENCRYPT ? now()->addMonths(3) : null,
'status' => SslStatus::CREATING,
]);
$ssl->save();
if ($input['type'] == SslType::CUSTOM) {
$site->createCustomSsl($input['certificate'], $input['private']);
}
dispatch(function () use ($site, $ssl) {
$site->server->webserver()->handler()->setupSSL($ssl);
$ssl->status = SslStatus::CREATED;
$ssl->save();
$site->type()->edit();
})->catch(function () use ($ssl) {
$ssl->delete();
});
}
/**

View File

@ -0,0 +1,14 @@
<?php
namespace App\Actions\SSL;
use App\Models\Ssl;
class DeleteSSL
{
public function delete(Ssl $ssl): void
{
$ssl->site->server->webserver()->handler()->removeSSL($ssl);
$ssl->delete();
}
}

View File

@ -3,13 +3,19 @@
namespace App\Actions\Server;
use App\Enums\FirewallRuleStatus;
use App\Enums\ServerProvider;
use App\Enums\ServerStatus;
use App\Exceptions\ServerProviderError;
use App\Jobs\Installation\ContinueInstallation;
use App\Facades\Notifier;
use App\Models\Server;
use App\Models\User;
use App\Notifications\ServerInstallationFailed;
use App\Notifications\ServerInstallationSucceed;
use App\ValidationRules\RestrictedIPAddressesRule;
use Exception;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
@ -71,14 +77,8 @@ public function create(User $creator, array $input): Server
$server->type()->createServices($input);
// install server
if ($server->provider == 'custom') {
$server->install();
} else {
$server->progress_step = __('Installation will begin in 3 minutes!');
$server->save();
dispatch(new ContinueInstallation($server))
->delay(now()->addMinutes(2));
}
$this->install($server);
DB::commit();
return $server;
@ -88,12 +88,44 @@ public function create(User $creator, array $input): Server
if ($e instanceof ServerProviderError) {
throw ValidationException::withMessages([
'provider' => __('Provider Error: ').$e->getMessage(),
])->errorBag('createServer');
]);
}
throw $e;
}
}
private function install(Server $server): void
{
$bus = Bus::chain([
function () use ($server) {
if (! $server->provider()->isRunning()) {
sleep(2);
}
$server->type()->install();
$server->update([
'status' => ServerStatus::READY,
]);
Notifier::send($server, new ServerInstallationSucceed($server));
},
])->catch(function (Throwable $e) use ($server) {
$server->update([
'status' => ServerStatus::INSTALLATION_FAILED,
]);
Notifier::send($server, new ServerInstallationFailed($server));
Log::error('server-installation-error', [
'error' => (string) $e,
]);
});
if ($server->provider != ServerProvider::CUSTOM) {
$server->progress_step = 'waiting-for-provider';
$server->save();
$bus->delay(now()->addMinutes(3));
}
$bus->onConnection('ssh')->dispatch();
}
/**
* @throws ValidationException
*/
@ -136,7 +168,7 @@ private function validateInputs(array $input): void
*/
private function validateType(Server $server, array $input): void
{
Validator::make($input, $server->type()->createValidationRules($input))
Validator::make($input, $server->type()->createRules($input))
->validate();
}
@ -145,7 +177,7 @@ private function validateType(Server $server, array $input): void
*/
private function validateProvider(Server $server, array $input): void
{
Validator::make($input, $server->provider()->createValidationRules($input))
Validator::make($input, $server->provider()->createRules($input))
->validate();
}

View File

@ -4,7 +4,6 @@
use App\Enums\ServerStatus;
use App\Models\Server;
use App\SSHCommands\System\RebootCommand;
use Throwable;
class RebootServer
@ -12,7 +11,7 @@ class RebootServer
public function reboot(Server $server): Server
{
try {
$server->ssh()->exec(new RebootCommand(), 'reboot');
$server->os()->reboot();
$server->status = ServerStatus::DISCONNECTED;
$server->save();
} catch (Throwable) {

View File

@ -2,9 +2,9 @@
namespace App\Actions\ServerProvider;
use App\Contracts\ServerProvider as ServerProviderContract;
use App\Models\ServerProvider;
use App\Models\User;
use App\ServerProviders\ServerProvider as ServerProviderContract;
use Exception;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

View File

@ -1,60 +0,0 @@
<?php
namespace App\Actions\Service;
use App\Enums\ServiceStatus;
use App\Models\Server;
use App\Models\Service;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
class InstallPHPMyAdmin
{
/**
* @throws ValidationException
*/
public function install(Server $server, array $input): Service
{
$this->validate($input);
$phpMyAdmin = $server->defaultService('phpmyadmin');
if ($phpMyAdmin) {
throw ValidationException::withMessages([
'allowed_ip' => __('Already installed'),
]);
}
$phpMyAdmin = new Service([
'server_id' => $server->id,
'type' => 'phpmyadmin',
'type_data' => [
'allowed_ip' => $input['allowed_ip'],
'port' => $input['port'],
'php' => $server->defaultService('php')->version,
],
'name' => 'phpmyadmin',
'version' => '5.1.2',
'status' => ServiceStatus::INSTALLING,
'is_default' => 1,
]);
$phpMyAdmin->save();
$phpMyAdmin->install();
return $phpMyAdmin;
}
/**
* @throws ValidationException
*/
private function validate(array $input): void
{
Validator::make($input, [
'allowed_ip' => 'required',
'port' => [
'required',
'numeric',
'min:1',
'max:65535',
],
])->validate();
}
}

View File

@ -0,0 +1,63 @@
<?php
namespace App\Actions\Service;
use App\Enums\ServiceStatus;
use App\Models\Service;
class Manage
{
public function start(Service $service): void
{
$service->status = ServiceStatus::STARTING;
$service->save();
dispatch(function () use ($service) {
$status = $service->server->systemd()->start($service->unit);
if (str($status)->contains('Active: active')) {
$service->status = ServiceStatus::READY;
} else {
$service->status = ServiceStatus::FAILED;
}
$service->save();
})->catch(function () use ($service) {
$service->status = ServiceStatus::FAILED;
$service->save();
})->onConnection('ssh');
}
public function stop(Service $service): void
{
$service->status = ServiceStatus::STOPPING;
$service->save();
dispatch(function () use ($service) {
$status = $service->server->systemd()->stop($service->unit);
if (str($status)->contains('Active: inactive')) {
$service->status = ServiceStatus::STOPPED;
} else {
$service->status = ServiceStatus::FAILED;
}
$service->save();
})->catch(function () use ($service) {
$service->status = ServiceStatus::FAILED;
$service->save();
})->onConnection('ssh');
}
public function restart(Service $service): void
{
$service->status = ServiceStatus::RESTARTING;
$service->save();
dispatch(function () use ($service) {
$status = $service->server->systemd()->restart($service->unit);
if (str($status)->contains('Active: active')) {
$service->status = ServiceStatus::READY;
} else {
$service->status = ServiceStatus::FAILED;
}
$service->save();
})->catch(function () use ($service) {
$service->status = ServiceStatus::FAILED;
$service->save();
})->onConnection('ssh');
}
}

View File

@ -1,51 +0,0 @@
<?php
namespace App\Actions\Site;
use App\Models\Redirect;
use App\Models\Site;
use Exception;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
class CreateRedirect
{
/**
* @throws Exception
*/
public function handle(Site $site, array $input): void
{
$this->validate($input);
$redirect = new Redirect([
'site_id' => $site->id,
'mode' => $input['mode'],
'from' => $input['from'],
'to' => $input['to'],
]);
$redirect->save();
$redirect->addToServer();
}
/**
* @throws ValidationException
*/
private function validate(array $input): void
{
$rules = [
'mode' => [
'required',
'in:301,302',
],
'from' => [
'required',
],
'to' => [
'required',
'url',
],
];
Validator::make($input, $rules)->validateWithBag('createRedirect');
}
}

View File

@ -4,8 +4,11 @@
use App\Enums\SiteStatus;
use App\Exceptions\SourceControlIsNotConnected;
use App\Facades\Notifier;
use App\Models\Server;
use App\Models\Site;
use App\Notifications\SiteInstallationFailed;
use App\Notifications\SiteInstallationSucceed;
use App\ValidationRules\DomainRule;
use Exception;
use Illuminate\Support\Facades\DB;
@ -30,11 +33,11 @@ public function create(Server $server, array $input): Site
'type' => $input['type'],
'domain' => $input['domain'],
'aliases' => isset($input['alias']) ? [$input['alias']] : [],
'path' => '/home/'.$server->ssh_user.'/'.$input['domain'],
'path' => '/home/'.$server->getSshUser().'/'.$input['domain'],
'status' => SiteStatus::INSTALLING,
]);
// fields based on type
// fields based on the type
$site->fill($site->type()->createFields($input));
// check has access to repository
@ -63,8 +66,19 @@ public function create(Server $server, array $input): Site
'content' => '',
]);
// install server
$site->install();
// install site
dispatch(function () use ($site) {
$site->type()->install();
$site->update([
'status' => SiteStatus::READY,
'progress' => 100,
]);
Notifier::send($site, new SiteInstallationSucceed($site));
})->catch(function () use ($site) {
$site->status = SiteStatus::INSTALLATION_FAILED;
$site->save();
Notifier::send($site, new SiteInstallationFailed($site));
})->onConnection('ssh');
DB::commit();
@ -105,7 +119,7 @@ private function validateInputs(Server $server, array $input): void
*/
private function validateType(Site $site, array $input): void
{
$rules = $site->type()->createValidationRules($input);
$rules = $site->type()->createRules($input);
Validator::make($input, $rules)->validate();
}

View File

@ -0,0 +1,14 @@
<?php
namespace App\Actions\Site;
use App\Models\Site;
class DeleteSite
{
public function delete(Site $site): void
{
$site->server->webserver()->handler()->deleteSite($site);
$site->delete();
}
}

View File

@ -0,0 +1,51 @@
<?php
namespace App\Actions\Site;
use App\Enums\DeploymentStatus;
use App\Exceptions\DeploymentScriptIsEmptyException;
use App\Exceptions\SourceControlIsNotConnected;
use App\Models\Deployment;
use App\Models\Site;
class Deploy
{
/**
* @throws SourceControlIsNotConnected
* @throws DeploymentScriptIsEmptyException
*/
public function run(Site $site): Deployment
{
if ($site->sourceControl()) {
$site->sourceControl()->getRepo($site->repository);
}
if (! $site->deploymentScript?->content) {
throw new DeploymentScriptIsEmptyException();
}
$deployment = new Deployment([
'site_id' => $site->id,
'deployment_script_id' => $site->deploymentScript->id,
'status' => DeploymentStatus::DEPLOYING,
]);
$lastCommit = $site->sourceControl()->provider()->getLastCommit($site->repository, $site->branch);
if ($lastCommit) {
$deployment->commit_id = $lastCommit['commit_id'];
$deployment->commit_data = $lastCommit['commit_data'];
}
$deployment->save();
dispatch(function () use ($site, $deployment) {
$log = $site->server->os()->runScript($site->path, $site->deploymentScript->content, $site->id);
$deployment->status = DeploymentStatus::FINISHED;
$deployment->log_id = $log->id;
$deployment->save();
})->catch(function () use ($deployment) {
$deployment->status = DeploymentStatus::FAILED;
$deployment->save();
})->onConnection('ssh');
return $deployment;
}
}

View File

@ -1,41 +0,0 @@
<?php
namespace App\Actions\Site;
use App\Models\Site;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
class EditSite
{
/**
* @throws ValidationException
*/
public function handle(Site $site, array $input): Site
{
// validate type
$this->validateType($site, $input);
// set type data
$site->type_data = $site->type()->data($input);
// save
$site->port = $input['port'] ?? null;
$site->save();
// edit
$site->type()->edit();
return $site;
}
/**
* @throws ValidationException
*/
private function validateType(Site $site, array $input): void
{
$rules = $site->type()->editValidationRules($input);
Validator::make($input, $rules)->validateWithBag('editSite');
}
}

View File

@ -1,14 +0,0 @@
<?php
namespace App\Actions\Site;
use App\Models\Server;
use Illuminate\Database\Eloquent\Collection;
class GetSites
{
public function handle(Server $server): Collection
{
return $server->sites()->orderBy('id', 'desc')->get();
}
}

View File

@ -3,6 +3,7 @@
namespace App\Actions\Site;
use App\Models\Site;
use App\SSH\Git\Git;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
@ -14,8 +15,9 @@ class UpdateBranch
public function update(Site $site, array $input): void
{
$this->validate($input);
$site->updateBranch($input['branch']);
$site->branch = $input['branch'];
app(Git::class)->checkout($site);
$site->save();
}
/**
@ -25,6 +27,6 @@ protected function validate(array $input): void
{
Validator::make($input, [
'branch' => 'required',
])->validateWithBag('updateBranch');
]);
}
}

View File

@ -27,6 +27,6 @@ protected function validate(array $input): void
{
Validator::make($input, [
'script' => 'required',
])->validateWithBag('updateDeploymentScript');
]);
}
}

View File

@ -3,22 +3,14 @@
namespace App\Actions\Site;
use App\Models\Site;
use App\SSHCommands\System\EditFileCommand;
class UpdateEnv
{
public function update(Site $site, array $input): void
{
$typeData = $site->type_data;
$typeData['env'] = $input['env'];
$site->type_data = $typeData;
$site->save();
$site->server->ssh()->exec(
new EditFileCommand(
$site->path.'/.env',
$site->env
)
$site->server->os()->editFile(
$site->path.'/.env',
$input['env']
);
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace App\Actions\SshKey;
use App\Enums\SshKeyStatus;
use App\Models\Server;
use App\Models\SshKey;
class DeleteKeyFromServer
{
public function delete(Server $server, SshKey $sshKey): void
{
$sshKey->servers()->updateExistingPivot($server->id, [
'status' => SshKeyStatus::DELETING,
]);
$server->os()->deleteSSHKey($sshKey->public_key);
$server->sshKeys()->detach($sshKey);
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace App\Actions\SshKey;
use App\Enums\SshKeyStatus;
use App\Models\Server;
use App\Models\SshKey;
use App\Models\User;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
class DeployKeyToServer
{
public function deploy(User $user, Server $server, array $input): void
{
$this->validate($user, $input);
/** @var SshKey $sshKey */
$sshKey = SshKey::findOrFail($input['key_id']);
$server->sshKeys()->attach($sshKey, [
'status' => SshKeyStatus::ADDING,
]);
$server->os()->deploySSHKey($sshKey->public_key);
$sshKey->servers()->updateExistingPivot($server->id, [
'status' => SshKeyStatus::ADDED,
]);
}
private function validate(User $user, array $input): void
{
Validator::make($input, [
'key_id' => [
'required',
Rule::exists('ssh_keys', 'id')->where('user_id', $user->id),
],
])->validate();
}
}

View File

@ -42,7 +42,7 @@ public function handle(): void
->chunk(100, function ($backups) {
/** @var Backup $backup */
foreach ($backups as $backup) {
$backup->run();
app(\App\Actions\Database\RunBackup::class)->run($backup);
}
});
}

View File

@ -1,10 +0,0 @@
<?php
namespace App\Contracts;
interface SSHCommand
{
public function file(): string;
public function content(): string;
}

View File

@ -0,0 +1,9 @@
<?php
namespace App\Exceptions;
use Exception;
class DeploymentScriptIsEmptyException extends Exception
{
}

View File

@ -2,9 +2,7 @@
namespace App\Exceptions;
use Exception;
class SSHCommandError extends Exception
class SSHCommandError extends SSHError
{
//
}

View File

@ -2,9 +2,7 @@
namespace App\Exceptions;
use Exception;
class SSHConnectionError extends Exception
class SSHConnectionError extends SSHError
{
//
}

View File

@ -4,7 +4,7 @@
use Exception;
class InstallationFailed extends Exception
class SSHError extends Exception
{
//
}

View File

@ -0,0 +1,10 @@
<?php
namespace App\Exceptions;
use Exception;
class ServiceInstallationFailed extends Exception
{
//
}

View File

@ -2,11 +2,11 @@
namespace App\Facades;
use App\Contracts\Notification;
use App\Notifications\NotificationInterface;
use Illuminate\Support\Facades\Facade;
/**
* @method static void send(object $notifiable, Notification $notification)
* @method static void send(object $notifiable, NotificationInterface $notification)
*/
class Notifier extends Facade
{

View File

@ -9,17 +9,19 @@
/**
* Class SSH
*
* @method static \App\Helpers\SSH init(Server $server, string $asUser = null)
* @method static init(Server $server, string $asUser = null)
* @method static setLog(string $logType, int $siteId = null)
* @method static connect()
* @method static exec($commands, string $log = '', int $siteId = null)
* @method static string exec(string $command, string $log = '', int $siteId = null)
* @method static string assertExecuted(array|string $commands)
* @method static string assertExecutedContains(string $command)
* @method static disconnect()
*/
class SSH extends FacadeAlias
{
public static function fake(): SSHFake
public static function fake(?string $output = null): SSHFake
{
static::swap($fake = new SSHFake());
static::swap($fake = new SSHFake($output));
return $fake;
}

View File

@ -2,8 +2,8 @@
namespace App\Helpers;
use App\Contracts\Notification;
use App\Models\NotificationChannel;
use App\Notifications\NotificationInterface;
class Notifier
{
@ -12,7 +12,7 @@ class Notifier
* For example, If it was a server then we will send the channels specified by that server
* For now, we will send all channels.
*/
public function send(object $notifiable, Notification $notification): void
public function send(object $notifiable, NotificationInterface $notification): void
{
NotificationChannel::notifyAll($notification);
}

View File

@ -2,7 +2,6 @@
namespace App\Helpers;
use App\Contracts\SSHCommand;
use App\Exceptions\SSHAuthenticationError;
use App\Exceptions\SSHCommandError;
use App\Exceptions\SSHConnectionError;
@ -23,7 +22,7 @@ class SSH
public ?ServerLog $log;
protected SSH2|SFTP|null $connection;
protected SSH2|SFTP|null $connection = null;
protected ?string $user;
@ -39,8 +38,8 @@ public function init(Server $server, ?string $asUser = null): self
$this->log = null;
$this->asUser = null;
$this->server = $server->refresh();
$this->user = $server->ssh_user;
if ($asUser && $asUser != $server->ssh_user) {
$this->user = $server->getSshUser();
if ($asUser && $asUser != $server->getSshUser()) {
$this->user = $asUser;
$this->asUser = $asUser;
}
@ -51,7 +50,7 @@ public function init(Server $server, ?string $asUser = null): self
return $this;
}
public function setLog(string $logType, $siteId = null): void
public function setLog(string $logType, $siteId = null): self
{
$this->log = $this->server->logs()->create([
'site_id' => $siteId,
@ -59,6 +58,8 @@ public function setLog(string $logType, $siteId = null): void
'type' => $logType,
'disk' => config('core.logs_disk'),
]);
return $this;
}
/**
@ -66,6 +67,7 @@ public function setLog(string $logType, $siteId = null): void
*/
public function connect(bool $sftp = false): void
{
// If the IP is an IPv6 address, we need to wrap it in square brackets
$ip = $this->server->ip;
if (str($ip)->contains(':')) {
$ip = '['.$ip.']';
@ -91,9 +93,10 @@ public function connect(bool $sftp = false): void
}
/**
* @throws Throwable
* @throws SSHCommandError
* @throws SSHConnectionError
*/
public function exec(string|array|SSHCommand $commands, string $log = '', ?int $siteId = null): string
public function exec(string|array $commands, string $log = '', ?int $siteId = null): string
{
if ($log) {
$this->setLog($log, $siteId);
@ -141,19 +144,15 @@ public function upload(string $local, string $remote): void
/**
* @throws Exception
*/
protected function executeCommand(string|SSHCommand $command): string
protected function executeCommand(string $command): string
{
if ($command instanceof SSHCommand) {
$commandContent = $command->content();
} else {
$commandContent = $command;
}
if ($this->asUser) {
$commandContent = 'sudo su - '.$this->asUser.' -c '.'"'.addslashes($commandContent).'"';
$command = 'sudo su - '.$this->asUser.' -c '.'"'.addslashes($command).'"';
}
$output = $this->connection->exec($commandContent);
$this->connection->setTimeout(0);
$output = $this->connection->exec($command);
$this->log?->write($output);
@ -174,4 +173,12 @@ public function disconnect(): void
$this->connection = null;
}
}
/**
* @throws Exception
*/
public function __destruct()
{
$this->disconnect();
}
}

View File

@ -2,9 +2,11 @@
namespace App\Http\Controllers;
use App\Actions\Site\Deploy;
use App\Actions\Site\UpdateBranch;
use App\Actions\Site\UpdateDeploymentScript;
use App\Actions\Site\UpdateEnv;
use App\Exceptions\DeploymentScriptIsEmptyException;
use App\Exceptions\SourceControlIsNotConnected;
use App\Facades\Toast;
use App\Helpers\HtmxResponse;
@ -19,13 +21,15 @@ class ApplicationController extends Controller
public function deploy(Server $server, Site $site): HtmxResponse
{
try {
$site->deploy();
app(Deploy::class)->run($site);
Toast::success('Deployment started!');
} catch (SourceControlIsNotConnected $e) {
Toast::error($e->getMessage());
return htmx()->redirect(route('source-controls'));
} catch (DeploymentScriptIsEmptyException) {
Toast::error('Deployment script is empty!');
}
return htmx()->back();
@ -33,7 +37,7 @@ public function deploy(Server $server, Site $site): HtmxResponse
public function showDeploymentLog(Server $server, Site $site, Deployment $deployment): RedirectResponse
{
return back()->with('content', $deployment->log->content);
return back()->with('content', $deployment->log?->getContent());
}
public function updateDeploymentScript(Server $server, Site $site, Request $request): RedirectResponse
@ -68,9 +72,9 @@ public function updateEnv(Server $server, Site $site, Request $request): Redirec
return back();
}
public function enableAutoDeployment(Server $server, Site $site): RedirectResponse
public function enableAutoDeployment(Server $server, Site $site): HtmxResponse
{
if (! $site->auto_deployment) {
if (! $site->isAutoDeployment()) {
try {
$site->enableAutoDeployment();
@ -82,12 +86,12 @@ public function enableAutoDeployment(Server $server, Site $site): RedirectRespon
}
}
return back();
return htmx()->back();
}
public function disableAutoDeployment(Server $server, Site $site): RedirectResponse
public function disableAutoDeployment(Server $server, Site $site): HtmxResponse
{
if ($site->auto_deployment) {
if ($site->isAutoDeployment()) {
try {
$site->disableAutoDeployment();
@ -99,6 +103,6 @@ public function disableAutoDeployment(Server $server, Site $site): RedirectRespo
}
}
return back();
return htmx()->back();
}
}

View File

@ -1,15 +0,0 @@
<?php
namespace App\Http\Controllers;
use App\Models\Server;
class DaemonController extends Controller
{
public function index(Server $server)
{
return view('daemons.index', [
'server' => $server,
]);
}
}

View File

@ -3,11 +3,12 @@
namespace App\Http\Controllers;
use App\Actions\Database\CreateBackup;
use App\Actions\Database\RestoreBackup;
use App\Actions\Database\RunBackup;
use App\Facades\Toast;
use App\Helpers\HtmxResponse;
use App\Models\Backup;
use App\Models\BackupFile;
use App\Models\Database;
use App\Models\Server;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
@ -27,7 +28,7 @@ public function show(Server $server, Backup $backup): View
public function run(Server $server, Backup $backup): RedirectResponse
{
$backup->run();
app(RunBackup::class)->run($backup);
Toast::success('Backup is running.');
@ -54,14 +55,7 @@ public function destroy(Server $server, Backup $backup): RedirectResponse
public function restore(Server $server, Backup $backup, BackupFile $backupFile, Request $request): HtmxResponse
{
$this->validate($request, [
'database' => 'required|exists:databases,id',
]);
/** @var Database $database */
$database = Database::query()->findOrFail($request->input('database'));
$backupFile->restore($database);
app(RestoreBackup::class)->restore($backupFile, $request->input());
Toast::success('Backup restored successfully.');

View File

@ -2,9 +2,11 @@
namespace App\Http\Controllers;
use App\Actions\Site\Deploy;
use App\Exceptions\SourceControlIsNotConnected;
use App\Facades\Notifier;
use App\Models\GitHook;
use App\Models\ServerLog;
use App\Notifications\SourceControlDisconnected;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
@ -26,10 +28,16 @@ public function __invoke(Request $request)
foreach ($gitHook->actions as $action) {
if ($action == 'deploy') {
try {
$gitHook->site->deploy();
app(Deploy::class)->run($gitHook->site);
} catch (SourceControlIsNotConnected) {
Notifier::send($gitHook->sourceControl, new SourceControlDisconnected($gitHook->sourceControl));
} catch (Throwable $e) {
ServerLog::log(
$gitHook->site->server,
'deploy-failed',
$e->getMessage(),
$gitHook->site
);
Log::error('git-hook-exception', (array) $e);
}
}

View File

@ -3,6 +3,8 @@
namespace App\Http\Controllers;
use App\Actions\Queue\CreateQueue;
use App\Actions\Queue\DeleteQueue;
use App\Actions\Queue\ManageQueue;
use App\Facades\Toast;
use App\Helpers\HtmxResponse;
use App\Models\Queue;
@ -34,7 +36,7 @@ public function store(Server $server, Site $site, Request $request): HtmxRespons
public function action(Server $server, Site $site, Queue $queue, string $action): HtmxResponse
{
$queue->{$action}();
app(ManageQueue::class)->{$action}($queue);
Toast::success('Queue is about to '.$action);
@ -43,7 +45,7 @@ public function action(Server $server, Site $site, Queue $queue, string $action)
public function destroy(Server $server, Site $site, Queue $queue): RedirectResponse
{
$queue->remove();
app(DeleteQueue::class)->delete($queue);
Toast::success('Queue is being deleted.');

View File

@ -3,6 +3,8 @@
namespace App\Http\Controllers;
use App\Actions\SshKey\CreateSshKey;
use App\Actions\SshKey\DeleteKeyFromServer;
use App\Actions\SshKey\DeployKeyToServer;
use App\Facades\Toast;
use App\Helpers\HtmxResponse;
use App\Models\Server;
@ -29,34 +31,29 @@ public function store(Server $server, Request $request): HtmxResponse
$request->input()
);
$key->deployTo($server);
$request->merge(['key_id' => $key->id]);
Toast::success('SSH Key added and being deployed to the server.');
return htmx()->back();
return $this->deploy($server, $request);
}
public function destroy(Server $server, SshKey $sshKey): RedirectResponse
{
$sshKey->deleteFrom($server);
app(DeleteKeyFromServer::class)->delete($server, $sshKey);
Toast::success('SSH Key is being deleted.');
Toast::success('SSH Key has been deleted.');
return back();
}
public function deploy(Server $server, Request $request): HtmxResponse
{
$this->validate($request, [
'key_id' => 'required|exists:ssh_keys,id',
]);
app(DeployKeyToServer::class)->deploy(
$request->user(),
$server,
$request->input()
);
/** @var SshKey $sshKey */
$sshKey = SshKey::query()->findOrFail($request->input('key_id'));
$sshKey->deployTo($server);
Toast::success('SSH Key is being deployed to the server.');
Toast::success('SSH Key has been deployed to the server.');
return htmx()->back();
}

View File

@ -3,6 +3,7 @@
namespace App\Http\Controllers;
use App\Actions\SSL\CreateSSL;
use App\Actions\SSL\DeleteSSL;
use App\Facades\Toast;
use App\Helpers\HtmxResponse;
use App\Models\Server;
@ -34,9 +35,9 @@ public function store(Server $server, Site $site, Request $request): HtmxRespons
public function destroy(Server $server, Site $site, Ssl $ssl): RedirectResponse
{
$ssl->remove();
app(DeleteSSL::class)->delete($ssl);
Toast::success('SSL certificate is being deleted.');
Toast::success('SSL certificate has been deleted.');
return back();
}

View File

@ -23,7 +23,7 @@ public function show(Server $server, ServerLog $serverLog): RedirectResponse
}
return back()->with([
'content' => $serverLog->content,
'content' => $serverLog->getContent(),
]);
}
}

View File

@ -3,6 +3,7 @@
namespace App\Http\Controllers;
use App\Actions\Server\EditServer;
use App\Actions\Server\RebootServer;
use App\Facades\Toast;
use App\Helpers\HtmxResponse;
use App\Models\Server;
@ -40,7 +41,9 @@ public function checkConnection(Server $server): RedirectResponse|HtmxResponse
public function reboot(Server $server): HtmxResponse
{
$server->reboot();
app(RebootServer::class)->reboot($server);
Toast::info('Server is rebooting.');
return htmx()->redirect(back()->getTargetUrl());
}

View File

@ -3,6 +3,7 @@
namespace App\Http\Controllers;
use App\Actions\Site\CreateSite;
use App\Actions\Site\DeleteSite;
use App\Enums\SiteType;
use App\Facades\Toast;
use App\Helpers\HtmxResponse;
@ -51,7 +52,7 @@ public function show(Server $server, Site $site): View
public function destroy(Server $server, Site $site): RedirectResponse
{
$site->remove();
app(DeleteSite::class)->delete($site);
Toast::success('Site is being deleted');

View File

@ -2,6 +2,8 @@
namespace App\Http\Middleware;
use App\Enums\ServerStatus;
use App\Facades\Toast;
use App\Models\Server;
use Closure;
use Illuminate\Http\Request;
@ -13,7 +15,9 @@ public function handle(Request $request, Closure $next)
/** @var Server $server */
$server = $request->route('server');
if (! $server->isReady()) {
if ($server->status !== ServerStatus::READY) {
Toast::error('Server is not ready yet');
return redirect()->route('servers.show', ['server' => $server]);
}

View File

@ -1,36 +0,0 @@
<?php
namespace App\Jobs\Backup;
use App\Enums\BackupFileStatus;
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 = BackupFileStatus::RESTORED;
$this->backupFile->restored_at = now();
$this->backupFile->save();
}
public function failed(): void
{
$this->backupFile->status = BackupFileStatus::RESTORE_FAILED;
$this->backupFile->save();
}
}

View File

@ -1,33 +0,0 @@
<?php
namespace App\Jobs\Backup;
use App\Enums\BackupFileStatus;
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 = BackupFileStatus::CREATED;
$this->backupFile->save();
}
public function failed(): void
{
$this->backupFile->status = BackupFileStatus::FAILED;
$this->backupFile->save();
}
}

View File

@ -1,37 +0,0 @@
<?php
namespace App\Jobs\Installation;
use App\Models\Server;
class ContinueInstallation extends InstallationJob
{
protected Server $server;
protected int $attempts;
public function __construct(Server $server, int $attempts = 0)
{
$this->server = $server;
$this->attempts = $attempts;
}
public function handle(): void
{
if ($this->server->provider()->isRunning()) {
$this->server->install();
return;
}
if ($this->attempts >= 2) {
$this->server->update([
'status' => 'installation_failed',
]);
return;
}
dispatch(new self($this->server, $this->attempts++))->delay(now()->addMinute());
}
}

View File

@ -1,72 +0,0 @@
<?php
namespace App\Jobs\Installation;
use App\Models\Server;
use App\SSHCommands\System\CreateUserCommand;
use App\SSHCommands\System\GetPublicKeyCommand;
use Throwable;
class Initialize extends InstallationJob
{
protected Server $server;
protected ?string $asUser;
public function __construct(Server $server, ?string $asUser = null)
{
$this->server = $server->refresh();
$this->asUser = $asUser;
}
/**
* @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)
->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

@ -1,25 +0,0 @@
<?php
namespace App\Jobs\Installation;
use App\Models\Server;
use App\SSHCommands\SSL\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

@ -1,25 +0,0 @@
<?php
namespace App\Jobs\Installation;
use App\Models\Server;
use App\SSHCommands\PHP\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

@ -1,35 +0,0 @@
<?php
namespace App\Jobs\Installation;
use App\Enums\ServiceStatus;
use App\Exceptions\InstallationFailed;
use App\Models\Service;
use App\SSHCommands\Database\InstallMariadbCommand;
use App\SSHCommands\Service\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

@ -1,35 +0,0 @@
<?php
namespace App\Jobs\Installation;
use App\Enums\ServiceStatus;
use App\Exceptions\InstallationFailed;
use App\Models\Service;
use App\SSHCommands\Database\InstallMysqlCommand;
use App\SSHCommands\Service\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

@ -1,35 +0,0 @@
<?php
namespace App\Jobs\Installation;
use App\Enums\ServiceStatus;
use App\Exceptions\InstallationFailed;
use App\Models\Service;
use App\SSHCommands\Nginx\InstallNginxCommand;
use App\SSHCommands\Service\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

@ -1,25 +0,0 @@
<?php
namespace App\Jobs\Installation;
use App\Models\Server;
use App\SSHCommands\Installation\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

@ -1,35 +0,0 @@
<?php
namespace App\Jobs\Installation;
use App\Enums\ServiceStatus;
use App\Exceptions\InstallationFailed;
use App\Models\Service;
use App\SSHCommands\PHP\InstallPHPCommand;
use App\SSHCommands\Service\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

@ -1,114 +0,0 @@
<?php
namespace App\Jobs\Installation;
use App\Actions\FirewallRule\CreateRule;
use App\Enums\ServiceStatus;
use App\Jobs\Job;
use App\Models\FirewallRule;
use App\Models\Service;
use App\SSHCommands\PHPMyAdmin\CreateNginxPHPMyAdminVHostCommand;
use App\SSHCommands\PHPMyAdmin\DownloadPHPMyAdminCommand;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Throwable;
/**
* @deprecated
*/
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();
$this->service->update([
'status' => ServiceStatus::READY,
]);
}
/**
* @throws Throwable
*/
private function setUpFirewall(): void
{
$this->firewallRule = FirewallRule::query()
->where('server_id', $this->service->server_id)
->where('port', $this->service->type_data['port'])
->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' => $this->service->type_data['port'],
'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(resource_path('commands/webserver/nginx/phpmyadmin-vhost.conf'));
$vhost = Str::replace('__php_version__', $this->service->server->defaultService('php')->version, $vhost);
$vhost = Str::replace('__port__', $this->service->type_data['port'], $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();
$this->service->update([
'status' => ServiceStatus::INSTALLATION_FAILED,
]);
throw $throwable;
}
}

View File

@ -1,35 +0,0 @@
<?php
namespace App\Jobs\Installation;
use App\Enums\ServiceStatus;
use App\Exceptions\InstallationFailed;
use App\Models\Service;
use App\SSHCommands\Installation\InstallRedisCommand;
use App\SSHCommands\Service\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

@ -1,28 +0,0 @@
<?php
namespace App\Jobs\Installation;
use App\Models\Server;
use App\SSHCommands\Installation\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

@ -1,35 +0,0 @@
<?php
namespace App\Jobs\Installation;
use App\Enums\ServiceStatus;
use App\Exceptions\InstallationFailed;
use App\Models\Service;
use App\SSHCommands\Service\ServiceStatusCommand;
use App\SSHCommands\Supervisor\InstallSupervisorCommand;
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

@ -1,35 +0,0 @@
<?php
namespace App\Jobs\Installation;
use App\Enums\ServiceStatus;
use App\Exceptions\InstallationFailed;
use App\Models\Service;
use App\SSHCommands\Firewall\InstallUfwCommand;
use App\SSHCommands\Service\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

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

View File

@ -1,28 +0,0 @@
<?php
namespace App\Jobs\Installation;
use App\Exceptions\InstallationFailed;
use App\Models\Service;
use App\SSHCommands\PHP\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

@ -1,66 +0,0 @@
<?php
namespace App\Jobs\Installation;
use App\Jobs\Job;
use App\Models\FirewallRule;
use App\Models\Service;
use App\SSHCommands\PHPMyAdmin\DeleteNginxPHPMyAdminVHostCommand;
use Exception;
use Throwable;
/**
* @deprecated
*/
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', $this->service->type_data['port'])
->first();
$rule?->removeFromServer();
}
/**
* @throws Throwable
*/
private function deleteVHost(): void
{
$this->service->server->ssh()->exec(
new DeleteNginxPHPMyAdminVHostCommand('/home/vito/phpmyadmin'),
'delete-phpmyadmin-vhost'
);
}
private function restartPHP(): void
{
$this->service->server->service(
'php',
$this->service->type_data['php']
)?->restart();
}
}

View File

@ -1,25 +0,0 @@
<?php
namespace App\Jobs\Installation;
use App\Models\Server;
use App\SSHCommands\System\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');
}
}

View File

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

View File

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

View File

@ -1,47 +0,0 @@
<?php
namespace App\Jobs\PHP;
use App\Exceptions\ProcessFailed;
use App\Jobs\Job;
use App\Models\Service;
use App\SSHCommands\PHP\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();
}
public function failed(): void
{
}
}

View File

@ -1,38 +0,0 @@
<?php
namespace App\Jobs\Queue;
use App\Enums\QueueStatus;
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();
}
public function failed(): void
{
$this->worker->delete();
}
}

View File

@ -1,26 +0,0 @@
<?php
namespace App\Jobs\Queue;
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);
}
public function failed(): void
{
}
}

View File

@ -1,56 +0,0 @@
<?php
namespace App\Jobs\Queue;
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();
}
public function failed(): void
{
$this->worker->status = $this->failStatus;
$this->worker->save();
}
}

View File

@ -1,29 +0,0 @@
<?php
namespace App\Jobs\Queue;
use App\Enums\QueueStatus;
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();
}
public function failed(): void
{
$this->worker->status = QueueStatus::FAILED;
$this->worker->save();
}
}

View File

@ -1,31 +0,0 @@
<?php
namespace App\Jobs\Redirect;
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();
}
public function failed(): void
{
$this->redirect->status = 'failed';
$this->redirect->delete();
}
}

View File

@ -1,33 +0,0 @@
<?php
namespace App\Jobs\Redirect;
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();
}
public function failed(): void
{
$this->redirect->status = 'failed';
$this->redirect->save();
}
}

View File

@ -1,50 +0,0 @@
<?php
namespace App\Jobs\Script;
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();
}
public function failed(): void
{
$this->scriptExecution->finished_at = now();
$this->scriptExecution->save();
}
}

View File

@ -1,24 +0,0 @@
<?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

@ -1,63 +0,0 @@
<?php
namespace App\Jobs\Service;
use App\Jobs\Job;
use App\Models\Service;
use App\SSHCommands\Service\RestartServiceCommand;
use App\SSHCommands\Service\StartServiceCommand;
use App\SSHCommands\Service\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();
}
public function failed(): void
{
$this->service->status = $this->failStatus;
$this->service->save();
}
}

View File

@ -1,35 +0,0 @@
<?php
namespace App\Jobs\Site;
use App\Jobs\Job;
use App\Models\Site;
use App\SSHCommands\Website\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,
$this->site->ssh_key_name
),
'clone-repository',
$this->site->id
);
}
}

View File

@ -1,34 +0,0 @@
<?php
namespace App\Jobs\Site;
use App\Exceptions\ComposerInstallFailed;
use App\Jobs\Job;
use App\Models\Site;
use App\SSHCommands\Website\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
);
}
}

View File

@ -1,21 +0,0 @@
<?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);
}
}

View File

@ -1,29 +0,0 @@
<?php
namespace App\Jobs\Site;
use App\Enums\SiteStatus;
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();
}
public function failed(): void
{
$this->site->status = SiteStatus::READY;
$this->site->save();
}
}

View File

@ -1,53 +0,0 @@
<?php
namespace App\Jobs\Site;
use App\Enums\DeploymentStatus;
use App\Jobs\Job;
use App\Models\Deployment;
use App\SSHCommands\System\RunScriptCommand;
use Throwable;
class Deploy extends Job
{
protected Deployment $deployment;
protected string $path;
protected string $script;
public function __construct(Deployment $deployment, string $path)
{
$this->script = $deployment->deploymentScript->content;
$this->deployment = $deployment;
$this->path = $path;
}
/**
* @throws Throwable
*/
public function handle(): void
{
$ssh = $this->deployment->site->server->ssh();
try {
$ssh->exec(
new RunScriptCommand($this->path, $this->script),
'deploy',
$this->deployment->site_id
);
$this->deployment->status = DeploymentStatus::FINISHED;
$this->deployment->log_id = $ssh->log->id;
$this->deployment->save();
} catch (Throwable) {
$this->deployment->log_id = $ssh->log->id;
$this->deployment->save();
$this->failed();
}
}
public function failed(): void
{
$this->deployment->status = DeploymentStatus::FAILED;
$this->deployment->save();
}
}

View File

@ -1,35 +0,0 @@
<?php
namespace App\Jobs\Site;
use App\Jobs\Job;
use App\Models\Site;
use App\SSHCommands\System\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
)
);
}
public function failed(): void
{
}
}

View File

@ -1,42 +0,0 @@
<?php
namespace App\Jobs\Site;
use App\Jobs\Job;
use App\Models\Site;
use App\SSHCommands\System\GenerateSshKeyCommand;
use App\SSHCommands\System\ReadSshKeyCommand;
use Throwable;
class DeployKey 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 GenerateSshKeyCommand($this->site->ssh_key_name),
'generate-ssh-key',
$this->site->id
);
$this->site->ssh_key = $this->site->server->ssh()->exec(
new ReadSshKeyCommand($this->site->ssh_key_name),
'read-public-key',
$this->site->id
);
$this->site->save();
$this->site->sourceControl()->provider()->deployKey(
$this->site->domain.'-key-'.$this->site->id,
$this->site->repository,
$this->site->ssh_key
);
}
}

View File

@ -1,109 +0,0 @@
<?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\Wordpress\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, 'Success')) {
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

@ -1,42 +0,0 @@
<?php
namespace App\Jobs\Site;
use App\Jobs\Job;
use App\Models\Site;
use App\SSHCommands\Website\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();
}
public function failed(): void
{
}
}

View File

@ -1,21 +0,0 @@
<?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);
}
}

Some files were not shown because too many files have changed in this diff Show More