Merge pull request #1 from vitodeployer/refactor

Refactor
This commit is contained in:
Saeed Vaziry 2023-08-06 17:37:04 +02:00 committed by GitHub
commit 1738f2ec84
362 changed files with 3402 additions and 2769 deletions

View File

@ -14,8 +14,8 @@ DB_DATABASE=vito
DB_USERNAME=root
DB_PASSWORD=
BROADCAST_DRIVER=log
CACHE_DRIVER=file
BROADCAST_DRIVER=null
CACHE_DRIVER=redis
FILESYSTEM_DRIVER=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=database

View File

@ -13,7 +13,7 @@ class CreateDatabaseUser
/**
* @throws ValidationException
*/
public function create(Server $server, array $input): DatabaseUser
public function create(Server $server, array $input, array $links = []): DatabaseUser
{
$this->validate($server, $input);
@ -22,6 +22,7 @@ public function create(Server $server, array $input): DatabaseUser
'username' => $input['username'],
'password' => $input['password'],
'host' => isset($input['remote']) && $input['remote'] ? $input['host'] : 'localhost',
'databases' => $links,
]);
$databaseUser->save();
$databaseUser->createOnServer();

View File

@ -21,7 +21,7 @@ public function create(Server $server, array $input): FirewallRule
'protocol' => $input['protocol'],
'port' => $input['port'],
'source' => $input['source'],
'mask' => $input['mask'],
'mask' => $input['mask'] ?? null,
'status' => FirewallRuleStatus::CREATING,
]);
$rule->save();
@ -49,14 +49,12 @@ private function validate(Server $server, array $input): void
'numeric',
'min:1',
'max:65535',
Rule::unique('firewall_rules', 'port')->where('server_id', $server->id),
],
'source' => [
'required',
'ip',
],
'mask' => [
'required',
'numeric',
],
])->validateWithBag('createRule');

View File

@ -3,6 +3,7 @@
namespace App\Actions\PHP;
use App\Models\Server;
use App\Models\Service;
use Illuminate\Validation\ValidationException;
class UninstallPHP
@ -11,6 +12,7 @@ public function uninstall(Server $server, string $version): void
{
$this->validate($server, $version);
/** @var Service $php */
$php = $server->services()->where('type', 'php')->where('version', $version)->first();
$php->uninstall();

View File

@ -29,6 +29,8 @@ public function update(Service $service, string $ini): void
'ini' => __("Couldn't update php.ini file!"),
]);
}
$service->restart();
}
private function deleteTempFile(string $name): void

View File

@ -36,8 +36,8 @@ public function create(User $creator, array $input): Server
'provider' => $input['provider'],
'authentication' => [
'user' => config('core.ssh_user'),
'pass' => Str::random(10),
'root_pass' => Str::random(10),
'pass' => Str::random(15),
'root_pass' => Str::random(15),
],
'progress' => 0,
'progress_step' => 'Initializing',
@ -77,8 +77,7 @@ public function create(User $creator, array $input): Server
$server->progress_step = __('Installation will begin in 3 minutes!');
$server->save();
dispatch(new ContinueInstallation($server))
->delay(now()->addMinutes(3))
->onQueue('default');
->delay(now()->addMinutes(2));
}
DB::commit();

View File

@ -6,7 +6,7 @@
class UpdateEnv
{
public function handle(Site $site, array $input): void
public function update(Site $site, array $input): void
{
$typeData = $site->type_data;
$typeData['env'] = $input['env'];

View File

@ -3,33 +3,48 @@
namespace App\Actions\SourceControl;
use App\Models\SourceControl;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
class ConnectSourceControl
{
public function connect(string $provider, array $input): void
public function connect(array $input): void
{
$sourceControl = SourceControl::query()
->where('provider', $provider)
->first();
if (! $sourceControl) {
$this->validate($input);
$sourceControl = new SourceControl([
'provider' => $provider,
'provider' => $input['provider'],
'profile' => $input['name'],
'access_token' => $input['token']
]);
}
if (! $input['token']) {
$sourceControl->delete();
return;
}
$sourceControl->access_token = $input['token'];
if (! $sourceControl->provider()->connect()) {
throw ValidationException::withMessages([
'token' => __('Cannot connect to :provider or invalid token!', ['provider' => $provider]),
'token' => __('Cannot connect to :provider or invalid token!', ['provider' => $sourceControl->provider]
),
]);
}
$sourceControl->save();
}
/**
* @throws ValidationException
*/
private function validate(array $input): void
{
$rules = [
'provider' => [
'required',
Rule::in(\App\Enums\SourceControl::getValues())
],
'name' => [
'required',
],
'token' => [
'required'
]
];
Validator::make($input, $rules)->validate();
}
}

View File

@ -4,7 +4,7 @@
interface Firewall
{
public function addRule(string $type, string $protocol, int $port, string $source, string $mask): void;
public function addRule(string $type, string $protocol, int $port, string $source, ?string $mask): void;
public function removeRule(string $type, string $protocol, int $port, string $source, string $mask): void;
public function removeRule(string $type, string $protocol, int $port, string $source, ?string $mask): void;
}

View File

@ -4,7 +4,7 @@
interface SSHCommand
{
public function file(string $os): string;
public function file(): string;
public function content(string $os): string;
public function content(): string;
}

View File

@ -8,11 +8,13 @@ public function connect(): bool;
public function getRepo(string $repo = null): mixed;
public function fullRepoUrl(string $repo): string;
public function fullRepoUrl(string $repo, string $key): string;
public function deployHook(string $repo, array $events, string $secret): array;
public function destroyHook(string $repo, string $hookId): void;
public function getLastCommit(string $repo, string $branch): ?array;
public function deployKey(string $title, string $repo, string $key): void;
}

View File

@ -3,23 +3,14 @@
namespace App\Events;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class Broadcast implements ShouldBroadcast
class Broadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public function __construct(public string $type, public array $data)
{
}
public function broadcastOn(): array
{
return [
new PrivateChannel('app'),
];
}
}

View File

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

View File

