Add support for node creation

This commit is contained in:
Dane Everitt 2016-01-04 23:59:45 -05:00
parent b5821ffb0f
commit d381c691ba
8 changed files with 431 additions and 0 deletions

View file

@ -0,0 +1,75 @@
<?php
namespace Pterodactyl\Http\Controllers\Admin;
use Alert;
use Debugbar;
use Log;
use DB;
use Pterodactyl\Models;
use Pterodactyl\Repositories\NodeRepository;
use Pterodactyl\Http\Controllers\Controller;
use Illuminate\Http\Request;
class NodesController extends Controller
{
/**
* Controller Constructor
*/
public function __construct()
{
//
}
public function getIndex(Request $request)
{
return view('admin.nodes.index', [
'nodes' => Models\Node::select(
'nodes.*',
'locations.long as a_locationName',
DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.node = nodes.id) as a_serverCount')
)->join('locations', 'nodes.location', '=', 'locations.id')->paginate(20),
]);
}
public function getNew(Request $request)
{
return view('admin.nodes.new', [
'locations' => Models\Location::all()
]);
}
public function postNew(Request $request)
{
try {
$node = new NodeRepository;
$new = $node->create($request->except([
'_token'
]));
Alert::success('Successfully created new node. You should allocate some IP addresses to it now.')->flash();
return redirect()->route('admin.nodes.view', [
'id' => $new
]);
} catch (\Pterodactyl\Exceptions\DisplayValidationException $e) {
return redirect()->route('admin.nodes.new')->withErrors(json_decode($e->getMessage()))->withInput();
} catch (\Pterodactyl\Exceptions\DisplayException $e) {
Alert::danger($e->getMessage())->flash();
} catch (\Exception $e) {
Log::error($e);
Alert::danger('An unhandled exception occured while attempting to add this node. Please try again.')->flash();
}
return redirect()->route('admin.nodes.new')->withInput();
}
public function getView(Request $request, $id)
{
$node = Models\Node::findOrFail($id);
return view('admin.nodes.view', [
'node' => $node
]);
}
}

View file

@ -137,6 +137,39 @@ class AdminRoutes {
});
// Node Routes
$router->group([
'prefix' => 'admin/nodes',
'middleware' => [
'auth',
'admin'
]
], function () use ($router) {
// View All Nodes
$router->get('/', [
'as' => 'admin.nodes',
'uses' => 'Admin\NodesController@getIndex'
]);
// Add New Node
$router->get('/new', [
'as' => 'admin.nodes.new',
'uses' => 'Admin\NodesController@getNew'
]);
$router->post('/new', [
'uses' => 'Admin\NodesController@postNew'
]);
// View Node
$router->get('/view/{id}', [
'as' => 'admin.nodes.view',
'uses' => 'Admin\NodesController@getView'
]);
});
}
}

View file

@ -22,6 +22,13 @@ class Node extends Model
*/
protected $hidden = ['daemonSecret'];
/**
* Fields that are not mass assignable.
*
* @var array
*/
protected $guarded = ['id', 'created_at', 'updated_at'];
/**
* @var array
*/

View file

@ -0,0 +1,66 @@
<?php
namespace Pterodactyl\Repositories;
use Validator;
use Pterodactyl\Models;
use Pterodactyl\Services\UuidService;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Exceptions\DisplayValidationException;
class NodeRepository {
public function __construct()
{
//
}
public function create(array $data)
{
// Validate Fields
$validator = Validator::make($data, [
'name' => 'required|regex:/^([\w .-]{1,100})$/',
'location' => 'required|numeric|min:1|exists:locations,id',
'public' => 'required|numeric|between:0,1',
'fqdn' => 'required|string|unique:nodes,fqdn',
'scheme' => 'required|regex:/^(http(s)?)$/',
'memory' => 'required|numeric|min:1',
'memory_overallocate' => 'required|numeric|min:-1',
'disk' => 'required|numeric|min:1',
'disk_overallocate' => 'required|numeric|min:-1',
'daemonBase' => 'required|regex:/^([\/][\d\w.\-\/]+)$/',
'daemonSFTP' => 'required|numeric|between:1,65535',
'daemonListen' => 'required|numeric|between:1,65535'
]);
// 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());
}
// Verify the FQDN
if (!filter_var(gethostbyname($data['fqdn']), FILTER_VALIDATE_IP)) {
throw new DisplayException('The FQDN provided does not resolve to a valid IP address.');
}
// Should we be nulling the overallocations?
$data['memory_overallocate'] = ($data['memory_overallocate'] < 0) ? null : $data['memory_overallocate'];
$data['disk_overallocate'] = ($data['disk_overallocate'] < 0) ? null : $data['disk_overallocate'];
// Set the Secret
$uuid = new UuidService;
$data['daemonSecret'] = (string) $uuid->generate('nodes', 'daemonSecret');
// Store the Data
$node = new Models\Node;
$node->fill($data);
$node->save();
return $node->id;
}
}

