33namespace Pterodactyl \Repositories ;
44
55use DB ;
6+ use Debugbar ;
67use Validator ;
78
89use Pterodactyl \Models ;
@@ -64,7 +65,7 @@ public function create(array $data)
6465 // Run validator, throw catchable and displayable exception if it fails.
6566 // Exception includes a JSON result of failed validation rules.
6667 if ($ validator ->fails ()) {
67- throw new DisplayValidationException (json_encode ( $ validator ->errors ()-> all () ));
68+ throw new DisplayValidationException ($ validator ->errors ());
6869 }
6970
7071 // Get the User ID; user exists since we passed the 'exists:users,email' part of the validation
@@ -91,6 +92,7 @@ public function create(array $data)
9192
9293 // Check those Variables
9394 $ variables = Models \ServiceVariables::where ('option_id ' , $ data ['option ' ])->get ();
95+ $ variableList = [];
9496 if ($ variables ) {
9597 foreach ($ variables as $ variable ) {
9698
@@ -100,7 +102,11 @@ public function create(array $data)
100102 throw new DisplayException ('A required service option variable field (env_ ' . $ variable ->env_variable . ') was missing from the request. ' );
101103 }
102104
103- $ data ['env_ ' . $ variable ->env_variable ] = $ variable ->default_value ;
105+ $ variableList = array_merge ($ variableList , [[
106+ 'var_id ' => $ variable ->id ,
107+ 'var_val ' => $ variable ->default_value
108+ ]]);
109+
104110 continue ;
105111 }
106112
@@ -109,13 +115,91 @@ public function create(array $data)
109115 throw new DisplayException ('Failed to validate service option variable field (env_ ' . $ variable ->env_variable . ') aganist regex ( ' . $ variable ->regex . '). ' );
110116 }
111117
118+ $ variableList = array_merge ($ variableList , [[
119+ 'var_id ' => $ variable ->id ,
120+ 'var_val ' => $ data ['env_ ' . $ variable ->env_variable ]
121+ ]]);
122+
112123 continue ;
124+ }
125+ }
126+
127+ // Check Overallocation
128+ if (is_numeric ($ node ->memory_overallocate ) || is_numeric ($ node ->disk_overallocate )) {
113129
130+ $ totals = Models \Server::select (DB ::raw ('SUM(memory) as memory, SUM(disk) as disk ' ))->where ('node ' , $ node ->id )->first ();
131+
132+ // Check memory limits
133+ if (is_numeric ($ node ->memory_overallocate )) {
134+ $ newMemory = $ totals ->memory + $ data ['memory ' ];
135+ $ memoryLimit = ($ node ->memory * (1 + ($ node ->memory_overallocate / 100 )));
136+ if ($ newMemory > $ memoryLimit ) {
137+ 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. ' );
138+ }
114139 }
140+
141+ // Check Disk Limits
142+ if (is_numeric ($ node ->disk_overallocate )) {
143+ $ newDisk = $ totals ->disk + $ data ['disk ' ];
144+ $ diskLimit = ($ node ->disk * (1 + ($ node ->disk_overallocate / 100 )));
145+ if ($ newDisk > $ diskLimit ) {
146+ 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. ' );
147+ }
148+ }
149+
150+ }
151+
152+ DB ::beginTransaction ();
153+
154+ $ uuid = new UuidService ;
155+
156+ // Add Server to the Database
157+ $ server = new Models \Server ;
158+ $ server ->fill ([
159+ 'uuid ' => $ uuid ->generate ('servers ' , 'uuid ' ),
160+ 'uuidShort ' => $ uuid ->generateShort (),
161+ 'node ' => $ data ['node ' ],
162+ 'name ' => $ data ['name ' ],
163+ 'active ' => 1 ,
164+ 'owner ' => $ user ->id ,
165+ 'memory ' => $ data ['memory ' ],
166+ 'disk ' => $ data ['disk ' ],
167+ 'io ' => $ data ['io ' ],
168+ 'cpu ' => $ data ['cpu ' ],
169+ 'ip ' => $ data ['ip ' ],
170+ 'port ' => $ data ['port ' ],
171+ 'service ' => $ data ['service ' ],
172+ 'option ' => $ data ['option ' ],
173+ 'daemonSecret ' => $ uuid ->generate ('servers ' , 'daemonSecret ' ),
174+ 'username ' => $ this ->generateSFTPUsername ($ data ['name ' ])
175+ ]);
176+ $ server ->save ();
177+
178+ // Mark Allocation in Use
179+ $ allocation ->assigned_to = $ server ->id ;
180+ $ allocation ->save ();
181+
182+ // Add Variables
183+ foreach ($ variableList as $ item ) {
184+ Models \ServerVariables::create ([
185+ 'server_id ' => $ server ->id ,
186+ 'variable_id ' => $ item ['var_id ' ],
187+ 'variable_value ' => $ item ['var_val ' ]
188+ ]);
115189 }
116190
117- return (new UuidService )->generateShort ();
118- //return $this->generateSFTPUsername($data['name']);
191+ try {
192+
193+ // Add logic for communicating with Wings to make the server in here.
194+ // We should add the server regardless of the Wings response, but
195+ // handle the error and then allow the server to be re-deployed.
196+
197+ DB ::commit ();
198+ return $ server ->id ;
199+ } catch (\Exception $ e ) {
200+ DB ::rollBack ();
201+ throw $ e ;
202+ }
119203
120204 }
121205
0 commit comments