Finish front-end server creation page.

This commit is contained in:
Dane Everitt 2015-12-10 18:30:47 -05:00
parent c7b76aee55
commit 59ff1ebbe6
7 changed files with 284 additions and 114 deletions

View file

@ -1,95 +0,0 @@
<?php
namespace Pterodactyl\Http\Controllers\Admin;
use Debugbar;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Models\Node;
use Pterodactyl\Models\ServiceOptions;
use Pterodactyl\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AjaxController extends Controller
{
/**
* Controller Constructor
*/
public function __construct()
{
// All routes in this controller are protected by the authentication middleware.
$this->middleware('auth');
$this->middleware('admin');
}
/**
* Returns a JSON tree of all avaliable nodes in a given location.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\View\View
*/
public function postNewServerGetNodes(Request $request)
{
if(!$request->input('location')) {
return response()->json([
'error' => 'Missing location in request.'
], 500);
}
return response()->json(Node::select('id', 'name', 'public')->where('location', $request->input('location'))->get());
}
/**
* Returns a JSON tree of all avaliable IPs and Ports on a given node.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\View\View
*/
public function postNewServerGetIps(Request $request)
{
if(!$request->input('node')) {
return response()->json([
'error' => 'Missing node in request.'
], 500);
}
$ips = Allocation::where('node', $request->input('node'))->whereNull('assigned_to')->get();
$listing = [];
foreach($ips as &$ip) {
if (array_key_exists($ip->ip, $listing)) {
$listing[$ip->ip] = array_merge($listing[$ip->ip], [$ip->port]);
} else {
$listing[$ip->ip] = [$ip->port];
}
}
return response()->json($listing);
}
/**
* Returns a JSON tree of all avaliable options for a given service.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\View\View
*/
public function postNewServerServiceOptions(Request $request)
{
if(!$request->input('service')) {
return response()->json([
'error' => 'Missing service in request.'
], 500);
}
return response()->json(ServiceOptions::select('id', 'name')->where('parent_service', $request->input('service'))->orderBy('name', 'asc')->get());
}
}

View file

@ -4,8 +4,12 @@ namespace Pterodactyl\Http\Controllers\Admin;
use Debugbar; use Debugbar;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Pterodactyl\Models\Node;
use Pterodactyl\Models\Location; use Pterodactyl\Models\Location;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Models\Service; use Pterodactyl\Models\Service;
use Pterodactyl\Models\ServiceOptions;
use Pterodactyl\Models\ServiceVariables;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -48,4 +52,95 @@ class ServersController extends Controller
// //
} }
public function postNewServer(Request $request)
{
return json_encode($request->all());
}
/**
* Returns a JSON tree of all avaliable nodes in a given location.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\View\View
*/
public function postNewServerGetNodes(Request $request)
{
if(!$request->input('location')) {
return response()->json([
'error' => 'Missing location in request.'
], 500);
}
return response()->json(Node::select('id', 'name', 'public')->where('location', $request->input('location'))->get());
}
/**
* Returns a JSON tree of all avaliable IPs and Ports on a given node.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\View\View
*/
public function postNewServerGetIps(Request $request)
{
if(!$request->input('node')) {
return response()->json([
'error' => 'Missing node in request.'
], 500);
}
$ips = Allocation::where('node', $request->input('node'))->whereNull('assigned_to')->get();
$listing = [];
foreach($ips as &$ip) {
if (array_key_exists($ip->ip, $listing)) {
$listing[$ip->ip] = array_merge($listing[$ip->ip], [$ip->port]);
} else {
$listing[$ip->ip] = [$ip->port];
}
}
return response()->json($listing);
}
/**
* Returns a JSON tree of all avaliable options for a given service.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\View\View
*/
public function postNewServerServiceOptions(Request $request)
{
if(!$request->input('service')) {
return response()->json([
'error' => 'Missing service in request.'
], 500);
}
return response()->json(ServiceOptions::select('id', 'name', 'docker_image')->where('parent_service', $request->input('service'))->orderBy('name', 'asc')->get());
}
/**
* Returns a JSON tree of all avaliable variables for a given service option.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\View\View
*/
public function postNewServerServiceVariables(Request $request)
{
if(!$request->input('option')) {
return response()->json([
'error' => 'Missing option in request.'
], 500);
}
return response()->json(ServiceVariables::where('option_id', $request->input('option'))->get());
}
} }

View file

