mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-02 22:46:16 +00:00
refactoring (#116)
- refactoring architecture - fix incomplete ssh logs - code editor for scripts in the app - remove Jobs and SSHCommands
This commit is contained in:
138
app/SSH/OS/OS.php
Normal file
138
app/SSH/OS/OS.php
Normal 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;
|
||||
}
|
||||
}
|
11
app/SSH/OS/scripts/create-user.sh
Executable file
11
app/SSH/OS/scripts/create-user.sh
Executable 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
|
1
app/SSH/OS/scripts/delete-ssh-key.sh
Normal file
1
app/SSH/OS/scripts/delete-ssh-key.sh
Normal 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__'
|
3
app/SSH/OS/scripts/deploy-ssh-key.sh
Normal file
3
app/SSH/OS/scripts/deploy-ssh-key.sh
Normal file
@ -0,0 +1,3 @@
|
||||
if ! echo '__key__' | sudo tee -a ~/.ssh/authorized_keys; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
3
app/SSH/OS/scripts/edit-file.sh
Normal file
3
app/SSH/OS/scripts/edit-file.sh
Normal file
@ -0,0 +1,3 @@
|
||||
if ! echo "__content__" | tee __path__; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
1
app/SSH/OS/scripts/generate-ssh-key.sh
Normal file
1
app/SSH/OS/scripts/generate-ssh-key.sh
Normal file
@ -0,0 +1 @@
|
||||
ssh-keygen -t rsa -b 4096 -N "" -f ~/.ssh/__name__
|
1
app/SSH/OS/scripts/get-public-key.sh
Executable file
1
app/SSH/OS/scripts/get-public-key.sh
Executable file
@ -0,0 +1 @@
|
||||
cat ~/.ssh/id_rsa.pub
|
3
app/SSH/OS/scripts/install-dependencies.sh
Executable file
3
app/SSH/OS/scripts/install-dependencies.sh
Executable 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__"
|
1
app/SSH/OS/scripts/read-file.sh
Normal file
1
app/SSH/OS/scripts/read-file.sh
Normal file
@ -0,0 +1 @@
|
||||
[ -f __path__ ] && cat __path__
|
1
app/SSH/OS/scripts/read-ssh-key.sh
Normal file
1
app/SSH/OS/scripts/read-ssh-key.sh
Normal file
@ -0,0 +1 @@
|
||||
cat ~/.ssh/__name__.pub
|
3
app/SSH/OS/scripts/reboot.sh
Normal file
3
app/SSH/OS/scripts/reboot.sh
Normal file
@ -0,0 +1,3 @@
|
||||
echo "Rebooting..."
|
||||
|
||||
sudo reboot
|
5
app/SSH/OS/scripts/run-script.sh
Normal file
5
app/SSH/OS/scripts/run-script.sh
Normal 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
9
app/SSH/OS/scripts/upgrade.sh
Executable 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
|
Reference in New Issue
Block a user