Skip to content

Commit 4f61637

Browse files
committed
More model updates to more places than I anticipated.
This probably broke a lot of things.
1 parent 0d61417 commit 4f61637

21 files changed

+340
-434
lines changed

app/Http/Controllers/Admin/LocationsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function postLocation(Request $request)
9797
{
9898
try {
9999
$location = new LocationRepository;
100-
$id = $location->create($request->only(['long', 'short']));
100+
$location->create($request->only(['long', 'short']));
101101
Alert::success('New location successfully added.')->flash();
102102

103103
return redirect()->route('admin.locations');

app/Http/Controllers/Admin/ServersController.php

Lines changed: 48 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -47,63 +47,8 @@ public function __construct()
4747

4848
public function getIndex(Request $request)
4949
{
50-
$query = Models\Server::withTrashed()->select(
51-
'servers.*',
52-
'nodes.name as a_nodeName',
53-
'users.email as a_ownerEmail',
54-
'allocations.ip',
55-
'allocations.port',
56-
'allocations.ip_alias'
57-
)->join('nodes', 'servers.node_id', '=', 'nodes.id')
58-
->join('users', 'servers.owner_id', '=', 'users.id')
59-
->join('allocations', 'servers.allocation_id', '=', 'allocations.id');
60-
61-
if ($request->input('filter') && ! is_null($request->input('filter'))) {
62-
preg_match_all('/[^\s"\']+|"([^"]*)"|\'([^\']*)\'/', urldecode($request->input('filter')), $matches);
63-
foreach ($matches[0] as $match) {
64-
$match = str_replace('"', '', $match);
65-
if (strpos($match, ':')) {
66-
list($field, $term) = explode(':', $match);
67-
if ($field === 'node') {
68-
$field = 'nodes.name';
69-
} elseif ($field === 'owner') {
70-
$field = 'users.email';
71-
} elseif (! strpos($field, '.')) {
72-
$field = 'servers.' . $field;
73-
}
74-
75-
$query->orWhere($field, 'LIKE', '%' . $term . '%');
76-
} else {
77-
$query->where('servers.name', 'LIKE', '%' . $match . '%');
78-
$query->orWhere([
79-
['servers.username', 'LIKE', '%' . $match . '%'],
80-
['users.email', 'LIKE', '%' . $match . '%'],
81-
['allocations.port', 'LIKE', '%' . $match . '%'],
82-
['allocations.ip', 'LIKE', '%' . $match . '%'],
83-
]);
84-
}
85-
}
86-
}
87-
88-
try {
89-
$servers = $query->paginate(20);
90-
} catch (\Exception $ex) {
91-
Alert::warning('There was an error with the search parameters provided.');
92-
$servers = Models\Server::withTrashed()->select(
93-
'servers.*',
94-
'nodes.name as a_nodeName',
95-
'users.email as a_ownerEmail',
96-
'allocations.ip',
97-
'allocations.port',
98-
'allocations.ip_alias'
99-
)->join('nodes', 'servers.node_id', '=', 'nodes.id')
100-
->join('users', 'servers.owner_id', '=', 'users.id')
101-
->join('allocations', 'servers.allocation_id', '=', 'allocations.id')
102-
->paginate(20);
103-
}
104-
10550
return view('admin.servers.index', [
106-
'servers' => $servers,
51+
'servers' => Models\Server::withTrashed()->with('node', 'user')->paginate(25),
10752
]);
10853
}
10954

@@ -117,47 +62,22 @@ public function getNew(Request $request)
11762

11863
public function getView(Request $request, $id)
11964
{
120-
$server = Models\Server::withTrashed()->select(
121-
'servers.*',
122-
'users.email as a_ownerEmail',
123-
'services.name as a_serviceName',
124-
DB::raw('IFNULL(service_options.executable, services.executable) as a_serviceExecutable'),
125-
'service_options.docker_image',
126-
'service_options.name as a_servceOptionName',
127-
'allocations.ip',
128-
'allocations.port',
129-
'allocations.ip_alias'
130-
)->join('nodes', 'servers.node_id', '=', 'nodes.id')
131-
->join('users', 'servers.owner_id', '=', 'users.id')
132-
->join('services', 'servers.service_id', '=', 'services.id')
133-
->join('service_options', 'servers.option_id', '=', 'service_options.id')
134-
->join('allocations', 'servers.allocation_id', '=', 'allocations.id')
135-
->where('servers.id', $id)
136-
->first();
137-
138-
if (! $server) {
139-
return abort(404);
140-
}
65+
66+
$server = Models\Server::withTrashed()->with(
67+
'user', 'option.variables', 'variables',
68+
'node.allocations', 'databases.host'
69+
)->findOrFail($id);
70+
71+
$server->option->variables->transform(function ($item, $key) use ($server) {
72+
$item->server_value = $server->variables->where('variable_id', $item->id)->pluck('variable_value')->first();
73+
74+
return $item;
75+
});
14176

14277
return view('admin.servers.view', [
14378
'server' => $server,
144-
'node' => Models\Node::select(
145-
'nodes.*',
146-
'locations.long as a_locationName'
147-
)->join('locations', 'nodes.location', '=', 'locations.id')
148-
->where('nodes.id', $server->node_id)
149-
->first(),
150-
'assigned' => Models\Allocation::where('assigned_to', $id)->orderBy('ip', 'asc')->orderBy('port', 'asc')->get(),
151-
'unassigned' => Models\Allocation::where('node', $server->node_id)->whereNull('assigned_to')->orderBy('ip', 'asc')->orderBy('port', 'asc')->get(),
152-
'startup' => Models\ServiceVariables::select('service_variables.*', 'server_variables.variable_value as a_serverValue')
153-
->join('server_variables', 'server_variables.variable_id', '=', 'service_variables.id')
154-
->where('service_variables.option_id', $server->option_id)
155-
->where('server_variables.server_id', $server->id)
156-
->get(),
157-
'databases' => Models\Database::select('databases.*', 'database_servers.host as a_host', 'database_servers.port as a_port')
158-
->where('server_id', $server->id)
159-
->join('database_servers', 'database_servers.id', '=', 'databases.db_server')
160-
->get(),
79+
'assigned' => $server->node->allocations->where('server_id', $server->id)->sortBy('port')->sortBy('ip'),
80+
'unassigned' => $server->node->allocations->where('server_id', null)->sortBy('port')->sortBy('ip'),
16181
'db_servers' => Models\DatabaseServer::all(),
16282
]);
16383
}
@@ -166,7 +86,14 @@ public function postNewServer(Request $request)
16686
{
16787
try {
16888
$server = new ServerRepository;
169-
$response = $server->create($request->all());
89+
$response = $server->create($request->only([
90+
'owner', 'name', 'memory', 'swap',
91+
'node', 'ip', 'port', 'allocation',
92+
'cpu', 'disk', 'service',
93+
'option', 'location', 'pack',
94+
'startup', 'custom_image_name',
95+
'auto_deploy', 'custom_id',
96+
]));
17097

17198
return redirect()->route('admin.servers.view', ['id' => $response]);
17299
} catch (DisplayValidationException $ex) {
@@ -261,18 +188,15 @@ public function postNewServerOptionDetails(Request $request)
261188
], 500);
262189
}
263190

264-
$option = Models\ServiceOptions::select(
265-
DB::raw('COALESCE(service_options.executable, services.executable) as executable'),
266-
DB::raw('COALESCE(service_options.startup, services.startup) as startup')
267-
)->leftJoin('services', 'services.id', '=', 'service_options.service_id')
268-
->where('service_options.id', $request->input('option'))
269-
->first();
191+
$option = Models\ServiceOptions::with('variables', ['packs' => function ($query) {
192+
$query->where('selectable', true);
193+
}])->findOrFail($request->input('option'));
270194

271195
return response()->json([
272-
'packs' => Models\ServicePack::select('id', 'name', 'version')->where('option', $request->input('option'))->where('selectable', true)->get(),
273-
'variables' => Models\ServiceVariables::where('option_id', $request->input('option'))->get(),
274-
'exec' => $option->executable,
275-
'startup' => $option->startup,
196+
'packs' => $option->packs,
197+
'variables' => $option->variables,
198+
'exec' => $option->display_executable,
199+
'startup' => $option->display_startup,
276200
]);
277201
}
278202

