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

@ -6,6 +6,7 @@
use App\Models\Queue;
use App\Models\Server;
use App\Models\Site;
use App\SSH\Services\ProcessManager\ProcessManager;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
@ -29,7 +30,9 @@ public function create(mixed $queueable, array $input): void
$queue->save();
dispatch(function () use ($queue) {
$queue->server->processManager()->handler()->create(
/** @var ProcessManager $processManager */
$processManager = $queue->server->processManager()->handler();
$processManager->create(
$queue->id,
$queue->command,
$queue->user,

View File

@ -44,6 +44,7 @@ public function create(Site $site, array $input): void
$webserver->setupSSL($ssl);
$ssl->status = SslStatus::CREATED;
$ssl->save();
$webserver->updateVHost($site);
$site->type()->edit();
})->catch(function () use ($ssl) {
$ssl->status = SslStatus::FAILED;

View File

@ -2,6 +2,7 @@
namespace App\Actions\SSL;
use App\Enums\SslStatus;
use App\Models\Ssl;
use App\SSH\Services\Webserver\Webserver;
@ -9,6 +10,8 @@ class DeleteSSL
{
public function delete(Ssl $ssl): void
{
$ssl->status = SslStatus::DELETING;
$ssl->save();
/** @var Webserver $webserver */
$webserver = $ssl->site->server->webserver()->handler();
$webserver->removeSSL($ssl);

View File

@ -2,12 +2,16 @@
namespace App\Actions\Site;
use App\Exceptions\SSHError;
use App\Models\Site;
use App\SSH\Services\PHP\PHP;
use App\SSH\Services\Webserver\Webserver;
class DeleteSite
{
/**
* @throws SSHError
*/
public function delete(Site $site): void
{
/** @var Webserver $webserverHandler */

View File

@ -4,7 +4,7 @@
use App\Enums\DeploymentStatus;
use App\Exceptions\DeploymentScriptIsEmptyException;
use App\Exceptions\SourceControlIsNotConnected;
use App\Exceptions\SSHError;
use App\Models\Deployment;
use App\Models\ServerLog;
use App\Models\Site;
@ -12,8 +12,8 @@
class Deploy
{
/**
* @throws SourceControlIsNotConnected
* @throws DeploymentScriptIsEmptyException
* @throws SSHError
*/
public function run(Site $site): Deployment
{

View File

@ -14,7 +14,7 @@ public function update(Site $site, array $input): void
/** @var Webserver $webserver */
$webserver = $site->server->webserver()->handler();
$webserver->updateVHost($site, ! $site->hasSSL());
$webserver->updateVHost($site);
$site->save();
}

View File

@ -2,6 +2,7 @@
namespace App\Actions\Site;
use App\Exceptions\SSHError;
use App\Models\Site;
use App\SSH\Git\Git;
use Illuminate\Validation\ValidationException;
@ -10,6 +11,7 @@ class UpdateBranch
{
/**
* @throws ValidationException
* @throws SSHError
*/
public function update(Site $site, array $input): void
{

View File

@ -2,6 +2,7 @@
namespace App\Actions\Site;
use App\Exceptions\SSHError;
use App\Models\Site;
use Illuminate\Validation\Rule;
@ -19,6 +20,9 @@ public static function rules(Site $site): array
];
}
/**
* @throws SSHError
*/
public function update(Site $site, array $input): void
{
$site->changePHPVersion($input['version']);

View File

@ -5,11 +5,7 @@
use App\Actions\Service\Manage;
use App\Enums\ServiceStatus;
use App\Exceptions\ServiceInstallationFailed;
use App\SSH\Services\Database\Database as DatabaseAlias;
use App\SSH\Services\PHP\PHP as PHPAlias;
use App\SSH\Services\ProcessManager\ProcessManager;
use App\SSH\Services\ServiceInterface;
use App\SSH\Services\WebServer\WebServer as WebServerAlias;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Str;
@ -79,9 +75,6 @@ public function server(): BelongsTo
return $this->belongsTo(Server::class);
}
/**
* @return ProcessManager|DatabaseAlias|PHPAlias|WebServerAlias
*/
public function handler(): ServiceInterface
{
$handler = config('core.service_handlers')[$this->name];

View File

@ -3,6 +3,7 @@
namespace App\Models;
use App\Enums\SiteStatus;
use App\Enums\SslStatus;
use App\Exceptions\FailedToDestroyGitHook;
use App\Exceptions\SourceControlIsNotConnected;
use App\Exceptions\SSHError;
@ -35,6 +36,7 @@
* @property int $port
* @property int $progress
* @property string $user
* @property bool $force_ssl
* @property Server $server
* @property ServerLog[] $logs
* @property Deployment[] $deployments
@ -198,6 +200,9 @@ public function php(): ?Service
return null;
}
/**
* @throws SSHError
*/
public function changePHPVersion($version): void
{
/** @var Webserver $handler */
@ -219,6 +224,7 @@ public function activeSsl(): HasOne
{
return $this->hasOne(Ssl::class)
->where('expires_at', '>=', now())
->where('status', SslStatus::CREATED)
->orderByDesc('id');
}
@ -301,11 +307,6 @@ public function getEnv(): string
}
}
public function hasSSL(): bool
{
return $this->ssls->isNotEmpty();
}
public function environmentVariables(?Deployment $deployment = null): array
{
return [
@ -319,30 +320,16 @@ public function environmentVariables(?Deployment $deployment = null): array
];
}
public function isolate(): void
{
if (! $this->isIsolated()) {
return;
}
$this->server->os()->createIsolatedUser(
$this->user,
Str::random(15),
$this->id
);
// Generate the FPM pool
/** @var PHP $php */
$php = $this->php()->handler();
$php->createFpmPool(
$this->user,
$this->php_version,
$this->id
);
}
public function isIsolated(): bool
{
return $this->user != $this->server->getSshUser();
}
public function webserver(): Webserver
{
/** @var Webserver $webserver */
$webserver = $this->server->webserver()->handler();
return $webserver;
}
}

View File

@ -2,19 +2,20 @@
namespace App\SSH\Composer;
use App\Exceptions\SSHError;
use App\Models\Site;
use App\SSH\HasScripts;
class Composer
{
use HasScripts;
/**
* @throws SSHError
*/
public function installDependencies(Site $site): void
{
$site->server->ssh($site->user)->exec(
$this->getScript('composer-install.sh', [
view('ssh.composer.composer-install', [
'path' => $site->path,
'php_version' => $site->php_version,
'phpVersion' => $site->php_version,
]),
'composer-install',
$site->id

View File

@ -1,7 +0,0 @@
if ! cd __path__; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! php__php_version__ /usr/local/bin/composer install --no-interaction --prefer-dist --optimize-autoloader --no-dev; then
echo 'VITO_SSH_ERROR' && exit 1
fi

View File

@ -2,26 +2,24 @@
namespace App\SSH\Cron;
use App\Exceptions\SSHError;
use App\Models\Server;
class Cron
{
public function __construct(protected Server $server) {}
/**
* @throws SSHError
*/
public function update(string $user, string $cron): void
{
$command = <<<EOD
if ! echo '$cron' | sudo -u $user crontab -; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo -u $user crontab -l; then
echo 'VITO_SSH_ERROR' && exit 1
fi
echo 'cron updated!'
EOD;
$this->server->ssh()->exec($command, 'update-cron');
$this->server->ssh()->exec(
view('ssh.cron.update', [
'cron' => $cron,
'user' => $user,
]),
'update-cron'
);
}
}

View File

@ -2,17 +2,18 @@
namespace App\SSH\Git;
use App\Exceptions\SSHError;
use App\Models\Site;
use App\SSH\HasScripts;
class Git
{
use HasScripts;
/**
* @throws SSHError
*/
public function clone(Site $site): void
{
$site->server->ssh($site->user)->exec(
$this->getScript('clone.sh', [
view('ssh.git.clone', [
'host' => str($site->getFullRepositoryUrl())->after('@')->before('-'),
'repo' => $site->getFullRepositoryUrl(),
'path' => $site->path,
@ -24,10 +25,13 @@ public function clone(Site $site): void
);
}
/**
* @throws SSHError
*/
public function checkout(Site $site): void
{
$site->server->ssh($site->user)->exec(
$this->getScript('checkout.sh', [
view('ssh.git.checkout', [
'path' => $site->path,
'branch' => $site->branch,
]),

View File

@ -1,7 +0,0 @@
if ! cd __path__; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! git checkout -f __branch__; then
echo 'VITO_SSH_ERROR' && exit 1
fi

View File

@ -1,29 +0,0 @@
echo "Host __host__-__key__
Hostname __host__
IdentityFile=~/.ssh/__key__" >> ~/.ssh/config
chmod 600 ~/.ssh/config
ssh-keyscan -H __host__ >> ~/.ssh/known_hosts
rm -rf __path__
if ! git config --global core.fileMode false; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! git clone -b __branch__ __repo__ __path__; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! find __path__ -type d -exec chmod 755 {} \;; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! find __path__ -type f -exec chmod 644 {} \;; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! cd __path__ && git config core.fileMode false; then
echo 'VITO_SSH_ERROR' && exit 1
fi

View File

@ -1,20 +0,0 @@
<?php
namespace App\SSH;
use ReflectionClass;
trait HasScripts
{
protected function getScript(string $name, array $vars = []): string
{
$reflector = new ReflectionClass($this);
$scriptsDir = dirname($reflector->getFileName()).'/scripts';
$script = file_get_contents($scriptsDir.'/'.$name);
foreach ($vars as $key => $value) {
$script = str_replace('__'.$key.'__', $value, $script);
}
return $script;
}
}

View File

@ -2,11 +2,11 @@
namespace App\SSH\OS;
use App\Exceptions\SSHError;
use App\Exceptions\SSHUploadFailed;
use App\Models\Server;
use App\Models\ServerLog;
use App\Models\Site;
use App\SSH\HasScripts;
use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
@ -14,30 +14,40 @@
class OS
{
use HasScripts;
public function __construct(protected Server $server) {}
/**
* @throws SSHError
*/
public function installDependencies(): void
{
$this->server->ssh()->exec(
$this->getScript('install-dependencies.sh'),
view('ssh.os.install-dependencies', [
'name' => $this->server->creator->name,
'email' => $this->server->creator->email,
]),
'install-dependencies'
);
}
/**
* @throws SSHError
*/
public function upgrade(): void
{
$this->server->ssh()->exec(
$this->getScript('upgrade.sh'),
view('ssh.os.upgrade'),
'upgrade'
);
}
/**
* @throws SSHError
*/
public function availableUpdates(): int
{
$result = $this->server->ssh()->exec(
$this->getScript('available-updates.sh'),
view('ssh.os.available-updates'),
'check-available-updates'
);
@ -47,10 +57,13 @@ public function availableUpdates(): int
return max($availableUpdates, 0);
}
/**
* @throws SSHError
*/
public function createUser(string $user, string $password, string $key): void
{
$this->server->ssh()->exec(
$this->getScript('create-user.sh', [
view('ssh.os.create-user', [
'user' => $user,
'password' => $password,
'key' => $key,
@ -59,12 +72,15 @@ public function createUser(string $user, string $password, string $key): void
);
}
/**
* @throws SSHError
*/
public function createIsolatedUser(string $user, string $password, int $site_id): void
{
$this->server->ssh()->exec(
$this->getScript('create-isolated-user.sh', [
view('ssh.os.create-isolated-user', [
'user' => $user,
'server_user' => $this->server->getSshUser(),
'serverUser' => $this->server->getSshUser(),
'password' => $password,
]),
'create-isolated-user',
@ -72,40 +88,52 @@ public function createIsolatedUser(string $user, string $password, int $site_id)
);
}
/**
* @throws SSHError
*/
public function deleteIsolatedUser(string $user): void
{
$this->server->ssh()->exec(
$this->getScript('delete-isolated-user.sh', [
view('ssh.os.delete-isolated-user', [
'user' => $user,
'server_user' => $this->server->getSshUser(),
'serverUser' => $this->server->getSshUser(),
]),
'delete-isolated-user'
);
}
/**
* @throws SSHError
*/
public function getPublicKey(string $user): string
{
return $this->server->ssh()->exec(
$this->getScript('read-file.sh', [
view('ssh.os.read-file', [
'path' => '/home/'.$user.'/.ssh/id_rsa.pub',
])
);
}
/**
* @throws SSHError
*/
public function deploySSHKey(string $key): void
{
$this->server->ssh()->exec(
$this->getScript('deploy-ssh-key.sh', [
view('ssh.os.deploy-ssh-key', [
'key' => $key,
]),
'deploy-ssh-key'
);
}
/**
* @throws SSHError
*/
public function deleteSSHKey(string $key): void
{
$this->server->ssh()->exec(
$this->getScript('delete-ssh-key.sh', [
view('ssh.os.delete-ssh-key', [
'key' => $key,
'user' => $this->server->getSshUser(),
]),
@ -113,10 +141,13 @@ public function deleteSSHKey(string $key): void
);
}
/**
* @throws SSHError
*/
public function generateSSHKey(string $name, ?Site $site = null): void
{
$site->server->ssh($site->user)->exec(
$this->getScript('generate-ssh-key.sh', [
view('ssh.os.generate-ssh-key', [
'name' => $name,
]),
'generate-ssh-key',
@ -124,19 +155,25 @@ public function generateSSHKey(string $name, ?Site $site = null): void
);
}
/**
* @throws SSHError
*/
public function readSSHKey(string $name, ?Site $site = null): string
{
return $site->server->ssh($site->user)->exec(
$this->getScript('read-ssh-key.sh', [
view('ssh.os.read-ssh-key', [
'name' => $name,
]),
);
}
/**
* @throws SSHError
*/
public function reboot(): void
{
$this->server->ssh()->exec(
$this->getScript('reboot.sh'),
view('ssh.os.reboot'),
);
}
@ -161,25 +198,34 @@ public function editFile(string $path, ?string $content = null): void
}
}
/**
* @throws SSHError
*/
public function readFile(string $path): string
{
return $this->server->ssh()->exec(
$this->getScript('read-file.sh', [
view('ssh.os.read-file', [
'path' => $path,
])
);
}
/**
* @throws SSHError
*/
public function tail(string $path, int $lines): string
{
return $this->server->ssh()->exec(
$this->getScript('tail.sh', [
view('ssh.os.tail', [
'path' => $path,
'lines' => $lines,
])
);
}
/**
* @throws SSHError
*/
public function runScript(string $path, string $script, ?ServerLog $serverLog, ?string $user = null, ?array $variables = []): ServerLog
{
$ssh = $this->server->ssh($user);
@ -190,7 +236,7 @@ public function runScript(string $path, string $script, ?ServerLog $serverLog, ?
foreach ($variables as $key => $variable) {
$command .= "$key=$variable\n";
}
$command .= $this->getScript('run-script.sh', [
$command .= view('ssh.os.run-script', [
'path' => $path,
'script' => $script,
]);
@ -201,16 +247,22 @@ public function runScript(string $path, string $script, ?ServerLog $serverLog, ?
return $ssh->log;
}
/**
* @throws SSHError
*/
public function download(string $url, string $path): string
{
return $this->server->ssh()->exec(
$this->getScript('download.sh', [
view('ssh.os.download', [
'url' => $url,
'path' => $path,
])
);
}
/**
* @throws SSHError
*/
public function unzip(string $path): string
{
return $this->server->ssh()->exec(
@ -218,18 +270,24 @@ public function unzip(string $path): string
);
}
/**
* @throws SSHError
*/
public function cleanup(): void
{
$this->server->ssh()->exec(
$this->getScript('cleanup.sh'),
view('ssh.os.cleanup'),
'cleanup'
);
}
/**
* @throws SSHError
*/
public function resourceInfo(): array
{
$info = $this->server->ssh()->exec(
$this->getScript('resource-info.sh'),
view('ssh.os.resource-info'),
);
return [
@ -243,10 +301,13 @@ public function resourceInfo(): array
];
}
/**
* @throws SSHError
*/
public function deleteFile(string $path): void
{
$this->server->ssh()->exec(
$this->getScript('delete-file.sh', [
view('ssh.os.delete-file', [
'path' => $path,
]),
'delete-file'

View File

@ -1,5 +0,0 @@
sudo DEBIAN_FRONTEND=noninteractive apt-get update
AVAILABLE_UPDATES=$(sudo DEBIAN_FRONTEND=noninteractive apt list --upgradable | wc -l)
echo "Available updates:$AVAILABLE_UPDATES"

View File

@ -1,19 +0,0 @@
# Update package lists
sudo DEBIAN_FRONTEND=noninteractive apt-get update -y
# Remove unnecessary dependencies
sudo DEBIAN_FRONTEND=noninteractive apt-get autoremove --purge -y
# Clear package cache
sudo DEBIAN_FRONTEND=noninteractive apt-get clean -y
# Remove old configuration files
sudo DEBIAN_FRONTEND=noninteractive apt-get purge -y $(dpkg -l | grep '^rc' | awk '{print $2}')
# Clear temporary files
sudo rm -rf /tmp/*
# Clear journal logs
sudo DEBIAN_FRONTEND=noninteractive journalctl --vacuum-time=1d
echo "Cleanup completed."

View File

@ -1,18 +0,0 @@
export DEBIAN_FRONTEND=noninteractive
if ! sudo useradd -p $(openssl passwd -1 __password__) __user__; then
echo 'VITO_SSH_ERROR' && exit 1
fi
sudo mkdir /home/__user__
sudo mkdir /home/__user__/.logs
sudo mkdir /home/__user__/tmp
sudo mkdir /home/__user__/bin
sudo mkdir /home/__user__/.ssh
echo 'export PATH="/home/__user__/bin:$PATH"' | sudo tee -a /home/__user__/.bashrc
echo 'export PATH="/home/__user__/bin:$PATH"' | sudo tee -a /home/__user__/.profile
sudo usermod -a -G __user__ __server_user__
sudo chown -R __user__:__user__ /home/__user__
sudo chmod -R 755 /home/__user__
sudo chmod -R 700 /home/__user__/.ssh
sudo chsh -s /bin/bash __user__
echo "Created user __user__."

View File

@ -1,11 +0,0 @@
export DEBIAN_FRONTEND=noninteractive
echo "__key__" | sudo tee -a /root/.ssh/authorized_keys
sudo useradd -p $(openssl passwd -1 __password__) __user__
sudo usermod -aG sudo __user__
echo "__user__ ALL=(ALL) NOPASSWD:ALL" | sudo tee -a /etc/sudoers
sudo mkdir /home/__user__
sudo mkdir /home/__user__/.ssh
echo "__key__" | sudo tee -a /home/__user__/.ssh/authorized_keys
sudo chown -R __user__:__user__ /home/__user__
sudo chsh -s /bin/bash __user__
sudo su - __user__ -c "ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa" <<< y

View File

@ -1 +0,0 @@
rm -f __path__

View File

@ -1,3 +0,0 @@
sudo gpasswd -d __server_user__ __user__
sudo userdel -r "__user__"
echo "User __user__ has been deleted."

View File

@ -1 +0,0 @@
bash -c 'ssh_key_to_delete="$1"; sed -i "\#${ssh_key_to_delete//\//\\/}#d" /home/vito/.ssh/authorized_keys' bash '__key__'

View File

@ -1,3 +0,0 @@
if ! echo '__key__' | sudo tee -a ~/.ssh/authorized_keys; then
echo 'VITO_SSH_ERROR' && exit 1
fi

View File

@ -1,3 +0,0 @@
if ! wget __url__ -O __path__; then
echo 'VITO_SSH_ERROR' && exit 1
fi

View File

@ -1 +0,0 @@
ssh-keygen -t rsa -b 4096 -N "" -f ~/.ssh/__name__

View File

@ -1 +0,0 @@
cat ~/.ssh/id_rsa.pub

View File

@ -1,8 +0,0 @@
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y software-properties-common curl zip unzip git gcc openssl ufw
git config --global user.email "__email__"
git config --global user.name "__name__"
# Install Node.js
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo DEBIAN_FRONTEND=noninteractive apt-get update
sudo DEBIAN_FRONTEND=noninteractive apt-get install nodejs -y

View File

@ -1 +0,0 @@
[ -f __path__ ] && sudo cat __path__

View File

@ -1 +0,0 @@
cat ~/.ssh/__name__.pub

View File

@ -1,3 +0,0 @@
echo "Rebooting..."
sudo reboot

View File

@ -1,7 +0,0 @@
echo "load:$(uptime | awk -F'load average:' '{print $2}' | awk -F, '{print $1}' | tr -d ' ')"
echo "memory_total:$(free -k | awk 'NR==2{print $2}')"
echo "memory_used:$(free -k | awk 'NR==2{print $3}')"
echo "memory_free:$(free -k | awk 'NR==2{print $7}')"
echo "disk_total:$(df -BM / | awk 'NR==2{print $2}' | sed 's/M//')"
echo "disk_used:$(df -BM / | awk 'NR==2{print $3}' | sed 's/M//')"
echo "disk_free:$(df -BM / | awk 'NR==2{print $4}' | sed 's/M//')"

View File

@ -1,5 +0,0 @@
if ! cd __path__; then
echo 'VITO_SSH_ERROR' && exit 1
fi
__script__

View File

@ -1 +0,0 @@
sudo tail -n __lines__ __path__

View File

@ -1,4 +0,0 @@
sudo DEBIAN_FRONTEND=noninteractive apt-get clean
sudo DEBIAN_FRONTEND=noninteractive apt-get update
sudo DEBIAN_FRONTEND=noninteractive apt-get upgrade -y
sudo DEBIAN_FRONTEND=noninteractive apt-get autoremove -y

View File

@ -2,17 +2,18 @@
namespace App\SSH\PHPMyAdmin;
use App\Exceptions\SSHError;
use App\Models\Site;
use App\SSH\HasScripts;
class PHPMyAdmin
{
use HasScripts;
/**
* @throws SSHError
*/
public function install(Site $site): void
{
$site->server->ssh($site->user)->exec(
$this->getScript('install.sh', [
view('ssh.phpmyadmin.install', [
'version' => $site->type_data['version'],
'path' => $site->path,
]),

View File

@ -1,29 +0,0 @@
if ! rm -rf phpmyadmin; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! rm -rf __path__; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! wget https://files.phpmyadmin.net/phpMyAdmin/__version__/phpMyAdmin-__version__-all-languages.zip; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! unzip phpMyAdmin-__version__-all-languages.zip; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! rm -rf phpMyAdmin-__version__-all-languages.zip; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! mv phpMyAdmin-__version__-all-languages __path__; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! mv __path__/config.sample.inc.php __path__/config.inc.php; then
echo 'VITO_SSH_ERROR' && exit 1
fi
echo "PHPMyAdmin installed!"

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

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