@@ -226,7 +226,7 @@ public function updateDetails($id, array $data)
226226 // Validate Fields
227227 $ validator = Validator::make ($ data , [
228228 'owner ' => 'email|exists:users,email ' ,
229- 'name ' => 'regex:([\w -]{4,35}) '
229+ 'name ' => 'regex:^ ([\w -]{4,35})$ '
230230 ]);
231231
232232 // Run validator, throw catchable and displayable exception if it fails.
@@ -304,11 +304,169 @@ public function updateDetails($id, array $data)
304304 throw new DisplayException ('Daemon returned a a non HTTP/204 error code. HTTP/ ' + $ res ->getStatusCode ());
305305 }
306306 } catch (\Exception $ ex ) {
307- DB ::rollback ();
307+ DB ::rollBack ();
308308 Log::error ($ ex );
309309 throw new DisplayException ('An error occured while attempting to update this server \'s information. ' );
310310 }
311311
312312 }
313313
314+ /**
315+ * [changeBuild description]
316+ * @param integer $id
317+ * @param array $data
318+ * @return boolean
319+ */
320+ public function changeBuild ($ id , array $ data )
321+ {
322+
323+ $ validator = Validator::make ($ data , [
324+ 'default ' => [
325+ 'string ' ,
326+ '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})$/ '
327+ ],
328+ 'add_additional ' => 'array ' ,
329+ 'remove_additional ' => 'array ' ,
330+ 'memory ' => 'integer|min:0 ' ,
331+ 'swap ' => 'integer|min:0 ' ,
332+ 'io ' => 'integer|min:10|max:1000 ' ,
333+ 'cpu ' => 'integer|min:0 ' ,
334+ 'disk ' => 'integer|min:0 '
335+ ]);
336+
337+ // Run validator, throw catchable and displayable exception if it fails.
338+ // Exception includes a JSON result of failed validation rules.
339+ if ($ validator ->fails ()) {
340+ throw new DisplayValidationException ($ validator ->errors ());
341+ }
342+
343+ DB ::beginTransaction ();
344+ $ server = Models \Server::findOrFail ($ id );
345+
346+ if (isset ($ data ['default ' ])) {
347+ list ($ ip , $ port ) = explode (': ' , $ data ['default ' ]);
348+ if ($ ip === $ server ->ip && $ port === $ server ->port ) {
349+ continue ;
350+ }
351+
352+ $ allocation = Models \Allocation::where ('ip ' , $ ip )->where ('port ' , $ port )->where ('assigned_to ' , $ server ->id )->get ();
353+ if (!$ allocation ) {
354+ throw new DisplayException ('The assigned default connection ( ' . $ ip . ': ' . $ prot . ') is not allocated to this server. ' );
355+ }
356+
357+ $ server ->ip = $ ip ;
358+ $ server ->port = $ port ;
359+ }
360+
361+ // Remove Assignments
362+ if (isset ($ data ['remove_additional ' ])) {
363+ foreach ($ data ['remove_additional ' ] as $ id => $ combo ) {
364+ list ($ ip , $ port ) = explode (': ' , $ combo );
365+ // Invalid, not worth killing the whole thing, we'll just skip over it.
366+ if (!filter_var ($ ip , FILTER_VALIDATE_IP ) || !preg_match ('/^(\d{1,5})$/ ' , $ port )) {
367+ continue ;
368+ }
369+
370+ // Can't remove the assigned IP/Port combo
371+ if ($ ip === $ server ->ip && $ port === $ server ->port ) {
372+ continue ;
373+ }
374+
375+ Models \Allocation::where ('ip ' , $ ip )->where ('port ' , $ port )->where ('assigned_to ' , $ server ->id )->update ([
376+ 'assigned_to ' => null
377+ ]);
378+ }
379+ }
380+
381+ // Add Assignments
382+ if (isset ($ data ['add_additional ' ])) {
383+ foreach ($ data ['add_additional ' ] as $ id => $ combo ) {
384+ list ($ ip , $ port ) = explode (': ' , $ combo );
385+ // Invalid, not worth killing the whole thing, we'll just skip over it.
386+ if (!filter_var ($ ip , FILTER_VALIDATE_IP ) || !preg_match ('/^(\d{1,5})$/ ' , $ port )) {
387+ continue ;
388+ }
389+
390+ // Don't allow double port assignments
391+ if (Models \Allocation::where ('port ' , $ port )->where ('assigned_to ' , $ server ->id )->count () !== 0 ) {
392+ continue ;
393+ }
394+
395+ Models \Allocation::where ('ip ' , $ ip )->where ('port ' , $ port )->whereNull ('assigned_to ' )->update ([
396+ 'assigned_to ' => $ server ->id
397+ ]);
398+ }
399+ }
400+
401+ // Loop All Assignments
402+ $ additionalAssignments = [];
403+ $ assignments = Models \Allocation::where ('assigned_to ' , $ server ->id )->get ();
404+ foreach ($ assignments as &$ assignment ) {
405+ if (array_key_exists ((string ) $ assignment ->ip , $ additionalAssignments )) {
406+ array_push ($ additionalAssignments [ (string ) $ assignment ->ip ], (int ) $ assignment ->port );
407+ } else {
408+ $ additionalAssignments [ (string ) $ assignment ->ip ] = [ (int ) $ assignment ->port ];
409+ }
410+ }
411+
412+ // @TODO: verify that server can be set to this much memory without
413+ // going over node limits.
414+ if (isset ($ data ['memory ' ])) {
415+ $ server ->memory = $ data ['memory ' ];
416+ }
417+
418+ if (isset ($ data ['swap ' ])) {
419+ $ server ->swap = $ data ['swap ' ];
420+ }
421+
422+ // @TODO: verify that server can be set to this much disk without
423+ // going over node limits.
424+ if (isset ($ data ['disk ' ])) {
425+ $ server ->disk = $ data ['disk ' ];
426+ }
427+
428+ if (isset ($ data ['cpu ' ])) {
429+ $ server ->cpu = $ data ['cpu ' ];
430+ }
431+
432+ if (isset ($ data ['io ' ])) {
433+ $ server ->io = $ data ['io ' ];
434+ }
435+
436+ try {
437+
438+ $ node = Models \Node::getByID ($ server ->node );
439+ $ client = Models \Node::guzzleRequest ($ server ->node );
440+
441+ $ client ->request ('PATCH ' , '/server ' , [
442+ 'headers ' => [
443+ 'X-Access-Server ' => $ server ->uuid ,
444+ 'X-Access-Token ' => $ node ->daemonSecret
445+ ],
446+ 'json ' => [
447+ 'build ' => [
448+ 'default ' => [
449+ 'ip ' => $ server ->ip ,
450+ 'port ' => (int ) $ server ->port
451+ ],
452+ 'ports|overwrite ' => $ additionalAssignments ,
453+ 'memory ' => (int ) $ server ->memory ,
454+ 'swap ' => (int ) $ server ->swap ,
455+ 'io ' => (int ) $ server ->io ,
456+ 'cpu ' => (int ) $ server ->cpu ,
457+ 'disk ' => (int ) $ server ->disk
458+ ]
459+ ]
460+ ]);
461+
462+ $ server ->save ();
463+ DB ::commit ();
464+ return true ;
465+ } catch (\GuzzleHttp \Exception \TransferException $ ex ) {
466+ DB ::rollBack ();
467+ throw new DisplayException ('An error occured while attempting to update the configuration: ' . $ ex ->getMessage ());
468+ }
469+
470+ }
471+
314472}
0 commit comments