Add build configuration to server management.

Allows modification of certain settings, as well as assigning
additional IP addresses and ports.
This commit is contained in:
Dane Everitt 2016-01-02 23:21:22 -05:00
parent 2c054e7edc
commit 4953608aee
4 changed files with 352 additions and 17 deletions

View file

@ -50,20 +50,24 @@ class ServersController extends Controller
public function getView(Request $request, $id)
{
$server = Models\Server::select(
'servers.*',
'nodes.name as a_nodeName',
'users.email as a_ownerEmail',
'locations.long as a_locationName',
'services.name as a_serviceName',
'service_options.name as a_servceOptionName'
)->join('nodes', 'servers.node', '=', 'nodes.id')
->join('users', 'servers.owner', '=', 'users.id')
->join('locations', 'nodes.location', '=', 'locations.id')
->join('services', 'servers.service', '=', 'services.id')
->join('service_options', 'servers.option', '=', 'service_options.id')
->first();
return view('admin.servers.view', [
'server' => Models\Server::select(
'servers.*',
'nodes.name as a_nodeName',
'users.email as a_ownerEmail',
'locations.long as a_locationName',
'services.name as a_serviceName',
'service_options.name as a_servceOptionName'
)->join('nodes', 'servers.node', '=', 'nodes.id')
->join('users', 'servers.owner', '=', 'users.id')
->join('locations', 'nodes.location', '=', 'locations.id')
->join('services', 'servers.service', '=', 'services.id')
->join('service_options', 'servers.option', '=', 'service_options.id')
->first()
'server' => $server,
'assigned' => Models\Allocation::select('id', 'ip', 'port')->where('assigned_to', $id)->orderBy('ip', 'asc')->orderBy('port', 'asc')->get(),
'unassigned' => Models\Allocation::select('id', 'ip', 'port')->where('node', $server->node)->whereNull('assigned_to')->orderBy('ip', 'asc')->orderBy('port', 'asc')->get()
]);
}
@ -225,4 +229,63 @@ class ServersController extends Controller
}
}
public function postUpdateServerToggleBuild(Request $request, $id) {
$server = Models\Server::findOrFail($id);
$node = Models\Node::findOrFail($server->node);
$client = Models\Node::guzzleRequest($server->node);
try {
$res = $client->request('POST', '/server/rebuild', [
'headers' => [
'X-Access-Server' => $server->uuid,
'X-Access-Token' => $node->daemonSecret
]
]);
Alert::success('A rebuild has been queued successfully. It will run the next time this server is booted.')->flash();
} catch (\GuzzleHttp\Exception\TransferException $ex) {
Log::warning($ex);
Alert::danger('An error occured while attempting to toggle a rebuild: ' . $ex->getMessage())->flash();
}
return redirect()->route('admin.servers.view', [
'id' => $id,
'tab' => 'tab_manage'
]);
}
public function postUpdateServerUpdateBuild(Request $request, $id)
{
try {
$server = new ServerRepository;
$server->changeBuild($id, [
'default' => $request->input('default'),
'add_additional' => $request->input('add_additional'),
'remove_additional' => $request->input('remove_additional'),
'memory' => $request->input('memory'),
'swap' => $request->input('swap'),
'io' => $request->input('io'),
'cpu' => $request->input('cpu'),
]);
Alert::success('Server details were successfully updated.')->flash();
} catch (\Exception $e) {
if ($e instanceof \Pterodactyl\Exceptions\DisplayValidationException) {
return redirect()->route('admin.servers.view', [
'id' => $id,
'tab' => 'tab_build'
])->withErrors(json_decode($e->getMessage()))->withInput();
} else if ($e instanceof \Pterodactyl\Exceptions\DisplayException) {
Alert::danger($e->getMessage())->flash();
} else {
Log::error($e);
Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash();
}
}
return redirect()->route('admin.servers.view', [
'id' => $id,
'tab' => 'tab_build'
]);
}
}

View file

