Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions app/Filament/Admin/Resources/Servers/Pages/EditServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
use App\Filament\Components\StateCasts\ServerConditionStateCast;
use App\Filament\Server\Pages\Console;
use App\Models\Allocation;
use App\Models\Backup;
use App\Models\Egg;
use App\Models\Server;
use App\Models\User;
use App\Repositories\Daemon\DaemonServerRepository;
use App\Services\Backups\DeleteBackupService;
use App\Services\Eggs\EggChangerService;
use App\Services\Servers\RandomWordService;
use App\Services\Servers\ReinstallServerService;
Expand All @@ -27,6 +29,7 @@
use Exception;
use Filament\Actions\Action;
use Filament\Actions\ActionGroup;
use Filament\Forms\Components\CheckboxList;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\KeyValue;
Expand All @@ -48,6 +51,7 @@
use Filament\Schemas\Components\StateCasts\BooleanStateCast;
use Filament\Schemas\Components\Tabs;
use Filament\Schemas\Components\Tabs\Tab;
use Filament\Schemas\Components\Text;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Components\Utilities\Set;
use Filament\Schemas\Schema;
Expand Down Expand Up @@ -961,9 +965,25 @@ public function form(Schema $schema): Schema
->disabled(fn (Server $server) => user()?->accessibleNodes()->count() <= 1 || $server->isInConflictState())
->modalHeading(trans('admin/server.transfer'))
->schema($this->transferServer())
->action(function (TransferServerService $transfer, Server $server, $data) {
->action(function (TransferServerService $transfer, DeleteBackupService $deleteBackup, Server $server, $data) {
try {
$transfer->handle($server, Arr::get($data, 'node_id'), Arr::get($data, 'allocation_id'), Arr::get($data, 'allocation_additional', []));
$selectedBackupUuids = Arr::get($data, 'backups', []);

$server->backups
->whereNotIn('uuid', $selectedBackupUuids)
->each(function ($backup) use ($deleteBackup) {
try {
$deleteBackup->handle($backup);
} catch (Exception $exception) {
Notification::make()
->title(trans('admin/server.notifications.backup_transfer_failed'))
->body($exception->getMessage())
->danger()
->send();
}
});

$transfer->handle($server, Arr::get($data, 'node_id'), Arr::get($data, 'allocation_id'), Arr::get($data, 'allocation_additional', []), $selectedBackupUuids);

Notification::make()
->title(trans('admin/server.notifications.transfer_started'))
Expand Down Expand Up @@ -1053,6 +1073,19 @@ protected function transferServer(): array
->options(fn (Get $get) => Allocation::where('node_id', $get('node_id'))->whereNull('server_id')->when($get('allocation_id'), fn ($query) => $query->whereNot('id', $get('allocation_id')))->get()->mapWithKeys(fn (Allocation $allocation) => [$allocation->id => $allocation->address]))
->searchable(['ip', 'port', 'ip_alias'])
->placeholder(trans('admin/server.select_additional')),
Grid::make()
->columnSpanFull()
->schema([
CheckboxList::make('backups')
->label(trans('admin/server.backups'))
->bulkToggleable()
->options(fn (Server $server) => $server->backups->where('disk', Backup::ADAPTER_DAEMON)->mapWithKeys(fn ($backup) => [$backup->uuid => $backup->name]))
->columns(fn (Server $record) => (int) ceil($record->backups->where('disk', Backup::ADAPTER_DAEMON)->count() / 4)),
Text::make('backup_helper')
->columnSpanFull()
->content(trans('admin/server.warning_backups')),
])
->hidden(fn (Server $server) => $server->backups->where('disk', Backup::ADAPTER_DAEMON)->count() === 0),
];
}

Expand Down
16 changes: 13 additions & 3 deletions app/Services/Servers/TransferServerService.php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The backup uuids should be added to the ServerTransfer model.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Services\Servers;

use App\Models\Allocation;
use App\Models\Backup;
use App\Models\Node;
use App\Models\Server;
use App\Models\ServerTransfer;
Expand All @@ -23,11 +24,19 @@ public function __construct(
private NodeJWTService $nodeJWTService,
) {}

private function notify(ServerTransfer $transfer, UnencryptedToken $token): void
/**
* @param string[] $backup_uuids
*/
private function notify(ServerTransfer $transfer, UnencryptedToken $token, array $backup_uuids = []): void
{
$backups = [];
if (config('backups.default') === Backup::ADAPTER_DAEMON) {
$backups = $backup_uuids;
}
Http::daemon($transfer->oldNode)->post("/api/servers/{$transfer->server->uuid}/transfer", [
'url' => $transfer->newNode->getConnectionAddress() . '/api/transfers',
'token' => 'Bearer ' . $token->toString(),
'backups' => $backups,
'server' => [
'uuid' => $transfer->server->uuid,
'start_on_completion' => false,
Expand All @@ -39,10 +48,11 @@ private function notify(ServerTransfer $transfer, UnencryptedToken $token): void
* Starts a transfer of a server to a new node.
*
* @param int[] $additional_allocations
* @param string[] $backup_uuid
*
* @throws Throwable
*/
public function handle(Server $server, int $node_id, ?int $allocation_id = null, ?array $additional_allocations = []): bool
public function handle(Server $server, int $node_id, ?int $allocation_id = null, ?array $additional_allocations = [], ?array $backup_uuid = []): bool
{
$additional_allocations = array_map(intval(...), $additional_allocations);

Expand Down Expand Up @@ -93,7 +103,7 @@ public function handle(Server $server, int $node_id, ?int $allocation_id = null,
->handle($transfer->newNode, $server->uuid, 'sha256');

// Notify the source node of the pending outgoing transfer.
$this->notify($transfer, $token);
$this->notify($transfer, $token, $backup_uuid);

return true;
}
Expand Down
3 changes: 3 additions & 0 deletions lang/en/admin/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@
'add_allocation' => 'Add Allocation',
'view' => 'View',
'no_log' => 'No Log Available',
'select_backups' => 'Select Backups',
'warning_backups' => 'Be aware, not transferred backups will be deleted.',
'tabs' => [
'information' => 'Information',
'egg_configuration' => 'Egg Configuration',
Expand Down Expand Up @@ -143,6 +145,7 @@
'transfer_started' => 'Transfer started',
'transfer_failed' => 'Transfer failed',
'already_transfering' => 'Server is currently being transferred.',
'backup_transfer_failed' => 'Backup Transfer Failed',
],
'notes' => 'Notes',
'no_notes' => 'No Notes',
Expand Down