@@ -309,9 +233,7 @@ public function postUpdateContainerDetails(Request $request, $id)
309233
{
310234
try {
311235
$server = new ServerRepository;
312-
$server->updateContainer($id, [
313-
'image' => $request->input('docker_image'),
314-
]);
236+
$server->updateContainer($id, ['image' => $request->input('docker_image')]);
315237
Alert::success('Successfully updated this server\'s docker image.')->flash();
316238
} catch (DisplayValidationException $ex) {
317239
return redirect()->route('admin.servers.view', [
@@ -333,17 +255,13 @@ public function postUpdateContainerDetails(Request $request, $id)
333255

334256
public function postUpdateServerToggleBuild(Request $request, $id)
335257
{
336-
$server = Models\Server::findOrFail($id);
337-
$node = Models\Node::findOrFail($server->node_id);
338-
$client = Models\Node::guzzleRequest($server->node_id);
258+
$server = Models\Server::with('node')->findOrFail($id);
339259

340260
try {
341-
$res = $client->request('POST', '/server/rebuild', [
342-
'headers' => [
343-
'X-Access-Server' => $server->uuid,
344-
'X-Access-Token' => $node->daemonSecret,
345-
],
346-
]);
261+
$res = $server->node->guzzleClient([
262+
'X-Access-Server' => $server->uuid,
263+
'X-Access-Token' => $node->daemonSecret,
264+
])->request('POST', '/server/rebuild');
347265
Alert::success('A rebuild has been queued successfully. It will run the next time this server is booted.')->flash();
348266
} catch (\GuzzleHttp\Exception\TransferException $ex) {
349267
Log::warning($ex);
@@ -360,15 +278,15 @@ public function postUpdateServerUpdateBuild(Request $request, $id)
360278
{
361279
try {
362280
$server = new ServerRepository;
363-
$server->changeBuild($id, [
364-
'default' => $request->input('default'),
365-
'add_additional' => $request->input('add_additional'),
366-
'remove_additional' => $request->input('remove_additional'),
367-
'memory' => $request->input('memory'),
368-
'swap' => $request->input('swap'),
369-
'io' => $request->input('io'),
370-
'cpu' => $request->input('cpu'),
371-
]);
281+
$server->changeBuild($id, $request->only([
282+
'default',
283+
'add_additional',
284+
'remove_additional',
285+
'memory',
286+
'swap',
287+
'io',
288+
'cpu',
289+
]));
372290
Alert::success('Server details were successfully updated.')->flash();
373291
} catch (DisplayValidationException $ex) {
374292
return redirect()->route('admin.servers.view', [
@@ -458,8 +376,10 @@ public function postDatabase(Request $request, $id)
458376
{
459377
try {
460378
$repo = new DatabaseRepository;
461-
$repo->create($id, $request->except([
462-
'_token',
379+
$repo->create($id, $request->only([
380+
'db_server',
381+
'database',
382+
'remote',
463383
]));
464384
Alert::success('Added new database to this server.')->flash();
465385
} catch (DisplayValidationException $ex) {

app/Http/Controllers/Admin/UserController.php

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -45,35 +45,11 @@ public function __construct()
4545
//
4646
}
4747

48+
// @TODO: implement nicolaslopezj/searchable to clean up this disaster.
4849
public function getIndex(Request $request)
4950
{
50-
$query = User::select('users.*');
51-
if ($request->input('filter') && ! is_null($request->input('filter'))) {
52-
preg_match_all('/[^\s"\']+|"([^"]*)"|\'([^\']*)\'/', urldecode($request->input('filter')), $matches);
53-
foreach ($matches[0] as $match) {
54-
$match = str_replace('"', '', $match);
55-
if (strpos($match, ':')) {
56-
list($field, $term) = explode(':', $match);
57-
$query->orWhere($field, 'LIKE', '%' . $term . '%');
58-
} else {
59-
$query->where('email', 'LIKE', '%' . $match . '%');
60-
$query->orWhere([
61-
['uuid', 'LIKE', '%' . $match . '%'],
62-
['root_admin', 'LIKE', '%' . $match . '%'],
63-
]);
64-
}
65-
}
66-
}
67-
68-
try {
69-
$users = $query->paginate(20);
70-
} catch (\Exception $ex) {
71-
Alert::warning('There was an error with the search parameters provided.');
72-
$users = User::all()->paginate(20);
73-
}
74-
7551
return view('admin.users.index', [
76-
'users' => $users,
52+
'users' => User::paginate(25),
7753
]);
7854
}
7955

@@ -85,12 +61,7 @@ public function getNew(Request $request)
8561
public function getView(Request $request, $id)
8662
{
8763
return view('admin.users.view', [
88-
'user' => User::findOrFail($id),
89-
'servers' => Server::select('servers.*', 'nodes.name as nodeName', 'locations.long as location')
90-
->join('nodes', 'servers.node_id', '=', 'nodes.id')
91-
->join('locations', 'nodes.location', '=', 'locations.id')
92-
->where('owner', $id)
93-
->get(),
64+
'user' => User::with('servers.node')->findOrFail($id),
9465
]);
9566
}
9667

@@ -162,10 +133,6 @@ public function updateUser(Request $request, $user)
162133

163134
public function getJson(Request $request)
164135
{
165-
foreach (User::select('email')->get() as $user) {
166-
$resp[] = $user->email;
167-
}
168-
169-
return $resp;
136+
return User::select('email')->get()->pluck('email');
170137
}
171138
}

app/Http/Controllers/Server/SubuserController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,14 @@ public function postNew(Request $request, $uuid)
156156

157157
try {
158158
$repo = new SubuserRepository;
159-
$id = $repo->create($server->id, $request->except([
160-
'_token',
159+
$subuser = $repo->create($server->id, $request->only([
160+
'permissions', 'email',
161161
]));
162162
Alert::success('Successfully created new subuser.')->flash();
163163

164164
return redirect()->route('server.subusers.view', [
165165
'uuid' => $uuid,
166-
'id' => md5($id),
166+
'id' => md5($subuser->id),
167167
]);
168168
} catch (DisplayValidationException $ex) {
169169
return redirect()->route('server.subusers.new', $uuid)->withErrors(json_decode($ex->getMessage()))->withInput();

app/Models/Allocation.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,15 @@ public function getAliasAttribute($value)
6363
{
6464
return (is_null($this->ip_alias)) ? $this->ip : $this->ip_alias;
6565
}
66+
67+
/**
68+
* Accessor to quickly determine if this allocation has an alias.
69+
*
70+
* @param null|string $value
71+
* @return boolean
72+
*/
73+
public function getHasAliasAttribute($value)
74+
{
75+
return (! is_null($this->ip_alias));
76+
}
6677
}

app/Models/DatabaseServer.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,14 @@ public function node()
6969
{
7070
return $this->belongsTo(Node::class, 'linked_node');
7171
}
72+
73+
/**
74+
* Gets the databases assocaited with this host.
75+
*
76+
* @return \Illuminate\Database\Eloquent\Relations\HasMany
77+
*/
78+
public function databases()
79+
{
80+
return $this->hasMany(Database::class, 'db_server');
81+
}
7282
}

app/Models/Node.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public static function getByID($id)
122122
/**
123123
* Return an instance of the Guzzle client for this specific node.
124124
*
125+
* @param array $headers
125126
* @return \GuzzleHttp\Client
126127
*/
127128
public function guzzleClient($headers = [])

app/Models/Server.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,14 @@ public function tasks()
276276
{
277277
return $this->hasMany(Task::class, 'server', 'id');
278278
}
279+
280+
/**
281+
* Gets all databases associated with a server.
282+
*
283+
* @return \Illuminate\Database\Eloquent\Relations\HasMany
284+
*/
285+
public function databases()
286+
{
287+
return $this->hasMany(Database::class);
288+
}
279289
}

app/Models/ServerVariables.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,14 @@ class ServerVariables extends Model
5151
'server_id' => 'integer',
5252
'variable_id' => 'integer',
5353
];
54+
55+
/**
56+
* Returns information about a given variables parent.
57+
*
58+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
59+
*/
60+
public function variable()
61+
{
62+
return $this->belongsTo(ServiceVariables::class, 'variable_id');
63+
}
5464
}

0 commit comments

Comments
 (0)