@ -19,16 +19,17 @@ class AdminRoutes {
// Server Routes // Server Routes
$router->group(['prefix' => 'servers'], function ($server) use ($router) { $router->group(['prefix' => 'servers'], function ($server) use ($router) {
$router->get('/', [ 'as' => 'admin.servers', 'uses' => 'Admin\ServersController@getIndex' ]); $router->get('/', [ 'as' => 'admin.servers', 'uses' => 'Admin\ServersController@getIndex' ]);
$router->get('/new', [ 'as' => 'admin.servers.new', 'uses' => 'Admin\ServersController@getNew' ]); $router->get('/new', [ 'as' => 'admin.servers.new', 'uses' => 'Admin\ServersController@getNew' ]);
$router->get('/view/{id}', [ 'as' => 'admin.servers.view', 'uses' => 'Admin\ServersController@getView' ]); $router->get('/view/{id}', [ 'as' => 'admin.servers.view', 'uses' => 'Admin\ServersController@getView' ]);
});
// AJAX Routes $router->post('/new', [ 'uses' => 'Admin\ServersController@postNewServer']);
$router->group(['prefix' => 'ajax'], function ($server) use ($router) { $router->post('/new/get-nodes', [ 'uses' => 'Admin\ServersController@postNewServerGetNodes' ]);
$router->post('/new/server/get-nodes', [ 'uses' => 'Admin\AjaxController@postNewServerGetNodes' ]); $router->post('/new/get-ips', [ 'uses' => 'Admin\ServersController@postNewServerGetIps' ]);
$router->post('/new/server/get-ips', [ 'uses' => 'Admin\AjaxController@postNewServerGetIps' ]); $router->post('/new/service-options', [ 'uses' => 'Admin\ServersController@postNewServerServiceOptions' ]);
$router->post('/new/server/service-options', [ 'uses' => 'Admin\AjaxController@postNewServerServiceOptions' ]); $router->post('/new/service-variables', [ 'uses' => 'Admin\ServersController@postNewServerServiceVariables' ]);
}); });
}); });

View file

@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ModifyServicesImages extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('services', function (Blueprint $table) {
$table->dropColumn('docker_image');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('services', function (Blueprint $table) {
$table->string('docker_image')->after('description');
});
}
}

View file

@ -0,0 +1,43 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddDockerImageToServiceOptions extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('service_options', function (Blueprint $table) {
$table->renameColumn('docker_tag', 'docker_image');
});
Schema::table('service_options', function (Blueprint $table) {
$table->text('docker_image')->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('service_options', function (Blueprint $table) {
$table->renameColumn('docker_image', 'docker_tag');
});
Schema::table('service_options', function (Blueprint $table) {
$table->string('docker_tag')->change();
});
}
}

View file

@ -0,0 +1,29 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AllowNullRegex extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('service_variables', function (Blueprint $table) {
$table->string('regex')->nullable()->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::statement('ALTER TABLE `service_variables` MODIFY `regex` VARCHAR(255) NOT NULL');
}
}

View file

