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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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