View file

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

View file

@ -0,0 +1,51 @@
@extends('layouts.admin')
@section('title')
Node List
@endsection
@section('content')
<div class="col-md-12">
<ul class="breadcrumb">
<li><a href="/admin">Admin Control</a></li>
<li class="active">Nodes</li>
</ul>
<h3>All Nodes</h3><hr />
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Name</th>
<th>Location</th>
<th>FQDN</th>
<th>Memory</th>
<th>Disk</th>
<th class="text-center">Servers</th>
<th class="text-center">HTTPS</th>
<th class="text-center">Public</th>
</tr>
</thead>
<tbody>
@foreach ($nodes as $node)
<tr>
<td><a href="/admin/nodes/view/{{ $node->id }}">{{ $node->name }}</td>
<td>{{ $node->a_locationName }}</td>
<td><code>{{ $node->fqdn }}</code></td>
<td>{{ $node->memory }} MB</td>
<td>{{ $node->disk }} MB</td>
<td class="text-center">{{ $node->a_serverCount }}</td>
<td class="text-center" style="color:{{ ($node->scheme === 'https') ? '#50af51' : '#d9534f' }}"><i class="fa fa-{{ ($node->scheme === 'https') ? 'lock' : 'unlock' }}"></i></td>
<td class="text-center"><i class="fa fa-{{ ($node->public === 1) ? 'check' : 'times' }}"></i></td>
</tr>
@endforeach
</tbody>
</table>
<div class="row">
<div class="col-md-12 text-center">{!! $nodes->render() !!}</div>
</div>
</div>
<script>
$(document).ready(function () {
$('#sidebar_links').find("a[href='/admin/nodes']").addClass('active');
});
</script>
@endsection

View file

