refactoring (#116)

- refactoring architecture
- fix incomplete ssh logs
- code editor for scripts in the app
- remove Jobs and SSHCommands
This commit is contained in:
Saeed Vaziry
2024-03-14 20:03:43 +01:00
committed by GitHub
parent cee4a70c3c
commit 428140b931
472 changed files with 24110 additions and 8159 deletions

138
app/SSH/OS/OS.php Normal file
View File

@ -0,0 +1,138 @@
<?php
namespace App\SSH\OS;
use App\Models\Server;
use App\Models\ServerLog;
use App\SSH\HasScripts;
class OS
{
use HasScripts;
public function __construct(protected Server $server)
{
}
public function installDependencies(): void
{
$this->server->ssh()->exec(
$this->getScript('install-dependencies.sh'),
'install-dependencies'
);
}
public function upgrade(): void
{
$this->server->ssh()->exec(
$this->getScript('upgrade.sh'),
'upgrade'
);
}
public function createUser(string $user, string $password, string $key): void
{
$this->server->ssh()->exec(
$this->getScript('create-user.sh', [
'user' => $user,
'password' => $password,
'key' => $key,
]),
'create-user'
);
}
public function getPublicKey(string $user): string
{
return $this->server->ssh()->exec(
$this->getScript('read-file.sh', [
'path' => '/home/'.$user.'/.ssh/id_rsa.pub',
])
);
}
public function deploySSHKey(string $key): void
{
$this->server->ssh()->exec(
$this->getScript('deploy-ssh-key.sh', [
'key' => $key,
]),
'deploy-ssh-key'
);
}
public function deleteSSHKey(string $key): void
{
info($this->getScript('delete-ssh-key.sh', [
'key' => $key,
'user' => $this->server->getSshUser(),
]));
$this->server->ssh()->exec(
$this->getScript('delete-ssh-key.sh', [
'key' => $key,
'user' => $this->server->getSshUser(),
]),
'delete-ssh-key'
);
}
public function generateSSHKey(string $name): void
{
$this->server->ssh()->exec(
$this->getScript('generate-ssh-key.sh', [
'name' => $name,
]),
'generate-ssh-key'
);
}
public function readSSHKey(string $name): string
{
return $this->server->ssh()->exec(
$this->getScript('read-ssh-key.sh', [
'name' => $name,
]),
);
}
public function reboot(): void
{
$this->server->ssh()->exec(
$this->getScript('reboot.sh'),
);
}
public function editFile(string $path, string $content): void
{
$this->server->ssh()->exec(
$this->getScript('edit-file.sh', [
'path' => $path,
'content' => $content,
]),
);
}
public function readFile(string $path): string
{
return $this->server->ssh()->exec(
$this->getScript('read-file.sh', [
'path' => $path,
])
);
}
public function runScript(string $path, string $script, ?int $siteId = null): ServerLog
{
$ssh = $this->server->ssh();
$ssh->exec(
$this->getScript('run-script.sh', [
'path' => $path,
'script' => $script,
]),
'run-script',
$siteId
);
return $ssh->log;
}
}

View File

@ -0,0 +1,11 @@
export DEBIAN_FRONTEND=noninteractive
echo "__key__" | sudo tee -a /home/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

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

View File

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

View File

@ -0,0 +1,3 @@
if ! echo "__content__" | tee __path__; then
echo 'VITO_SSH_ERROR' && exit 1
fi

View File

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

View File

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

View File

@ -0,0 +1,3 @@
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y software-properties-common curl zip unzip git gcc
git config --global user.email "__email__"
git config --global user.name "__name__"

View File

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

View File

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

View File

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

View File

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

9
app/SSH/OS/scripts/upgrade.sh Executable file
View File

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