mirror of
https://github.com/vitodeploy/vito.git
synced 2025-04-22 19:22:19 +00:00
* WIP to isolate users * Resolved issue with SSH AsUser Updated Isolated User Script to use Server User for Team Access Updated Path creation script to simplify for running as the isolated user * Included the server user * PHPMyAdmin script updated Wordpress Script Updated Updated Execute Script to support executing as isolated users * Issue Resolution & Resolved Failing Unit Tests * Fix for isolated_username vs user * Run the deploy as the isolated user * queue updates for isolated user * Support isolated users in cronjobs * script tests for isolated users * Queue tests for isolated users * Cronjob tests for isolated user * Removed default queue command for laravel apps * add default user to factory * laravel pint fixes * ensure echos are consistent * removed unneeded parameter * update * fix queues for isolated users * revert addslashes --------- Co-authored-by: Saeed Vaziry <mr.saeedvaziry@gmail.com>
253 lines
6.8 KiB
PHP
253 lines
6.8 KiB
PHP
<?php
|
|
|
|
namespace App\SSH\OS;
|
|
|
|
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;
|
|
use Throwable;
|
|
|
|
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 availableUpdates(): int
|
|
{
|
|
$result = $this->server->ssh()->exec(
|
|
$this->getScript('available-updates.sh'),
|
|
'check-available-updates'
|
|
);
|
|
|
|
// -1 because the first line is not a package
|
|
$availableUpdates = str($result)->after('Available updates:')->trim()->toInteger() - 1;
|
|
|
|
return max($availableUpdates, 0);
|
|
}
|
|
|
|
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 createIsolatedUser(string $user, string $password, int $site_id): void
|
|
{
|
|
$this->server->ssh()->exec(
|
|
$this->getScript('create-isolated-user.sh', [
|
|
'user' => $user,
|
|
'server_user' => $this->server->getSshUser(),
|
|
'password' => $password,
|
|
]),
|
|
'create-isolated-user',
|
|
$site_id
|
|
);
|
|
}
|
|
|
|
public function deleteIsolatedUser(string $user): void
|
|
{
|
|
$this->server->ssh()->exec(
|
|
$this->getScript('delete-isolated-user.sh', [
|
|
'user' => $user,
|
|
'server_user' => $this->server->getSshUser(),
|
|
]),
|
|
'delete-isolated-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
|
|
{
|
|
$this->server->ssh()->exec(
|
|
$this->getScript('delete-ssh-key.sh', [
|
|
'key' => $key,
|
|
'user' => $this->server->getSshUser(),
|
|
]),
|
|
'delete-ssh-key'
|
|
);
|
|
}
|
|
|
|
public function generateSSHKey(string $name, ?Site $site = null): void
|
|
{
|
|
$site->server->ssh($site->user)->exec(
|
|
$this->getScript('generate-ssh-key.sh', [
|
|
'name' => $name,
|
|
]),
|
|
'generate-ssh-key',
|
|
$site?->id
|
|
);
|
|
}
|
|
|
|
public function readSSHKey(string $name, ?Site $site = null): string
|
|
{
|
|
return $site->server->ssh($site->user)->exec(
|
|
$this->getScript('read-ssh-key.sh', [
|
|
'name' => $name,
|
|
]),
|
|
);
|
|
}
|
|
|
|
public function reboot(): void
|
|
{
|
|
$this->server->ssh()->exec(
|
|
$this->getScript('reboot.sh'),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @throws SSHUploadFailed
|
|
*/
|
|
public function editFile(string $path, ?string $content = null): void
|
|
{
|
|
$tmpName = Str::random(10).strtotime('now');
|
|
try {
|
|
/** @var FilesystemAdapter $storageDisk */
|
|
$storageDisk = Storage::disk('local');
|
|
$storageDisk->put($tmpName, $content);
|
|
$this->server->ssh()->upload(
|
|
$storageDisk->path($tmpName),
|
|
$path
|
|
);
|
|
} catch (Throwable) {
|
|
throw new SSHUploadFailed;
|
|
} finally {
|
|
$this->deleteTempFile($tmpName);
|
|
}
|
|
}
|
|
|
|
public function readFile(string $path): string
|
|
{
|
|
return $this->server->ssh()->exec(
|
|
$this->getScript('read-file.sh', [
|
|
'path' => $path,
|
|
])
|
|
);
|
|
}
|
|
|
|
public function tail(string $path, int $lines): string
|
|
{
|
|
return $this->server->ssh()->exec(
|
|
$this->getScript('tail.sh', [
|
|
'path' => $path,
|
|
'lines' => $lines,
|
|
])
|
|
);
|
|
}
|
|
|
|
public function runScript(string $path, string $script, ?ServerLog $serverLog, ?string $user = null, ?array $variables = []): ServerLog
|
|
{
|
|
$ssh = $this->server->ssh($user);
|
|
if ($serverLog) {
|
|
$ssh->setLog($serverLog);
|
|
}
|
|
$command = '';
|
|
foreach ($variables as $key => $variable) {
|
|
$command .= "$key=$variable\n";
|
|
}
|
|
$command .= $this->getScript('run-script.sh', [
|
|
'path' => $path,
|
|
'script' => $script,
|
|
]);
|
|
$ssh->exec($command, 'run-script');
|
|
|
|
info($command);
|
|
|
|
return $ssh->log;
|
|
}
|
|
|
|
public function download(string $url, string $path): string
|
|
{
|
|
return $this->server->ssh()->exec(
|
|
$this->getScript('download.sh', [
|
|
'url' => $url,
|
|
'path' => $path,
|
|
])
|
|
);
|
|
}
|
|
|
|
public function unzip(string $path): string
|
|
{
|
|
return $this->server->ssh()->exec(
|
|
'unzip '.$path
|
|
);
|
|
}
|
|
|
|
public function cleanup(): void
|
|
{
|
|
$this->server->ssh()->exec(
|
|
$this->getScript('cleanup.sh'),
|
|
'cleanup'
|
|
);
|
|
}
|
|
|
|
public function resourceInfo(): array
|
|
{
|
|
$info = $this->server->ssh()->exec(
|
|
$this->getScript('resource-info.sh'),
|
|
);
|
|
|
|
return [
|
|
'load' => str($info)->after('load:')->before(PHP_EOL)->toString(),
|
|
'memory_total' => str($info)->after('memory_total:')->before(PHP_EOL)->toString(),
|
|
'memory_used' => str($info)->after('memory_used:')->before(PHP_EOL)->toString(),
|
|
'memory_free' => str($info)->after('memory_free:')->before(PHP_EOL)->toString(),
|
|
'disk_total' => str($info)->after('disk_total:')->before(PHP_EOL)->toString(),
|
|
'disk_used' => str($info)->after('disk_used:')->before(PHP_EOL)->toString(),
|
|
'disk_free' => str($info)->after('disk_free:')->before(PHP_EOL)->toString(),
|
|
];
|
|
}
|
|
|
|
private function deleteTempFile(string $name): void
|
|
{
|
|
if (Storage::disk('local')->exists($name)) {
|
|
Storage::disk('local')->delete($name);
|
|
}
|
|
}
|
|
}
|