add postgresql (#118)

This commit is contained in:
Saeed Vaziry 2024-03-16 01:04:57 +01:00 committed by GitHub
parent a406491160
commit 287c9c2a8a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
25 changed files with 366 additions and 150 deletions

View File

@ -13,4 +13,14 @@ final class Database extends Enum
const MYSQL80 = 'mysql80';
const MARIADB = 'mariadb';
const POSTGRESQL12 = 'postgresql12';
const POSTGRESQL13 = 'postgresql13';
const POSTGRESQL14 = 'postgresql14';
const POSTGRESQL15 = 'postgresql15';
const POSTGRESQL16 = 'postgresql16';
}

View File

@ -2,6 +2,7 @@
namespace App\SSH\Services\Database;
use App\Models\BackupFile;
use App\Models\Server;
use App\Models\Service;
use App\SSH\HasScripts;
@ -15,6 +16,8 @@ abstract class AbstractDatabase implements Database, ServiceInterface
protected Server $server;
abstract protected function getScriptsDir(): string;
public function __construct(Service $service)
{
$this->service = $service;
@ -29,4 +32,115 @@ public function install(): void
$status = $this->server->systemd()->status($this->service->unit);
$this->service->validateInstall($status);
}
public function create(string $name): void
{
$this->server->ssh()->exec(
$this->getScript($this->getScriptsDir().'/create.sh', [
'name' => $name,
]),
'create-database'
);
}
public function delete(string $name): void
{
$this->server->ssh()->exec(
$this->getScript($this->getScriptsDir().'/delete.sh', [
'name' => $name,
]),
'delete-database'
);
}
public function createUser(string $username, string $password, string $host): void
{
$this->server->ssh()->exec(
$this->getScript($this->getScriptsDir().'/create-user.sh', [
'username' => $username,
'password' => $password,
'host' => $host,
]),
'create-user'
);
}
public function deleteUser(string $username, string $host): void
{
$this->server->ssh()->exec(
$this->getScript($this->getScriptsDir().'/delete-user.sh', [
'username' => $username,
'host' => $host,
]),
'delete-user'
);
}
public function link(string $username, string $host, array $databases): void
{
$ssh = $this->server->ssh();
foreach ($databases as $database) {
$ssh->exec(
$this->getScript($this->getScriptsDir().'/link.sh', [
'username' => $username,
'host' => $host,
'database' => $database,
]),
'link-user-to-database'
);
}
}
public function unlink(string $username, string $host): void
{
$this->server->ssh()->exec(
$this->getScript($this->getScriptsDir().'/unlink.sh', [
'username' => $username,
'host' => $host,
]),
'unlink-user-from-databases'
);
}
public function runBackup(BackupFile $backupFile): void
{
// backup
$this->server->ssh()->exec(
$this->getScript($this->getScriptsDir().'/backup.sh', [
'file' => $backupFile->name,
'database' => $backupFile->backup->database->name,
]),
'backup-database'
);
// upload to storage
$upload = $backupFile->backup->storage->provider()->ssh($this->server)->upload(
$backupFile->path(),
$backupFile->storagePath(),
);
// cleanup
$this->server->ssh()->exec('rm '.$backupFile->name.'.zip');
$backupFile->size = $upload['size'];
$backupFile->save();
}
public function restoreBackup(BackupFile $backupFile, string $database): void
{
// download
$backupFile->backup->storage->provider()->ssh($this->server)->download(
$backupFile->storagePath(),
$backupFile->name.'.zip',
);
$this->server->ssh()->exec(
$this->getScript($this->getScriptsDir().'/restore.sh', [
'database' => $database,
'file' => $backupFile->name,
]),
'restore-database'
);
}
}

View File

@ -2,7 +2,10 @@
namespace App\SSH\Services\Database;
class Mariadb extends Mysql
class Mariadb extends AbstractDatabase
{
//
protected function getScriptsDir(): string
{
return 'mysql';
}
}

View File

