Skip to content

Commit 9343ac7

Browse files
committed
add more default variables, closes pterodactyl#427
1 parent a52d9eb commit 9343ac7

File tree

1 file changed

+46
-21
lines changed

1 file changed

+46
-21
lines changed

app/Repositories/ServerRepository.php

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -297,22 +297,17 @@ public function create(array $data)
297297
}
298298
}
299299

300-
// Add Variables
301-
$environmentVariables = [
302-
'STARTUP' => $data['startup'],
303-
];
304-
305300
foreach ($variableList as $item) {
306-
$environmentVariables[$item['env']] = $item['val'];
307-
308301
ServerVariable::create([
309302
'server_id' => $server->id,
310303
'variable_id' => $item['id'],
311304
'variable_value' => $item['val'],
312305
]);
313306
}
314307

308+
$environment = $this->parseVariables($server);
315309
$server->load('allocation', 'allocations');
310+
316311
$node->guzzleClient(['X-Access-Token' => $node->daemonSecret])->request('POST', '/servers', [
317312
'json' => [
318313
'uuid' => (string) $server->uuid,
@@ -325,7 +320,7 @@ public function create(array $data)
325320
'ports' => $server->allocations->groupBy('ip')->map(function ($item) {
326321
return $item->pluck('port');
327322
})->toArray(),
328-
'env' => $environmentVariables,
323+
'env' => $environment->pluck('value', 'variable')->toArray(),
329324
'memory' => (int) $server->memory,
330325
'swap' => (int) $server->swap,
331326
'io' => (int) $server->io,
@@ -576,6 +571,8 @@ public function changeBuild($id, array $data)
576571
$newBuild['ports|overwrite'] = $server->allocations->groupBy('ip')->map(function ($item) {
577572
return $item->pluck('port');
578573
})->toArray();
574+
575+
$newBuild['env|overwrite'] = $this->parseVariables($server)->pluck('value', 'variable')->toArray();
579576
}
580577

581578
// @TODO: verify that server can be set to this much memory without
@@ -632,20 +629,15 @@ public function changeBuild($id, array $data)
632629
}
633630

634631
/**
635-
* Update the service configuration for a server.
632+
* Process the variables for a server, and save to the database.
636633
*
637-
* @param int $id
638-
* @param array $data
639-
* @return void
634+
* @param \Pterodactyl\Models\Server $server
635+
* @param array $data
636+
* @param bool $admin
637+
* @return \Illuminate\Support\Collection
640638
*
641-
* @throws \GuzzleHttp\Exception\RequestException
642-
* @throws \Pterodactyl\Exceptions\DisplayException
643639
* @throws \Pterodactyl\Exceptions\DisplayValidationException
644640
*/
645-
protected function changeService($id, array $data)
646-
{
647-
}
648-
649641
protected function processVariables(Server $server, $data, $admin = false)
650642
{
651643
$server->load('option.variables');
@@ -698,17 +690,50 @@ protected function processVariables(Server $server, $data, $admin = false)
698690
}
699691
}
700692

693+
return $this->parseVariables($server);
694+
}
695+
696+
/**
697+
* Parse the variables and return in a standardized format.
698+
*
699+
* @param \Pterodactyl\Models\Server $server
700+
* @return \Illuminate\Support\Collection
701+
*/
702+
protected function parseVariables(Server $server)
703+
{
701704
// Reload Variables
702705
$server->load('variables');
703706

704-
return $server->option->variables->map(function ($item, $key) use ($server) {
707+
$parsed = $server->option->variables->map(function ($item, $key) use ($server) {
705708
$display = $server->variables->where('variable_id', $item->id)->pluck('variable_value')->first();
706709

707710
return [
708711
'variable' => $item->env_variable,
709712
'value' => (! is_null($display)) ? $display : $item->default_value,
710713
];
711714
});
715+
716+
$merge = [[
717+
'variable' => 'STARTUP',
718+
'value' => $server->startup,
719+
], [
720+
'variable' => 'P_VARIABLE__LOCATION',
721+
'value' => $server->location->short,
722+
]];
723+
724+
$allocations = $server->allocations->where('id', '!=', $server->allocation_id);
725+
$i = 0;
726+
727+
foreach($allocations as $allocation) {
728+
$merge[] = [
729+
'variable' => 'ALLOC_' . $i . '__PORT',
730+
'value' => $allocation->port,
731+
];
732+
733+
$i++;
734+
}
735+
736+
return $parsed->merge($merge);
712737
}
713738

714739
/**
@@ -767,7 +792,7 @@ public function updateStartup($id, array $data, $admin = false)
767792
])->request('PATCH', '/server', [
768793
'json' => [
769794
'build' => [
770-
'env|overwrite' => $environment->pluck('value', 'variable')->merge(['STARTUP' => $server->startup])->toArray(),
795+
'env|overwrite' => $environment->pluck('value', 'variable')->toArray(),
771796
],
772797
],
773798
]);
@@ -816,7 +841,7 @@ public function updateStartup($id, array $data, $admin = false)
816841
])->request('POST', '/server/reinstall', [
817842
'json' => [
818843
'build' => [
819-
'env|overwrite' => $environment->pluck('value', 'variable')->merge(['STARTUP' => $server->startup])->toArray(),
844+
'env|overwrite' => $environment->pluck('value', 'variable')->toArray(),
820845
],
821846
'service' => [
822847
'type' => $server->option->service->folder,

0 commit comments

Comments
 (0)