Add support for modification of server startup variables and command

This commit is contained in:
Dane Everitt 2016-01-10 18:57:22 -05:00
parent 62313eeac5
commit 1d97b0be98
5 changed files with 199 additions and 6 deletions

View file

@ -522,6 +522,111 @@ class ServerRepository
}
public function updateStartup($id, array $data)
{
$server = Models\Server::findOrFail($id);
DB::beginTransaction();
// Check the startup
if (isset($data['startup'])) {
$server->startup = $data['startup'];
$server->save();
}
// Check those Variables
$variables = Models\ServiceVariables::select('service_variables.*', 'server_variables.variable_value as a_currentValue')
->join('server_variables', 'server_variables.variable_id', '=', 'service_variables.id')
->where('option_id', $server->option)->get();
$variableList = [];
if ($variables) {
foreach($variables as &$variable) {
// Move on if the new data wasn't even sent
if (!isset($data[$variable->env_variable])) {
$variableList = array_merge($variableList, [[
'id' => $variable->id,
'env' => $variable->env_variable,
'val' => $variable->a_currentValue
]]);
continue;
}
// Update Empty but skip validation
if (empty($data[$variable->env_variable])) {
$variableList = array_merge($variableList, [[
'id' => $variable->id,
'env' => $variable->env_variable,
'val' => null
]]);
continue;
}
// Is the variable required?
// @TODO: is this even logical to perform this check?
if (isset($data[$variable->env_variable]) && empty($data[$variable->env_variable])) {
if ($variable->required === 1) {
throw new DisplayException('A required service option variable field (' . $variable->env_variable . ') was included in this request but was left blank.');
}
}
// Check aganist Regex Pattern
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, [[
'id' => $variable->id,
'env' => $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']
]);
$var = Models\ServerVariables::where('server_id', $server->id)->where('variable_id', $item['id'])->update([
'variable_value' => $item['val']
]);
}
try {
$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
],
'json' => [
'build' => [
'env|overwrite' => $environmentVariables
]
]
]);
DB::commit();
return true;
} catch (\GuzzleHttp\Exception\TransferException $ex) {
DB::rollBack();
throw new DisplayException('An error occured while attempting to update the server configuration: ' . $ex->getMessage());
} catch (\Exception $e) {
DB::rollBack();
throw $e;
}
}
public function deleteServer($id, $force)
{
$server = Models\Server::findOrFail($id);