@ -11,8 +11,16 @@
<li><a href="/admin/servers">Servers</a></li> <li><a href="/admin/servers">Servers</a></li>
<li class="active">Create New Server</li> <li class="active">Create New Server</li>
</ul> </ul>
@foreach (Alert::getMessages() as $type => $messages)
@foreach ($messages as $message)
<div class="alert alert-{{ $type }} alert-dismissable" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
{{ $message }}
</div>
@endforeach
@endforeach
<h3>Create New Server</h3><hr /> <h3>Create New Server</h3><hr />
<form action="#" method="POST"> <form action="/admin/servers/new" method="POST">
<div class="well"> <div class="well">
<div class="row"> <div class="row">
<div class="form-group col-md-6"> <div class="form-group col-md-6">
@ -47,7 +55,7 @@
</div> </div>
</div> </div>
<div class="form-group col-md-6 hidden"> <div class="form-group col-md-6 hidden">
<label for="location" 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="getNode" class="form-control">
<option></option> <option></option>
@ -58,18 +66,18 @@
</div> </div>
<div class="row"> <div class="row">
<div class="form-group col-md-6 hidden"> <div class="form-group col-md-6 hidden">
<label for="location" class="control-label">Server IP</label> <label for="ip" class="control-label">Server IP</label>
<div> <div>
<select name="node" id="getIP" class="form-control"> <select name="ip" id="getIP" class="form-control">
<option></option> <option></option>
</select> </select>
<p class="text-muted"><small>Select the main IP that this server will be listening on. You can assign additional open IPs and ports below.</small></p> <p class="text-muted"><small>Select the main IP that this server will be listening on. You can assign additional open IPs and ports below.</small></p>
</div> </div>
</div> </div>
<div class="form-group col-md-6 hidden"> <div class="form-group col-md-6 hidden">
<label for="location" class="control-label">Server Port</label> <label for="port" class="control-label">Server Port</label>
<div> <div>
<select name="node" id="getPort" class="form-control"></select> <select name="port" id="getPort" class="form-control"></select>
<p class="text-muted"><small>Select the main port that this server will be listening on.</small></p> <p class="text-muted"><small>Select the main port that this server will be listening on.</small></p>
</div> </div>
</div> </div>
@ -159,11 +167,31 @@
</div> </div>
</div> </div>
</div> </div>
<div class="well">
<div class="row">
<div class="col-md-12">
<h3 class="nopad">Service Environment Variables</h3>
<hr />
<div class="alert alert-info">Some service options have additional environment variables that you can define for a given instance. They will show up below when you select a service option. If none show up, chances are that none were defined, and there is nothing to worry about.</div>
<span id="serverVariables"></span>
</div>
</div>
</div>
<div class="well">
<div class="row">
<div class="col-md-12 text-center">
{!! csrf_field() !!}
<input type="submit" name="submit" class="btn btn-primary btn-sm" value="Create New Server" />
</div>
</div>
</div>
</form> </form>
</div> </div>
<script> <script>
$(document).ready(function () { $(document).ready(function () {
$('#sidebar_links').find("a[href='/admin/servers/new']").addClass('active');
$('input[name="use_custom_image"]').change(function () { $('input[name="use_custom_image"]').change(function () {
$('input[name="custom_image_name"]').val('').prop('disabled', !($(this).is(':checked'))); $('input[name="custom_image_name"]').val('').prop('disabled', !($(this).is(':checked')));
}); });
@ -190,7 +218,7 @@ $(document).ready(function () {
$.ajax({ $.ajax({
method: 'POST', method: 'POST',
url: '/admin/ajax/new/server/get-nodes', url: '/admin/servers/new/get-nodes',
headers: { headers: {
'X-CSRF-TOKEN': '{{ csrf_token() }}' 'X-CSRF-TOKEN': '{{ csrf_token() }}'
}, },
@ -207,7 +235,7 @@ $(document).ready(function () {
}).fail(function (jqXHR) { }).fail(function (jqXHR) {
alert('An error occured while attempting to load a list of nodes in this location.'); alert('An error occured while attempting to load a list of nodes in this location.');
currentLocation = null; currentLocation = null;
console.log(jqXHR); console.error(jqXHR);
}).always(function () { }).always(function () {
handleLoader('#load_settings'); handleLoader('#load_settings');
}) })
@ -228,7 +256,7 @@ $(document).ready(function () {
$.ajax({ $.ajax({
method: 'POST', method: 'POST',
url: '/admin/ajax/new/server/get-ips', url: '/admin/servers/new/get-ips',
headers: { headers: {
'X-CSRF-TOKEN': '{{ csrf_token() }}' 'X-CSRF-TOKEN': '{{ csrf_token() }}'
}, },
@ -244,7 +272,7 @@ $(document).ready(function () {
}).fail(function (jqXHR) { }).fail(function (jqXHR) {
alert('An error occured while attempting to get IPs and Ports avaliable on this node.'); alert('An error occured while attempting to get IPs and Ports avaliable on this node.');
currentNode = null; currentNode = null;
console.log(jqXHR); console.error(jqXHR);
}).always(function () { }).always(function () {
handleLoader('#load_settings'); handleLoader('#load_settings');
}); });
@ -278,7 +306,7 @@ $(document).ready(function () {
$.ajax({ $.ajax({
method: 'POST', method: 'POST',
url: '/admin/ajax/new/server/service-options', url: '/admin/servers/new/service-options',
headers: { headers: {
'X-CSRF-TOKEN': '{{ csrf_token() }}' 'X-CSRF-TOKEN': '{{ csrf_token() }}'
}, },
@ -287,13 +315,51 @@ $(document).ready(function () {
} }
}).done(function (data) { }).done(function (data) {
$.each(data, function (i, option) { $.each(data, function (i, option) {
$('#getOption').append('<option value="' + option.id + '">' + option.name + '</option>'); $('#getOption').append('<option value="' + option.id + '" data-image="' + option.docker_image + '">' + option.name + '</option>');
}); });
$('#getOption').parent().parent().removeClass('hidden'); $('#getOption').parent().parent().removeClass('hidden');
}).fail(function (jqXHR) { }).fail(function (jqXHR) {
alert('An error occured while attempting to list options for this service.'); alert('An error occured while attempting to list options for this service.');
currentService = null; currentService = null;
console.log(jqXHR); console.error(jqXHR);
}).always(function () {
handleLoader('#load_services');
});
});
$('#getOption').on('change', function (event) {
handleLoader('#load_services', true);
$('#serverVariables').html('');
$('input[name="custom_image_name"]').val($(this).find(':selected').data('image'));
$.ajax({
method: 'POST',
url: '/admin/servers/new/service-variables',
headers: {
'X-CSRF-TOKEN': '{{ csrf_token() }}'
},
data: {
option: $('#getOption').val()
}
}).done(function (data) {
$.each(data, function (i, item) {
var isRequired = (item.required === 1) ? '<code>Required</code>' : '';
var dataAppend = ' \
<div class="form-group col-md-6">\
<label for="var_ref_' + item.id + '" class="control-label">' + item.name + '</label> ' + isRequired + '\
<div>\
<input type="text" autocomplete="off" name="var_ref_' + item.id + '" class="form-control" value="' + item.default_value + '" />\
<p class="text-muted"><small>' + item.description + '</small></p>\
<p class="text-muted"><small>Regex Requirements for Input: <code>' + item.regex + '</code></small></p>\
</div>\
</div>\
';
$('#serverVariables').append(dataAppend);
});
}).fail(function (jqXHR) {
console.error(jqXHR);
}).always(function () { }).always(function () {
handleLoader('#load_services'); handleLoader('#load_services');
}); });