API model updates, as well as general model updates and code fixes.

This commit is contained in:
Dane Everitt 2017-02-10 20:26:38 -05:00
parent 8dc1f41b73
commit 32a1dc17ed
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
16 changed files with 162 additions and 272 deletions

View file

@ -49,11 +49,12 @@ class LocationController extends BaseController
*/ */
public function lists(Request $request) public function lists(Request $request)
{ {
return Location::select('locations.*', DB::raw('GROUP_CONCAT(nodes.id) as nodes')) return Location::with('nodes')->get()->map(function ($item) {
->join('nodes', 'locations.id', '=', 'nodes.location') $item->nodes->transform(function ($item) {
->groupBy('locations.id') return collect($item)->only(['id', 'name', 'fqdn', 'scheme', 'daemonListen', 'daemonSFTP']);
->get()->each(function ($location) { });
$location->nodes = explode(',', $location->nodes);
})->all(); return $item;
})->toArray();
} }
} }

View file

@ -24,6 +24,7 @@
namespace Pterodactyl\Http\Controllers\API; namespace Pterodactyl\Http\Controllers\API;
use Log;
use Pterodactyl\Models; use Pterodactyl\Models;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Dingo\Api\Exception\ResourceException; use Dingo\Api\Exception\ResourceException;
@ -96,15 +97,21 @@ class NodeController extends BaseController
public function create(Request $request) public function create(Request $request)
{ {
try { try {
$node = new NodeRepository; $repo = new NodeRepository;
$new = $node->create($request->all()); $node = $repo->create($request->only([
'name', 'location_id', 'public', 'fqdn',
'scheme', 'memory', 'memory_overallocate',
'disk', 'disk_overallocate', 'daemonBase',
'daemonSFTP', 'daemonListen',
]));
return ['id' => $new]; return ['id' => $repo->id];
} catch (DisplayValidationException $ex) { } catch (DisplayValidationException $ex) {
throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true)); throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true));
} catch (DisplayException $ex) { } catch (DisplayException $ex) {
throw new ResourceException($ex->getMessage()); throw new ResourceException($ex->getMessage());
} catch (\Exception $e) { } catch (\Exception $ex) {
Log::error($ex);
throw new BadRequestHttpException('There was an error while attempting to add this node to the system.'); throw new BadRequestHttpException('There was an error while attempting to add this node to the system.');
} }
} }
@ -124,88 +131,35 @@ class NodeController extends BaseController
*/ */
public function view(Request $request, $id, $fields = null) public function view(Request $request, $id, $fields = null)
{ {
$node = Models\Node::where('id', $id); $node = Models\Node::with('allocations')->where('id', $id)->first();
if (! $node) {
throw new NotFoundHttpException('No node by that ID was found.');
}
$node->allocations->transform(function ($item) {
return collect($item)->only([
'id', 'ip', 'ip_alias', 'port', 'server_id'
]);
});
if (! is_null($request->input('fields'))) { if (! is_null($request->input('fields'))) {
foreach (explode(',', $request->input('fields')) as $field) { $fields = explode(',', $request->input('fields'));
if (! empty($field)) { if (! empty($fields) && is_array($fields)) {
$node->addSelect($field); return collect($node)->only($fields);
}
} }
} }
try { return $node;
if (! $node->first()) {
throw new NotFoundHttpException('No node by that ID was found.');
}
return [
'node' => $node->first(),
'allocations' => [
'assigned' => Models\Allocation::where('node', $id)->whereNotNull('assigned_to')->get(),
'unassigned' => Models\Allocation::where('node', $id)->whereNull('assigned_to')->get(),
],
];
} catch (NotFoundHttpException $ex) {
throw $ex;
} catch (\Exception $ex) {
throw new BadRequestHttpException('There was an issue with the fields passed in the request.');
}
} }
public function config(Request $request, $id) public function config(Request $request, $id)
{ {
if (! $request->secure()) {
throw new BadRequestHttpException('This API route can only be accessed using a secure connection.');
}
$node = Models\Node::where('id', $id)->first(); $node = Models\Node::where('id', $id)->first();
if (! $node) { if (! $node) {
throw new NotFoundHttpException('No node by that ID was found.'); throw new NotFoundHttpException('No node by that ID was found.');
} }
return [ return $node->getConfigurationAsJson();
'web' => [
'listen' => $node->daemonListen,
'host' => '0.0.0.0',
'ssl' => [
'enabled' => ($node->scheme === 'https'),
'certificate' => '/etc/certs/' . $node->fqdn . '/fullchain.pem',
'key' => '/etc/certs/' . $node->fqdn . '/privkey.pem',
],
],
'docker' => [
'socket' => '/var/run/docker.sock',
'autoupdate_images' => true,
],
'sftp' => [
'path' => $node->daemonBase,
'port' => (int) $node->daemonSFTP,
'container' => 'ptdl-sftp',
],
'query' => [
'kill_on_fail' => true,
'fail_limit' => 5,
],
'logger' => [
'path' => 'logs/',
'src' => false,
'level' => 'info',
'period' => '1d',
'count' => 3,
],
'remote' => [
'base' => config('app.url'),
'download' => route('remote.download'),
'installed' => route('remote.install'),
],
'uploads' => [
'size_limit' => $node->upload_size,
],
'keys' => [
$node->daemonSecret,
],
];
} }
/** /**
@ -219,12 +173,7 @@ class NodeController extends BaseController
*/ */
public function allocations(Request $request) public function allocations(Request $request)
{ {
$allocations = Models\Allocation::all(); return Models\Allocation::all()->toArray();
if ($allocations->count() < 1) {
throw new NotFoundHttpException('No allocations have been created.');
}
return $allocations;
} }
/** /**
@ -238,18 +187,7 @@ class NodeController extends BaseController
*/ */
public function allocationsView(Request $request, $id) public function allocationsView(Request $request, $id)
{ {
$query = Models\Allocation::where('assigned_to', $id)->get(); return Models\Allocation::where('assigned_to', $id)->get()->toArray();
try {
if (empty($query)) {
throw new NotFoundHttpException('No allocations for that server were found.');
}
return $query;
} catch (NotFoundHttpException $ex) {
throw $ex;
} catch (\Exception $ex) {
throw new BadRequestHttpException('There was an issue with the fields passed in the request.');
}
} }
/** /**

View file

@ -72,10 +72,10 @@ class ServerController extends BaseController
public function create(Request $request) public function create(Request $request)
{ {
try { try {
$server = new ServerRepository; $repo = new ServerRepository;
$new = $server->create($request->all()); $server = $repo->create($request->all());
return ['id' => $new]; return ['id' => $server->id];
} catch (DisplayValidationException $ex) { } catch (DisplayValidationException $ex) {
throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true)); throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true));
} catch (DisplayException $ex) { } catch (DisplayException $ex) {
@ -101,58 +101,38 @@ class ServerController extends BaseController
*/ */
public function view(Request $request, $id) public function view(Request $request, $id)
{ {
$query = Models\Server::where('id', $id); $server = Models\Server::with('node', 'allocations', 'pack')->where('id', $id)->first();
if (! $server) {
throw new NotFoundHttpException('No server by that ID was found.');
}
if (! is_null($request->input('fields'))) { if (! is_null($request->input('fields'))) {
foreach (explode(',', $request->input('fields')) as $field) { $fields = explode(',', $request->input('fields'));
if (! empty($field)) { if (! empty($fields) && is_array($fields)) {
$query->addSelect($field); return collect($server)->only($fields);
}
} }
} }
try { if ($request->input('daemon') === 'true') {
if (! $query->first()) { try {
throw new NotFoundHttpException('No server by that ID was found.'); $response = $server->node->guzzleClient([
} 'X-Access-Token' => $server->node->daemonSecret,
])->request('GET', '/servers');
// Requested Daemon Stats
$server = $query->with(
'allocations',
'pack'
)->first();
if ($request->input('daemon') === 'true') {
$node = Models\Node::findOrFail($server->node_id);
$client = Models\Node::guzzleRequest($node->id);
$response = $client->request('GET', '/servers', [
'headers' => [
'X-Access-Token' => $node->daemonSecret,
],
]);
// Only return the daemon token if the request is using HTTPS
if ($request->secure()) {
$server->daemon_token = $server->daemonSecret;
}
$server->daemon = json_decode($response->getBody())->{$server->uuid}; $server->daemon = json_decode($response->getBody())->{$server->uuid};
} catch (\GuzzleHttp\Exception\TransferException $ex) {
return $server->toArray(); // Couldn't hit the daemon, return what we have though.
$server->daemon = [
'error' => 'There was an error encountered while attempting to connect to the remote daemon.',
];
} }
return $server->toArray();
} catch (NotFoundHttpException $ex) {
throw $ex;
} catch (\GuzzleHttp\Exception\TransferException $ex) {
// Couldn't hit the daemon, return what we have though.
$server->daemon = [
'error' => 'There was an error encountered while attempting to connect to the remote daemon.',
];
return $server->toArray();
} catch (\Exception $ex) {
throw new BadRequestHttpException('There was an issue with the fields passed in the request.');
} }
$server->allocations->transform(function ($item) {
return collect($item)->except(['created_at', 'updated_at']);
});
return $server->toArray();
} }
/** /**
@ -179,7 +159,9 @@ class ServerController extends BaseController
{ {
try { try {
$server = new ServerRepository; $server = new ServerRepository;
$server->updateDetails($id, $request->all()); $server->updateDetails($id, $request->only([
'owner', 'name', 'reset_token',
]));
return Models\Server::findOrFail($id); return Models\Server::findOrFail($id);
} catch (DisplayValidationException $ex) { } catch (DisplayValidationException $ex) {
@ -224,7 +206,10 @@ class ServerController extends BaseController
{ {
try { try {
$server = new ServerRepository; $server = new ServerRepository;
$server->changeBuild($id, $request->all()); $server->changeBuild($id, $request->only([
'default', 'add_additional', 'remove_additional',
'memory', 'swap', 'io', 'cpu', 'disk',
]));
return Models\Server::findOrFail($id); return Models\Server::findOrFail($id);
} catch (DisplayValidationException $ex) { } catch (DisplayValidationException $ex) {

View file

@ -45,18 +45,11 @@ class ServiceController extends BaseController
public function view(Request $request, $id) public function view(Request $request, $id)
{ {
$service = Models\Service::find($id); $service = Models\Service::with('options.variables', 'options.packs')->find($id);
if (! $service) { if (! $service) {
throw new NotFoundHttpException('No service by that ID was found.'); throw new NotFoundHttpException('No service by that ID was found.');
} }
return [ return $service->toArray();
'service' => $service,
'options' => Models\ServiceOptions::select('id', 'name', 'description', 'tag', 'docker_image')
->where('service_id', $service->id)
->with('variables')
->with('packs')
->get(),
];
} }
} }

View file

@ -32,19 +32,16 @@ class InfoController extends BaseController
{ {
public function me(Request $request) public function me(Request $request)
{ {
return $request->user()->serverAccessCollection()->map(function ($server) { return $request->user()->serverAccessCollection()->load('allocation', 'option')->map(function ($server) {
return [ return [
'id' => $server->uuidShort, 'id' => $server->uuidShort,
'uuid' => $server->uuid, 'uuid' => $server->uuid,
'name' => $server->name, 'name' => $server->name,
'node' => $server->node_idName, 'node' => $server->node->name,
'ip' => [ 'ip' => $server->allocation->alias,
'set' => $server->ip, 'port' => $server->allocation->port,
'alias' => $server->ip_alias, 'service' => $server->service->name,
], 'option' => $server->option->name,
'port' => $server->port,
'service' => $server->a_serviceName,
'option' => $server->a_serviceOptionName,
]; ];
})->all(); })->all();
} }

View file

@ -25,7 +25,6 @@
namespace Pterodactyl\Http\Controllers\API\User; namespace Pterodactyl\Http\Controllers\API\User;
use Log; use Log;
use Auth;
use Pterodactyl\Models; use Pterodactyl\Models;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Pterodactyl\Http\Controllers\API\BaseController; use Pterodactyl\Http\Controllers\API\BaseController;
@ -43,20 +42,14 @@ class ServerController extends BaseController
$daemon = [ $daemon = [
'status' => $json->status, 'status' => $json->status,
'stats' => $json->proc, 'stats' => $json->proc,
'query' => $json->query,
]; ];
} catch (\Exception $ex) { } catch (\Exception $ex) {
$daemon = [ $daemon = [
'error' => 'An error was encountered while trying to connect to the daemon to collece information. It might be offline.', 'error' => 'An error was encountered while trying to connect to the daemon to collect information. It might be offline.',
]; ];
Log::error($ex); Log::error($ex);
} }
foreach ($server->allocations as &$allocation) {
$allocation->default = ($allocation->id === $server->allocation_id);
unset($allocation->id);
}
return [ return [
'uuidShort' => $server->uuidShort, 'uuidShort' => $server->uuidShort,
'uuid' => $server->uuid, 'uuid' => $server->uuid,
@ -70,12 +63,18 @@ class ServerController extends BaseController
'cpu' => $server->cpu, 'cpu' => $server->cpu,
'oom_disabled' => (bool) $server->oom_disabled, 'oom_disabled' => (bool) $server->oom_disabled,
], ],
'allocations' => $server->allocations, 'allocations' => $server->allocations->map(function ($item) use ($server) {
return [
'ip' => $item->alias,
'port' => $item->port,
'default' => ($item->id === $server->allocation_id),
];
}),
'sftp' => [ 'sftp' => [
'username' => (Auth::user()->can('view-sftp', $server)) ? $server->username : null, 'username' => ($request->user()->can('view-sftp', $server)) ? $server->username : null,
], ],
'daemon' => [ 'daemon' => [
'token' => ($request->secure()) ? $server->daemonSecret : false, 'token' => $server->daemonSecret,
'response' => $daemon, 'response' => $daemon,
], ],
]; ];

View file

@ -75,31 +75,27 @@ class UserController extends BaseController
*/ */
public function view(Request $request, $id) public function view(Request $request, $id)
{ {
$query = Models\User::where((is_numeric($id) ? 'id' : 'email'), $id); $user = Models\User::with('servers')->where((is_numeric($id) ? 'id' : 'email'), $id)->first();
if (! $user->first()) {
throw new NotFoundHttpException('No user by that ID was found.');
}
$user->servers->transform(function ($item) {
return collect($item)->only([
'id', 'node_id', 'uuidShort',
'uuid', 'name', 'suspended',
'owner_id',
]);
});
if (! is_null($request->input('fields'))) { if (! is_null($request->input('fields'))) {
foreach (explode(',', $request->input('fields')) as $field) { $fields = explode(',', $request->input('fields'));
if (! empty($field)) { if (! empty($fields) && is_array($fields)) {
$query->addSelect($field); return collect($user)->only($fields);
}
} }
} }
try { return $user->toArray();
if (! $query->first()) {
throw new NotFoundHttpException('No user by that ID was found.');
}
$user = $query->first();
$userArray = $user->toArray();
$userArray['servers'] = Models\Server::select('id', 'uuid', 'node', 'suspended')->where('owner', $user->id)->get();
return $userArray;
} catch (NotFoundHttpException $ex) {
throw $ex;
} catch (\Exception $ex) {
throw new BadRequestHttpException('There was an issue with the fields passed in the request.');
}
} }
/** /**
@ -123,7 +119,9 @@ class UserController extends BaseController
try { try {
$user = new UserRepository; $user = new UserRepository;
$create = $user->create($request->only([ $create = $user->create($request->only([
'email', 'username', 'name_first', 'name_last', 'password', 'root_admin', 'custom_id', 'email', 'username', 'name_first',
'name_last', 'password',
'root_admin', 'custom_id',
])); ]));
$create = $user->create($request->input('email'), $request->input('password'), $request->input('admin'), $request->input('custom_id')); $create = $user->create($request->input('email'), $request->input('password'), $request->input('admin'), $request->input('custom_id'));
@ -160,7 +158,9 @@ class UserController extends BaseController
try { try {
$user = new UserRepository; $user = new UserRepository;
$user->update($id, $request->only([ $user->update($id, $request->only([
'username', 'email', 'name_first', 'name_last', 'password', 'root_admin', 'language', 'username', 'email', 'name_first',
'name_last', 'password',
'root_admin', 'language',
])); ]));
return Models\User::findOrFail($id); return Models\User::findOrFail($id);

View file

@ -86,16 +86,9 @@ class ServersController extends Controller
{ {
try { try {
$server = new ServerRepository; $server = new ServerRepository;
$response = $server->create($request->only([ $response = $server->create($request->except('_token'));
'owner', 'name', 'memory', 'swap',
'node', 'ip', 'port', 'allocation',
'cpu', 'disk', 'service',
'option', 'location', 'pack',
'startup', 'custom_image_name',
'auto_deploy', 'custom_id',
]));
return redirect()->route('admin.servers.view', ['id' => $response]); return redirect()->route('admin.servers.view', ['id' => $response->id]);
} catch (DisplayValidationException $ex) { } catch (DisplayValidationException $ex) {
return redirect()->route('admin.servers.new')->withErrors(json_decode($ex->getMessage()))->withInput(); return redirect()->route('admin.servers.new')->withErrors(json_decode($ex->getMessage()))->withInput();
} catch (DisplayException $ex) { } catch (DisplayException $ex) {
@ -188,7 +181,7 @@ class ServersController extends Controller
], 500); ], 500);
} }
$option = Models\ServiceOptions::with('variables', ['packs' => function ($query) { $option = Models\ServiceOptions::with('variables')->with(['packs' => function ($query) {
$query->where('selectable', true); $query->where('selectable', true);
}])->findOrFail($request->input('option')); }])->findOrFail($request->input('option'));

View file

@ -121,7 +121,7 @@ class APISecretToken extends Authorization
// Log the Route Access // Log the Route Access
APILogService::log($request, null, true); APILogService::log($request, null, true);
return Auth::loginUsingId($key->user); return Auth::loginUsingId($key->user_id);
} }
protected function _generateHMAC($body, $key) protected function _generateHMAC($body, $key)

View file

@ -66,28 +66,12 @@ class Node extends Model
* @var array * @var array
*/ */
protected $fillable = [ protected $fillable = [
'uuid', 'public', 'name', 'location_id',
'uuidShort', 'fqdn', 'scheme', 'memory',
'node_id', 'memory_overallocate', 'disk',
'name', 'disk_overallocate', 'upload_size',
'suspended', 'daemonSecret', 'daemonBase',
'owner_id', 'daemonSFTP', 'daemonListen',
'memory',
'swap',
'disk',
'io',
'cpu',
'oom_disabled',
'allocation_id',
'service_id',
'option_id',
'pack_id',
'startup',
'daemonSecret',
'image',
'username',
'sftp_password',
'installed',
]; ];
/** /**

View file

@ -62,8 +62,8 @@ class APIRepository
// Node Management Routes // Node Management Routes
'nodes.list', 'nodes.list',
'nodes.view',
'nodes.create', 'nodes.create',
'nodes.list',
'nodes.allocations', 'nodes.allocations',
'nodes.delete', 'nodes.delete',

View file

@ -44,7 +44,7 @@ class NodeRepository
// Validate Fields // Validate Fields
$validator = Validator::make($data, [ $validator = Validator::make($data, [
'name' => 'required|regex:/^([\w .-]{1,100})$/', 'name' => 'required|regex:/^([\w .-]{1,100})$/',
'location' => 'required|numeric|min:1|exists:locations,id', 'location_id' => 'required|numeric|min:1|exists:locations,id',
'public' => 'required|numeric|between:0,1', 'public' => 'required|numeric|between:0,1',
'fqdn' => 'required|string|unique:nodes,fqdn', 'fqdn' => 'required|string|unique:nodes,fqdn',
'scheme' => 'required|regex:/^(http(s)?)$/', 'scheme' => 'required|regex:/^(http(s)?)$/',
@ -91,7 +91,7 @@ class NodeRepository
// Validate Fields // Validate Fields
$validator = $validator = Validator::make($data, [ $validator = $validator = Validator::make($data, [
'name' => 'regex:/^([\w .-]{1,100})$/', 'name' => 'regex:/^([\w .-]{1,100})$/',
'location' => 'numeric|min:1|exists:locations,id', 'location_id' => 'numeric|min:1|exists:locations,id',
'public' => 'numeric|between:0,1', 'public' => 'numeric|between:0,1',
'fqdn' => 'string|unique:nodes,fqdn,' . $id, 'fqdn' => 'string|unique:nodes,fqdn,' . $id,
'scheme' => 'regex:/^(http(s)?)$/', 'scheme' => 'regex:/^(http(s)?)$/',
@ -210,13 +210,13 @@ class NodeRepository
foreach ($portBlock as $assignPort) { foreach ($portBlock as $assignPort) {
$alloc = Models\Allocation::firstOrNew([ $alloc = Models\Allocation::firstOrNew([
'node' => $node->id, 'node_id' => $node->id,
'ip' => $ip, 'ip' => $ip,
'port' => $assignPort, 'port' => $assignPort,
]); ]);
if (! $alloc->exists) { if (! $alloc->exists) {
$alloc->fill([ $alloc->fill([
'node' => $node->id, 'node_id' => $node->id,
'ip' => $ip, 'ip' => $ip,
'port' => $assignPort, 'port' => $assignPort,
'ip_alias' => $setAlias, 'ip_alias' => $setAlias,
@ -227,13 +227,13 @@ class NodeRepository
} }
} else { } else {
$alloc = Models\Allocation::firstOrNew([ $alloc = Models\Allocation::firstOrNew([
'node' => $node->id, 'node_id' => $node->id,
'ip' => $ip, 'ip' => $ip,
'port' => $port, 'port' => $port,
]); ]);
if (! $alloc->exists) { if (! $alloc->exists) {
$alloc->fill([ $alloc->fill([
'node' => $node->id, 'node_id' => $node->id,
'ip' => $ip, 'ip' => $ip,
'port' => $port, 'port' => $port,
'ip_alias' => $setAlias, 'ip_alias' => $setAlias,
@ -269,7 +269,7 @@ class NodeRepository
]); ]);
// Delete Allocations // Delete Allocations
Models\Allocation::where('node', $node->id)->delete(); Models\Allocation::where('node_id', $node->id)->delete();
// Delete configure tokens // Delete configure tokens
Models\NodeConfigurationToken::where('node', $node->id)->delete(); Models\NodeConfigurationToken::where('node', $node->id)->delete();

View file

@ -90,17 +90,17 @@ class ServerRepository
'io' => 'required|numeric|min:10|max:1000', 'io' => 'required|numeric|min:10|max:1000',
'cpu' => 'required|numeric|min:0', 'cpu' => 'required|numeric|min:0',
'disk' => 'required|numeric|min:0', 'disk' => 'required|numeric|min:0',
'service' => 'required|numeric|min:1|exists:services,id', 'service_id' => 'required|numeric|min:1|exists:services,id',
'option' => 'required|numeric|min:1|exists:service_options,id', 'option_id' => 'required|numeric|min:1|exists:service_options,id',
'location' => 'required|numeric|min:1|exists:locations,id', 'location_id' => 'required|numeric|min:1|exists:locations,id',
'pack' => 'sometimes|nullable|numeric|min:0', 'pack_id' => 'sometimes|nullable|numeric|min:0',
'startup' => 'string', 'startup' => 'string',
'custom_image_name' => 'required_if:use_custom_image,on', 'custom_image_name' => 'required_if:use_custom_image,on',
'auto_deploy' => 'sometimes|boolean', 'auto_deploy' => 'sometimes|boolean',
'custom_id' => 'sometimes|required|numeric|unique:servers,id', 'custom_id' => 'sometimes|required|numeric|unique:servers,id',
]); ]);
$validator->sometimes('node', 'bail|required|numeric|min:1|exists:nodes,id', function ($input) { $validator->sometimes('node_id', 'bail|required|numeric|min:1|exists:nodes,id', function ($input) {
return ! ($input->auto_deploy); return ! ($input->auto_deploy);
}); });
@ -112,7 +112,7 @@ class ServerRepository
return ! $input->auto_deploy && ! $input->allocation; return ! $input->auto_deploy && ! $input->allocation;
}); });
$validator->sometimes('allocation', 'numeric|exists:allocations,id', function ($input) { $validator->sometimes('allocation_id', 'numeric|exists:allocations,id', function ($input) {
return ! ($input->auto_deploy || ($input->port && $input->ip)); return ! ($input->auto_deploy || ($input->port && $input->ip));
}); });
@ -131,19 +131,19 @@ class ServerRepository
if (isset($data['auto_deploy']) && in_array($data['auto_deploy'], [true, 1, '1'])) { if (isset($data['auto_deploy']) && in_array($data['auto_deploy'], [true, 1, '1'])) {
// This is an auto-deployment situation // This is an auto-deployment situation
// Ignore any other passed node data // Ignore any other passed node data
unset($data['node'], $data['ip'], $data['port'], $data['allocation']); unset($data['node_id'], $data['ip'], $data['port'], $data['allocation_id']);
$autoDeployed = true; $autoDeployed = true;
$node = DeploymentService::smartRandomNode($data['memory'], $data['disk'], $data['location']); $node = DeploymentService::smartRandomNode($data['memory'], $data['disk'], $data['location_id']);
$allocation = DeploymentService::randomAllocation($node->id); $allocation = DeploymentService::randomAllocation($node->id);
} else { } else {
$node = Models\Node::findOrFail($data['node']); $node = Models\Node::findOrFail($data['node_id']);
} }
// Verify IP & Port are a.) free and b.) assigned to the node. // Verify IP & Port are a.) free and b.) assigned to the node.
// We know the node exists because of 'exists:nodes,id' in the validation // We know the node exists because of 'exists:nodes,id' in the validation
if (! $autoDeployed) { if (! $autoDeployed) {
if (! isset($data['allocation'])) { if (! isset($data['allocation_id'])) {
$allocation = Models\Allocation::where('ip', $data['ip'])->where('port', $data['port'])->where('node', $data['node'])->whereNull('assigned_to')->first(); $allocation = Models\Allocation::where('ip', $data['ip'])->where('port', $data['port'])->where('node', $data['node'])->whereNull('assigned_to')->first();
} else { } else {
$allocation = Models\Allocation::where('id', $data['allocation'])->where('node', $data['node'])->whereNull('assigned_to')->first(); $allocation = Models\Allocation::where('id', $data['allocation'])->where('node', $data['node'])->whereNull('assigned_to')->first();
@ -165,12 +165,12 @@ class ServerRepository
} }
// Validate the Pack // Validate the Pack
if ($data['pack'] == 0) { if ($data['pack_id'] == 0) {
$data['pack'] = null; $data['pack_id'] = null;
} }
if (! is_null($data['pack'])) { if (! is_null($data['pack_id'])) {
$pack = Models\ServicePack::where('id', $data['pack'])->where('option', $data['option'])->first(); $pack = Models\ServicePack::where('id', $data['pack_id'])->where('option', $data['option_id'])->first();
if (! $pack) { if (! $pack) {
throw new DisplayException('The requested service pack does not seem to exist for this combination.'); throw new DisplayException('The requested service pack does not seem to exist for this combination.');
} }
@ -180,7 +180,7 @@ class ServerRepository
$service = Models\Service::find($option->service_id); $service = Models\Service::find($option->service_id);
// Check those Variables // Check those Variables
$variables = Models\ServiceVariables::where('option_id', $data['option'])->get(); $variables = Models\ServiceVariables::where('option_id', $data['option_id'])->get();
$variableList = []; $variableList = [];
if ($variables) { if ($variables) {
foreach ($variables as $variable) { foreach ($variables as $variable) {
@ -254,10 +254,10 @@ class ServerRepository
$server->fill([ $server->fill([
'uuid' => $genUuid, 'uuid' => $genUuid,
'uuidShort' => $genShortUuid, 'uuidShort' => $genShortUuid,
'node' => $node->id, 'node_id' => $node->id,
'name' => $data['name'], 'name' => $data['name'],
'suspended' => 0, 'suspended' => 0,
'owner' => $user->id, 'owner_id' => $user->id,
'memory' => $data['memory'], 'memory' => $data['memory'],
'swap' => $data['swap'], 'swap' => $data['swap'],
'disk' => $data['disk'], 'disk' => $data['disk'],
@ -265,9 +265,9 @@ class ServerRepository
'cpu' => $data['cpu'], 'cpu' => $data['cpu'],
'oom_disabled' => (isset($data['oom_disabled'])) ? true : false, 'oom_disabled' => (isset($data['oom_disabled'])) ? true : false,
'allocation' => $allocation->id, 'allocation' => $allocation->id,
'service' => $data['service'], 'service_id' => $data['service_id'],
'option' => $data['option'], 'option_id' => $data['option_id'],
'pack' => $data['pack'], 'pack_id' => $data['pack_id'],
'startup' => $data['startup'], 'startup' => $data['startup'],
'daemonSecret' => $uuid->generate('servers', 'daemonSecret'), 'daemonSecret' => $uuid->generate('servers', 'daemonSecret'),
'image' => (isset($data['custom_image_name'])) ? $data['custom_image_name'] : $option->docker_image, 'image' => (isset($data['custom_image_name'])) ? $data['custom_image_name'] : $option->docker_image,
@ -277,7 +277,7 @@ class ServerRepository
$server->save(); $server->save();
// Mark Allocation in Use // Mark Allocation in Use
$allocation->assigned_to = $server->id; $allocation->server_id = $server->id;
$allocation->save(); $allocation->save();
// Add Variables // Add Variables
@ -329,7 +329,7 @@ class ServerRepository
DB::commit(); DB::commit();
return $server->id; return $server;
} catch (TransferException $ex) { } catch (TransferException $ex) {
DB::rollBack(); DB::rollBack();
throw new DisplayException('There was an error while attempting to connect to the daemon to add this server.', $ex); throw new DisplayException('There was an error while attempting to connect to the daemon to add this server.', $ex);
@ -782,8 +782,8 @@ class ServerRepository
DB::beginTransaction(); DB::beginTransaction();
try { try {
// Unassign Allocations // Unassign Allocations
Models\Allocation::where('assigned_to', $server->id)->update([ Models\Allocation::where('server_id', $server->id)->update([
'assigned_to' => null, 'server_id' => null,
]); ]);
// Remove Variables // Remove Variables

View file

@ -44,9 +44,9 @@
<div class="form-group col-md-4"> <div class="form-group col-md-4">
<label for="name" class="control-label">Location</label> <label for="name" class="control-label">Location</label>
<div> <div>
<select name="location" class="form-control"> <select name="location_id" class="form-control">
@foreach($locations as $location) @foreach($locations as $location)
<option value="{{ $location->id }}" {{ (old('location') === $location->id) ? 'checked' : '' }}>{{ $location->long }} ({{ $location->short }})</option> <option value="{{ $location->id }}" {{ (old('location_id') === $location->id) ? 'selected' : '' }}>{{ $location->long }} ({{ $location->short }})</option>
@endforeach @endforeach
</select> </select>
</div> </div>

View file

@ -138,9 +138,9 @@
<div class="form-group col-md-4"> <div class="form-group col-md-4">
<label for="name" class="control-label">Location</label> <label for="name" class="control-label">Location</label>
<div> <div>
<select name="location" class="form-control"> <select name="location_id" class="form-control">
@foreach($locations as $location) @foreach($locations as $location)
<option value="{{ $location->id }}" {{ (old('location', $node->location) === $location->id) ? 'checked' : '' }}>{{ $location->long }} ({{ $location->short }})</option> <option value="{{ $location->id }}" {{ (old('location_id', $node->location) === $location->id) ? 'selected' : '' }}>{{ $location->long }} ({{ $location->short }})</option>
@endforeach @endforeach
</select> </select>
</div> </div>

View file

@ -63,7 +63,7 @@
<div class="form-group col-md-6"> <div class="form-group col-md-6">
<label for="location" class="control-label">Server Location</label> <label for="location" class="control-label">Server Location</label>
<div> <div>
<select name="location" id="getLocation" class="form-control"> <select name="location_id" id="getLocation" class="form-control">
<option disabled selected> -- Select a Location</option> <option disabled selected> -- Select a Location</option>
@foreach($locations as $location) @foreach($locations as $location)
<option value="{{ $location->id }}">{{ $location->long }} ({{ $location->short }})</option> <option value="{{ $location->id }}">{{ $location->long }} ({{ $location->short }})</option>
@ -75,7 +75,7 @@
<div class="form-group col-md-6 hidden" id="allocationNode"> <div class="form-group col-md-6 hidden" id="allocationNode">
<label for="node" class="control-label">Server Node</label> <label for="node" class="control-label">Server Node</label>
<div> <div>
<select name="node" id="getNode" class="form-control"> <select name="node_id" id="getNode" class="form-control">
<option disabled selected> -- Select a Node</option> <option disabled selected> -- Select a Node</option>
</select> </select>
<p class="text-muted"><small>The node which this server will be deployed to.</small></p> <p class="text-muted"><small>The node which this server will be deployed to.</small></p>
@ -181,9 +181,9 @@
<div class="row"> <div class="row">
<div class="ajax_loading_box" style="display:none;"><i class="fa fa-refresh fa-spin ajax_loading_position"></i></div> <div class="ajax_loading_box" style="display:none;"><i class="fa fa-refresh fa-spin ajax_loading_position"></i></div>
<div class="form-group col-md-12"> <div class="form-group col-md-12">
<label for="service" class="control-label">Service Type</label> <label for="service_id" class="control-label">Service Type</label>
<div> <div>
<select name="service" id="getService" class="form-control"> <select name="service_id" id="getService" class="form-control">
<option disabled selected> -- Select a Service</option> <option disabled selected> -- Select a Service</option>
@foreach($services as $service) @foreach($services as $service)
<option value="{{ $service->id }}">{{ $service->name }}</option> <option value="{{ $service->id }}">{{ $service->name }}</option>
@ -193,18 +193,18 @@
</div> </div>
</div> </div>
<div class="form-group col-md-12 hidden"> <div class="form-group col-md-12 hidden">
<label for="option" class="control-label">Service Option</label> <label for="option_id" class="control-label">Service Option</label>
<div> <div>
<select name="option" id="getOption" class="form-control"> <select name="option_id" id="getOption" class="form-control">
<option disabled selected> -- Select a Service Option</option> <option disabled selected> -- Select a Service Option</option>
</select> </select>
<p class="text-muted"><small>Select the type of service that this server will be running.</small></p> <p class="text-muted"><small>Select the type of service that this server will be running.</small></p>
</div> </div>
</div> </div>
<div class="form-group col-md-12 hidden"> <div class="form-group col-md-12 hidden">
<label for="option" class="control-label">Service Pack</label> <label for="pack_id" class="control-label">Service Pack</label>
<div> <div>
<select name="pack" id="getPack" class="form-control"> <select name="pack_id" id="getPack" class="form-control">
<option disabled selected> -- Select a Service Pack</option> <option disabled selected> -- Select a Service Pack</option>
</select> </select>
<p class="text-muted"><small>Select the service pack that should be used for this server. This option can be changed later.</small></p> <p class="text-muted"><small>Select the service pack that should be used for this server. This option can be changed later.</small></p>