use blade as conmmands template (#444)

* use blade as conmmands template

* fix lint

* fix ssl
This commit is contained in:
Saeed Vaziry
2025-01-27 21:27:58 +01:00
committed by GitHub
parent a73476c1dd
commit cdbde063f0
208 changed files with 1080 additions and 1012 deletions

View File

@ -3,16 +3,18 @@
namespace App\SSH\Services\Database;
use App\Enums\BackupStatus;
use App\Exceptions\ServiceInstallationFailed;
use App\Exceptions\SSHError;
use App\Models\BackupFile;
use App\SSH\HasScripts;
use App\SSH\Services\AbstractService;
use Closure;
abstract class AbstractDatabase extends AbstractService implements Database
{
use HasScripts;
abstract protected function getScriptsDir(): string;
protected function getScriptView(string $script): string
{
return 'ssh.services.database.'.$this->service->name.'.'.$script;
}
public function creationRules(array $input): array
{
@ -29,10 +31,14 @@ function (string $attribute, mixed $value, Closure $fail) {
];
}
/**
* @throws ServiceInstallationFailed
* @throws SSHError
*/
public function install(): void
{
$version = $this->service->version;
$command = $this->getScript($this->service->name.'/install-'.$version.'.sh');
$version = str_replace('.', '', $this->service->version);
$command = view($this->getScriptView('install-'.$version));
$this->service->server->ssh()->exec($command, 'install-'.$this->service->name.'-'.$version);
$status = $this->service->server->systemd()->status($this->service->unit);
$this->service->validateInstall($status);
@ -63,38 +69,50 @@ function (string $attribute, mixed $value, Closure $fail) {
];
}
/**
* @throws SSHError
*/
public function uninstall(): void
{
$version = $this->service->version;
$command = $this->getScript($this->service->name.'/uninstall.sh');
$command = view($this->getScriptView('uninstall'));
$this->service->server->ssh()->exec($command, 'uninstall-'.$this->service->name.'-'.$version);
$this->service->server->os()->cleanup();
}
/**
* @throws SSHError
*/
public function create(string $name): void
{
$this->service->server->ssh()->exec(
$this->getScript($this->getScriptsDir().'/create.sh', [
view($this->getScriptView('create'), [
'name' => $name,
]),
'create-database'
);
}
/**
* @throws SSHError
*/
public function delete(string $name): void
{
$this->service->server->ssh()->exec(
$this->getScript($this->getScriptsDir().'/delete.sh', [
view($this->getScriptView('delete'), [
'name' => $name,
]),
'delete-database'
);
}
/**
* @throws SSHError
*/
public function createUser(string $username, string $password, string $host): void
{
$this->service->server->ssh()->exec(
$this->getScript($this->getScriptsDir().'/create-user.sh', [
view($this->getScriptView('create-user'), [
'username' => $username,
'password' => $password,
'host' => $host,
@ -103,10 +121,13 @@ public function createUser(string $username, string $password, string $host): vo
);
}
/**
* @throws SSHError
*/
public function deleteUser(string $username, string $host): void
{
$this->service->server->ssh()->exec(
$this->getScript($this->getScriptsDir().'/delete-user.sh', [
view($this->getScriptView('delete-user'), [
'username' => $username,
'host' => $host,
]),
@ -114,6 +135,9 @@ public function deleteUser(string $username, string $host): void
);
}
/**
* @throws SSHError
*/
public function link(string $username, string $host, array $databases): void
{
$ssh = $this->service->server->ssh();
@ -121,7 +145,7 @@ public function link(string $username, string $host, array $databases): void
foreach ($databases as $database) {
$ssh->exec(
$this->getScript($this->getScriptsDir().'/link.sh', [
view($this->getScriptView('link'), [
'username' => $username,
'host' => $host,
'database' => $database,
@ -132,12 +156,15 @@ public function link(string $username, string $host, array $databases): void
}
}
/**
* @throws SSHError
*/
public function unlink(string $username, string $host): void
{
$version = $this->service->version;
$this->service->server->ssh()->exec(
$this->getScript($this->getScriptsDir().'/unlink.sh', [
view($this->getScriptView('unlink'), [
'username' => $username,
'host' => $host,
'version' => $version,
@ -146,11 +173,14 @@ public function unlink(string $username, string $host): void
);
}
/**
* @throws SSHError
*/
public function runBackup(BackupFile $backupFile): void
{
// backup
$this->service->server->ssh()->exec(
$this->getScript($this->getScriptsDir().'/backup.sh', [
view($this->getScriptView('backup'), [
'file' => $backupFile->name,
'database' => $backupFile->backup->database->name,
]),
@ -170,6 +200,9 @@ public function runBackup(BackupFile $backupFile): void
$backupFile->save();
}
/**
* @throws SSHError
*/
public function restoreBackup(BackupFile $backupFile, string $database): void
{
// download
@ -179,7 +212,7 @@ public function restoreBackup(BackupFile $backupFile, string $database): void
);
$this->service->server->ssh()->exec(
$this->getScript($this->getScriptsDir().'/restore.sh', [
view($this->getScriptView('restore'), [
'database' => $database,
'file' => rtrim($backupFile->tempPath(), '.zip'),
]),

View File

@ -4,8 +4,5 @@
class Mariadb extends AbstractDatabase
{
protected function getScriptsDir(): string
{
return 'mysql';
}
//
}

View File

@ -4,8 +4,5 @@
class Mysql extends AbstractDatabase
{
protected function getScriptsDir(): string
{
return 'mysql';
}
//
}

View File

@ -2,26 +2,7 @@
namespace App\SSH\Services\Database;
use App\Exceptions\SSHError;
class Postgresql extends AbstractDatabase
{
protected function getScriptsDir(): string
{
return 'postgresql';
}
/**
* @throws SSHError
*/
public function create(string $name): void
{
$this->service->server->ssh()->exec(
$this->getScript($this->getScriptsDir().'/create.sh', [
'name' => $name,
'ssh_user' => $this->service->server->ssh_user,
]),
'create-database'
);
}
//
}

View File

@ -1,14 +0,0 @@
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.11"
sudo DEBIAN_FRONTEND=noninteractive apt-get update
sudo DEBIAN_FRONTEND=noninteractive apt-get install mariadb-server mariadb-backup -y
sudo systemctl unmask mysql.service
sudo service mysql start

View File

@ -1,14 +0,0 @@
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 systemctl unmask mysql.service
sudo service mysql start

View File

@ -1,14 +0,0 @@
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.4"
sudo DEBIAN_FRONTEND=noninteractive apt-get update
sudo DEBIAN_FRONTEND=noninteractive apt-get install mariadb-server mariadb-backup -y
sudo systemctl unmask mysql.service
sudo service mysql start

View File

@ -1,14 +0,0 @@
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.6"
sudo DEBIAN_FRONTEND=noninteractive apt-get update
sudo DEBIAN_FRONTEND=noninteractive apt-get install mariadb-server mariadb-backup -y
sudo systemctl unmask mysql.service
sudo service mysql start

View File

@ -1,14 +0,0 @@
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-11.4"
sudo DEBIAN_FRONTEND=noninteractive apt-get update
sudo DEBIAN_FRONTEND=noninteractive apt-get install mariadb-server mariadb-backup -y
sudo systemctl unmask mysql.service
sudo service mysql start

View File

@ -1,9 +0,0 @@
sudo service mysql stop
sudo DEBIAN_FRONTEND=noninteractive apt-get remove mariadb-server mariadb-backup -y
sudo rm -rf /etc/mysql
sudo rm -rf /var/lib/mysql
sudo rm -rf /var/log/mysql
sudo rm -rf /var/run/mysqld
sudo rm -rf /var/run/mysqld/mysqld.sock

View File

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

View File

@ -1,9 +0,0 @@
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"

View File

@ -1,5 +0,0 @@
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"

View File

@ -1,9 +0,0 @@
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"

View File

@ -1,5 +0,0 @@
if ! sudo mysql -e "DROP DATABASE IF EXISTS __name__"; then
echo 'VITO_SSH_ERROR' && exit 1
fi
echo "Command executed"

View File

@ -1,15 +0,0 @@
sudo DEBIAN_FRONTEND=noninteractive apt-get install mysql-server -y
sudo systemctl unmask mysql.service
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

View File

@ -1,17 +0,0 @@
sudo DEBIAN_FRONTEND=noninteractive apt-get update
sudo DEBIAN_FRONTEND=noninteractive apt-get install mysql-server -y
sudo systemctl unmask mysql.service
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

View File

@ -1,27 +0,0 @@
#!/bin/bash
sudo DEBIAN_FRONTEND=noninteractive apt-get update
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y lsb-release
DEBIAN_FRONTEND=noninteractive wget https://dev.mysql.com/get/mysql-apt-config_0.8.32-1_all.deb
sudo DEBIAN_FRONTEND=noninteractive dpkg -i mysql-apt-config_0.8.32-1_all.deb
sudo DEBIAN_FRONTEND=noninteractive apt-get update
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server
sudo systemctl unmask mysql.service
sudo systemctl enable mysql
sudo systemctl start mysql
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

View File

@ -1,9 +0,0 @@
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"

View File

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

View File

@ -1,9 +0,0 @@
sudo service mysql stop
sudo DEBIAN_FRONTEND=noninteractive apt-get remove mysql-server -y
sudo rm -rf /etc/mysql
sudo rm -rf /var/lib/mysql
sudo rm -rf /var/log/mysql
sudo rm -rf /var/run/mysqld
sudo rm -rf /var/run/mysqld/mysqld.sock

View File

@ -1,5 +0,0 @@
if ! sudo mysql -e "REVOKE ALL PRIVILEGES, GRANT OPTION FROM '__username__'@'__host__'"; then
echo 'VITO_SSH_ERROR' && exit 1
fi
echo "Command executed"

View File

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

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

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

View File

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

View File

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

View File

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

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

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

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

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

@ -1,16 +0,0 @@
USER_TO_LINK='__username__'
DB_NAME='__database__'
DB_VERSION='__version__'
if ! sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE \"$DB_NAME\" TO $USER_TO_LINK;"; then
echo 'VITO_SSH_ERROR' && exit 1
fi
# Check if PostgreSQL version is 15 or greater
if [ "$DB_VERSION" -ge 15 ]; then
if ! sudo -u postgres psql -d "$DB_NAME" -c "GRANT USAGE, CREATE ON SCHEMA public TO $USER_TO_LINK;"; then
echo 'VITO_SSH_ERROR' && exit 1
fi
fi
echo "Linking to $DB_NAME finished"

View File

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

@ -1,11 +0,0 @@
sudo service postgresql stop
sudo DEBIAN_FRONTEND=noninteractive apt-get remove postgresql-* -y
sudo rm -rf /etc/postgresql
sudo rm -rf /var/lib/postgresql
sudo rm -rf /var/log/postgresql
sudo rm -rf /var/run/postgresql
sudo rm -rf /var/run/postgresql/postmaster.pid
sudo rm -rf /var/run/postgresql/.s.PGSQL.5432
sudo rm -rf /var/run/postgresql/.s.PGSQL.5432.lock

View File

@ -1,16 +0,0 @@
USER_TO_REVOKE='__username__'
DB_VERSION='__version__'
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;"
# Check if PostgreSQL version is 15 or greater
if [ "$DB_VERSION" -ge 15 ]; then
sudo -u postgres psql -d "$DB" -c "REVOKE USAGE, CREATE ON SCHEMA public FROM $USER_TO_REVOKE;"
fi
done
echo "Privileges revoked from $USER_TO_REVOKE"

View File

@ -2,16 +2,17 @@
namespace App\SSH\Services\Firewall;
use App\SSH\HasScripts;
use App\Exceptions\SSHError;
class Ufw extends AbstractFirewall
{
use HasScripts;
/**
* @throws SSHError
*/
public function install(): void
{
$this->service->server->ssh()->exec(
$this->getScript('ufw/install-ufw.sh'),
view('ssh.services.firewall.ufw.install-ufw'),
'install-ufw'
);
$this->service->server->os()->cleanup();
@ -22,10 +23,13 @@ public function uninstall(): void
//
}
/**
* @throws SSHError
*/
public function addRule(string $type, string $protocol, int $port, string $source, ?string $mask): void
{
$this->service->server->ssh()->exec(
$this->getScript('ufw/add-rule.sh', [
view('ssh.services.firewall.ufw.add-rule', [
'type' => $type,
'protocol' => $protocol,
'port' => $port,
@ -36,10 +40,13 @@ public function addRule(string $type, string $protocol, int $port, string $sourc
);
}
/**
* @throws SSHError
*/
public function removeRule(string $type, string $protocol, int $port, string $source, ?string $mask): void
{
$this->service->server->ssh()->exec(
$this->getScript('ufw/remove-rule.sh', [
view('ssh.services.firewall.ufw.remove-rule', [
'type' => $type,
'protocol' => $protocol,
'port' => $port,

View File

@ -1,11 +0,0 @@
if ! sudo ufw __type__ from __source____mask__ to any proto __protocol__ port __port__; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo ufw reload; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo service ufw restart; then
echo 'VITO_SSH_ERROR' && exit 1
fi

View File

@ -1,27 +0,0 @@
if ! sudo ufw default deny incoming; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo ufw default allow outgoing; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo ufw allow from 0.0.0.0/0 to any proto tcp port 22; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo ufw allow from 0.0.0.0/0 to any proto tcp port 80; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo ufw allow from 0.0.0.0/0 to any proto tcp port 443; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo ufw --force enable; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo ufw reload; then
echo 'VITO_SSH_ERROR' && exit 1
fi

View File

@ -1,11 +0,0 @@
if ! sudo ufw delete __type__ from __source____mask__ to any proto __protocol__ port __port__; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo ufw reload; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo service ufw restart; then
echo 'VITO_SSH_ERROR' && exit 1
fi

View File

@ -2,8 +2,9 @@
namespace App\SSH\Services\Monitoring\VitoAgent;
use App\Exceptions\ServiceInstallationFailed;
use App\Exceptions\SSHError;
use App\Models\Metric;
use App\SSH\HasScripts;
use App\SSH\Services\AbstractService;
use Closure;
use Illuminate\Support\Facades\Http;
@ -12,8 +13,6 @@
class VitoAgent extends AbstractService
{
use HasScripts;
const TAGS_URL = 'https://api.github.com/repos/vitodeploy/agent/tags';
const DOWNLOAD_URL = 'https://github.com/vitodeploy/agent/releases/download/%s';
@ -54,11 +53,15 @@ public function data(): array
];
}
/**
* @throws SSHError
* @throws ServiceInstallationFailed
*/
public function install(): void
{
$tags = Http::get(self::TAGS_URL)->json();
if (empty($tags)) {
throw new \Exception('Failed to fetch tags');
throw new ServiceInstallationFailed('Failed to fetch tags');
}
$this->service->version = $tags[0]['name'];
$this->service->save();
@ -71,10 +74,10 @@ public function install(): void
$this->service->refresh();
$this->service->server->ssh()->exec(
$this->getScript('install.sh', [
'download_url' => $downloadUrl,
'config_url' => $this->data()['url'],
'config_secret' => $this->data()['secret'],
view('ssh.services.monitoring.vito-agent.install', [
'downloadUrl' => $downloadUrl,
'configUrl' => $this->data()['url'],
'configSecret' => $this->data()['secret'],
]),
'install-vito-agent'
);
@ -82,12 +85,15 @@ public function install(): void
$this->service->validateInstall($status);
}
/**
* @throws SSHError
*/
public function uninstall(): void
{
$this->service->server->ssh()->exec(
$this->getScript('uninstall.sh'),
view('ssh.services.monitoring.vito-agent.uninstall'),
'uninstall-vito-agent'
);
Metric::where('server_id', $this->service->server_id)->delete();
Metric::query()->where('server_id', $this->service->server_id)->delete();
}
}

View File

@ -1,53 +0,0 @@
arch=$(uname -m)
if [ "$arch" == "x86_64" ]; then
executable="vitoagent-linux-amd64"
elif [ "$arch" == "i686" ]; then
executable="vitoagent-linux-amd"
elif [ "$arch" == "armv7l" ]; then
executable="vitoagent-linux-arm"
elif [ "$arch" == "aarch64" ]; then
executable="vitoagent-linux-arm64"
else
executable="vitoagent-linux-amd64"
fi
wget __download_url__/$executable
chmod +x ./$executable
sudo mv ./$executable /usr/local/bin/vito-agent
# create service
export VITO_AGENT_SERVICE="
[Unit]
Description=Vito Agent
After=network.target
[Service]
Type=simple
User=root
ExecStart=/usr/local/bin/vito-agent
Restart=on-failure
[Install]
WantedBy=multi-user.target
"
echo "${VITO_AGENT_SERVICE}" | sudo tee /etc/systemd/system/vito-agent.service
sudo mkdir -p /etc/vito-agent
export VITO_AGENT_CONFIG="
{
\"url\": \"__config_url__\",
\"secret\": \"__config_secret__\"
}
"
echo "${VITO_AGENT_CONFIG}" | sudo tee /etc/vito-agent/config.json
sudo systemctl daemon-reload
sudo systemctl enable vito-agent
sudo systemctl start vito-agent
echo "Vito Agent installed successfully"

View File

@ -1,13 +0,0 @@
sudo service vito-agent stop
sudo systemctl disable vito-agent
sudo rm -f /usr/local/bin/vito-agent
sudo rm -f /etc/systemd/system/vito-agent.service
sudo rm -rf /etc/vito-agent
sudo systemctl daemon-reload
echo "Vito Agent uninstalled successfully"

View File

@ -2,15 +2,13 @@
namespace App\SSH\Services\NodeJS;
use App\SSH\HasScripts;
use App\Exceptions\SSHError;
use App\SSH\Services\AbstractService;
use Closure;
use Illuminate\Validation\Rule;
class NodeJS extends AbstractService
{
use HasScripts;
public function creationRules(array $input): array
{
return [
@ -41,34 +39,43 @@ function (string $attribute, mixed $value, Closure $fail) {
];
}
/**
* @throws SSHError
*/
public function install(): void
{
$server = $this->service->server;
$server->ssh()->exec(
$this->getScript('install-nodejs.sh', [
view('ssh.services.nodejs.install-nodejs', [
'version' => $this->service->version,
'user' => $server->getSshUser(),
]),
'install-nodejs-'.$this->service->version
);
$this->service->server->os()->cleanup();
}
/**
* @throws SSHError
*/
public function uninstall(): void
{
$this->service->server->ssh()->exec(
$this->getScript('uninstall-nodejs.sh', [
view('ssh.services.nodejs.uninstall-nodejs', [
'version' => $this->service->version,
'default' => $this->service->is_default,
]),
'uninstall-nodejs-'.$this->service->version
);
$this->service->server->os()->cleanup();
}
/**
* @throws SSHError
*/
public function setDefaultCli(): void
{
$this->service->server->ssh()->exec(
$this->getScript('change-default-nodejs.sh', [
view('ssh.services.nodejs.change-default-nodejs', [
'version' => $this->service->version,
]),
'change-default-nodejs'

View File

@ -1,13 +0,0 @@
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
if ! nvm alias default __version__; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! nvm use default; then
echo 'VITO_SSH_ERROR' && exit 1
fi
echo "Default Node.js is now:"
node -v

View File

@ -1,68 +0,0 @@
# Download NVM, if not already downloaded
if [ ! -d "$HOME/.nvm" ]; then
if ! git clone https://github.com/nvm-sh/nvm.git "$HOME/.nvm"; then
echo 'VITO_SSH_ERROR' && exit 1
fi
fi
# Checkout the latest stable version of NVM
if ! git -C "$HOME/.nvm" checkout v0.40.1; then
echo 'VITO_SSH_ERROR' && exit 1
fi
# Load NVM
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
# Define the NVM initialization script
NVM_INIT='
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
'
# List of potential configuration files
CONFIG_FILES=("$HOME/.bash_profile" "$HOME/.bash_login" "$HOME/.profile")
# Flag to track if at least one file exists
FILE_EXISTS=false
# Loop through each configuration file and check if it exists
for config_file in "${CONFIG_FILES[@]}"; do
if [ -f "$config_file" ]; then
FILE_EXISTS=true
# Check if the NVM initialization is already present
if ! grep -q 'export NVM_DIR="$HOME/.nvm"' "$config_file"; then
echo "Adding NVM initialization to $config_file"
echo "$NVM_INIT" >> "$config_file"
else
echo "NVM initialization already exists in $config_file"
fi
fi
done
# If no file exists, fallback to .profile
if [ "$FILE_EXISTS" = false ]; then
FALLBACK_FILE="$HOME/.profile"
echo "No configuration files found. Creating $FALLBACK_FILE and adding NVM initialization."
echo "$NVM_INIT" >> "$FALLBACK_FILE"
fi
echo "NVM initialization process completed."
# Install NVM if not already installed
if ! command -v nvm > /dev/null 2>&1; then
if ! bash "$HOME/.nvm/install.sh"; then
echo 'VITO_SSH_ERROR' && exit 1
fi
fi
# Install the requested Node.js version
if ! nvm install __version__; then
echo 'VITO_SSH_ERROR' && exit 1
fi
echo "Node.js version __version__ installed successfully!"
echo "Node version:" && node -v
echo "NPM version:" && npm -v
echo "NPX version:" && npx -v

View File

@ -1,6 +0,0 @@
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
if ! nvm uninstall __version__; then
echo 'VITO_SSH_ERROR' && exit 1
fi

View File

@ -3,7 +3,7 @@
namespace App\SSH\Services\PHP;
use App\Exceptions\SSHCommandError;
use App\SSH\HasScripts;
use App\Exceptions\SSHError;
use App\SSH\Services\AbstractService;
use Closure;
use Illuminate\Support\Str;
@ -11,8 +11,6 @@
class PHP extends AbstractService
{
use HasScripts;
public function creationRules(array $input): array
{
return [
@ -43,11 +41,14 @@ function (string $attribute, mixed $value, Closure $fail) {
];
}
/**
* @throws SSHError
*/
public function install(): void
{
$server = $this->service->server;
$server->ssh()->exec(
$this->getScript('install-php.sh', [
view('ssh.services.php.install-php', [
'version' => $this->service->version,
'user' => $server->getSshUser(),
]),
@ -57,10 +58,13 @@ public function install(): void
$this->service->server->os()->cleanup();
}
/**
* @throws SSHError
*/
public function uninstall(): void
{
$this->service->server->ssh()->exec(
$this->getScript('uninstall-php.sh', [
view('ssh.services.php.uninstall-php', [
'version' => $this->service->version,
]),
'uninstall-php-'.$this->service->version
@ -68,10 +72,13 @@ public function uninstall(): void
$this->service->server->os()->cleanup();
}
/**
* @throws SSHError
*/
public function setDefaultCli(): void
{
$this->service->server->ssh()->exec(
$this->getScript('change-default-php.sh', [
view('ssh.services.php.change-default-php', [
'version' => $this->service->version,
]),
'change-default-php'
@ -79,12 +86,12 @@ public function setDefaultCli(): void
}
/**
* @throws SSHCommandError
* @throws SSHError
*/
public function installExtension($name): void
{
$result = $this->service->server->ssh()->exec(
$this->getScript('install-php-extension.sh', [
view('ssh.services.php.install-php-extension', [
'version' => $this->service->version,
'name' => $name,
]),
@ -96,14 +103,20 @@ public function installExtension($name): void
}
}
/**
* @throws SSHError
*/
public function installComposer(): void
{
$this->service->server->ssh()->exec(
$this->getScript('install-composer.sh'),
view('ssh.services.php.install-composer'),
'install-composer'
);
}
/**
* @throws SSHError
*/
public function getPHPIni(string $type): string
{
return $this->service->server->os()->readFile(
@ -111,26 +124,30 @@ public function getPHPIni(string $type): string
);
}
/**
* @throws SSHError
*/
public function createFpmPool(string $user, string $version, $site_id): void
{
$this->service->server->ssh()->exec(
$this->getScript('create-fpm-pool.sh', [
$this->service->server->ssh()->write(
"/etc/php/{$version}/fpm/pool.d/{$user}.conf",
view('ssh.services.php.fpm-pool', [
'user' => $user,
'version' => $version,
'config' => $this->getScript('fpm-pool.conf', [
'user' => $user,
'version' => $version,
]),
]),
"create-{$version}fpm-pool-{$user}",
$site_id
true
);
$this->service->server->systemd()->restart($this->service->unit);
}
/**
* @throws SSHError
*/
public function removeFpmPool(string $user, string $version, $site_id): void
{
$this->service->server->ssh()->exec(
$this->getScript('remove-fpm-pool.sh', [
view('ssh.services.php.remove-fpm-pool', [
'user' => $user,
'version' => $version,
]),

View File

@ -1,11 +0,0 @@
if ! sudo rm /usr/bin/php; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo ln -s /usr/bin/php__version__ /usr/bin/php; then
echo 'VITO_SSH_ERROR' && exit 1
fi
echo "Default php is: "
php -v

View File

@ -1,2 +0,0 @@
echo '__config__' | sudo tee /etc/php/__version__/fpm/pool.d/__user__.conf
sudo service php__version__-fpm restart

View File

@ -1,22 +0,0 @@
[__user__]
user = __user__
group = __user__
listen = /run/php/php__version__-fpm-__user__.sock
listen.owner = vito
listen.group = vito
listen.mode = 0660
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_requests = 500
php_admin_value[open_basedir] = /home/__user__/:/tmp/
php_admin_value[upload_tmp_dir] = /home/__user__/tmp
php_admin_value[session.save_path] = /home/__user__/tmp
php_admin_value[display_errors] = off
php_admin_value[log_errors] = on
php_admin_value[error_log] = /home/__user__/.logs/php_errors.log

View File

@ -1,9 +0,0 @@
cd ~
curl -sS https://getcomposer.org/installer -o composer-setup.php
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
rm composer-setup.php
composer

View File

@ -1,5 +0,0 @@
sudo apt-get install -y php__version__-__name__
sudo service php__version__-fpm restart
php__version__ -m

View File

@ -1,15 +0,0 @@
sudo add-apt-repository ppa:ondrej/php -y
sudo DEBIAN_FRONTEND=noninteractive apt-get update
if ! sudo DEBIAN_FRONTEND=noninteractive apt-get install -y php__version__ php__version__-fpm php__version__-mbstring php__version__-mysql php__version__-gd php__version__-xml php__version__-curl php__version__-gettext php__version__-zip php__version__-bcmath php__version__-soap php__version__-redis php__version__-sqlite3 php__version__-tokenizer php__version__-pgsql php__version__-pdo; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo sed -i 's/www-data/__user__/g' /etc/php/__version__/fpm/pool.d/www.conf; then
echo 'VITO_SSH_ERROR' && exit 1
fi
sudo service php__version__-fpm enable
sudo service php__version__-fpm start

View File

@ -1,2 +0,0 @@
sudo rm -f /etc/php/__version__/fpm/pool.d/__user__.conf
sudo service php__version__-fpm restart

View File

@ -1,5 +0,0 @@
sudo service php__version__-fpm stop
if ! sudo DEBIAN_FRONTEND=noninteractive apt-get remove -y php__version__ php__version__-fpm php__version__-mbstring php__version__-mysql php__version__-mcrypt php__version__-gd php__version__-xml php__version__-curl php__version__-gettext php__version__-zip php__version__-bcmath php__version__-soap php__version__-redis php__version__-sqlite3; then
echo 'VITO_SSH_ERROR' && exit 1
fi

View File

@ -1,11 +0,0 @@
if ! sudo sed -i 's,^__variable__ =.*$,__variable__ = __value__,' /etc/php/__version__/cli/php.ini; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo sed -i 's,^__variable__ =.*$,__variable__ = __value__,' /etc/php/__version__/fpm/php.ini; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo service php__version__-fpm restart; then
echo 'VITO_SSH_ERROR' && exit 1
fi

View File

@ -2,33 +2,37 @@
namespace App\SSH\Services\ProcessManager;
use App\SSH\HasScripts;
use App\Exceptions\SSHError;
use Throwable;
class Supervisor extends AbstractProcessManager
{
use HasScripts;
/**
* @throws SSHError
*/
public function install(): void
{
$this->service->server->ssh()->exec(
$this->getScript('supervisor/install-supervisor.sh'),
view('ssh.services.process-manager.supervisor.install-supervisor'),
'install-supervisor'
);
$this->service->server->os()->cleanup();
}
/**
* @throws SSHError
*/
public function uninstall(): void
{
$this->service->server->ssh()->exec(
$this->getScript('supervisor/uninstall-supervisor.sh'),
view('ssh.services.process-manager.supervisor.uninstall-supervisor'),
'uninstall-supervisor'
);
$this->service->server->os()->cleanup();
}
/**
* @throws Throwable
* @throws SSHError
*/
public function create(
int $id,
@ -42,21 +46,22 @@ public function create(
): void {
$this->service->server->ssh()->write(
"/etc/supervisor/conf.d/$id.conf",
$this->generateConfigFile(
$id,
$command,
$user,
$autoStart,
$autoRestart,
$numprocs,
$logFile
),
view('ssh.services.process-manager.supervisor.worker', [
'name' => (string) $id,
'command' => $command,
'user' => $user,
'autoStart' => var_export($autoStart, true),
'autoRestart' => var_export($autoRestart, true),
'numprocs' => (string) $numprocs,
'logFile' => $logFile,
]),
true
);
$this->service->server->ssh()->exec(
$this->getScript('supervisor/create-worker.sh', [
view('ssh.services.process-manager.supervisor.create-worker', [
'id' => $id,
'log_file' => $logFile,
'logFile' => $logFile,
'user' => $user,
]),
'create-worker',
@ -70,7 +75,7 @@ public function create(
public function delete(int $id, ?int $siteId = null): void
{
$this->service->server->ssh()->exec(
$this->getScript('supervisor/delete-worker.sh', [
view('ssh.services.process-manager.supervisor.delete-worker', [
'id' => $id,
]),
'delete-worker',
@ -84,7 +89,7 @@ public function delete(int $id, ?int $siteId = null): void
public function restart(int $id, ?int $siteId = null): void
{
$this->service->server->ssh()->exec(
$this->getScript('supervisor/restart-worker.sh', [
view('ssh.services.process-manager.supervisor.restart-worker', [
'id' => $id,
]),
'restart-worker',
@ -98,7 +103,7 @@ public function restart(int $id, ?int $siteId = null): void
public function stop(int $id, ?int $siteId = null): void
{
$this->service->server->ssh()->exec(
$this->getScript('supervisor/stop-worker.sh', [
view('ssh.services.process-manager.supervisor.stop-worker', [
'id' => $id,
]),
'stop-worker',
@ -112,7 +117,7 @@ public function stop(int $id, ?int $siteId = null): void
public function start(int $id, ?int $siteId = null): void
{
$this->service->server->ssh()->exec(
$this->getScript('supervisor/start-worker.sh', [
view('ssh.services.process-manager.supervisor.start-worker', [
'id' => $id,
]),
'start-worker',
@ -129,24 +134,4 @@ public function getLogs(string $user, string $logPath): string
"tail -100 $logPath"
);
}
private function generateConfigFile(
int $id,
string $command,
string $user,
bool $autoStart,
bool $autoRestart,
int $numprocs,
string $logFile
): string {
return $this->getScript('supervisor/worker.conf', [
'name' => (string) $id,
'command' => $command,
'user' => $user,
'auto_start' => var_export($autoStart, true),
'auto_restart' => var_export($autoRestart, true),
'numprocs' => (string) $numprocs,
'log_file' => $logFile,
]);
}
}

View File

@ -1,23 +0,0 @@
if ! sudo mkdir -p "$(dirname __log_file__)"; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo touch __log_file__; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo chown __user__:__user__ __log_file__; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo supervisorctl reread; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo supervisorctl update; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo supervisorctl start __id__:*; then
echo 'VITO_SSH_ERROR' && exit 1
fi

View File

@ -1,19 +0,0 @@
if ! sudo supervisorctl stop __id__:*; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo rm -rf ~/.logs/workers/__id__.log; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo rm -rf /etc/supervisor/conf.d/__id__.conf; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo supervisorctl reread; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo supervisorctl update; then
echo 'VITO_SSH_ERROR' && exit 1
fi

View File

@ -1,5 +0,0 @@
sudo DEBIAN_FRONTEND=noninteractive apt-get install supervisor -y
sudo service supervisor enable
sudo service supervisor start

View File

@ -1,3 +0,0 @@
if ! sudo supervisorctl restart __id__:*; then
echo 'VITO_SSH_ERROR' && exit 1
fi

View File

@ -1,3 +0,0 @@
if ! sudo supervisorctl start __id__:*; then
echo 'VITO_SSH_ERROR' && exit 1
fi

View File

@ -1,3 +0,0 @@
if ! sudo supervisorctl stop __id__:*; then
echo 'VITO_SSH_ERROR' && exit 1
fi

View File

@ -1,8 +0,0 @@
sudo service supervisor stop
sudo DEBIAN_FRONTEND=noninteractive apt-get remove supervisor -y
sudo rm -rf /etc/supervisor
sudo rm -rf /var/log/supervisor
sudo rm -rf /var/run/supervisor
sudo rm -rf /var/run/supervisor/supervisor.sock

View File

@ -1,10 +0,0 @@
[program:__name__]
process_name=%(program_name)s_%(process_num)02d
command=__command__
autostart=__auto_start__
autorestart=__auto_restart__
user=__user__
numprocs=__numprocs__
redirect_stderr=true
stdout_logfile=__log_file__
stopwaitsecs=3600

View File

@ -2,14 +2,13 @@
namespace App\SSH\Services\Redis;
use App\SSH\HasScripts;
use App\Exceptions\ServiceInstallationFailed;
use App\Exceptions\SSHError;
use App\SSH\Services\AbstractService;
use Closure;
class Redis extends AbstractService
{
use HasScripts;
public function creationRules(array $input): array
{
return [
@ -25,10 +24,14 @@ function (string $attribute, mixed $value, Closure $fail) {
];
}
/**
* @throws ServiceInstallationFailed
* @throws SSHError
*/
public function install(): void
{
$this->service->server->ssh()->exec(
$this->getScript('install.sh'),
view('ssh.services.redis.install'),
'install-redis'
);
$status = $this->service->server->systemd()->status($this->service->unit);
@ -36,10 +39,13 @@ public function install(): void
$this->service->server->os()->cleanup();
}
/**
* @throws SSHError
*/
public function uninstall(): void
{
$this->service->server->ssh()->exec(
$this->getScript('uninstall.sh'),
view('ssh.services.redis.uninstall'),
'uninstall-redis'
);
$this->service->server->os()->cleanup();

View File

@ -1,7 +0,0 @@
sudo DEBIAN_FRONTEND=noninteractive apt-get install redis-server -y
sudo sed -i 's/bind 127.0.0.1 ::1/bind 0.0.0.0/g' /etc/redis/redis.conf
sudo service redis enable
sudo service redis start

View File

@ -1,15 +0,0 @@
sudo service redis stop
sudo DEBIAN_FRONTEND=noninteractive apt-get remove redis-server -y
sudo rm -rf /etc/redis
sudo rm -rf /var/lib/redis
sudo rm -rf /var/log/redis
sudo rm -rf /var/run/redis
sudo rm -rf /var/run/redis/redis-server.pid
sudo rm -rf /var/run/redis/redis-server.sock
sudo rm -rf /var/run/redis/redis-server.sock
sudo DEBIAN_FRONTEND=noninteractive sudo apt-get autoremove -y
sudo DEBIAN_FRONTEND=noninteractive sudo apt-get autoclean -y

View File

@ -6,25 +6,31 @@
use App\Exceptions\SSLCreationException;
use App\Models\Site;
use App\Models\Ssl;
use App\SSH\HasScripts;
use Closure;
use Illuminate\Support\Str;
use Throwable;
class Nginx extends AbstractWebserver
{
use HasScripts;
/**
* @throws SSHError
*/
public function install(): void
{
$this->service->server->ssh()->exec(
$this->getScript('nginx/install-nginx.sh', [
'config' => $this->getScript('nginx/nginx.conf', [
'user' => $this->service->server->getSshUser(),
]),
]),
view('ssh.services.webserver.nginx.install-nginx'),
'install-nginx'
);
$this->service->server->ssh()->write(
'/etc/nginx/nginx.conf',
view('ssh.services.webserver.nginx.nginx', [
'user' => $this->service->server->getSshUser(),
]),
true
);
$this->service->server->systemd()->restart('nginx');
$this->service->server->os()->cleanup();
}
@ -43,10 +49,13 @@ function (string $attribute, mixed $value, Closure $fail) {
];
}
/**
* @throws SSHError
*/
public function uninstall(): void
{
$this->service->server->ssh()->exec(
$this->getScript('nginx/uninstall-nginx.sh'),
view('ssh.services.webserver.nginx.uninstall-nginx'),
'uninstall-nginx'
);
$this->service->server->os()->cleanup();
@ -62,50 +71,68 @@ public function createVHost(Site $site): void
$ssh = $this->service->server->ssh($site->user);
$ssh->exec(
$this->getScript('nginx/create-path.sh', [
view('ssh.services.webserver.nginx.create-path', [
'path' => $site->path,
]),
'create-path',
$site->id
);
$this->service->server->ssh()->write(
'/etc/nginx/sites-available/'.$site->domain,
view('ssh.services.webserver.nginx.vhost', [
'site' => $site,
]),
true
);
$this->service->server->ssh()->exec(
$this->getScript('nginx/create-vhost.sh', [
view('ssh.services.webserver.nginx.create-vhost', [
'domain' => $site->domain,
'path' => $site->path,
'vhost' => $this->generateVhost($site),
'vhost' => view('ssh.services.webserver.nginx.vhost', [
'site' => $site,
]),
]),
'create-vhost',
$site->id
);
}
public function updateVHost(Site $site, bool $noSSL = false, ?string $vhost = null): void
/**
* @throws SSHError
*/
public function updateVHost(Site $site, ?string $vhost = null): void
{
$this->service->server->ssh()->exec(
$this->getScript('nginx/update-vhost.sh', [
'domain' => $site->domain,
'path' => $site->path,
'vhost' => $vhost ?? $this->generateVhost($site, $noSSL),
$this->service->server->ssh()->write(
'/etc/nginx/sites-available/'.$site->domain,
$vhost ?? view('ssh.services.webserver.nginx.vhost', [
'site' => $site,
]),
'update-vhost',
$site->id
true
);
$this->service->server->systemd()->restart('nginx');
}
/**
* @throws SSHError
*/
public function getVHost(Site $site): string
{
return $this->service->server->ssh()->exec(
$this->getScript('nginx/get-vhost.sh', [
view('ssh.services.webserver.nginx.get-vhost', [
'domain' => $site->domain,
]),
);
}
/**
* @throws SSHError
*/
public function deleteSite(Site $site): void
{
$this->service->server->ssh()->exec(
$this->getScript('nginx/delete-site.sh', [
view('ssh.services.webserver.nginx.delete-site', [
'domain' => $site->domain,
'path' => $site->path,
]),
@ -115,13 +142,16 @@ public function deleteSite(Site $site): void
$this->service->restart();
}
/**
* @throws SSHError
*/
public function changePHPVersion(Site $site, $version): void
{
$this->service->server->ssh()->exec(
$this->getScript('nginx/change-php-version.sh', [
view('ssh.services.webserver.nginx.change-php-version', [
'domain' => $site->domain,
'old_version' => $site->php_version,
'new_version' => $version,
'oldVersion' => $site->php_version,
'newVersion' => $version,
]),
'change-php-version',
$site->id
@ -137,19 +167,18 @@ public function setupSSL(Ssl $ssl): void
foreach ($ssl->getDomains() as $domain) {
$domains .= ' -d '.$domain;
}
$command = $this->getScript('nginx/create-letsencrypt-ssl.sh', [
$command = view('ssh.services.webserver.nginx.create-letsencrypt-ssl', [
'email' => $ssl->site->server->creator->email,
'domain' => $ssl->site->domain,
'domains' => $domains,
'web_directory' => $ssl->site->getWebDirectoryPath(),
]);
if ($ssl->type == 'custom') {
$command = $this->getScript('nginx/create-custom-ssl.sh', [
$command = view('ssh.services.webserver.nginx.create-custom-ssl', [
'path' => $ssl->getCertsDirectoryPath(),
'certificate' => $ssl->certificate,
'pk' => $ssl->pk,
'certificate_path' => $ssl->getCertificatePath(),
'pk_path' => $ssl->getPkPath(),
'certificatePath' => $ssl->getCertificatePath(),
'pkPath' => $ssl->getPkPath(),
]);
}
$result = $this->service->server->ssh()->setLog($ssl->log)->exec(
@ -160,8 +189,6 @@ public function setupSSL(Ssl $ssl): void
if (! $ssl->validateSetup($result)) {
throw new SSLCreationException;
}
$this->updateVHost($ssl->site);
}
/**
@ -175,55 +202,6 @@ public function removeSSL(Ssl $ssl): void
$ssl->site_id
);
$this->updateVHost($ssl->site, true);
$this->service->server->systemd()->restart('nginx');
}
protected function generateVhost(Site $site, bool $noSSL = false): string
{
$ssl = $site->activeSsl;
if ($noSSL) {
$ssl = null;
}
$vhost = $this->getScript('nginx/vhost.conf');
if ($ssl) {
$vhost = $this->getScript('nginx/vhost-ssl.conf');
}
if ($site->type()->language() === 'php') {
$vhost = $this->getScript('nginx/php-vhost.conf');
if ($ssl) {
$vhost = $this->getScript('nginx/php-vhost-ssl.conf');
}
}
if ($site->port) {
$vhost = $this->getScript('nginx/reverse-vhost.conf');
if ($ssl) {
$vhost = $this->getScript('nginx/reverse-vhost-ssl.conf');
}
$vhost = Str::replace('__port__', (string) $site->port, $vhost);
}
$php_socket = 'unix:/var/run/php/php-fpm.sock';
if ($site->isIsolated()) {
$php_socket = "unix:/run/php/php{$site->php_version}-fpm-{$site->user}.sock";
}
$vhost = Str::replace('__domain__', $site->domain, $vhost);
$vhost = Str::replace('__aliases__', $site->getAliasesString(), $vhost);
$vhost = Str::replace('__path__', $site->path, $vhost);
$vhost = Str::replace('__web_directory__', $site->web_directory, $vhost);
$vhost = Str::replace('__php_socket__', $php_socket, $vhost);
if ($ssl) {
$vhost = Str::replace('__certificate__', $ssl->getCertificatePath(), $vhost);
$vhost = Str::replace('__private_key__', $ssl->getPkPath(), $vhost);
}
if ($site->php_version) {
$vhost = Str::replace('__php_version__', $site->php_version, $vhost);
}
return $vhost;
$this->updateVHost($ssl->site);
}
}

View File

@ -9,7 +9,7 @@ interface Webserver
{
public function createVHost(Site $site): void;
public function updateVHost(Site $site, bool $noSSL = false, ?string $vhost = null): void;
public function updateVHost(Site $site, ?string $vhost = null): void;
public function getVHost(Site $site): string;

View File

@ -1,9 +0,0 @@
if ! sudo sed -i 's/php__old_version__/php__new_version__/g' /etc/nginx/sites-available/__domain__; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo service nginx restart; then
echo 'VITO_SSH_ERROR' && exit 1
fi
echo "PHP Version Changed to __new_version__"

View File

@ -1,13 +0,0 @@
if ! sudo mkdir -p __path__; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! echo "__certificate__" | sudo tee __certificate_path__; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! echo "__pk__" | sudo tee __pk_path__; then
echo 'VITO_SSH_ERROR' && exit 1
fi
echo "Successfully received certificate."

View File

@ -1,3 +0,0 @@
if ! sudo certbot certonly --force-renewal --nginx --noninteractive --agree-tos --cert-name __domain__ -m __email__ __domains__ --verbose; then
echo 'VITO_SSH_ERROR' && exit 1
fi

View File

@ -1,16 +0,0 @@
export DEBIAN_FRONTEND=noninteractive
if ! rm -rf __path__; then
echo 'VITO_SSH_ERROR'
exit 1
fi
if ! mkdir __path__; then
echo 'VITO_SSH_ERROR'
exit 1
fi
if ! chmod -R 755 __path__; then
echo 'VITO_SSH_ERROR'
exit 1
fi

View File

@ -1,15 +0,0 @@
if ! echo '' | sudo tee /etc/nginx/conf.d/__domain___redirects; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! echo '__vhost__' | sudo tee /etc/nginx/sites-available/__domain__; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo ln -s /etc/nginx/sites-available/__domain__ /etc/nginx/sites-enabled/; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo service nginx restart; then
echo 'VITO_SSH_ERROR' && exit 1
fi

View File

@ -1,7 +0,0 @@
rm -rf __path__
sudo rm /etc/nginx/sites-available/__domain__
sudo rm /etc/nginx/sites-enabled/__domain__
echo "Site deleted"

View File

@ -1 +0,0 @@
cat /etc/nginx/sites-available/__domain__

View File

@ -1,8 +0,0 @@
sudo DEBIAN_FRONTEND=noninteractive apt-get install nginx -y
if ! echo '__config__' | sudo tee /etc/nginx/nginx.conf; then
echo 'VITO_SSH_ERROR' && exit 1
fi
sudo service nginx start
# install certbot
sudo DEBIAN_FRONTEND=noninteractive apt-get install certbot python3-certbot-nginx -y

View File

@ -1,85 +0,0 @@
user __user__;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}

View File

@ -1,38 +0,0 @@
server {
listen 80;
listen 443 ssl;
server_name __domain__ __aliases__;
root __path__/__web_directory__;
ssl_certificate __certificate__;
ssl_certificate_key __private_key__;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass __php_socket__;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_hide_header X-Powered-By;
}
location ~ /\.(?!well-known).* {
deny all;
}
include conf.d/__domain___redirects;
}

View File

@ -1,34 +0,0 @@
server {
listen 80;
server_name __domain__ __aliases__;
root __path__/__web_directory__;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass __php_socket__;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_hide_header X-Powered-By;
}
location ~ /\.(?!well-known).* {
deny all;
}
include conf.d/__domain___redirects;
}

View File

@ -1,3 +0,0 @@
location __from__ {
return __mode__ __to__;
}

View File

@ -1,35 +0,0 @@
server {
listen 80;
listen 443 ssl;
server_name __domain__ __aliases__;
root __path__;
ssl_certificate __certificate__;
ssl_certificate_key __private_key__;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
proxy_pass http://127.0.0.1:__port__/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-For $remote_addr;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ /\.(?!well-known).* {
deny all;
}
include conf.d/__domain___redirects;
}

View File

@ -1,31 +0,0 @@
server {
listen 80;
server_name __domain__ __aliases__;
root __path__;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
proxy_pass http://127.0.0.1:__port__/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-For $remote_addr;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ /\.(?!well-known).* {
deny all;
}
include conf.d/__domain___redirects;
}

View File

@ -1,12 +0,0 @@
sudo service nginx stop
sudo DEBIAN_FRONTEND=noninteractive apt-get purge nginx nginx-common nginx-full -y
sudo rm -rf /etc/nginx
sudo rm -rf /var/log/nginx
sudo rm -rf /var/lib/nginx
sudo rm -rf /var/cache/nginx
sudo rm -rf /usr/share/nginx
sudo rm -rf /etc/systemd/system/nginx.service
sudo systemctl daemon-reload

View File

@ -1,7 +0,0 @@
if ! echo '__redirects__' | sudo tee /etc/nginx/conf.d/__domain___redirects; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo service nginx restart; then
echo 'VITO_SSH_ERROR' && exit 1
fi

View File

@ -1,3 +0,0 @@
echo '__vhost__' | sudo tee /etc/nginx/sites-available/__domain__
sudo service nginx restart

View File

@ -1,31 +0,0 @@
server {
listen 80;
listen 443 ssl;
server_name __domain__ __aliases__;
root __path__/__web_directory__;
ssl_certificate __certificate__;
ssl_certificate_key __private_key__;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.html;
charset utf-8;
location / {
try_files $uri $uri/ /index.html;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.html;
location ~ /\.(?!well-known).* {
deny all;
}
include conf.d/__domain___redirects;
}

View File

@ -1,27 +0,0 @@
server {
listen 80;
server_name __domain__ __aliases__;
root __path__/__web_directory__;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.html;
charset utf-8;
location / {
try_files $uri $uri/ /index.html;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.html;
location ~ /\.(?!well-known).* {
deny all;
}
include conf.d/__domain___redirects;
}