@@ -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 ) {
0 commit comments