@ -31,6 +31,8 @@ class AdminRoutes {
$router->get('/view/{id}', [ 'as' => 'admin.servers.view', 'uses' => 'Admin\ServersController@getView' ]);
$router->post('/view/{id}/details', [ 'uses' => 'Admin\ServersController@postUpdateServerDetails' ]);
$router->post('/view/{id}/rebuild', [ 'uses' => 'Admin\ServersController@postUpdateServerToggleBuild' ]);
$router->post('/view/{id}/build', [ 'uses' => 'Admin\ServersController@postUpdateServerUpdateBuild' ]);
$router->post('/new', [ 'uses' => 'Admin\ServersController@postNewServer']);
$router->post('/new/get-nodes', [ 'uses' => 'Admin\ServersController@postNewServerGetNodes' ]);

View file

@ -226,7 +226,7 @@ class ServerRepository
// Validate Fields
$validator = Validator::make($data, [
'owner' => 'email|exists:users,email',
'name' => 'regex:([\w -]{4,35})'
'name' => 'regex:^([\w -]{4,35})$'
]);
// Run validator, throw catchable and displayable exception if it fails.
@ -304,11 +304,169 @@ class ServerRepository
throw new DisplayException('Daemon returned a a non HTTP/204 error code. HTTP/' + $res->getStatusCode());
}
} catch (\Exception $ex) {
DB::rollback();
DB::rollBack();
Log::error($ex);
throw new DisplayException('An error occured while attempting to update this server\'s information.');
}
}
/**
* [changeBuild description]
* @param integer $id
* @param array $data
* @return boolean
*/
public function changeBuild($id, array $data)
{
$validator = Validator::make($data, [
'default' => [
'string',
'regex:/^(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5])):(\d{1,5})$/'
],
'add_additional' => 'array',
'remove_additional' => 'array',
'memory' => 'integer|min:0',
'swap' => 'integer|min:0',
'io' => 'integer|min:10|max:1000',
'cpu' => 'integer|min:0',
'disk' => 'integer|min:0'
]);
// Run validator, throw catchable and displayable exception if it fails.
// Exception includes a JSON result of failed validation rules.
if ($validator->fails()) {
throw new DisplayValidationException($validator->errors());
}
DB::beginTransaction();
$server = Models\Server::findOrFail($id);
if (isset($data['default'])) {
list($ip, $port) = explode(':', $data['default']);
if ($ip === $server->ip && $port === $server->port) {
continue;
}
$allocation = Models\Allocation::where('ip', $ip)->where('port', $port)->where('assigned_to', $server->id)->get();
if (!$allocation) {
throw new DisplayException('The assigned default connection (' . $ip . ':' . $prot . ') is not allocated to this server.');
}
$server->ip = $ip;
$server->port = $port;
}
// Remove Assignments
if (isset($data['remove_additional'])) {
foreach ($data['remove_additional'] as $id => $combo) {
list($ip, $port) = explode(':', $combo);
// Invalid, not worth killing the whole thing, we'll just skip over it.
if (!filter_var($ip, FILTER_VALIDATE_IP) || !preg_match('/^(\d{1,5})$/', $port)) {
continue;
}
// Can't remove the assigned IP/Port combo
if ($ip === $server->ip && $port === $server->port) {
continue;
}
Models\Allocation::where('ip', $ip)->where('port', $port)->where('assigned_to', $server->id)->update([
'assigned_to' => null
]);
}
}
// Add Assignments
if (isset($data['add_additional'])) {
foreach ($data['add_additional'] as $id => $combo) {
list($ip, $port) = explode(':', $combo);
// Invalid, not worth killing the whole thing, we'll just skip over it.
if (!filter_var($ip, FILTER_VALIDATE_IP) || !preg_match('/^(\d{1,5})$/', $port)) {
continue;
}
// Don't allow double port assignments
if (Models\Allocation::where('port', $port)->where('assigned_to', $server->id)->count() !== 0) {
continue;
}
Models\Allocation::where('ip', $ip)->where('port', $port)->whereNull('assigned_to')->update([
'assigned_to' => $server->id
]);
}
}
// Loop All Assignments
$additionalAssignments = [];
$assignments = Models\Allocation::where('assigned_to', $server->id)->get();
foreach ($assignments as &$assignment) {
if (array_key_exists((string) $assignment->ip, $additionalAssignments)) {
array_push($additionalAssignments[ (string) $assignment->ip ], (int) $assignment->port);
} else {
$additionalAssignments[ (string) $assignment->ip ] = [ (int) $assignment->port ];
}
}
// @TODO: verify that server can be set to this much memory without
// going over node limits.
if (isset($data['memory'])) {
$server->memory = $data['memory'];
}
if (isset($data['swap'])) {
$server->swap = $data['swap'];
}
// @TODO: verify that server can be set to this much disk without
// going over node limits.
if (isset($data['disk'])) {
$server->disk = $data['disk'];
}
if (isset($data['cpu'])) {
$server->cpu = $data['cpu'];
}
if (isset($data['io'])) {
$server->io = $data['io'];
}
try {
$node = Models\Node::getByID($server->node);
$client = Models\Node::guzzleRequest($server->node);
$client->request('PATCH', '/server', [
'headers' => [
'X-Access-Server' => $server->uuid,
'X-Access-Token' => $node->daemonSecret
],
'json' => [
'build' => [
'default' => [
'ip' => $server->ip,
'port' => (int) $server->port
],
'ports|overwrite' => $additionalAssignments,
'memory' => (int) $server->memory,
'swap' => (int) $server->swap,
'io' => (int) $server->io,
'cpu' => (int) $server->cpu,
'disk' => (int) $server->disk
]
]
]);
$server->save();
DB::commit();
return true;
} catch (\GuzzleHttp\Exception\TransferException $ex) {
DB::rollBack();
throw new DisplayException('An error occured while attempting to update the configuration: ' . $ex->getMessage());
}
}
}