2026-10-05 18:10:18 -04:00
<? php
declare ( strict_types = 1 );
namespace Pterodactyl\Actions\Servers ;
use Illuminate\Support\Facades\DB ;
use Illuminate\Support\Facades\Log ;
2026-10-11 08:58:48 -04:00
use Illuminate\Support\Facades\Validator ;
use Illuminate\Validation\Rule ;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Contracts\Servers\UpdatesServerBuild ;
use Pterodactyl\Exceptions\DisplayException ;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException ;
use Pterodactyl\Facades\Daemon ;
use Pterodactyl\Models\Allocation ;
use Pterodactyl\Models\Server ;
final readonly class UpdateServerBuild implements UpdatesServerBuild
{
/**
2026-10-11 23:50:53 -04:00
* Pass the server being updated so its own default allocation is not taken for a clash.
2026-10-11 08:58:48 -04:00
*
* @return NormalizedValidationRules
*/
public static function rules ( ? Server $server = null ) : array
{
$rules = CreateServer :: rules ();
return [
'allocation_id' => [ 'required' , 'bail' , Rule :: unique ( 'servers' , 'allocation_id' ) -> ignore ( $server ), 'exists:allocations,id' ],
'oom_disabled' => [ 'sometimes' , 'boolean' ],
'memory' => $rules [ 'memory' ],
'swap' => $rules [ 'swap' ],
'io' => $rules [ 'io' ],
'cpu' => $rules [ 'cpu' ],
'threads' => $rules [ 'threads' ],
'disk' => $rules [ 'disk' ],
'add_allocations' => [ 'bail' , 'array' , 'list' ],
'add_allocations.*' => [ 'integer' ],
'remove_allocations' => [ 'bail' , 'array' , 'list' ],
'remove_allocations.*' => [ 'integer' ],
'database_limit' => [ 'present' , 'nullable' , 'integer' , 'min:0' ],
'allocation_limit' => $rules [ 'allocation_limit' ],
'backup_limit' => [ 'present' , 'nullable' , 'integer' , 'min:0' ],
];
}
2026-10-11 23:18:58 -04:00
public function update ( Server $server , array $input ) : Server
2026-10-11 08:58:48 -04:00
{
2026-10-11 23:18:58 -04:00
$rules = array_map ( fn ( array $rules ) : array => [ 'sometimes' , ... $rules ], self :: rules ( $server ));
$validator = Validator :: make ( $input , $rules );
$validated = $validator -> safe ();
2026-10-11 08:58:48 -04:00
2026-10-11 23:18:58 -04:00
// Only the fields given change, apart from the feature limits: a database or allocation
// limit left out or blank is cleared, and a backup limit left out becomes 0.
2026-10-11 08:58:48 -04:00
$attributes = [
2026-10-11 23:18:58 -04:00
... $validator -> safe ([ 'oom_disabled' , 'memory' , 'swap' , 'io' , 'cpu' , 'threads' , 'disk' ]),
'database_limit' => $validated -> filled ( 'database_limit' ) ? $validated [ 'database_limit' ] : null ,
'allocation_limit' => $validated -> filled ( 'allocation_limit' ) ? $validated [ 'allocation_limit' ] : null ,
'backup_limit' => $validated [ 'backup_limit' ] ?? 0 ,
2026-10-11 08:58:48 -04:00
];
2026-10-11 23:18:58 -04:00
if ( $validated -> has ( 'threads' ) && ! $validated -> filled ( 'threads' )) {
$attributes [ 'threads' ] = null ;
2026-10-11 08:58:48 -04:00
}
2026-10-11 23:18:58 -04:00
// Allocations are matched by id, so they are read as integers.
if ( $validated -> has ( 'allocation_id' )) {
$attributes [ 'allocation_id' ] = $validated -> integer ( 'allocation_id' );
2026-10-11 08:58:48 -04:00
}
2026-10-11 23:18:58 -04:00
$add = array_map ( fn ( int | string $key ) : int => $validated -> integer ( 'add_allocations.' . $key ), array_keys ( $validated -> array ( 'add_allocations' )));
$remove = array_map ( fn ( int | string $key ) : int => $validated -> integer ( 'remove_allocations.' . $key ), array_keys ( $validated -> array ( 'remove_allocations' )));
2026-10-11 08:58:48 -04:00
2026-10-05 18:10:18 -04:00
// Allocation assignments and server attributes must change together.
2026-10-11 23:18:58 -04:00
$server = DB :: transaction ( function () use ( $server , $attributes , $add , $remove ) : Server {
$this -> processAllocations ( $server , $add , $remove , $attributes );
2026-10-11 08:58:48 -04:00
if ( isset ( $attributes [ 'allocation_id' ]) && $attributes [ 'allocation_id' ] !== $server -> allocation_id ) {
$allocationExists = Allocation :: query () -> where ( 'id' , $attributes [ 'allocation_id' ])
2026-10-05 18:10:18 -04:00
-> where ( 'server_id' , $server -> id ) -> exists ();
throw_if ( ! $allocationExists , DisplayException :: class , 'The requested default allocation is not currently assigned to this server.' );
}
$server -> forceFill ( $attributes ) -> saveOrFail ();
return $server -> refresh ();
});
2026-10-12 00:21:31 -04:00
try {
Daemon :: server ( $server ) -> sync ();
} catch ( DaemonConnectionException $daemonConnectionException ) {
// Wings fetches fresh Panel configuration when the server boots, so a
// failed live sync can be logged without undoing the committed change.
Log :: warning ( $daemonConnectionException -> getMessage (), [ 'exception' => $daemonConnectionException , 'server_id' => $server -> id ]);
2026-10-05 18:10:18 -04:00
}
return $server ;
}
2026-10-11 23:18:58 -04:00
/**
* @param list<int> $add
* @param list<int> $remove
* @param array<string, mixed> $attributes
*/
private function processAllocations ( Server $server , array $add , array $remove , array & $attributes ) : void
2026-10-05 18:10:18 -04:00
{
2026-10-11 23:18:58 -04:00
if ( $add === [] && $remove === []) {
2026-10-05 18:10:18 -04:00
return ;
}
2026-10-11 23:18:58 -04:00
if ( $add !== []) {
2026-10-05 18:10:18 -04:00
// Assign only currently unassigned allocations on this server's node.
2026-10-09 14:41:44 +01:00
$assignable = Allocation :: query () -> where ( 'node_id' , $server -> node_id )
2026-10-11 23:18:58 -04:00
-> whereIn ( 'id' , $add ) -> whereNull ( 'server_id' )
2026-10-09 14:41:44 +01:00
-> orderBy ( 'id' ) -> lockForUpdate () -> get ([ 'id' ]);
Allocation :: query () -> whereKey ( $assignable -> modelKeys ()) -> update ([ 'server_id' => $server -> id , 'notes' => null ]);
2026-10-05 18:10:18 -04:00
// If the default allocation is removed below, use the first newly assigned
// allocation as its replacement.
2026-10-09 14:41:44 +01:00
$freshlyAllocated = $assignable -> first () ?-> id ;
2026-10-05 18:10:18 -04:00
}
2026-10-11 23:18:58 -04:00
if ( $remove !== []) {
foreach ( $remove as $allocation ) {
2026-10-11 08:58:48 -04:00
if ( $allocation === ( $attributes [ 'allocation_id' ] ?? $server -> allocation_id )) {
2026-10-05 18:10:18 -04:00
throw_if ( empty ( $freshlyAllocated ), DisplayException :: class , 'You are attempting to delete the default allocation for this server but there is no fallback allocation to use.' );
2026-10-11 08:58:48 -04:00
$attributes [ 'allocation_id' ] = $freshlyAllocated ;
2026-10-05 18:10:18 -04:00
}
}
// Release allocations after clearing notes so they do not leak to a future server.
// Do not remove an allocation also requested in add_allocations.
Allocation :: query () -> where ( 'node_id' , $server -> node_id )
-> where ( 'server_id' , $server -> id )
2026-10-11 23:18:58 -04:00
-> whereIn ( 'id' , array_diff ( $remove , $add ))
2026-10-05 18:10:18 -04:00
-> update ([ 'notes' => null , 'server_id' => null ]);
}
}
}