@ -9,7 +9,7 @@
/**
* Class SSH
*
* @method static \App\Helpers\SSH init(Server $server, string $asUser = null, bool $defaultKeys = false)
* @method static \App\Helpers\SSH 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)

View File

@ -4,11 +4,15 @@
use App\Contracts\SSHCommand;
use App\Exceptions\SSHAuthenticationError;
use App\Exceptions\SSHConnectionError;
use App\Models\Server;
use App\Models\ServerLog;
use Exception;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use phpseclib3\Crypt\Common\PrivateKey;
use phpseclib3\Crypt\PublicKeyLoader;
use phpseclib3\Net\SFTP;
use phpseclib3\Net\SSH2;
use Throwable;
class SSH
@ -17,7 +21,7 @@ class SSH
public ?ServerLog $log;
protected mixed $connection;
protected SSH2|SFTP|null $connection;
protected ?string $user;
@ -25,9 +29,9 @@ class SSH
protected string $publicKey;
protected string $privateKey;
protected PrivateKey $privateKey;
public function init(Server $server, string $asUser = null, bool $defaultKeys = false): self
public function init(Server $server, string $asUser = null): self
{
$this->connection = null;
$this->log = null;
@ -38,8 +42,9 @@ public function init(Server $server, string $asUser = null, bool $defaultKeys =
$this->user = $asUser;
$this->asUser = $asUser;
}
$this->publicKey = $this->server->sshKey($defaultKeys)['public_key_path'];
$this->privateKey = $this->server->sshKey($defaultKeys)['private_key_path'];
$this->privateKey = PublicKeyLoader::loadPrivateKey(
file_get_contents($this->server->sshKey()['private_key_path'])
);
return $this;
}
@ -57,29 +62,30 @@ public function setLog(string $logType, $siteId = null): void
/**
* @throws Throwable
*/
public function connect(): void
public function connect(bool $sftp = false): void
{
$defaultTimeout = ini_get('default_socket_timeout');
ini_set('default_socket_timeout', 7);
try {
if (! ($this->connection = ssh2_connect($this->server->ip, $this->server->port))) {
throw new SSHConnectionError('Cannot connect to the server');
if ($sftp) {
$this->connection = new SFTP($this->server->ip, $this->server->port);
} else {
$this->connection = new SSH2($this->server->ip, $this->server->port);
}
if (! ssh2_auth_pubkey_file($this->connection, $this->user, $this->publicKey, $this->privateKey)) {
throw new SSHAuthenticationError('Authentication failed');
$login = $this->connection->login($this->user, $this->privateKey);
if (! $login) {
throw new SSHAuthenticationError("Error authenticating");
}
Log::info("Login status", [
'status' => $login
]);
} catch (Throwable $e) {
ini_set('default_socket_timeout', $defaultTimeout);
if ($this->server->status == 'ready') {
$this->server->status = 'disconnected';
$this->server->save();
}
Log::error("Error connecting", [
"msg" => $e->getMessage()
]);
throw $e;
}
ini_set('default_socket_timeout', $defaultTimeout);
}
/**
@ -114,31 +120,17 @@ public function exec(string|array|SSHCommand $commands, string $log = '', int $s
*/
public function upload(string $local, string $remote): void
{
$this->log = null;
Log::info("Starting to upload");
if (! $this->connection) {
$this->connect();
$this->connect(true);
}
$sftp = @ssh2_sftp($this->connection);
if (! $sftp) {
throw new Exception('Could not initialize SFTP');
}
$stream = @fopen("ssh2.sftp://$sftp$remote", 'w');
if (! $stream) {
throw new Exception("Could not open file: $remote");
}
$data_to_send = @file_get_contents($local);
if ($data_to_send === false) {
throw new Exception("Could not open local file: $local.");
}
if (@fwrite($stream, $data_to_send) === false) {
throw new Exception("Could not send data from file: $local.");
}
@fclose($stream);
Log::info("Uploading");
$uploaded = $this->connection->put($remote, $local, SFTP::SOURCE_LOCAL_FILE);
Log::info("Upload finished", [
'status' => $uploaded
]);
}
/**
@ -152,31 +144,30 @@ protected function executeCommand(string|SSHCommand $command): string
$commandContent = $command;
}
Log::info("command", [
"asUser" => $this->asUser,
"content" => $commandContent
]);
if ($this->asUser) {
$commandContent = 'sudo su - '.$this->asUser.' -c '.'"'.addslashes($commandContent).'"';
}
if (! ($stream = ssh2_exec($this->connection, $commandContent, 'vt102', [], 100, 30))) {
throw new Exception('SSH command failed');
}
Log::info("Running command", [
"cmd" => $commandContent
]);
$data = '';
try {
stream_set_blocking($stream, true);
while ($buf = fread($stream, 1024)) {
$data .= $buf;
$this->log?->write($buf);
}
fclose($stream);
} catch (Throwable) {
$data = 'Error reading data';
}
$output = $this->connection->exec($commandContent);
if (Str::contains($data, 'VITO_SSH_ERROR')) {
Log::info("Command executed");
$this->log?->write($output);
if (Str::contains($output, 'VITO_SSH_ERROR')) {
throw new Exception('SSH command failed with an error');
}
return $data;
return $output;
}
/**
@ -185,11 +176,7 @@ protected function executeCommand(string|SSHCommand $command): string
public function disconnect(): void
{
if ($this->connection) {
try {
ssh2_disconnect($this->connection);
} catch (Exception) {
//
}
$this->connection->disconnect();
$this->connection = null;
}
}

View File

@ -2,6 +2,7 @@
namespace App\Http;
use App\Http\Middleware\ServerIsReadyMiddleware;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
@ -63,5 +64,6 @@ class Kernel extends HttpKernel
'signed' => \App\Http\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'server-is-ready' => ServerIsReadyMiddleware::class
];
}

View File

@ -0,0 +1,37 @@
<?php
namespace App\Http\Livewire\Application;
use App\Actions\Site\UpdateEnv;
use App\Models\Site;
use App\Traits\RefreshComponentOnBroadcast;
use Illuminate\Contracts\View\View;
use Livewire\Component;
class Env extends Component
{
use RefreshComponentOnBroadcast;
public Site $site;
public string $env;
public function mount(): void
{
$this->env = $this->site->env;
}
public function save(): void
{
app(UpdateEnv::class)->update($this->site, $this->all());
session()->flash('status', 'updating-env');
$this->emit(Deploy::class, '$refresh');
}
public function render(): View
{
return view('livewire.application.env');
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace App\Http\Livewire;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Cache;
use Livewire\Component;
class Broadcast extends Component
{
public function render(): View
{
$event = Cache::get('broadcast');
if ($event) {
Cache::forget('broadcast');
$this->emit('broadcast', $event);
}
return view('livewire.broadcast');
}
}

View File

@ -32,10 +32,10 @@ class DatabaseList extends Component
public function create(): void
{
app(CreateDatabase::class)->create($this->server, $this->all());
$database = app(CreateDatabase::class)->create($this->server, $this->all());
if ($this->all()['user']) {
app(CreateDatabaseUser::class)->create($this->server, $this->all());
app(CreateDatabaseUser::class)->create($this->server, $this->all(), [$database->name]);
}
$this->refreshComponent([]);
@ -45,6 +45,7 @@ public function create(): void
public function delete(): void
{
/** @var Database $database */
$database = Database::query()->findOrFail($this->deleteId);
$database->deleteFromServer();

View File

@ -43,6 +43,7 @@ public function create(): void
public function delete(): void
{
/** @var DatabaseUser $databaseUser */
$databaseUser = DatabaseUser::query()->findOrFail($this->deleteId);
$databaseUser->deleteFromServer();
@ -56,6 +57,7 @@ public function delete(): void
public function viewPassword(int $id): void
{
/** @var DatabaseUser $databaseUser */
$databaseUser = DatabaseUser::query()->findOrFail($id);
$this->viewPassword = $databaseUser->password;
@ -65,6 +67,7 @@ public function viewPassword(int $id): void
public function showLink(int $id): void
{
/** @var DatabaseUser $databaseUser */
$databaseUser = DatabaseUser::query()->findOrFail($id);
$this->linkId = $id;
@ -75,6 +78,7 @@ public function showLink(int $id): void
public function link(): void
{
/** @var DatabaseUser $databaseUser */
$databaseUser = DatabaseUser::query()->findOrFail($this->linkId);
app(LinkUser::class)->link($databaseUser, $this->link);

View File

@ -22,7 +22,7 @@ class CreateFirewallRule extends Component
public string $source = '0.0.0.0';
public string $mask = '0';
public string $mask = '';
public function create(): void
{

View File

@ -18,6 +18,7 @@ class FirewallRulesList extends Component
public function delete(): void
{
/** @var FirewallRule $rule */
$rule = FirewallRule::query()->findOrFail($this->deleteId);
$rule->removeFromServer();

View File

@ -6,7 +6,7 @@
use App\Actions\PHP\UpdatePHPIni;
use App\Models\Server;
use App\Models\Service;
use App\SSHCommands\GetPHPIniCommand;
use App\SSHCommands\PHP\GetPHPIniCommand;
use App\Traits\RefreshComponentOnBroadcast;
use Illuminate\Contracts\View\View;
use Livewire\Component;

View File

@ -0,0 +1,20 @@
<?php
namespace App\Http\Livewire\Servers;
use App\Models\Server;
use App\Traits\RefreshComponentOnBroadcast;
use Illuminate\Contracts\View\View;
use Livewire\Component;
class ServerStatus extends Component
{
use RefreshComponentOnBroadcast;
public Server $server;
public function render(): View
{
return view('livewire.servers.server-status');
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace App\Http\Livewire\Sites;
use App\Models\Site;
use App\Traits\RefreshComponentOnBroadcast;
use Illuminate\Contracts\View\View;
use Livewire\Component;
class SiteStatus extends Component
{
use RefreshComponentOnBroadcast;
public Site $site;
public function render(): View
{
return view('livewire.sites.site-status');
}
}

View File

@ -11,8 +11,12 @@ class Bitbucket extends Component
{
public string $token;
public ?string $url;
public function mount(): void
{
$this->url = request()->input('redirect') ?? null;
$this->token = SourceControl::query()
->where('provider', \App\Enums\SourceControl::BITBUCKET)
->first()?->access_token ?? '';
@ -23,6 +27,10 @@ public function connect(): void
app(ConnectSourceControl::class)->connect(\App\Enums\SourceControl::BITBUCKET, $this->all());
session()->flash('status', 'bitbucket-updated');
if ($this->url) {
$this->redirect($this->url);
}
}
public function render(): View

View File

@ -0,0 +1,38 @@
<?php
namespace App\Http\Livewire\SourceControls;
use App\Actions\SourceControl\ConnectSourceControl;
use Illuminate\Contracts\View\View;
use Livewire\Component;
class Connect extends Component
{
public string $provider = '';
public string $name;
public string $token;
public string $url;
public function connect(): void
{
app(ConnectSourceControl::class)->connect($this->all());
$this->emitTo(SourceControlsList::class, '$refresh');
$this->dispatchBrowserEvent('connected', true);
}
public function render(): View
{
if (request()->query('provider')) {
$this->provider = request()->query('provider');
}
return view('livewire.source-controls.connect', [
'open' => ! is_null(request()->query('provider')),
]);
}
}

View File

@ -11,8 +11,12 @@ class Github extends Component
{
public string $token;
public ?string $url;
public function mount(): void
{
$this->url = request()->input('redirect') ?? null;
$this->token = SourceControl::query()
->where('provider', \App\Enums\SourceControl::GITHUB)
->first()?->access_token ?? '';
@ -20,9 +24,13 @@ public function mount(): void
public function connect(): void
{
app(ConnectSourceControl::class)->connect(\App\Enums\SourceControl::GITHUB, $this->all());
app(ConnectSourceControl::class)->connect(\App\Enums\SourceControl::GITHUB, array_merge($this->all()));
session()->flash('status', 'github-updated');
if ($this->url) {
$this->redirect($this->url);
}
}
public function render(): View

View File

@ -11,8 +11,12 @@ class Gitlab extends Component
{
public string $token;
public ?string $url;
public function mount(): void
{
$this->url = request()->input('redirect') ?? null;
$this->token = SourceControl::query()
->where('provider', \App\Enums\SourceControl::GITLAB)
->first()?->access_token ?? '';
@ -23,6 +27,10 @@ public function connect(): void
app(ConnectSourceControl::class)->connect(\App\Enums\SourceControl::GITLAB, $this->all());
session()->flash('status', 'gitlab-updated');
if ($this->url) {
$this->redirect($this->url);
}
}
public function render(): View

View File

@ -0,0 +1,37 @@
<?php
namespace App\Http\Livewire\SourceControls;
use App\Models\SourceControl;
use App\Traits\RefreshComponentOnBroadcast;
use Illuminate\Contracts\View\View;
use Livewire\Component;
class SourceControlsList extends Component
{
use RefreshComponentOnBroadcast;
public int $deleteId;
protected $listeners = [
'$refresh',
];
public function delete(): void
{
$provider = SourceControl::query()->findOrFail($this->deleteId);
$provider->delete();
$this->refreshComponent([]);
$this->dispatchBrowserEvent('confirmed', true);
}
public function render(): View
{
return view('livewire.source-controls.source-controls-list', [
'sourceControls' => SourceControl::query()->latest()->get(),
]);
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace App\Http\Middleware;
use App\Models\Server;
use Closure;
use Illuminate\Http\Request;
class ServerIsReadyMiddleware
{
public function handle(Request $request, Closure $next)
{
/** @var Server $server */
$server = $request->route('server');
if (! $server->isReady()) {
return redirect()->route('servers.show', ['server' => $server]);
}
return $next($request);
}
}

View File

@ -6,7 +6,7 @@
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\CronJob;
use App\SSHCommands\UpdateCronJobsCommand;
use App\SSHCommands\CronJob\UpdateCronJobsCommand;
use Throwable;
class AddToServer extends Job

View File

@ -5,7 +5,7 @@
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\CronJob;
use App\SSHCommands\UpdateCronJobsCommand;
use App\SSHCommands\CronJob\UpdateCronJobsCommand;
use Throwable;
class RemoveFromServer extends Job

View File

@ -25,6 +25,11 @@ public function handle(): void
);
$this->databaseUser->status = DatabaseUserStatus::READY;
$this->databaseUser->save();
if (count($this->databaseUser->databases) > 0) {
(new LinkUser($this->databaseUser))->handle();
}
event(
new Broadcast('create-database-user-finished', [
'id' => $this->databaseUser->id,

View File

@ -3,8 +3,8 @@
namespace App\Jobs\Installation;
use App\Models\Server;
use App\SSHCommands\CreateUserCommand;
use App\SSHCommands\GetPublicKeyCommand;
use App\SSHCommands\System\CreateUserCommand;
use App\SSHCommands\System\GetPublicKeyCommand;
use Throwable;
class Initialize extends InstallationJob
@ -13,13 +13,10 @@ class Initialize extends InstallationJob
protected ?string $asUser;
protected bool $defaultKeys;
public function __construct(Server $server, string $asUser = null, bool $defaultKeys = false)
public function __construct(Server $server, string $asUser = null)
{
$this->server = $server->refresh();
$this->asUser = $asUser;
$this->defaultKeys = $defaultKeys;
}
/**
@ -38,7 +35,7 @@ public function handle(): void
protected function authentication(): void
{
$this->server
->ssh($this->asUser ?? $this->server->ssh_user, $this->defaultKeys)
->ssh($this->asUser ?? $this->server->ssh_user)
->exec(
new CreateUserCommand(
$this->server->authentication['user'],

View File

@ -3,7 +3,7 @@
namespace App\Jobs\Installation;
use App\Models\Server;
use App\SSHCommands\InstallCertbotCommand;
use App\SSHCommands\SSL\InstallCertbotCommand;
use Throwable;
class InstallCertbot extends InstallationJob

View File

@ -3,7 +3,7 @@
namespace App\Jobs\Installation;
use App\Models\Server;
use App\SSHCommands\InstallComposerCommand;
use App\SSHCommands\PHP\InstallComposerCommand;
use Throwable;
class InstallComposer extends InstallationJob

View File

@ -5,8 +5,8 @@
use App\Enums\ServiceStatus;
use App\Exceptions\InstallationFailed;
use App\Models\Service;
use App\SSHCommands\InstallMariadbCommand;
use App\SSHCommands\ServiceStatusCommand;
use App\SSHCommands\Database\InstallMariadbCommand;
use App\SSHCommands\Service\ServiceStatusCommand;
use Throwable;
class InstallMariadb extends InstallationJob

View File

@ -5,8 +5,8 @@
use App\Enums\ServiceStatus;
use App\Exceptions\InstallationFailed;
use App\Models\Service;
use App\SSHCommands\InstallMysqlCommand;
use App\SSHCommands\ServiceStatusCommand;
use App\SSHCommands\Database\InstallMysqlCommand;
use App\SSHCommands\Service\ServiceStatusCommand;
use Throwable;
class InstallMysql extends InstallationJob

View File

@ -5,8 +5,8 @@
use App\Enums\ServiceStatus;
use App\Exceptions\InstallationFailed;
use App\Models\Service;
use App\SSHCommands\InstallNginxCommand;
use App\SSHCommands\ServiceStatusCommand;
use App\SSHCommands\Nginx\InstallNginxCommand;
use App\SSHCommands\Service\ServiceStatusCommand;
use Throwable;
class InstallNginx extends InstallationJob

View File

@ -3,7 +3,7 @@
namespace App\Jobs\Installation;
use App\Models\Server;
use App\SSHCommands\InstallNodejsCommand;
use App\SSHCommands\Installation\InstallNodejsCommand;
use Throwable;
class InstallNodejs extends InstallationJob

View File

@ -5,8 +5,8 @@
use App\Enums\ServiceStatus;
use App\Exceptions\InstallationFailed;
use App\Models\Service;
use App\SSHCommands\InstallPHPCommand;
use App\SSHCommands\ServiceStatusCommand;
use App\SSHCommands\PHP\InstallPHPCommand;
use App\SSHCommands\Service\ServiceStatusCommand;
use Throwable;
class InstallPHP extends InstallationJob

View File

@ -6,8 +6,8 @@
use App\Jobs\Job;
use App\Models\FirewallRule;
use App\Models\Service;
use App\SSHCommands\CreateNginxPHPMyAdminVHostCommand;
use App\SSHCommands\DownloadPHPMyAdminCommand;
use App\SSHCommands\PHPMyAdmin\CreateNginxPHPMyAdminVHostCommand;
use App\SSHCommands\PHPMyAdmin\DownloadPHPMyAdminCommand;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Throwable;
@ -76,7 +76,7 @@ private function downloadSource(): void
*/
private function setUpVHost(): void
{
$vhost = File::get(base_path('system/command-templates/nginx/phpmyadmin-vhost.conf'));
$vhost = File::get(resource_path('commands/webserver/nginx/phpmyadmin-vhost.conf'));
$vhost = Str::replace('__php_version__', $this->service->server->defaultService('php')->version, $vhost);
$this->service->server->ssh()->exec(
new CreateNginxPHPMyAdminVHostCommand($vhost),

View File

@ -5,8 +5,8 @@
use App\Enums\ServiceStatus;
use App\Exceptions\InstallationFailed;
use App\Models\Service;
use App\SSHCommands\InstallRedisCommand;
use App\SSHCommands\ServiceStatusCommand;
use App\SSHCommands\Installation\InstallRedisCommand;
use App\SSHCommands\Service\ServiceStatusCommand;
use Throwable;
class InstallRedis extends InstallationJob

View File

@ -3,7 +3,7 @@
namespace App\Jobs\Installation;
use App\Models\Server;
use App\SSHCommands\InstallRequirementsCommand;
use App\SSHCommands\Installation\InstallRequirementsCommand;
use Throwable;
class InstallRequirements extends InstallationJob

View File

@ -5,8 +5,8 @@
use App\Enums\ServiceStatus;
use App\Exceptions\InstallationFailed;
use App\Models\Service;
use App\SSHCommands\InstallSupervisorCommand;
use App\SSHCommands\ServiceStatusCommand;
use App\SSHCommands\Service\ServiceStatusCommand;
use App\SSHCommands\Supervisor\InstallSupervisorCommand;
use Throwable;
class InstallSupervisor extends InstallationJob

View File

@ -5,8 +5,8 @@
use App\Enums\ServiceStatus;
use App\Exceptions\InstallationFailed;
use App\Models\Service;
use App\SSHCommands\InstallUfwCommand;
use App\SSHCommands\ServiceStatusCommand;
use App\SSHCommands\Firewall\InstallUfwCommand;
use App\SSHCommands\Service\ServiceStatusCommand;
use Throwable;
class InstallUfw extends InstallationJob

View File

@ -4,7 +4,7 @@
use App\Exceptions\InstallationFailed;
use App\Models\Service;
use App\SSHCommands\UninstallPHPCommand;
use App\SSHCommands\PHP\UninstallPHPCommand;
use Throwable;
class UninstallPHP extends InstallationJob

View File

@ -5,7 +5,7 @@
use App\Jobs\Job;
use App\Models\FirewallRule;
use App\Models\Service;
use App\SSHCommands\DeleteNginxPHPMyAdminVHost;
use App\SSHCommands\PHPMyAdmin\DeleteNginxPHPMyAdminVHost;
use Exception;
use Throwable;

View File

@ -3,7 +3,7 @@
namespace App\Jobs\Installation;
use App\Models\Server;
use App\SSHCommands\UpgradeCommand;
use App\SSHCommands\System\UpgradeCommand;
use Throwable;
class Upgrade extends InstallationJob

View File

@ -6,7 +6,7 @@
use App\Exceptions\ProcessFailed;
use App\Jobs\Job;
use App\Models\Service;
use App\SSHCommands\InstallPHPExtensionCommand;
use App\SSHCommands\PHP\InstallPHPExtensionCommand;
use Illuminate\Support\Str;
use Throwable;

View File

@ -6,7 +6,7 @@
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\Service;
use App\SSHCommands\ChangeDefaultPHPCommand;
use App\SSHCommands\PHP\ChangeDefaultPHPCommand;
use Throwable;
class SetDefaultCli extends Job

View File

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

View File

@ -5,7 +5,7 @@
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\Server;
use App\SSHCommands\RebootCommand;
use App\SSHCommands\System\RebootCommand;
use Throwable;
class RebootServer extends Job

View File

@ -5,9 +5,9 @@
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\Service;
use App\SSHCommands\RestartServiceCommand;
use App\SSHCommands\StartServiceCommand;
use App\SSHCommands\StopServiceCommand;
use App\SSHCommands\Service\RestartServiceCommand;
use App\SSHCommands\Service\StartServiceCommand;
use App\SSHCommands\Service\StopServiceCommand;
use Exception;
use Throwable;

View File

@ -4,7 +4,7 @@
use App\Jobs\Job;
use App\Models\Site;
use App\SSHCommands\CloneRepositoryCommand;
use App\SSHCommands\Website\CloneRepositoryCommand;
use Throwable;
class CloneRepository extends Job
@ -25,7 +25,8 @@ public function handle(): void
new CloneRepositoryCommand(
$this->site->full_repository_url,
$this->site->path,
$this->site->branch
$this->site->branch,
$this->site->ssh_key_name
),
'clone-repository',
$this->site->id

View File

@ -5,7 +5,7 @@
use App\Exceptions\ComposerInstallFailed;
use App\Jobs\Job;
use App\Models\Site;
use App\SSHCommands\ComposerInstallCommand;
use App\SSHCommands\Website\ComposerInstallCommand;
use Throwable;
class ComposerInstall extends Job

View File

@ -7,7 +7,7 @@
use App\Helpers\SSH;
use App\Jobs\Job;
use App\Models\Deployment;
use App\SSHCommands\RunScript;
use App\SSHCommands\System\RunScript;
use Throwable;
class Deploy extends Job

View File

@ -5,7 +5,7 @@
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\Site;
use App\SSHCommands\EditFileCommand;
use App\SSHCommands\System\EditFileCommand;
use Throwable;
class DeployEnv extends Job
@ -26,7 +26,9 @@ public function handle(): void
new EditFileCommand(
$this->site->path.'/.env',
$this->site->env
)
),
'update-env',
$this->site->id
);
event(
new Broadcast('deploy-site-env-finished', [

42
app/Jobs/Site/DeployKey.php Executable file
View File

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

@ -7,7 +7,7 @@
use App\Models\Database;
use App\Models\DatabaseUser;
use App\Models\Site;
use App\SSHCommands\InstallWordpressCommand;
use App\SSHCommands\Wordpress\InstallWordpressCommand;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Throwable;

View File

@ -5,7 +5,7 @@
use App\Events\Broadcast;
use App\Jobs\Job;
use App\Models\Site;
use App\SSHCommands\UpdateBranchCommand;
use App\SSHCommands\Website\UpdateBranchCommand;
use Throwable;
class UpdateBranch extends Job

View File

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

View File

@ -6,7 +6,7 @@
use App\Jobs\Job;
use App\Models\Server;
use App\Models\SshKey;
use App\SSHCommands\DeleteSshKeyCommand;
use App\SSHCommands\System\DeleteSshKeyCommand;
use Throwable;
class DeleteSshKeyFromServer extends Job

View File

@ -7,7 +7,7 @@
use App\Jobs\Job;
use App\Models\Server;
use App\Models\SshKey;
use App\SSHCommands\DeploySshKeyCommand;
use App\SSHCommands\System\DeploySshKeyCommand;
use Throwable;
class DeploySshKeyToServer extends Job

View File

@ -0,0 +1,21 @@
<?php
namespace App\Listeners;
use App\Events\Broadcast;
use Illuminate\Support\Facades\Cache;
class BroadcastListener
{
public function __construct()
{
}
public function handle(Broadcast $event): void
{
Cache::set('broadcast', [
'type' => $event->type,
'data' => $event->data
], now()->addMinutes(5));
}
}

View File

@ -15,7 +15,7 @@
* @property string $real_protocol
* @property int $port
* @property string $source
* @property string $mask
* @property ?string $mask
* @property string $note
* @property string $status
* @property Server $server

View File

@ -3,6 +3,7 @@
namespace App\Models;
use App\Contracts\ServerType;
use App\Enums\ServerStatus;
use App\Facades\SSH;
use App\Jobs\Installation\Upgrade;
use App\Jobs\Server\CheckConnection;
@ -232,9 +233,9 @@ public function install(): void
// $this->team->notify(new ServerInstallationStarted($this));
}
public function ssh(string $user = null, bool $defaultKeys = false): \App\Helpers\SSH|SSHFake
public function ssh(string $user = null): \App\Helpers\SSH|SSHFake
{
return SSH::init($this, $user, $defaultKeys);
return SSH::init($this, $user);
}
public function installedPHPVersions(): array
@ -323,7 +324,7 @@ public function getSshUserAttribute(string $value): string
return config('core.ssh_user');
}
public function sshKey(bool $default = false): array
public function sshKey(): array
{
if (app()->environment() == 'testing') {
return [
@ -333,14 +334,6 @@ public function sshKey(bool $default = false): array
];
}
if ($default) {
return [
'public_key' => Str::replace("\n", '', File::get(storage_path(config('core.ssh_public_key_name')))),
'public_key_path' => storage_path(config('core.ssh_public_key_name')),
'private_key_path' => storage_path(config('core.ssh_private_key_name')),
];
}
return [
'public_key' => Str::replace("\n", '', Storage::disk(config('core.key_pairs_disk'))->get($this->id.'.pub')),
'public_key_path' => Storage::disk(config('core.key_pairs_disk'))->path($this->id.'.pub'),
@ -385,4 +378,9 @@ public function getHostnameAttribute(): string
{
return Str::of($this->name)->slug();
}
public function isReady(): bool
{
return $this->status == ServerStatus::READY;
}
}

View File

@ -14,6 +14,7 @@
use App\Jobs\Site\UpdateBranch;
use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
@ -32,7 +33,9 @@
* @property string $path
* @property string $php_version
* @property string $source_control
* @property int $source_control_id
* @property string $repository
* @property string $ssh_key
* @property string $branch
* @property string $status
* @property int $port
@ -52,6 +55,7 @@
* @property string $aliases_string
* @property string $deployment_script_text
* @property string $env
* @property string $ssh_key_name
*/
class Site extends AbstractModel
{
@ -67,7 +71,9 @@ class Site extends AbstractModel
'path',
'php_version',
'source_control',
'source_control_id',
'repository',
'ssh_key',
'branch',
'status',
'port',
@ -81,6 +87,7 @@ class Site extends AbstractModel
'progress' => 'integer',
'auto_deployment' => 'boolean',
'aliases' => 'array',
'source_control_id' => 'integer',
];
protected $appends = [
@ -151,22 +158,21 @@ public function ssls(): HasMany
/**
* @throws SourceControlIsNotConnected
*/
public function sourceControl(): SourceControl|HasOne|null
public function sourceControl(): SourceControl|HasOne|null|Model
{
if (! $this->source_control) {
$sourceControl = null;
if (! $this->source_control && ! $this->source_control_id) {
return null;
}
if ($this->source_control == 'custom') {
return new SourceControl([
'user_id' => $this->id,
'provider' => 'custom',
'token' => '',
'connected' => true,
]);
if ($this->source_control) {
$sourceControl = SourceControl::query()->where('provider', $this->source_control)->first();
}
$sourceControl = SourceControl::query()->where('provider', $this->source_control)->first();
if ($this->source_control_id) {
$sourceControl = SourceControl::query()->find($this->source_control_id);
}
if (! $sourceControl) {
throw new SourceControlIsNotConnected($this->source_control);
@ -180,7 +186,7 @@ public function sourceControl(): SourceControl|HasOne|null
*/
public function getFullRepositoryUrlAttribute()
{
return $this->sourceControl()->provider()->fullRepoUrl($this->repository);
return $this->sourceControl()->provider()->fullRepoUrl($this->repository, $this->ssh_key_name);
}
public function getAliasesStringAttribute(): string
@ -394,4 +400,9 @@ public function updateBranch(string $branch): void
{
dispatch(new UpdateBranch($this, $branch))->onConnection('ssh');
}
public function getSshKeyNameAttribute(): string
{
return str('site_'.$this->id)->toString();
}
}

View File

@ -7,6 +7,8 @@
/**
* @property string $provider
* @property ?string $profile
* @property ?string $url
* @property string $access_token
*/
class SourceControl extends AbstractModel
@ -15,6 +17,8 @@ class SourceControl extends AbstractModel
protected $fillable = [
'provider',
'profile',
'url',
'access_token',
];

View File

@ -43,7 +43,7 @@ public function sendMessage(string $subject, string $text): void
Http::post($data['webhook_url'], [
'content' => '*'.$subject.'*'."\n".$text,
]);
})->onQueue('default');
});
}
private function checkConnection(string $subject, string $text): bool

View File

@ -43,7 +43,7 @@ public function sendMessage(string $subject, string $text): void
Http::post($data['webhook_url'], [
'text' => '*'.$subject.'*'."\n".$text,
]);
})->onQueue('default');
});
}
private function checkConnection(string $subject, string $text): bool

View File

@ -2,6 +2,8 @@
namespace App\Providers;
use App\Events\Broadcast;
use App\Listeners\BroadcastListener;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
@ -18,6 +20,9 @@ class EventServiceProvider extends ServiceProvider
Registered::class => [
SendEmailVerificationNotification::class,
],
Broadcast::class => [
BroadcastListener::class,
],
];
/**

View File

@ -1,32 +0,0 @@
<?php
namespace App\SSHCommands;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class ChangeDefaultPHPCommand extends Command
{
/**
* @var string
*/
protected $version;
/**
* InstallPHPCommand constructor.
*/
public function __construct($version)
{
$this->version = $version;
}
public function file(string $os): string
{
return File::get(base_path('system/commands/ubuntu/change-default-php.sh'));
}
public function content(string $os): string
{
return Str::replace('__version__', $this->version, $this->file($os));
}
}

View File

@ -1,47 +0,0 @@
<?php
namespace App\SSHCommands;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class ChangeNginxPHPVersionCommand extends Command
{
/**
* @var string
*/
protected $domain;
/**
* @var string
*/
protected $oldVersion;
/**
* @var string
*/
protected $newVersion;
/**
* CreateVHostCommand constructor.
*/
public function __construct(string $domain, string $oldVersion, string $newVersion)
{
$this->domain = $domain;
$this->oldVersion = $oldVersion;
$this->newVersion = $newVersion;
}
public function file(string $os): string
{
return File::get(base_path('system/commands/ubuntu/webserver/nginx/change-php-version.sh'));
}
public function content(string $os): string
{
$command = Str::replace('__domain__', $this->domain, $this->file($os));
$command = Str::replace('__old_version__', $this->oldVersion, $command);
return Str::replace('__new_version__', $this->newVersion, $command);
}
}

View File

@ -1,45 +0,0 @@
<?php
namespace App\SSHCommands;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class CloneRepositoryCommand extends Command
{
/**
* @var string
*/
protected $repository;
/**
* @var string
*/
protected $path;
protected $branch;
/**
* CloneRepositoryCommand constructor.
*/
public function __construct($repository, $path, $branch)
{
$this->repository = $repository;
$this->path = $path;
$this->branch = $branch;
}
public function file(string $os): string
{
return File::get(base_path('system/commands/common/clone-repository.sh'));
}
public function content(string $os): string
{
$command = Str::replace('__repo__', $this->repository, $this->file($os));
$command = Str::replace('__host__', get_hostname_from_repo($this->repository), $command);
$command = Str::replace('__branch__', $this->branch, $command);
return Str::replace('__path__', $this->path, $command);
}
}

View File

@ -1,29 +0,0 @@
<?php
namespace App\SSHCommands;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class ComposerInstallCommand extends Command
{
protected $path;
/**
* ComposerInstallCommand constructor.
*/
public function __construct($path)
{
$this->path = $path;
}
public function file(string $os): string
{
return File::get(base_path('system/commands/common/composer-install.sh'));
}
public function content(string $os): string
{
return Str::replace('__path__', $this->path, $this->file($os));
}
}

View File

@ -1,43 +0,0 @@
<?php
namespace App\SSHCommands;
use Illuminate\Support\Facades\File;
class CreateCustomSSLCommand extends Command
{
protected $path;
protected $certificate;
protected $pk;
protected $certificatePath;
protected $pkPath;
public function __construct($path, $certificate, $pk, $certificatePath, $pkPath)
{
$this->path = $path;
$this->certificate = $certificate;
$this->pk = $pk;
$this->certificatePath = $certificatePath;
$this->pkPath = $pkPath;
}
public function file(string $os): string
{
return File::get(base_path('system/commands/common/create-custom-ssl.sh'));
}
public function content(string $os): string
{
$content = $this->file($os);
$content = str_replace('__path__', $this->path, $content);
$content = str_replace('__certificate__', $this->certificate, $content);
$content = str_replace('__pk__', $this->pk, $content);
$content = str_replace('__certificate_path__', $this->certificatePath, $content);
return str_replace('__pk_path__', $this->pkPath, $content);
}
}

View File

@ -1,35 +0,0 @@
<?php
namespace App\SSHCommands;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class CreateLetsencryptSSLCommand extends Command
{
protected $email;
protected $domain;
protected $webDirectory;
public function __construct($email, $domain, $webDirectory)
{
$this->email = $email;
$this->domain = $domain;
$this->webDirectory = $webDirectory;
}
public function file(string $os): string
{
return File::get(base_path('system/commands/common/create-letsencrypt-ssl.sh'));
}
public function content(string $os): string
{
$command = Str::replace('__email__', $this->email, $this->file($os));
$command = Str::replace('__web_directory__', $this->webDirectory, $command);
return Str::replace('__domain__', $this->domain, $command);
}
}

View File

@ -1,29 +0,0 @@
<?php
namespace App\SSHCommands;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class CreateNginxPHPMyAdminVHostCommand extends Command
{
/**
* @var string
*/
protected $vhost;
public function __construct(string $vhost)
{
$this->vhost = $vhost;
}
public function file(string $os): string
{
return File::get(base_path('system/commands/ubuntu/webserver/nginx/create-phpmyadmin-vhost.sh'));
}
public function content(string $os): string
{
return Str::replace('__vhost__', $this->vhost, $this->file($os));
}
}

View File

@ -1,50 +0,0 @@
<?php
namespace App\SSHCommands;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class CreateNginxVHostCommand extends Command
{
/**
* @var string
*/
protected $domain;
/**
* @var string
*/
protected $path;
/**
* @var string
*/
protected $vhost;
/**
* CreateVHostCommand constructor.
*/
public function __construct(
string $domain,
string $path,
string $vhost
) {
$this->domain = $domain;
$this->path = $path;
$this->vhost = $vhost;
}
public function file(string $os): string
{
return File::get(base_path('system/commands/ubuntu/webserver/nginx/create-vhost.sh'));
}
public function content(string $os): string
{
$command = Str::replace('__path__', $this->path, $this->file($os));
$command = Str::replace('__domain__', $this->domain, $command);
return Str::replace('__vhost__', $this->vhost, $command);
}
}

View File

@ -1,39 +0,0 @@
<?php
namespace App\SSHCommands;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class CreateUserCommand extends Command
{
protected $user;
protected $password;
protected $key;
/**
* CreateUserCommand constructor.
*/
public function __construct($user, $password, $key)
{
$this->user = $user;
$this->password = $password;
$this->key = $key;
}
public function file(string $os): string
{
return File::get(base_path('system/commands/ubuntu/create-user.sh'));
}
public function content(string $os): string
{
$command = $this->file($os);
$command = Str::replace('__user__', $this->user, $command);
$command = Str::replace('__key__', $this->key, $command);
return Str::replace('__password__', $this->password, $command);
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace App\SSHCommands\CronJob;
use App\SSHCommands\Command;
use Illuminate\Support\Facades\File;
class UpdateCronJobsCommand extends Command
{
public function __construct(protected string $user, protected string $data)
{
}
public function file(): string
{
return File::get(resource_path('commands/cronjobs/update-cron-jobs.sh'));
}
public function content(): string
{
return str($this->file())
->replace('__user__', $this->user)
->replace('__data__', $this->data)
->toString();
}
}

View File

@ -4,33 +4,23 @@
use App\SSHCommands\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class BackupDatabaseCommand extends Command
{
protected $provider;
protected $database;
protected $fileName;
public function __construct($provider, $database, $fileName)
public function __construct(protected string $provider, protected string $database, protected string $fileName)
{
$this->provider = $provider;
$this->database = $database;
$this->fileName = $fileName;
}
public function file(string $os): string
public function file(): string
{
return File::get(base_path('system/commands/database/'.$this->provider.'/backup.sh'));
return File::get(resource_path(sprintf("commands/database/%s/backup.sh", $this->provider)));
}
public function content(string $os): string
public function content(): string
{
$command = $this->file($os);
$command = Str::replace('__database__', $this->database, $command);
return Str::replace('__file__', $this->fileName, $command);
return str($this->file())
->replace('__database__', $this->database)
->replace('__file__', $this->fileName)
->toString();
}
}

View File

@ -4,33 +4,22 @@
use App\SSHCommands\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class CreateCommand extends Command
{
/**
* @var string
*/
protected $provider;
/**
* @var string
*/
protected $name;
public function __construct($provider, $name)
public function __construct(protected string $provider, protected string $name)
{
$this->provider = $provider;
$this->name = $name;
}
public function file(string $os): string
public function file(): string
{
return File::get(base_path('system/commands/database/'.$this->provider.'/create.sh'));
return File::get(resource_path(sprintf("commands/database/%s/create.sh", $this->provider)));
}
public function content(string $os): string
public function content(): string
{
return Str::replace('__name__', $this->name, $this->file($os));
return str($this->file())
->replace('__name__', $this->name)
->toString();
}
}

View File

@ -4,48 +4,28 @@
use App\SSHCommands\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class CreateUserCommand extends Command
{
/**
* @var string
*/
protected $provider;
/**
* @var string
*/
protected $username;
/**
* @var string
*/
protected $password;
/**
* @var string
*/
protected $host;
public function __construct($provider, $username, $password, $host)
{
$this->provider = $provider;
$this->username = $username;
$this->password = $password;
$this->host = $host;
public function __construct(
protected string $provider,
protected string $username,
protected string $password,
protected string $host
) {
}
public function file(string $os): string
public function file(): string
{
return File::get(base_path('system/commands/database/'.$this->provider.'/create-user.sh'));
return File::get(resource_path(sprintf("commands/database/%s/create-user.sh", $this->provider)));
}
public function content(string $os): string
public function content(): string
{
$command = Str::replace('__username__', $this->username, $this->file($os));
$command = Str::replace('__password__', $this->password, $command);
return Str::replace('__host__', $this->host, $command);
return str($this->file())
->replace('__username__', $this->username)
->replace('__password__', $this->password)
->replace('__host__', $this->host)
->toString();
}
}

View File

@ -4,33 +4,22 @@
use App\SSHCommands\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class DeleteCommand extends Command
{
/**
* @var string
*/
protected $provider;
/**
* @var string
*/
protected $name;
public function __construct($provider, $name)
public function __construct(protected string $provider, protected string $name)
{
$this->provider = $provider;
$this->name = $name;
}
public function file(string $os): string
public function file(): string
{
return File::get(base_path('system/commands/database/'.$this->provider.'/delete.sh'));
return File::get(resource_path(sprintf("commands/database/%s/delete.sh", $this->provider)));
}
public function content(string $os): string
public function content(): string
{
return Str::replace('__name__', $this->name, $this->file($os));
return str($this->file())
->replace('__name__', $this->name)
->toString();
}
}

View File

@ -4,41 +4,23 @@
use App\SSHCommands\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class DeleteUserCommand extends Command
{
/**
* @var string
*/
protected $provider;
/**
* @var string
*/
protected $username;
/**
* @var string
*/
protected $host;
public function __construct($provider, $username, $host)
public function __construct(protected string $provider, protected string $username, protected string $host)
{
$this->provider = $provider;
$this->username = $username;
$this->host = $host;
}
public function file(string $os): string
public function file(): string
{
return File::get(base_path('system/commands/database/'.$this->provider.'/delete-user.sh'));
return File::get(resource_path(sprintf("commands/database/%s/delete-user.sh", $this->provider)));
}
public function content(string $os): string
public function content(): string
{
$command = Str::replace('__username__', $this->username, $this->file($os));
return Str::replace('__host__', $this->host, $command);
return str($this->file())
->replace('__username__', $this->username)
->replace('__host__', $this->host)
->toString();
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace App\SSHCommands\Database;
use App\SSHCommands\Command;
use Illuminate\Support\Facades\File;
class InstallMariadbCommand extends Command
{
public function file(): string
{
return File::get(resource_path('commands/database/install-mariadb.sh'));
}
public function content(): string
{
return $this->file();
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace App\SSHCommands\Database;
use App\SSHCommands\Command;
use Illuminate\Support\Facades\File;
class InstallMysqlCommand extends Command
{
public function __construct(protected string $version)
{
}
public function file(): string
{
if ($this->version == '8.0') {
return File::get(resource_path('commands/database/install-mysql-8.sh'));
}
return File::get(resource_path('commands/database/install-mysql.sh'));
}
public function content(): string
{
return $this->file();
}
}

View File

@ -4,45 +4,28 @@
use App\SSHCommands\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class LinkCommand extends Command
{
/**
* @var string
*/
protected $provider;
/**
* @var string
*/
protected $username;
protected $host;
/**
* @var string
*/
protected $database;
public function __construct($provider, $username, $host, $database)
{
$this->provider = $provider;
$this->username = $username;
$this->host = $host;
$this->database = $database;
public function __construct(
protected string $provider,
protected string $username,
protected string $host,
protected string $database
) {
}
public function file(string $os): string
public function file(): string
{
return File::get(base_path('system/commands/database/'.$this->provider.'/link.sh'));
return File::get(resource_path(sprintf("commands/database/%s/link.sh", $this->provider)));
}
public function content(string $os): string
public function content(): string
{
$command = Str::replace('__username__', $this->username, $this->file($os));
$command = Str::replace('__host__', $this->host, $command);
return Str::replace('__database__', $this->database, $command);
return str($this->file())
->replace('__username__', $this->username)
->replace('__host__', $this->host)
->replace('__database__', $this->database)
->toString();
}
}

View File

@ -4,33 +4,23 @@
use App\SSHCommands\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class RestoreDatabaseCommand extends Command
{
protected $provider;
protected $database;
protected $fileName;
public function __construct($provider, $database, $fileName)
public function __construct(protected string $provider, protected string $database, protected string $fileName)
{
$this->provider = $provider;
$this->database = $database;
$this->fileName = $fileName;
}
public function file(string $os): string
public function file(): string
{
return File::get(base_path('system/commands/database/'.$this->provider.'/restore.sh'));
return File::get(resource_path(sprintf("commands/database/%s/restore.sh", $this->provider)));
}
public function content(string $os): string
public function content(): string
{
$command = $this->file($os);
$command = Str::replace('__database__', $this->database, $command);
return Str::replace('__file__', $this->fileName, $command);
return str($this->file())
->replace('__database__', $this->database)
->replace('__file__', $this->fileName)
->toString();
}
}

View File

@ -4,38 +4,26 @@
use App\SSHCommands\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class UnlinkCommand extends Command
{
/**
* @var string
*/
protected $provider;
/**
* @var string
*/
protected $username;
protected $host;
public function __construct($provider, $username, $host)
{
$this->provider = $provider;
$this->username = $username;
$this->host = $host;
public function __construct(
protected string $provider,
protected string $username,
protected string $host
) {
}
public function file(string $os): string
public function file(): string
{
return File::get(base_path('system/commands/database/'.$this->provider.'/unlink.sh'));
return File::get(resource_path(sprintf("commands/database/%s/unlink.sh", $this->provider)));
}
public function content(string $os): string
public function content(): string
{
$command = Str::replace('__username__', $this->username, $this->file($os));
return Str::replace('__host__', $this->host, $command);
return str($this->file())
->replace('__username__', $this->username)
->replace('__host__', $this->host)
->toString();
}
}

View File

@ -1,30 +0,0 @@
<?php
namespace App\SSHCommands;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class DeleteNginxPHPMyAdminVHost extends Command
{
/**
* @var string
*/
protected $path;
public function __construct(
string $path,
) {
$this->path = $path;
}
public function file(string $os): string
{
return File::get(base_path('system/commands/ubuntu/webserver/nginx/delete-phpmyadmin-vhost.sh'));
}
public function content(string $os): string
{
return Str::replace('__path__', $this->path, $this->file($os));
}
}

View File

@ -1,40 +0,0 @@
<?php
namespace App\SSHCommands;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class DeleteNginxSiteCommand extends Command
{
/**
* @var string
*/
protected $domain;
/**
* @var string
*/
protected $path;
/**
* CloneRepositoryCommand constructor.
*/
public function __construct($domain, $path)
{
$this->domain = $domain;
$this->path = $path;
}
public function file(string $os): string
{
return File::get(base_path('system/commands/ubuntu/webserver/nginx/delete-site.sh'));
}
public function content(string $os): string
{
$command = Str::replace('__domain__', $this->domain, $this->file($os));
return Str::replace('__path__', $this->path, $command);
}
}

View File

@ -1,32 +0,0 @@
<?php
namespace App\SSHCommands;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class DeleteSshKeyCommand extends Command
{
/**
* @var string
*/
protected $key;
/**
* InstallPHPCommand constructor.
*/
public function __construct($key)
{
$this->key = $key;
}
public function file(string $os): string
{
return File::get(base_path('system/commands/ubuntu/delete-ssh-key.sh'));
}
public function content(string $os): string
{
return Str::replace('__key__', Str::replace('/', '\/', $this->key), $this->file($os));
}
}

View File

@ -1,32 +0,0 @@
<?php
namespace App\SSHCommands;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class DeploySshKeyCommand extends Command
{
/**
* @var string
*/
protected $key;
/**
* InstallPHPCommand constructor.
*/
public function __construct($key)
{
$this->key = $key;
}
public function file(string $os): string
{
return File::get(base_path('system/commands/ubuntu/deploy-ssh-key.sh'));
}
public function content(string $os): string
{
return Str::replace('__key__', addslashes($this->key), $this->file($os));
}
}

View File

@ -1,18 +0,0 @@
<?php
namespace App\SSHCommands;
use Illuminate\Support\Facades\File;
class DownloadPHPMyAdminCommand extends Command
{
public function file(string $os): string
{
return File::get(base_path('system/commands/common/download-phpmyadmin.sh'));
}
public function content(string $os): string
{
return $this->file($os);
}
}

View File

@ -1,31 +0,0 @@
<?php
namespace App\SSHCommands;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class EditFileCommand extends Command
{
protected $path;
protected $content;
public function __construct($path, $content)
{
$this->path = $path;
$this->content = $content;
}
public function file(string $os): string
{
return File::get(base_path('system/commands/common/edit-file.sh'));
}
public function content(string $os): string
{
$command = Str::replace('__path__', $this->path, $this->file($os));
return Str::replace('__content__', addslashes($this->content), $command);
}
}

View File

@ -9,48 +9,18 @@ class AddRuleCommand extends Command
{
use CommandContent;
/**
* @var string
*/
protected $provider;
/**
* @var string
*/
protected $type;
/**
* @var string
*/
protected $protocol;
/**
* @var string
*/
protected $port;
/**
* @var string
*/
protected $source;
/**
* @var string
*/
protected $mask;
public function __construct($provider, $type, $protocol, $port, $source, $mask)
{
$this->provider = $provider;
$this->type = $type;
$this->protocol = $protocol;
$this->port = $port;
$this->source = $source;
$this->mask = $mask;
public function __construct(
protected string $provider,
protected string $type,
protected string $protocol,
protected string $port,
protected string $source,
protected ?string $mask = null
) {
}
public function file(string $os): string
public function file(): string
{
return File::get(base_path('system/commands/firewall/'.$this->provider.'/add-rule.sh'));
return File::get(resource_path(sprintf("commands/firewall/%s/add-rule.sh", $this->provider)));
}
}

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