mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-01 05:56:16 +00:00
refactoring (#116)
- refactoring architecture - fix incomplete ssh logs - code editor for scripts in the app - remove Jobs and SSHCommands
This commit is contained in:
@ -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();
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ public function create($type, Server $server, array $input): Backup
|
||||
]);
|
||||
$backup->save();
|
||||
|
||||
$backup->run();
|
||||
app(RunBackup::class)->run($backup);
|
||||
|
||||
return $backup;
|
||||
}
|
||||
|
@ -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();
|
||||
|
@ -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;
|
||||
|
39
app/Actions/Database/RestoreBackup.php
Normal file
39
app/Actions/Database/RestoreBackup.php
Normal 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();
|
||||
}
|
||||
}
|
30
app/Actions/Database/RunBackup.php
Normal file
30
app/Actions/Database/RunBackup.php
Normal 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');
|
||||
}
|
||||
}
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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()]
|
||||
|
@ -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');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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');
|
||||
}
|
||||
|
||||
/**
|
||||
|
14
app/Actions/Queue/DeleteQueue.php
Normal file
14
app/Actions/Queue/DeleteQueue.php
Normal 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();
|
||||
}
|
||||
}
|
52
app/Actions/Queue/ManageQueue.php
Normal file
52
app/Actions/Queue/ManageQueue.php
Normal 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');
|
||||
}
|
||||
}
|
@ -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();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
14
app/Actions/SSL/DeleteSSL.php
Normal file
14
app/Actions/SSL/DeleteSSL.php
Normal 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();
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
63
app/Actions/Service/Manage.php
Normal file
63
app/Actions/Service/Manage.php
Normal 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');
|
||||
}
|
||||
}
|
@ -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');
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
14
app/Actions/Site/DeleteSite.php
Normal file
14
app/Actions/Site/DeleteSite.php
Normal 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();
|
||||
}
|
||||
}
|
51
app/Actions/Site/Deploy.php
Normal file
51
app/Actions/Site/Deploy.php
Normal 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;
|
||||
}
|
||||
}
|
@ -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');
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -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');
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@ -27,6 +27,6 @@ protected function validate(array $input): void
|
||||
{
|
||||
Validator::make($input, [
|
||||
'script' => 'required',
|
||||
])->validateWithBag('updateDeploymentScript');
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@ -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']
|
||||
);
|
||||
}
|
||||
}
|
||||
|
19
app/Actions/SshKey/DeleteKeyFromServer.php
Normal file
19
app/Actions/SshKey/DeleteKeyFromServer.php
Normal 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);
|
||||
}
|
||||
}
|
38
app/Actions/SshKey/DeployKeyToServer.php
Normal file
38
app/Actions/SshKey/DeployKeyToServer.php
Normal 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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user