Merge branch 'develop' into feature/service-changes
This commit is contained in:
commit
fc38b09e1f
169 changed files with 2268 additions and 2289 deletions
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
/**
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>
|
||||
* 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
|
||||
|
@ -21,29 +21,25 @@
|
|||
* 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 Debugbar;
|
||||
use Validator;
|
||||
use Log;
|
||||
|
||||
use Crypt;
|
||||
use Validator;
|
||||
use Pterodactyl\Models;
|
||||
use Pterodactyl\Events\ServerDeleted;
|
||||
use Pterodactyl\Services\UuidService;
|
||||
use Pterodactyl\Services\DeploymentService;
|
||||
use Pterodactyl\Notifications\ServerCreated;
|
||||
use Pterodactyl\Events\ServerDeleted;
|
||||
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Pterodactyl\Exceptions\AccountNotFoundException;
|
||||
use Pterodactyl\Notifications\ServerCreated;
|
||||
use Pterodactyl\Exceptions\DisplayValidationException;
|
||||
|
||||
class ServerRepository
|
||||
{
|
||||
|
||||
protected $daemonPermissions = [
|
||||
's:*'
|
||||
's:*',
|
||||
];
|
||||
|
||||
public function __construct()
|
||||
|
@ -53,7 +49,7 @@ class ServerRepository
|
|||
|
||||
/**
|
||||
* Generates a SFTP username for a server given a server name.
|
||||
* format: mumble_67c7a4b0
|
||||
* format: mumble_67c7a4b0.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $uuid
|
||||
|
@ -61,16 +57,15 @@ class ServerRepository
|
|||
*/
|
||||
protected function generateSFTPUsername($name, $uuid = null)
|
||||
{
|
||||
|
||||
$uuid = is_null($uuid) ? str_random(8) : $uuid;
|
||||
return strtolower(substr(preg_replace('/\s+/', '', $name), 0, 6) . '_' . $uuid);
|
||||
|
||||
return strtolower(substr(preg_replace('/\s+/', '', $name), 0, 6) . '_' . $uuid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new server to the system.
|
||||
* @param array $data An array of data descriptors for creating the server. These should align to the columns in the database.
|
||||
* @return integer
|
||||
* @return int
|
||||
*/
|
||||
public function create(array $data)
|
||||
{
|
||||
|
@ -89,26 +84,26 @@ class ServerRepository
|
|||
'pack' => 'bail|required|numeric|min:0',
|
||||
'startup' => 'string',
|
||||
'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',
|
||||
]);
|
||||
|
||||
$validator->sometimes('node', 'bail|required|numeric|min:1|exists:nodes,id', function ($input) {
|
||||
return !($input->auto_deploy);
|
||||
return ! ($input->auto_deploy);
|
||||
});
|
||||
|
||||
$validator->sometimes('ip', 'required|ip', function ($input) {
|
||||
return (!$input->auto_deploy && !$input->allocation);
|
||||
return ! $input->auto_deploy && ! $input->allocation;
|
||||
});
|
||||
|
||||
$validator->sometimes('port', 'required|numeric|min:1|max:65535', function ($input) {
|
||||
return (!$input->auto_deploy && !$input->allocation);
|
||||
return ! $input->auto_deploy && ! $input->allocation;
|
||||
});
|
||||
|
||||
$validator->sometimes('allocation', 'numeric|exists:allocations,id', function ($input) {
|
||||
return !($input->auto_deploy || ($input->port && $input->ip));
|
||||
return ! ($input->auto_deploy || ($input->port && $input->ip));
|
||||
});
|
||||
|
||||
|
||||
// Run validator, throw catchable and displayable exception if it fails.
|
||||
// Exception includes a JSON result of failed validation rules.
|
||||
if ($validator->fails()) {
|
||||
|
@ -121,12 +116,12 @@ class ServerRepository
|
|||
$user = Models\User::select('id', 'email')->where('email', $data['owner'])->first();
|
||||
}
|
||||
|
||||
if (!$user) {
|
||||
if (! $user) {
|
||||
throw new DisplayException('The user id or email passed to the function was not found on the system.');
|
||||
}
|
||||
|
||||
$autoDeployed = false;
|
||||
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
|
||||
// Ignore any other passed node data
|
||||
unset($data['node'], $data['ip'], $data['port'], $data['allocation']);
|
||||
|
@ -140,16 +135,16 @@ class ServerRepository
|
|||
|
||||
// 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
|
||||
if (!$autoDeployed) {
|
||||
if (!isset($data['allocation'])) {
|
||||
if (! $autoDeployed) {
|
||||
if (! isset($data['allocation'])) {
|
||||
$allocation = Models\Allocation::where('ip', $data['ip'])->where('port', $data['port'])->where('node', $data['node'])->whereNull('assigned_to')->first();
|
||||
} 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();
|
||||
}
|
||||
}
|
||||
|
||||
// Something failed in the query, either that combo doesn't exist, or it is in use.
|
||||
if (!$allocation) {
|
||||
if (! $allocation) {
|
||||
throw new DisplayException('The selected IP/Port combination or Allocation ID is either already in use, or unavaliable for this node.');
|
||||
}
|
||||
|
||||
|
@ -158,7 +153,7 @@ class ServerRepository
|
|||
// We need to verify that the option exists for the service, and then check for
|
||||
// any required variable fields. (fields are labeled env_<env_variable>)
|
||||
$option = Models\ServiceOptions::where('id', $data['option'])->where('parent_service', $data['service'])->first();
|
||||
if (!$option) {
|
||||
if (! $option) {
|
||||
throw new DisplayException('The requested service option does not exist for the specified service.');
|
||||
}
|
||||
|
||||
|
@ -181,46 +176,45 @@ class ServerRepository
|
|||
$variables = Models\ServiceVariables::where('option_id', $data['option'])->get();
|
||||
$variableList = [];
|
||||
if ($variables) {
|
||||
foreach($variables as $variable) {
|
||||
foreach ($variables as $variable) {
|
||||
|
||||
// Is the variable required?
|
||||
if (!$data['env_' . $variable->env_variable]) {
|
||||
if (! $data['env_' . $variable->env_variable]) {
|
||||
if ($variable->required === 1) {
|
||||
throw new DisplayException('A required service option variable field (env_' . $variable->env_variable . ') was missing from the request.');
|
||||
}
|
||||
$variableList = array_merge($variableList, [[
|
||||
$variableList[] = [
|
||||
'id' => $variable->id,
|
||||
'env' => $variable->env_variable,
|
||||
'val' => $variable->default_value
|
||||
]]);
|
||||
'val' => $variable->default_value,
|
||||
];
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check aganist Regex Pattern
|
||||
if (!is_null($variable->regex) && !preg_match($variable->regex, $data['env_' . $variable->env_variable])) {
|
||||
if (! is_null($variable->regex) && ! preg_match($variable->regex, $data['env_' . $variable->env_variable])) {
|
||||
throw new DisplayException('Failed to validate service option variable field (env_' . $variable->env_variable . ') aganist regex (' . $variable->regex . ').');
|
||||
}
|
||||
|
||||
$variableList = array_merge($variableList, [[
|
||||
$variableList[] = [
|
||||
'id' => $variable->id,
|
||||
'env' => $variable->env_variable,
|
||||
'val' => $data['env_' . $variable->env_variable]
|
||||
]]);
|
||||
'val' => $data['env_' . $variable->env_variable],
|
||||
];
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Check Overallocation
|
||||
if (!$autoDeployed) {
|
||||
if (! $autoDeployed) {
|
||||
if (is_numeric($node->memory_overallocate) || is_numeric($node->disk_overallocate)) {
|
||||
|
||||
$totals = Models\Server::select(DB::raw('SUM(memory) as memory, SUM(disk) as disk'))->where('node', $node->id)->first();
|
||||
|
||||
// Check memory limits
|
||||
if (is_numeric($node->memory_overallocate)) {
|
||||
$newMemory = $totals->memory + $data['memory'];
|
||||
$memoryLimit = ($node->memory * (1 + ($node->memory_overallocate / 100)));
|
||||
if($newMemory > $memoryLimit) {
|
||||
if ($newMemory > $memoryLimit) {
|
||||
throw new DisplayException('The amount of memory allocated to this server would put the node over its allocation limits. This node is allowed ' . ($node->memory_overallocate + 100) . '% of its assigned ' . $node->memory . 'Mb of memory (' . $memoryLimit . 'Mb) of which ' . (($totals->memory / $node->memory) * 100) . '% (' . $totals->memory . 'Mb) is in use already. By allocating this server the node would be at ' . (($newMemory / $node->memory) * 100) . '% (' . $newMemory . 'Mb) usage.');
|
||||
}
|
||||
}
|
||||
|
@ -229,7 +223,7 @@ class ServerRepository
|
|||
if (is_numeric($node->disk_overallocate)) {
|
||||
$newDisk = $totals->disk + $data['disk'];
|
||||
$diskLimit = ($node->disk * (1 + ($node->disk_overallocate / 100)));
|
||||
if($newDisk > $diskLimit) {
|
||||
if ($newDisk > $diskLimit) {
|
||||
throw new DisplayException('The amount of disk allocated to this server would put the node over its allocation limits. This node is allowed ' . ($node->disk_overallocate + 100) . '% of its assigned ' . $node->disk . 'Mb of disk (' . $diskLimit . 'Mb) of which ' . (($totals->disk / $node->disk) * 100) . '% (' . $totals->disk . 'Mb) is in use already. By allocating this server the node would be at ' . (($newDisk / $node->disk) * 100) . '% (' . $newDisk . 'Mb) usage.');
|
||||
}
|
||||
}
|
||||
|
@ -245,6 +239,11 @@ class ServerRepository
|
|||
$server = new Models\Server;
|
||||
$genUuid = $uuid->generate('servers', 'uuid');
|
||||
$genShortUuid = $uuid->generateShort('servers', 'uuidShort', $genUuid);
|
||||
|
||||
if (isset($data['custom_id'])) {
|
||||
$server->id = $data['custom_id'];
|
||||
}
|
||||
|
||||
$server->fill([
|
||||
'uuid' => $genUuid,
|
||||
'uuidShort' => $genShortUuid,
|
||||
|
@ -266,7 +265,7 @@ class ServerRepository
|
|||
'daemonSecret' => $uuid->generate('servers', 'daemonSecret'),
|
||||
'image' => (isset($data['custom_image_name'])) ? $data['custom_image_name'] : $option->docker_image,
|
||||
'username' => $this->generateSFTPUsername($data['name'], $genShortUuid),
|
||||
'sftp_password' => Crypt::encrypt('not set')
|
||||
'sftp_password' => Crypt::encrypt('not set'),
|
||||
]);
|
||||
$server->save();
|
||||
|
||||
|
@ -275,18 +274,17 @@ class ServerRepository
|
|||
$allocation->save();
|
||||
|
||||
// Add Variables
|
||||
$environmentVariables = [];
|
||||
$environmentVariables = array_merge($environmentVariables, [
|
||||
'STARTUP' => $data['startup']
|
||||
]);
|
||||
foreach($variableList as $item) {
|
||||
$environmentVariables = array_merge($environmentVariables, [
|
||||
$item['env'] => $item['val']
|
||||
]);
|
||||
$environmentVariables = [
|
||||
'STARTUP' => $data['startup'],
|
||||
];
|
||||
|
||||
foreach ($variableList as $item) {
|
||||
$environmentVariables[$item['env']] = $item['val'];
|
||||
|
||||
Models\ServerVariables::create([
|
||||
'server_id' => $server->id,
|
||||
'variable_id' => $item['id'],
|
||||
'variable_value' => $item['val']
|
||||
'variable_value' => $item['val'],
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -297,13 +295,13 @@ class ServerRepository
|
|||
'node' => $node->name,
|
||||
'service' => $service->name,
|
||||
'option' => $option->name,
|
||||
'uuidShort' => $server->uuidShort
|
||||
'uuidShort' => $server->uuidShort,
|
||||
])));
|
||||
|
||||
$client = Models\Node::guzzleRequest($node->id);
|
||||
$client->request('POST', '/servers', [
|
||||
'headers' => [
|
||||
'X-Access-Token' => $node->daemonSecret
|
||||
'X-Access-Token' => $node->daemonSecret,
|
||||
],
|
||||
'json' => [
|
||||
'uuid' => (string) $server->uuid,
|
||||
|
@ -314,7 +312,7 @@ class ServerRepository
|
|||
'port' => (int) $allocation->port,
|
||||
],
|
||||
'ports' => [
|
||||
(string) $allocation->ip => [ (int) $allocation->port ],
|
||||
(string) $allocation->ip => [(int) $allocation->port],
|
||||
],
|
||||
'env' => $environmentVariables,
|
||||
'memory' => (int) $server->memory,
|
||||
|
@ -333,10 +331,11 @@ class ServerRepository
|
|||
(string) $server->daemonSecret => $this->daemonPermissions,
|
||||
],
|
||||
'rebuild' => false,
|
||||
]
|
||||
],
|
||||
]);
|
||||
|
||||
DB::commit();
|
||||
|
||||
return $server->id;
|
||||
} catch (\GuzzleHttp\Exception\TransferException $ex) {
|
||||
DB::rollBack();
|
||||
|
@ -345,25 +344,23 @@ class ServerRepository
|
|||
DB::rollBack();
|
||||
throw $ex;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* [updateDetails description]
|
||||
* @param integer $id
|
||||
* [updateDetails description].
|
||||
* @param int $id
|
||||
* @param array $data
|
||||
* @return boolean
|
||||
* @return bool
|
||||
*/
|
||||
public function updateDetails($id, array $data)
|
||||
{
|
||||
|
||||
$uuid = new UuidService;
|
||||
$resetDaemonKey = false;
|
||||
|
||||
// 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.
|
||||
|
@ -400,8 +397,9 @@ class ServerRepository
|
|||
$server->save();
|
||||
|
||||
// Do we need to update? If not, return successful.
|
||||
if (!$resetDaemonKey) {
|
||||
if (! $resetDaemonKey) {
|
||||
DB::commit();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -412,19 +410,20 @@ class ServerRepository
|
|||
$res = $client->request('PATCH', '/server', [
|
||||
'headers' => [
|
||||
'X-Access-Server' => $server->uuid,
|
||||
'X-Access-Token' => $node->daemonSecret
|
||||
'X-Access-Token' => $node->daemonSecret,
|
||||
],
|
||||
'exceptions' => false,
|
||||
'json' => [
|
||||
'keys' => [
|
||||
(string) $oldDaemonKey => [],
|
||||
(string) $server->daemonSecret => $this->daemonPermissions
|
||||
]
|
||||
]
|
||||
(string) $server->daemonSecret => $this->daemonPermissions,
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
if ($res->getStatusCode() === 204) {
|
||||
DB::commit();
|
||||
|
||||
return true;
|
||||
} else {
|
||||
throw new DisplayException('Daemon returned a a non HTTP/204 error code. HTTP/' + $res->getStatusCode());
|
||||
|
@ -434,11 +433,10 @@ class ServerRepository
|
|||
Log::error($ex);
|
||||
throw new DisplayException('An error occured while attempting to update this server\'s information.');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* [updateContainer description]
|
||||
* [updateContainer description].
|
||||
* @param int $id
|
||||
* @param array $data
|
||||
* @return bool
|
||||
|
@ -446,7 +444,7 @@ class ServerRepository
|
|||
public function updateContainer($id, array $data)
|
||||
{
|
||||
$validator = Validator::make($data, [
|
||||
'image' => 'required|string'
|
||||
'image' => 'required|string',
|
||||
]);
|
||||
|
||||
// Run validator, throw catchable and displayable exception if it fails.
|
||||
|
@ -468,16 +466,17 @@ class ServerRepository
|
|||
$client->request('PATCH', '/server', [
|
||||
'headers' => [
|
||||
'X-Access-Server' => $server->uuid,
|
||||
'X-Access-Token' => $node->daemonSecret
|
||||
'X-Access-Token' => $node->daemonSecret,
|
||||
],
|
||||
'json' => [
|
||||
'build' => [
|
||||
'image' => $server->image
|
||||
]
|
||||
]
|
||||
'image' => $server->image,
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
DB::commit();
|
||||
|
||||
return true;
|
||||
} catch (\GuzzleHttp\Exception\TransferException $ex) {
|
||||
DB::rollBack();
|
||||
|
@ -486,22 +485,20 @@ class ServerRepository
|
|||
DB::rollBack();
|
||||
throw $ex;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* [changeBuild description]
|
||||
* @param integer $id
|
||||
* [changeBuild description].
|
||||
* @param int $id
|
||||
* @param array $data
|
||||
* @return boolean
|
||||
* @return bool
|
||||
*/
|
||||
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})$/'
|
||||
'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' => 'nullable|array',
|
||||
'remove_additional' => 'nullable|array',
|
||||
|
@ -509,7 +506,7 @@ class ServerRepository
|
|||
'swap' => 'integer|min:-1',
|
||||
'io' => 'integer|min:10|max:1000',
|
||||
'cpu' => 'integer|min:0',
|
||||
'disk' => 'integer|min:0'
|
||||
'disk' => 'integer|min:0',
|
||||
]);
|
||||
|
||||
// Run validator, throw catchable and displayable exception if it fails.
|
||||
|
@ -530,14 +527,14 @@ class ServerRepository
|
|||
list($ip, $port) = explode(':', $data['default']);
|
||||
if ($ip !== $allocation->ip || (int) $port !== $allocation->port) {
|
||||
$selection = Models\Allocation::where('ip', $ip)->where('port', $port)->where('assigned_to', $server->id)->first();
|
||||
if (!$selection) {
|
||||
if (! $selection) {
|
||||
throw new DisplayException('The requested default connection (' . $ip . ':' . $port . ') is not allocated to this server.');
|
||||
}
|
||||
|
||||
$server->allocation = $selection->id;
|
||||
$newBuild['default'] = [
|
||||
'ip' => $ip,
|
||||
'port' => (int) $port
|
||||
'port' => (int) $port,
|
||||
];
|
||||
|
||||
// Re-Run to keep updated for rest of function
|
||||
|
@ -551,7 +548,7 @@ class ServerRepository
|
|||
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)) {
|
||||
if (! filter_var($ip, FILTER_VALIDATE_IP) || ! preg_match('/^(\d{1,5})$/', $port)) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -562,7 +559,7 @@ class ServerRepository
|
|||
|
||||
$newPorts = true;
|
||||
Models\Allocation::where('ip', $ip)->where('port', $port)->where('assigned_to', $server->id)->update([
|
||||
'assigned_to' => null
|
||||
'assigned_to' => null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@ -572,7 +569,7 @@ class ServerRepository
|
|||
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)) {
|
||||
if (! filter_var($ip, FILTER_VALIDATE_IP) || ! preg_match('/^(\d{1,5})$/', $port)) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -583,7 +580,7 @@ class ServerRepository
|
|||
|
||||
$newPorts = true;
|
||||
Models\Allocation::where('ip', $ip)->where('port', $port)->whereNull('assigned_to')->update([
|
||||
'assigned_to' => $server->id
|
||||
'assigned_to' => $server->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@ -593,9 +590,9 @@ class ServerRepository
|
|||
$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);
|
||||
array_push($additionalAssignments[(string) $assignment->ip], (int) $assignment->port);
|
||||
} else {
|
||||
$additionalAssignments[ (string) $assignment->ip ] = [ (int) $assignment->port ];
|
||||
$additionalAssignments[(string) $assignment->ip] = [(int) $assignment->port];
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -636,22 +633,23 @@ class ServerRepository
|
|||
// This won't be committed unless the HTTP request succeedes anyways
|
||||
$server->save();
|
||||
|
||||
if (!empty($newBuild)) {
|
||||
if (! empty($newBuild)) {
|
||||
$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
|
||||
'X-Access-Token' => $node->daemonSecret,
|
||||
],
|
||||
'json' => [
|
||||
'build' => $newBuild
|
||||
]
|
||||
'build' => $newBuild,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
|
||||
return true;
|
||||
} catch (\GuzzleHttp\Exception\TransferException $ex) {
|
||||
DB::rollBack();
|
||||
|
@ -660,12 +658,10 @@ class ServerRepository
|
|||
DB::rollBack();
|
||||
throw $ex;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function updateStartup($id, array $data, $admin = false)
|
||||
{
|
||||
|
||||
$server = Models\Server::findOrFail($id);
|
||||
|
||||
DB::beginTransaction();
|
||||
|
@ -687,24 +683,24 @@ class ServerRepository
|
|||
|
||||
$variableList = [];
|
||||
if ($variables) {
|
||||
foreach($variables as &$variable) {
|
||||
foreach ($variables as &$variable) {
|
||||
// Move on if the new data wasn't even sent
|
||||
if (!isset($data[$variable->env_variable])) {
|
||||
$variableList = array_merge($variableList, [[
|
||||
if (! isset($data[$variable->env_variable])) {
|
||||
$variableList[] = [
|
||||
'id' => $variable->id,
|
||||
'env' => $variable->env_variable,
|
||||
'val' => $variable->a_currentValue
|
||||
]]);
|
||||
'val' => $variable->a_currentValue,
|
||||
];
|
||||
continue;
|
||||
}
|
||||
|
||||
// Update Empty but skip validation
|
||||
if (empty($data[$variable->env_variable])) {
|
||||
$variableList = array_merge($variableList, [[
|
||||
$variableList[] = [
|
||||
'id' => $variable->id,
|
||||
'env' => $variable->env_variable,
|
||||
'val' => null
|
||||
]]);
|
||||
'val' => null,
|
||||
];
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -717,37 +713,34 @@ class ServerRepository
|
|||
}
|
||||
|
||||
// Variable hidden and/or not user editable
|
||||
if (($variable->user_viewable === 0 || $variable->user_editable === 0) && !$admin) {
|
||||
if (($variable->user_viewable === 0 || $variable->user_editable === 0) && ! $admin) {
|
||||
throw new DisplayException('A service option variable field (' . $variable->env_variable . ') does not exist or you do not have permission to edit it.');
|
||||
}
|
||||
|
||||
// Check aganist Regex Pattern
|
||||
if (!is_null($variable->regex) && !preg_match($variable->regex, $data[$variable->env_variable])) {
|
||||
if (! is_null($variable->regex) && ! preg_match($variable->regex, $data[$variable->env_variable])) {
|
||||
throw new DisplayException('Failed to validate service option variable field (' . $variable->env_variable . ') aganist regex (' . $variable->regex . ').');
|
||||
}
|
||||
|
||||
$variableList = array_merge($variableList, [[
|
||||
$variableList[] = [
|
||||
'id' => $variable->id,
|
||||
'env' => $variable->env_variable,
|
||||
'val' => $data[$variable->env_variable]
|
||||
]]);
|
||||
'val' => $data[$variable->env_variable],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// Add Variables
|
||||
$environmentVariables = [];
|
||||
$environmentVariables = array_merge($environmentVariables, [
|
||||
'STARTUP' => $server->startup
|
||||
]);
|
||||
foreach($variableList as $item) {
|
||||
$environmentVariables = array_merge($environmentVariables, [
|
||||
$item['env'] => $item['val']
|
||||
]);
|
||||
$environmentVariables = [
|
||||
'STARTUP' => $server->startup,
|
||||
];
|
||||
foreach ($variableList as $item) {
|
||||
$environmentVariables[$item['env']] = $item['val'];
|
||||
|
||||
// Update model or make a new record if it doesn't exist.
|
||||
$model = Models\ServerVariables::firstOrNew([
|
||||
'variable_id' => $item['id'],
|
||||
'server_id' => $server->id
|
||||
'server_id' => $server->id,
|
||||
]);
|
||||
$model->variable_value = $item['val'];
|
||||
$model->save();
|
||||
|
@ -759,16 +752,17 @@ class ServerRepository
|
|||
$client->request('PATCH', '/server', [
|
||||
'headers' => [
|
||||
'X-Access-Server' => $server->uuid,
|
||||
'X-Access-Token' => $node->daemonSecret
|
||||
'X-Access-Token' => $node->daemonSecret,
|
||||
],
|
||||
'json' => [
|
||||
'build' => [
|
||||
'env|overwrite' => $environmentVariables
|
||||
]
|
||||
]
|
||||
'env|overwrite' => $environmentVariables,
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
DB::commit();
|
||||
|
||||
return true;
|
||||
} catch (\GuzzleHttp\Exception\TransferException $ex) {
|
||||
DB::rollBack();
|
||||
|
@ -777,7 +771,6 @@ class ServerRepository
|
|||
DB::rollBack();
|
||||
throw $ex;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function deleteServer($id, $force)
|
||||
|
@ -801,13 +794,14 @@ class ServerRepository
|
|||
}
|
||||
}
|
||||
|
||||
public function deleteNow($id, $force = false) {
|
||||
public function deleteNow($id, $force = false)
|
||||
{
|
||||
$server = Models\Server::withTrashed()->findOrFail($id);
|
||||
$node = Models\Node::findOrFail($server->node);
|
||||
|
||||
// Handle server being restored previously or
|
||||
// an accidental queue.
|
||||
if (!$server->trashed()) {
|
||||
if (! $server->trashed()) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -815,7 +809,7 @@ class ServerRepository
|
|||
try {
|
||||
// Unassign Allocations
|
||||
Models\Allocation::where('assigned_to', $server->id)->update([
|
||||
'assigned_to' => null
|
||||
'assigned_to' => null,
|
||||
]);
|
||||
|
||||
// Remove Variables
|
||||
|
@ -837,7 +831,7 @@ class ServerRepository
|
|||
// This is the one un-recoverable point where
|
||||
// transactions will not save us.
|
||||
$repository = new DatabaseRepository;
|
||||
foreach(Models\Database::select('id')->where('server_id', $server->id)->get() as &$database) {
|
||||
foreach (Models\Database::select('id')->where('server_id', $server->id)->get() as &$database) {
|
||||
$repository->drop($database->id);
|
||||
}
|
||||
|
||||
|
@ -845,8 +839,8 @@ class ServerRepository
|
|||
$client->request('DELETE', '/servers', [
|
||||
'headers' => [
|
||||
'X-Access-Token' => $node->daemonSecret,
|
||||
'X-Access-Server' => $server->uuid
|
||||
]
|
||||
'X-Access-Server' => $server->uuid,
|
||||
],
|
||||
]);
|
||||
|
||||
$server->forceDelete();
|
||||
|
@ -882,13 +876,14 @@ class ServerRepository
|
|||
throw new DisplayException('This server was marked as having a failed install, you cannot override this.');
|
||||
}
|
||||
$server->installed = ($server->installed === 1) ? 0 : 1;
|
||||
|
||||
return $server->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Suspends a server instance making it unable to be booted or used by a user.
|
||||
* @param integer $id
|
||||
* @return boolean
|
||||
* @param int $id
|
||||
* @return bool
|
||||
*/
|
||||
public function suspend($id, $deleted = false)
|
||||
{
|
||||
|
@ -911,8 +906,8 @@ class ServerRepository
|
|||
$client->request('POST', '/server/suspend', [
|
||||
'headers' => [
|
||||
'X-Access-Token' => $node->daemonSecret,
|
||||
'X-Access-Server' => $server->uuid
|
||||
]
|
||||
'X-Access-Server' => $server->uuid,
|
||||
],
|
||||
]);
|
||||
|
||||
return DB::commit();
|
||||
|
@ -927,8 +922,8 @@ class ServerRepository
|
|||
|
||||
/**
|
||||
* Unsuspends a server instance.
|
||||
* @param integer $id
|
||||
* @return boolean
|
||||
* @param int $id
|
||||
* @return bool
|
||||
*/
|
||||
public function unsuspend($id)
|
||||
{
|
||||
|
@ -951,8 +946,8 @@ class ServerRepository
|
|||
$client->request('POST', '/server/unsuspend', [
|
||||
'headers' => [
|
||||
'X-Access-Token' => $node->daemonSecret,
|
||||
'X-Access-Server' => $server->uuid
|
||||
]
|
||||
'X-Access-Server' => $server->uuid,
|
||||
],
|
||||
]);
|
||||
|
||||
return DB::commit();
|
||||
|
@ -973,7 +968,7 @@ class ServerRepository
|
|||
$validator = Validator::make([
|
||||
'password' => $password,
|
||||
], [
|
||||
'password' => 'required|regex:/^((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})$/'
|
||||
'password' => 'required|regex:/^((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})$/',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
|
@ -990,7 +985,7 @@ class ServerRepository
|
|||
$client->request('POST', '/server/password', [
|
||||
'headers' => [
|
||||
'X-Access-Token' => $node->daemonSecret,
|
||||
'X-Access-Server' => $server->uuid
|
||||
'X-Access-Server' => $server->uuid,
|
||||
],
|
||||
'json' => [
|
||||
'password' => $password,
|
||||
|
@ -998,6 +993,7 @@ class ServerRepository
|
|||
]);
|
||||
|
||||
DB::commit();
|
||||
|
||||
return true;
|
||||
} catch (\GuzzleHttp\Exception\TransferException $ex) {
|
||||
DB::rollBack();
|
||||
|
@ -1006,7 +1002,5 @@ class ServerRepository
|
|||
DB::rollBack();
|
||||
throw $ex;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Reference in a new issue