mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-08 09:22:34 +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:
32
app/SSH/Services/Database/AbstractDatabase.php
Executable file
32
app/SSH/Services/Database/AbstractDatabase.php
Executable file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\SSH\Services\Database;
|
||||
|
||||
use App\Models\Server;
|
||||
use App\Models\Service;
|
||||
use App\SSH\HasScripts;
|
||||
use App\SSH\Services\ServiceInterface;
|
||||
|
||||
abstract class AbstractDatabase implements Database, ServiceInterface
|
||||
{
|
||||
use HasScripts;
|
||||
|
||||
protected Service $service;
|
||||
|
||||
protected Server $server;
|
||||
|
||||
public function __construct(Service $service)
|
||||
{
|
||||
$this->service = $service;
|
||||
$this->server = $service->server;
|
||||
}
|
||||
|
||||
public function install(): void
|
||||
{
|
||||
$version = $this->service->version;
|
||||
$command = $this->getScript($this->service->name.'/install-'.$version.'.sh');
|
||||
$this->server->ssh()->exec($command, 'install-'.$this->service->name.'-'.$version);
|
||||
$status = $this->server->systemd()->status($this->service->unit);
|
||||
$this->service->validateInstall($status);
|
||||
}
|
||||
}
|
24
app/SSH/Services/Database/Database.php
Executable file
24
app/SSH/Services/Database/Database.php
Executable file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\SSH\Services\Database;
|
||||
|
||||
use App\Models\BackupFile;
|
||||
|
||||
interface Database
|
||||
{
|
||||
public function create(string $name): void;
|
||||
|
||||
public function delete(string $name): void;
|
||||
|
||||
public function createUser(string $username, string $password, string $host): void;
|
||||
|
||||
public function deleteUser(string $username, string $host): void;
|
||||
|
||||
public function link(string $username, string $host, array $databases): void;
|
||||
|
||||
public function unlink(string $username, string $host): void;
|
||||
|
||||
public function runBackup(BackupFile $backupFile): void;
|
||||
|
||||
public function restoreBackup(BackupFile $backupFile, string $database): void;
|
||||
}
|
8
app/SSH/Services/Database/Mariadb.php
Normal file
8
app/SSH/Services/Database/Mariadb.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\SSH\Services\Database;
|
||||
|
||||
class Mariadb extends Mysql
|
||||
{
|
||||
//
|
||||
}
|
122
app/SSH/Services/Database/Mysql.php
Executable file
122
app/SSH/Services/Database/Mysql.php
Executable file
@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
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
|
||||
{
|
||||
$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'
|
||||
);
|
||||
}
|
||||
}
|
12
app/SSH/Services/Database/scripts/mariadb/install-10.sh
Executable file
12
app/SSH/Services/Database/scripts/mariadb/install-10.sh
Executable file
@ -0,0 +1,12 @@
|
||||
wget https://downloads.mariadb.com/MariaDB/mariadb_repo_setup
|
||||
|
||||
chmod +x mariadb_repo_setup
|
||||
|
||||
sudo DEBIAN_FRONTEND=noninteractive ./mariadb_repo_setup \
|
||||
--mariadb-server-version="mariadb-10.3"
|
||||
|
||||
sudo DEBIAN_FRONTEND=noninteractive apt-get update
|
||||
|
||||
sudo DEBIAN_FRONTEND=noninteractive apt-get install mariadb-server mariadb-backup -y
|
||||
|
||||
sudo service mysql start
|
11
app/SSH/Services/Database/scripts/mysql/backup.sh
Normal file
11
app/SSH/Services/Database/scripts/mysql/backup.sh
Normal file
@ -0,0 +1,11 @@
|
||||
if ! sudo DEBIAN_FRONTEND=noninteractive mysqldump -u root __database__ > __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
|
9
app/SSH/Services/Database/scripts/mysql/create-user.sh
Executable file
9
app/SSH/Services/Database/scripts/mysql/create-user.sh
Executable file
@ -0,0 +1,9 @@
|
||||
if ! sudo mysql -e "CREATE USER IF NOT EXISTS '__username__'@'__host__' IDENTIFIED BY '__password__'"; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
||||
|
||||
if ! sudo mysql -e "FLUSH PRIVILEGES"; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
||||
|
||||
echo "Command executed"
|
5
app/SSH/Services/Database/scripts/mysql/create.sh
Executable file
5
app/SSH/Services/Database/scripts/mysql/create.sh
Executable file
@ -0,0 +1,5 @@
|
||||
if ! sudo mysql -e "CREATE DATABASE IF NOT EXISTS __name__ CHARACTER SET utf8 COLLATE utf8_general_ci"; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
||||
|
||||
echo "Command executed"
|
9
app/SSH/Services/Database/scripts/mysql/delete-user.sh
Executable file
9
app/SSH/Services/Database/scripts/mysql/delete-user.sh
Executable file
@ -0,0 +1,9 @@
|
||||
if ! sudo mysql -e "DROP USER IF EXISTS '__username__'@'__host__'"; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
||||
|
||||
if ! sudo mysql -e "FLUSH PRIVILEGES"; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
||||
|
||||
echo "Command executed"
|
5
app/SSH/Services/Database/scripts/mysql/delete.sh
Executable file
5
app/SSH/Services/Database/scripts/mysql/delete.sh
Executable file
@ -0,0 +1,5 @@
|
||||
if ! sudo mysql -e "DROP DATABASE IF EXISTS __name__"; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
||||
|
||||
echo "Command executed"
|
13
app/SSH/Services/Database/scripts/mysql/install-5.7.sh
Executable file
13
app/SSH/Services/Database/scripts/mysql/install-5.7.sh
Executable file
@ -0,0 +1,13 @@
|
||||
sudo DEBIAN_FRONTEND=noninteractive apt-get install mysql-server -y
|
||||
|
||||
sudo service mysql enable
|
||||
|
||||
sudo service mysql start
|
||||
|
||||
if ! sudo mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH auth_socket;"; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
||||
|
||||
if ! sudo mysql -e "FLUSH PRIVILEGES"; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
19
app/SSH/Services/Database/scripts/mysql/install-8.0.sh
Executable file
19
app/SSH/Services/Database/scripts/mysql/install-8.0.sh
Executable file
@ -0,0 +1,19 @@
|
||||
wget -c https://dev.mysql.com/get/mysql-apt-config_0.8.22-1_all.deb
|
||||
|
||||
sudo DEBIAN_FRONTEND=noninteractive dpkg -i mysql-apt-config_0.8.22-1_all.deb
|
||||
|
||||
sudo DEBIAN_FRONTEND=noninteractive apt-get update
|
||||
|
||||
sudo DEBIAN_FRONTEND=noninteractive apt-get install mysql-server -y
|
||||
|
||||
sudo service mysql enable
|
||||
|
||||
sudo service mysql start
|
||||
|
||||
if ! sudo mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH auth_socket;"; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
||||
|
||||
if ! sudo mysql -e "FLUSH PRIVILEGES"; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
9
app/SSH/Services/Database/scripts/mysql/link.sh
Executable file
9
app/SSH/Services/Database/scripts/mysql/link.sh
Executable file
@ -0,0 +1,9 @@
|
||||
if ! sudo mysql -e "GRANT ALL PRIVILEGES ON __database__.* TO '__username__'@'__host__'"; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
||||
|
||||
if ! sudo mysql -e "FLUSH PRIVILEGES"; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
||||
|
||||
echo "Linking to __database__ finished"
|
11
app/SSH/Services/Database/scripts/mysql/restore.sh
Normal file
11
app/SSH/Services/Database/scripts/mysql/restore.sh
Normal file
@ -0,0 +1,11 @@
|
||||
if ! DEBIAN_FRONTEND=noninteractive unzip __file__.zip; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
||||
|
||||
if ! sudo DEBIAN_FRONTEND=noninteractive mysql -u root __database__ < __file__.sql; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
||||
|
||||
if ! rm __file__.sql __file__.zip; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
5
app/SSH/Services/Database/scripts/mysql/unlink.sh
Executable file
5
app/SSH/Services/Database/scripts/mysql/unlink.sh
Executable file
@ -0,0 +1,5 @@
|
||||
if ! sudo mysql -e "REVOKE ALL PRIVILEGES, GRANT OPTION FROM '__username__'@'__host__'"; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
||||
|
||||
echo "Command executed"
|
Reference in New Issue
Block a user