mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-02 14:36:17 +00:00
Feature/add remote server logs (#159)
This commit is contained in:
35
app/Actions/Server/CreateServerLog.php
Executable file
35
app/Actions/Server/CreateServerLog.php
Executable file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Server;
|
||||
|
||||
use App\Models\Server;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class CreateServerLog
|
||||
{
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function create(Server $server, array $input): void
|
||||
{
|
||||
$this->validate($input);
|
||||
|
||||
$server->logs()->create([
|
||||
'is_remote' => true,
|
||||
'name' => $input['path'],
|
||||
'type' => 'remote',
|
||||
'disk' => 'ssh',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
protected function validate(array $input): void
|
||||
{
|
||||
Validator::make($input, [
|
||||
'path' => 'required',
|
||||
])->validate();
|
||||
}
|
||||
}
|
@ -54,7 +54,6 @@ public function show(Server $server): View
|
||||
{
|
||||
return view('servers.show', [
|
||||
'server' => $server,
|
||||
'logs' => $server->logs()->latest()->limit(10)->get(),
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -2,10 +2,13 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Actions\Server\CreateServerLog;
|
||||
use App\Facades\Toast;
|
||||
use App\Models\Server;
|
||||
use App\Models\ServerLog;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ServerLogController extends Controller
|
||||
{
|
||||
@ -13,6 +16,7 @@ public function index(Server $server): View
|
||||
{
|
||||
return view('server-logs.index', [
|
||||
'server' => $server,
|
||||
'pageTitle' => __('Vito Logs'),
|
||||
]);
|
||||
}
|
||||
|
||||
@ -26,4 +30,31 @@ public function show(Server $server, ServerLog $serverLog): RedirectResponse
|
||||
'content' => $serverLog->getContent(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function remote(Server $server): View
|
||||
{
|
||||
return view('server-logs.remote-logs', [
|
||||
'server' => $server,
|
||||
'remote' => true,
|
||||
'pageTitle' => __('Remote Logs'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(Server $server, Request $request): \App\Helpers\HtmxResponse
|
||||
{
|
||||
app(CreateServerLog::class)->create($server, $request->input());
|
||||
|
||||
Toast::success('Log added successfully.');
|
||||
|
||||
return htmx()->redirect(route('servers.logs.remote', ['server' => $server]));
|
||||
}
|
||||
|
||||
public function destroy(Server $server, ServerLog $serverLog): RedirectResponse
|
||||
{
|
||||
$serverLog->delete();
|
||||
|
||||
Toast::success('Remote log deleted successfully.');
|
||||
|
||||
return redirect()->route('servers.logs.remote', ['server' => $server]);
|
||||
}
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ public function index(Server $server, Site $site): View
|
||||
return view('site-logs.index', [
|
||||
'server' => $server,
|
||||
'site' => $site,
|
||||
'pageTitle' => __('Vito Logs'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@ -27,11 +27,13 @@ class ServerLog extends AbstractModel
|
||||
'type',
|
||||
'name',
|
||||
'disk',
|
||||
'is_remote',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'server_id' => 'integer',
|
||||
'site_id' => 'integer',
|
||||
'is_remote' => 'boolean',
|
||||
];
|
||||
|
||||
public static function boot(): void
|
||||
@ -64,6 +66,17 @@ public function site(): BelongsTo
|
||||
return $this->belongsTo(Site::class);
|
||||
}
|
||||
|
||||
public static function getRemote($query, bool $active = true, ?Site $site = null)
|
||||
{
|
||||
$query->where('is_remote', $active);
|
||||
|
||||
if ($site) {
|
||||
$query->where('name', 'like', $site->path.'%');
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
public function write($buf): void
|
||||
{
|
||||
if (Str::contains($buf, 'VITO_SSH_ERROR')) {
|
||||
@ -78,6 +91,10 @@ public function write($buf): void
|
||||
|
||||
public function getContent(): ?string
|
||||
{
|
||||
if ($this->is_remote) {
|
||||
return $this->server->os()->readFile($this->name, 150);
|
||||
}
|
||||
|
||||
if (Storage::disk($this->disk)->exists($this->name)) {
|
||||
return Storage::disk($this->disk)->get($this->name);
|
||||
}
|
||||
|
@ -108,12 +108,18 @@ public function editFile(string $path, ?string $content = null): void
|
||||
);
|
||||
}
|
||||
|
||||
public function readFile(string $path): string
|
||||
public function readFile(string $path, ?int $lastLines = null): string
|
||||
{
|
||||
$params = [
|
||||
'path' => $path,
|
||||
];
|
||||
|
||||
if ($lastLines !== null) {
|
||||
$params['lines'] = $lastLines;
|
||||
}
|
||||
|
||||
return $this->server->ssh()->exec(
|
||||
$this->getScript('read-file.sh', [
|
||||
'path' => $path,
|
||||
])
|
||||
$this->getScript('read-file.sh', $params)
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -1 +1,7 @@
|
||||
[ -f __path__ ] && cat __path__
|
||||
if [ -f __path__ ]; then
|
||||
if [ -n __lines__ ]; then
|
||||
sudo tail -n __lines__ __path__
|
||||
else
|
||||
sudo cat __path__
|
||||
fi
|
||||
fi
|
||||
|
Reference in New Issue
Block a user