Add basic support for per-server databases
Still missing ability to define database servers
This commit is contained in:
parent
76049c5860
commit
7013d10987
8 changed files with 503 additions and 1 deletions
|
@ -29,6 +29,7 @@ use Log;
|
|||
|
||||
use Pterodactyl\Models;
|
||||
use Pterodactyl\Repositories\ServerRepository;
|
||||
use Pterodactyl\Repositories\DatabaseRepository;
|
||||
|
||||
use Pterodactly\Exceptions\DisplayException;
|
||||
use Pterodactly\Exceptions\DisplayValidationException;
|
||||
|
@ -95,7 +96,12 @@ class ServersController extends Controller
|
|||
->join('server_variables', 'server_variables.variable_id', '=', 'service_variables.id')
|
||||
->where('service_variables.option_id', $server->option)
|
||||
->where('server_variables.server_id', $server->id)
|
||||
->get()
|
||||
->get(),
|
||||
'databases' => Models\Database::select('databases.*', 'database_servers.host as a_host', 'database_servers.port as a_port')
|
||||
->where('server', $server->id)
|
||||
->join('database_servers', 'database_servers.id', '=', 'databases.db_server')
|
||||
->get(),
|
||||
'db_servers' => Models\DatabaseServer::all()
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -375,4 +381,42 @@ class ServersController extends Controller
|
|||
}
|
||||
}
|
||||
|
||||
public function postDatabase(Request $request, $id)
|
||||
{
|
||||
try {
|
||||
$repo = new DatabaseRepository;
|
||||
$repo->create($id, $request->except([
|
||||
'_token'
|
||||
]));
|
||||
Alert::success('Added new database to this server.')->flash();
|
||||
} catch (\Pterodactyl\Exceptions\DisplayValidationException $ex) {
|
||||
return redirect()->route('admin.servers.view', [
|
||||
'id' => $id,
|
||||
'tab' => 'tab_database'
|
||||
])->withInput()->withErrors(json_decode($ex->getMessage()))->withInput();
|
||||
} catch (\Exception $ex) {
|
||||
Log::error($ex);
|
||||
Alert::danger('An exception occured while attempting to add a new database for this server.')->flash();
|
||||
}
|
||||
|
||||
return redirect()->route('admin.servers.view', [
|
||||
'id' => $id,
|
||||
'tab' => 'tab_database'
|
||||
])->withInput();
|
||||
}
|
||||
|
||||
public function deleteDatabase(Request $request, $id, $database)
|
||||
{
|
||||
try {
|
||||
$repo = new DatabaseRepository;
|
||||
$repo->drop($database);
|
||||
return response('', 204);
|
||||
} catch (\Exception $ex) {
|
||||
Log::error($ex);
|
||||
return response()->json([
|
||||
'error' => 'An exception occured while attempting to delete this database.'
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -152,6 +152,16 @@ class AdminRoutes {
|
|||
'uses' => 'Admin\ServersController@getView'
|
||||
]);
|
||||
|
||||
// Database Stuffs
|
||||
$router->post('/view/{id}/database', [
|
||||
'as' => 'admin.servers.database',
|
||||
'uses' => 'Admin\ServersController@postDatabase'
|
||||
]);
|
||||
|
||||
$router->delete('/view/{id}/database/{database}', [
|
||||
'uses' => 'Admin\ServersController@deleteDatabase'
|
||||
]);
|
||||
|
||||
// Change Server Details
|
||||
$router->post('/view/{id}/details', [
|
||||
'uses' => 'Admin\ServersController@postUpdateServerDetails'
|
||||
|
|
62
app/Models/Database.php
Normal file
62
app/Models/Database.php
Normal file
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
/**
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
namespace Pterodactyl\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Database extends Model
|
||||
{
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'databases';
|
||||
|
||||
/**
|
||||
* The attributes excluded from the model's JSON form.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $hidden = ['password'];
|
||||
|
||||
/**
|
||||
* Fields that are not mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $guarded = ['id', 'created_at', 'updated_at'];
|
||||
|
||||
/**
|
||||
* Cast values to correct type.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'server' => 'integer',
|
||||
'db_server' => 'integer',
|
||||
];
|
||||
|
||||
}
|
61
app/Models/DatabaseServer.php
Normal file
61
app/Models/DatabaseServer.php
Normal file
|
@ -0,0 +1,61 @@
|
|||
<?php
|
||||
/**
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
namespace Pterodactyl\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class DatabaseServer extends Model
|
||||
{
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'database_servers';
|
||||
|
||||
/**
|
||||
* The attributes excluded from the model's JSON form.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $hidden = ['password'];
|
||||
|
||||
/**
|
||||
* Fields that are not mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $guarded = ['id', 'created_at', 'updated_at'];
|
||||
|
||||
/**
|
||||
* Cast values to correct type.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'id' => 'integer',
|
||||
];
|
||||
|
||||
}
|
146
app/Repositories/DatabaseRepository.php
Normal file
146
app/Repositories/DatabaseRepository.php
Normal file
|
@ -0,0 +1,146 @@
|
|||
<?php
|
||||
/**
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
namespace Pterodactyl\Repositories;
|
||||
|
||||
use Crypt;
|
||||
use DB;
|
||||
use Validator;
|
||||
|
||||
use Pterodactyl\Models;
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Pterodactyl\Exceptions\DisplayValidationException;
|
||||
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
|
||||
class DatabaseRepository {
|
||||
|
||||
/**
|
||||
* Adds a new database to a given database server.
|
||||
* @param int $server Id of the server to add a database for.
|
||||
* @param array $options Array of options for creating that database.
|
||||
* @return void
|
||||
*/
|
||||
public function create($server, $options)
|
||||
{
|
||||
$server = Models\Server::findOrFail($server);
|
||||
$validator = Validator::make($options, [
|
||||
'db_server' => 'required|exists:database_servers,id',
|
||||
'database' => 'required|regex:/^\w{1,100}$/',
|
||||
'remote' => 'required|regex:/^[0-9%.]{1,15}$/',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
throw new DisplayValidationException($validator->errors());
|
||||
}
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
$db = new Models\Database;
|
||||
$db->fill([
|
||||
'server' => $server->id,
|
||||
'db_server' => $options['db_server'],
|
||||
'database' => $server->uuidShort . '_' . $options['database'],
|
||||
'username' => $server->uuidShort . '_' . str_random(7),
|
||||
'remote' => $options['remote'],
|
||||
'password' => Crypt::encrypt(str_random(20))
|
||||
]);
|
||||
$db->save();
|
||||
|
||||
// Contact Remote
|
||||
$dbr = Models\DatabaseServer::findOrFail($options['db_server']);
|
||||
|
||||
try {
|
||||
|
||||
$capsule = new Capsule;
|
||||
$capsule->addConnection([
|
||||
'driver' => 'mysql',
|
||||
'host' => $dbr->host,
|
||||
'port' => $dbr->port,
|
||||
'database' => 'mysql',
|
||||
'username' => $dbr->username,
|
||||
'password' => Crypt::decrypt($dbr->password),
|
||||
'charset' => 'utf8',
|
||||
'collation' => 'utf8_unicode_ci',
|
||||
'prefix' => ''
|
||||
]);
|
||||
|
||||
$capsule->setAsGlobal();
|
||||
|
||||
Capsule::statement('CREATE DATABASE ' . $db->database);
|
||||
Capsule::statement('CREATE USER \'' . $db->username . '\'@\'' . $db->remote . '\' IDENTIFIED BY \'' . Crypt::decrypt($db->password) . '\'');
|
||||
Capsule::statement('GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, INDEX ON ' . $db->database . '.* TO \'' . $db->username . '\'@\'' . $db->remote . '\'');
|
||||
Capsule::statement('FLUSH PRIVILEGES');
|
||||
|
||||
DB::commit();
|
||||
return true;
|
||||
} catch (\Exception $ex) {
|
||||
DB::rollback();
|
||||
throw $ex;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops a database from the associated MySQL Server
|
||||
* @param int $database The ID of the database to drop.
|
||||
* @return boolean
|
||||
*/
|
||||
public function drop($database)
|
||||
{
|
||||
$db = Models\Database::findOrFail($database);
|
||||
$dbr = Models\DatabaseServer::findOrFail($db->db_server);
|
||||
|
||||
try {
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
$capsule = new Capsule;
|
||||
$capsule->addConnection([
|
||||
'driver' => 'mysql',
|
||||
'host' => $dbr->host,
|
||||
'port' => $dbr->port,
|
||||
'database' => 'mysql',
|
||||
'username' => $dbr->username,
|
||||
'password' => Crypt::decrypt($dbr->password),
|
||||
'charset' => 'utf8',
|
||||
'collation' => 'utf8_unicode_ci',
|
||||
'prefix' => ''
|
||||
]);
|
||||
|
||||
$capsule->setAsGlobal();
|
||||
|
||||
Capsule::statement('DROP USER \'' . $db->username . '\'@\'' . $db->remote . '\'');
|
||||
Capsule::statement('DROP DATABASE ' . $db->database);
|
||||
|
||||
$db->delete();
|
||||
|
||||
DB::commit();
|
||||
return true;
|
||||
} catch (\Exception $ex) {
|
||||
DB::rollback();
|
||||
throw $ex;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue