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

@ -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