@ -2,121 +2,10 @@
namespace App\SSH\Services\Database;
use App\Models\BackupFile;
use App\SSH\HasScripts;
class Mysql extends AbstractDatabase
{
use HasScripts;
public function create(string $name): void
protected function getScriptsDir(): string
{
$this->server->ssh()->exec(
$this->getScript('mysql/create.sh', [
'name' => $name,
]),
'create-database'
);
}
public function delete(string $name): void
{
$this->server->ssh()->exec(
$this->getScript('mysql/delete.sh', [
'name' => $name,
]),
'delete-database'
);
}
public function createUser(string $username, string $password, string $host): void
{
$this->server->ssh()->exec(
$this->getScript('mysql/create-user.sh', [
'username' => $username,
'password' => $password,
'host' => $host,
]),
'create-user'
);
}
public function deleteUser(string $username, string $host): void
{
$this->server->ssh()->exec(
$this->getScript('mysql/delete-user.sh', [
'username' => $username,
'host' => $host,
]),
'delete-user'
);
}
public function link(string $username, string $host, array $databases): void
{
$ssh = $this->server->ssh();
foreach ($databases as $database) {
$ssh->exec(
$this->getScript('mysql/link.sh', [
'username' => $username,
'host' => $host,
'database' => $database,
]),
'link-user-to-database'
);
}
}
public function unlink(string $username, string $host): void
{
$this->server->ssh()->exec(
$this->getScript('mysql/unlink.sh', [
'username' => $username,
'host' => $host,
]),
'unlink-user-from-databases'
);
}
public function runBackup(BackupFile $backupFile): void
{
// backup
$this->server->ssh()->exec(
$this->getScript('mysql/backup.sh', [
'file' => $backupFile->name,
'database' => $backupFile->backup->database->name,
]),
'backup-database'
);
// upload to storage
$upload = $backupFile->backup->storage->provider()->ssh($this->server)->upload(
$backupFile->path(),
$backupFile->storagePath(),
);
// cleanup
$this->server->ssh()->exec('rm '.$backupFile->name.'.zip');
$backupFile->size = $upload['size'];
$backupFile->save();
}
public function restoreBackup(BackupFile $backupFile, string $database): void
{
// download
$backupFile->backup->storage->provider()->ssh($this->server)->download(
$backupFile->storagePath(),
$backupFile->name.'.zip',
);
$this->server->ssh()->exec(
$this->getScript('mysql/restore.sh', [
'database' => $database,
'file' => $backupFile->name,
]),
'restore-database'
);
return 'mysql';
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace App\SSH\Services\Database;
class Postgresql extends AbstractDatabase
{
protected function getScriptsDir(): string
{
return 'postgresql';
}
}

View File

@ -0,0 +1,19 @@
if ! sudo -u postgres pg_dump -d __database__ -f /var/lib/postgresql/__file__.sql; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo mv /var/lib/postgresql/__file__.sql /home/vito/__file__.sql; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo chown vito:vito /home/vito/__file__.sql; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! DEBIAN_FRONTEND=noninteractive zip __file__.zip __file__.sql; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! rm __file__.sql; then
echo 'VITO_SSH_ERROR' && exit 1
fi

View File

@ -0,0 +1,5 @@
if ! sudo -u postgres psql -c "CREATE ROLE __username__ WITH LOGIN PASSWORD '__password__';"; then
echo 'VITO_SSH_ERROR' && exit 1
fi
echo "User __username__ created"

View File

@ -0,0 +1,6 @@
if ! sudo -u postgres psql -c "CREATE DATABASE __name__"; then
echo 'VITO_SSH_ERROR' && exit 1
fi
echo "Database __name__ created"

View File

@ -0,0 +1,5 @@
if ! sudo -u postgres psql -c "DROP USER __username__"; then
echo 'VITO_SSH_ERROR' && exit 1
fi
echo "User __username__ deleted"

View File

@ -0,0 +1,5 @@
if ! sudo -u postgres psql -c "DROP DATABASE __name__"; then
echo 'VITO_SSH_ERROR' && exit 1
fi
echo "Database __name__ deleted"

View File

@ -0,0 +1,11 @@
sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo DEBIAN_FRONTEND=noninteractive apt-get update -y
sudo DEBIAN_FRONTEND=noninteractive apt-get install postgresql-12 -y
systemctl status postgresql
sudo -u postgres psql -c "SELECT version();"

View File

@ -0,0 +1,11 @@
sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo DEBIAN_FRONTEND=noninteractive apt-get update -y
sudo DEBIAN_FRONTEND=noninteractive apt-get install postgresql-13 -y
systemctl status postgresql
sudo -u postgres psql -c "SELECT version();"

View File

@ -0,0 +1,11 @@
sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo DEBIAN_FRONTEND=noninteractive apt-get update -y
sudo DEBIAN_FRONTEND=noninteractive apt-get install postgresql-14 -y
systemctl status postgresql
sudo -u postgres psql -c "SELECT version();"

View File

@ -0,0 +1,11 @@
sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo DEBIAN_FRONTEND=noninteractive apt-get update -y
sudo DEBIAN_FRONTEND=noninteractive apt-get install postgresql-15 -y
systemctl status postgresql
sudo -u postgres psql -c "SELECT version();"

View File

@ -0,0 +1,11 @@
sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo DEBIAN_FRONTEND=noninteractive apt-get update -y
sudo DEBIAN_FRONTEND=noninteractive apt-get install postgresql-16 -y
systemctl status postgresql
sudo -u postgres psql -c "SELECT version();"

View File

@ -0,0 +1,5 @@
if ! sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE __database__ TO __username__;"; then
echo 'VITO_SSH_ERROR' && exit 1
fi
echo "Linking to __database__ finished"

View File

@ -0,0 +1,11 @@
if ! DEBIAN_FRONTEND=noninteractive unzip __file__.zip; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo -u postgres psql -d __database__ -f __file__.sql; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! rm __file__.sql __file__.zip; then
echo 'VITO_SSH_ERROR' && exit 1
fi

View File

@ -0,0 +1,10 @@
USER_TO_REVOKE='__username__'
DATABASES=$(sudo -u postgres psql -t -c "SELECT datname FROM pg_database WHERE datistemplate = false;")
for DB in $DATABASES; do
echo "Revoking privileges in database: $DB"
sudo -u postgres psql -d "$DB" -c "REVOKE ALL PRIVILEGES ON DATABASE \"$DB\" FROM $USER_TO_REVOKE;"
done
echo "Privileges revoked from $USER_TO_REVOKE"

View File

@ -4,6 +4,7 @@
use App\Exceptions\SSHCommandError;
use App\SSH\HasScripts;
use Illuminate\Support\Facades\Log;
class Dropbox extends AbstractStorage
{
@ -23,7 +24,8 @@ public function upload(string $src, string $dest): array
$data = json_decode($upload, true);
if (isset($data['error'])) {
throw new SSHCommandError('Failed to upload to Dropbox '.$data['error']);
Log::error('Failed to upload to Dropbox', $data);
throw new SSHCommandError('Failed to upload to Dropbox');
}
return [

View File

@ -18,6 +18,7 @@
use App\SourceControlProviders\Gitlab;
use App\SSH\Services\Database\Mariadb;
use App\SSH\Services\Database\Mysql;
use App\SSH\Services\Database\Postgresql;
use App\SSH\Services\Firewall\Ufw;
use App\SSH\Services\PHP\PHP;
use App\SSH\Services\ProcessManager\Supervisor;
@ -57,16 +58,42 @@
'8.1',
'8.2',
],
'databases' => ['none', 'mysql57', 'mysql80', 'mariadb'],
'databases' => [
'none',
'mysql57',
'mysql80',
'mariadb',
'postgresql12',
'postgresql13',
'postgresql14',
'postgresql15',
'postgresql16',
],
'databases_name' => [
'mysql57' => 'mysql',
'mysql80' => 'mysql',
'mariadb' => 'mariadb',
'postgresql12' => 'postgresql',
'postgresql13' => 'postgresql',
'postgresql14' => 'postgresql',
'postgresql15' => 'postgresql',
'postgresql16' => 'postgresql',
],
'databases_version' => [
'mysql57' => '5.7',
'mysql80' => '8.0',
'mariadb' => '10.3',
'postgresql12' => '12',
'postgresql13' => '13',
'postgresql14' => '14',
'postgresql15' => '15',
'postgresql16' => '16',
],
'database_features' => [
'remote' => [
'mysql',
'mariadb',
],
],
/*
@ -126,6 +153,7 @@
'nginx' => Nginx::class,
'mysql' => Mysql::class,
'mariadb' => Mariadb::class,
'postgresql' => Postgresql::class,
'redis' => Redis::class,
'php' => PHP::class,
'ufw' => Ufw::class,
@ -168,6 +196,29 @@
'10.3' => 'mariadb',
],
],
'postgresql' => [
'ubuntu_18' => [
'12' => 'postgresql',
'13' => 'postgresql',
'14' => 'postgresql',
'15' => 'postgresql',
'16' => 'postgresql',
],
'ubuntu_20' => [
'12' => 'postgresql',
'13' => 'postgresql',
'14' => 'postgresql',
'15' => 'postgresql',
'16' => 'postgresql',
],
'ubuntu_22' => [
'12' => 'postgresql',
'13' => 'postgresql',
'14' => 'postgresql',
'15' => 'postgresql',
'16' => 'postgresql',
],
],
'php' => [
'ubuntu_18' => [
'5.6' => 'php5.6-fpm',

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 13 KiB

View File

@ -65,20 +65,22 @@ class="mt-1 w-full"
@enderror
</div>
<div class="mt-6">
<label for="db-remote" class="inline-flex items-center">
<input
id="db-remote"
type="checkbox"
x-model="remote"
name="remote"
class="rounded border-gray-300 text-indigo-600 shadow-sm focus:ring-indigo-500 dark:border-gray-700 dark:bg-gray-900 dark:focus:ring-indigo-600 dark:focus:ring-offset-gray-800"
/>
<span class="ml-2 text-sm text-gray-600 dark:text-gray-400">
{{ __("Enable remote access") }}
</span>
</label>
</div>
@if (in_array($server->database()?->name, config("core.database_features.remote")))
<div class="mt-6">
<label for="db-remote" class="inline-flex items-center">
<input
id="db-remote"
type="checkbox"
x-model="remote"
name="remote"
class="rounded border-gray-300 text-indigo-600 shadow-sm focus:ring-indigo-500 dark:border-gray-700 dark:bg-gray-900 dark:focus:ring-indigo-600 dark:focus:ring-offset-gray-800"
/>
<span class="ml-2 text-sm text-gray-600 dark:text-gray-400">
{{ __("Enable remote access") }}
</span>
</label>
</div>
@endif
<div x-show="remote">
<div class="mt-6">

View File

@ -41,20 +41,22 @@ class="mt-1 w-full"
@enderror
</div>
<div class="mt-6">
<label for="user-remote" class="inline-flex items-center">
<input
id="user-remote"
type="checkbox"
x-model="remote"
name="remote"
class="rounded border-gray-300 text-indigo-600 shadow-sm focus:ring-indigo-500 dark:border-gray-700 dark:bg-gray-900 dark:focus:ring-indigo-600 dark:focus:ring-offset-gray-800"
/>
<span class="ml-2 text-sm text-gray-600 dark:text-gray-400">
{{ __("Enable remote access") }}
</span>
</label>
</div>
@if (in_array($server->database()?->name, config("core.database_features.remote")))
<div class="mt-6">
<label for="user-remote" class="inline-flex items-center">
<input
id="user-remote"
type="checkbox"
x-model="remote"
name="remote"
class="rounded border-gray-300 text-indigo-600 shadow-sm focus:ring-indigo-500 dark:border-gray-700 dark:bg-gray-900 dark:focus:ring-indigo-600 dark:focus:ring-offset-gray-800"
/>
<span class="ml-2 text-sm text-gray-600 dark:text-gray-400">
{{ __("Enable remote access") }}
</span>
</label>
</div>
@endif
<div x-show="remote">
<div class="mt-6">

View File

@ -1,9 +1,10 @@
@php
use App\Enums\Database;
use App\Enums\Webserver;
use App\Enums\ServerType;
@endphp
<x-container x-data="">
<x-container x-data="{type: '{{ old('type', ServerType::REGULAR) }}'}">
<x-card>
<x-slot name="title">{{ __("Create new Server") }}</x-slot>
<x-slot name="description">
@ -193,7 +194,7 @@ class="mt-1 block w-full"
<div>
<x-input-label for="type" value="Server Type" />
<x-select-input id="type" name="type" class="mt-1 w-full">
<x-select-input x-model="type" id="type" name="type" class="mt-1 w-full">
@foreach (config("core.server_types") as $serverType)
<option value="{{ $serverType }}" @if($serverType == old('type')) selected @endif>
{{ $serverType }}
@ -206,7 +207,7 @@ class="mt-1 block w-full"
</div>
<div class="grid grid-cols-1 gap-3 lg:grid-cols-3">
<div>
<div x-show="['regular'].includes(type)">
<x-input-label for="webserver" value="Webserver" />
<x-select-input id="webserver" name="webserver" class="mt-1 w-full">
@foreach (config("core.webservers") as $ws)
@ -219,7 +220,7 @@ class="mt-1 block w-full"
<x-input-error class="mt-2" :messages="$message" />
@enderror
</div>
<div>
<div x-show="['regular', 'database'].includes(type)">
<x-input-label for="database" value="Database" />
<x-select-input id="database" name="database" class="mt-1 w-full">
@foreach (config("core.databases") as $db)
@ -232,7 +233,7 @@ class="mt-1 block w-full"
<x-input-error class="mt-2" :messages="$message" />
@enderror
</div>
<div>
<div x-show="['regular'].includes(type)">
<x-input-label for="php" value="PHP" />
<x-select-input id="php" name="php" class="mt-1 w-full">
@foreach (config("core.php_versions") as $p)