@ -0,0 +1,168 @@
@extends('layouts.admin')
@section('title')
Create Node
@endsection
@section('content')
<div class="col-md-12">
<ul class="breadcrumb">
<li><a href="/admin">Admin Control</a></li>
<li><a href="/admin/nodes">Nodes</a></li>
<li class="active">Create New Node</li>
</ul>
<h3>Create New Node</h3><hr />
<form action="/admin/nodes/new" method="POST">
<div class="well">
<div class="row">
<div class="form-group col-md-6">
<label for="name" class="control-label">Node Name</label>
<div>
<input type="text" autocomplete="off" name="name" class="form-control" value="{{ old('name') }}" />
<p class="text-muted"><small>Character limits: <code>a-zA-Z0-9_.-</code> and <code>[Space]</code> (min 1, max 100 characters).</small></p>
</div>
</div>
<div class="form-group col-md-4">
<label for="name" class="control-label">Location</label>
<div>
<select name="location" class="form-control">
@foreach($locations as $location)
<option value="{{ $location->id }}" {{ (old('location') === $location->id) ? 'checked' : '' }}>{{ $location->long }} ({{ $location->short }})</option>
@endforeach
</select>
</div>
</div>
<div class="form-group col-md-2">
<label for="public" class="control-label">Public <sup><a data-toggle="tooltip" data-placement="top" title="Allow automatic allocation to this Node?">?</a></sup></label>
<div>
<input type="radio" name="public" value="1" {{ (old('public') === '1') ? 'checked' : '' }} id="public_1" checked> <label for="public_1" style="padding-left:5px;">Yes</label><br />
<input type="radio" name="public" value="0" {{ (old('public') === '0') ? 'checked' : '' }} id="public_0"> <label for="public_0" style="padding-left:5px;">No</label>
</div>
</div>
</div>
</div>
<div class="well">
<div class="row">
<div class="form-group col-md-6">
<label for="fqdn" class="control-label">Fully Qualified Domain Name</label>
<div>
<input type="text" autocomplete="off" name="fqdn" class="form-control" value="{{ old('fqdn') }}" />
</div>
<p class="text-muted"><small>This <strong>must</strong> be a fully qualified domain name, you may not enter an IP address or a domain that does not exist.
<a tabindex="0" data-toggle="popover" data-trigger="focus" title="Why do I need a FQDN?" data-content="In order to secure communications between your server and this node we use SSL. We cannot generate a SSL certificate for IP Addresses, and as such you will need to provide a FQDN.">Why?</a>
</small></p>
</div>
<div class="form-group col-md-6">
<label for="scheme" class="control-label">Secure Socket Layer</label>
<div class="row" style="padding: 7px 0;">
<div class="col-xs-6">
<input type="radio" name="scheme" value="https" id="scheme_ssl" checked /> <label for="scheme_ssl" style="padding-left: 5px;">Enable HTTPS/SSL</label>
</div>
<div class="col-xs-6">
<input type="radio" name="scheme" value="http" id="scheme_nossl" /> <label for="scheme_nossl" style="padding-left: 5px;">Disable HTTPS/SSL</label>
</div>
</div>
<p class="text-muted"><small>You should always leave SSL enabled for nodes. Disabling SSL could allow a malicious user to intercept traffic between the panel and the daemon potentially exposing sensitive information.</small></p>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="well">
<div class="row">
<div class="form-group col-md-6 col-xs-6">
<label for="memory" class="control-label">Total Memory</label>
<div class="input-group">
<input type="text" name="memory" class="form-control" value="{{ old('memory') }}"/>
<span class="input-group-addon">MB</span>
</div>
</div>
<div class="form-group col-md-6 col-xs-6">
<label for="memory_overallocate" class="control-label">Overallocate</label>
<div class="input-group">
<input type="text" name="memory_overallocate" class="form-control" value="{{ old('memory_overallocate') }}"/>
<span class="input-group-addon">%</span>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<p class="text-muted"><small>Enter the total amount of memory avaliable for new servers. If you would like to allow overallocation of memory enter the percentage that you want to allow. To disable checking for overallocation enter <code>-1</code> into the field. Entering <code>0</code> will prevent creating new servers if it would put the node over the limit.</small></p>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<div class="well">
<div class="row">
<div class="form-group col-md-6 col-xs-6">
<label for="disk" class="control-label">Disk Space</label>
<div class="input-group">
<input type="text" name="disk" class="form-control" value="{{ old('disk') }}"/>
<span class="input-group-addon">MB</span>
</div>
</div>
<div class="form-group col-md-6 col-xs-6">
<label for="disk_overallocate" class="control-label">Overallocate</label>
<div class="input-group">
<input type="text" name="disk_overallocate" class="form-control" value="{{ old('disk_overallocate') }}"/>
<span class="input-group-addon">%</span>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<p class="text-muted"><small>Enter the total amount of disk space avaliable for new servers. If you would like to allow overallocation of disk space enter the percentage that you want to allow. To disable checking for overallocation enter <code>-1</code> into the field. Entering <code>0</code> will prevent creating new servers if it would put the node over the limit.</small></p>
</div>
</div>
</div>
</div>
</div>
<div class="well">
<div class="row">
<div class="form-group col-md-6">
<label for="daemonBase" class="control-label">Daemon Server File Location</label>
<div>
<input type="text" name="daemonBase" class="form-control" value="{{ old('daemonBase', '/srv/daemon-data') }}"/>
</div>
<p class="text-muted"><small>The location at which your server files will be stored. Most users do not need to change this.</small></p>
</div>
<div class="form-group col-md-6">
<div class="row">
<div class="form-group col-md-6">
<label for="daemonListen" class="control-label">Daemon Listening Port</label>
<div>
<input type="text" name="daemonListen" class="form-control" value="{{ old('daemonListen', '8080') }}"/>
</div>
</div>
<div class="form-group col-md-6">
<label for="daemonSFTP" class="control-label">Daemon SFTP Port</label>
<div>
<input type="text" name="daemonSFTP" class="form-control" value="{{ old('daemonSFTP', '2022') }}"/>
</div>
</div>
</div>
<p class="text-muted"><small>The daemon runs its own SFTP management container and does not use the SSHd process on the main physical server. <Strong>Do not use the same port that you have assigned for your physcial server's SSH process.</strong></small></p>
</div>
</div>
</div>
<div class="well">
<div class="row">
<div class="col-md-12">
{!! csrf_field() !!}
<input type="submit" class="btn btn-sm btn-primary" value="Create Node" />
</div>
</div>
</div>
</form>
</div>
<script>
$(document).ready(function () {
$('#sidebar_links').find("a[href='/admin/nodes/new']").addClass('active');
$('[data-toggle="popover"]').popover({
placement: 'auto'
});
$('[data-toggle="tooltip"]').tooltip();
});
</script>
@endsection