@@ -83,7 +83,7 @@ public function create(array $data)
8383
8484 // Validate Fields
8585 $ validator = Validator::make ($ data , [
86- 'owner ' => 'bail| required ' ,
86+ 'user_id ' => 'required|exists:users,id ' ,
8787 'name ' => 'required|regex:/^([\w .-]{1,200})$/ ' ,
8888 'memory ' => 'required|numeric|min:0 ' ,
8989 'swap ' => 'required|numeric|min:-1 ' ,
@@ -95,25 +95,20 @@ public function create(array $data)
9595 'location_id ' => 'required|numeric|min:1|exists:locations,id ' ,
9696 'pack_id ' => 'sometimes|nullable|numeric|min:0 ' ,
9797 'startup ' => 'string ' ,
98- 'custom_image_name ' => 'required_if:use_custom_image,on ' ,
9998 'auto_deploy ' => 'sometimes|boolean ' ,
10099 'custom_id ' => 'sometimes|required|numeric|unique:servers,id ' ,
101100 ]);
102101
103- $ validator ->sometimes ('node_id ' , 'bail| required|numeric|min:1|exists:nodes,id ' , function ($ input ) {
102+ $ validator ->sometimes ('node_id ' , 'required|numeric|min:1|exists:nodes,id ' , function ($ input ) {
104103 return ! ($ input ->auto_deploy );
105104 });
106105
107- $ validator ->sometimes ('ip ' , 'required|ip ' , function ($ input ) {
108- return ! $ input ->auto_deploy && ! $ input ->allocation ;
109- });
110-
111- $ validator ->sometimes ('port ' , 'required|numeric|min:1|max:65535 ' , function ($ input ) {
112- return ! $ input ->auto_deploy && ! $ input ->allocation ;
106+ $ validator ->sometimes ('allocation_id ' , 'required|numeric|exists:allocations,id ' , function ($ input ) {
107+ return ! ($ input ->auto_deploy );
113108 });
114109
115- $ validator ->sometimes ('allocation_id ' , 'numeric|exists:allocations,id ' , function ($ input ) {
116- return ! ($ input ->auto_deploy || ( $ input -> port && $ input -> ip ) );
110+ $ validator ->sometimes ('allocation_additional.* ' , 'sometimes|required| numeric|exists:allocations,id ' , function ($ input ) {
111+ return ! ($ input ->auto_deploy );
117112 });
118113
119114 // Run validator, throw catchable and displayable exception if it fails.
@@ -122,16 +117,13 @@ public function create(array $data)
122117 throw new DisplayValidationException ($ validator ->errors ());
123118 }
124119
125- $ user = Models \User::select ('id ' , 'email ' )->where ((is_int ($ data ['owner ' ])) ? 'id ' : 'email ' , $ data ['owner ' ])->first ();
126- if (! $ user ) {
127- throw new DisplayException ('The user id or email passed to the function was not found on the system. ' );
128- }
120+ $ user = Models \User::findOrFail ($ data ['user_id ' ]);
129121
130122 $ autoDeployed = false ;
131- if (isset ($ data ['auto_deploy ' ]) && in_array ( $ data ['auto_deploy ' ], [ true , 1 , ' 1 ' ]) ) {
123+ if (isset ($ data ['auto_deploy ' ]) && $ data ['auto_deploy ' ]) {
132124 // This is an auto-deployment situation
133125 // Ignore any other passed node data
134- unset($ data ['node_id ' ], $ data ['ip ' ], $ data [ ' port ' ], $ data [ ' allocation_id ' ]);
126+ unset($ data ['node_id ' ], $ data ['allocation_id ' ]);
135127
136128 $ autoDeployed = true ;
137129 $ node = DeploymentService::smartRandomNode ($ data ['memory ' ], $ data ['disk ' ], $ data ['location_id ' ]);
@@ -143,17 +135,12 @@ public function create(array $data)
143135 // Verify IP & Port are a.) free and b.) assigned to the node.
144136 // We know the node exists because of 'exists:nodes,id' in the validation
145137 if (! $ autoDeployed ) {
146- if (! isset ($ data ['allocation_id ' ])) {
147- $ model = Models \Allocation::where ('ip ' , $ data ['ip ' ])->where ('port ' , $ data ['port ' ]);
148- } else {
149- $ model = Models \Allocation::where ('id ' , $ data ['allocation_id ' ]);
150- }
151- $ allocation = $ model ->where ('node_id ' , $ data ['node_id ' ])->whereNull ('server_id ' )->first ();
138+ $ allocation = Models \Allocation::where ('id ' , $ data ['allocation_id ' ])->where ('node_id ' , $ data ['node_id ' ])->whereNull ('server_id ' )->first ();
152139 }
153140
154141 // Something failed in the query, either that combo doesn't exist, or it is in use.
155142 if (! $ allocation ) {
156- throw new DisplayException ('The selected IP/Port combination or Allocation ID is either already in use, or unavaliable for this node. ' );
143+ throw new DisplayException ('The selected Allocation ID is either already in use, or unavaliable for this node. ' );
157144 }
158145
159146 // Validate those Service Option Variables
@@ -166,11 +153,9 @@ public function create(array $data)
166153 }
167154
168155 // Validate the Pack
169- if ($ data ['pack_id ' ] == 0 ) {
156+ if (! isset ( $ data ['pack_id ' ]) || ( int ) $ data [ ' pack_id ' ] < 1 ) {
170157 $ data ['pack_id ' ] = null ;
171- }
172-
173- if (! is_null ($ data ['pack_id ' ])) {
158+ } else {
174159 $ pack = Models \ServicePack::where ('id ' , $ data ['pack_id ' ])->where ('option_id ' , $ data ['option_id ' ])->first ();
175160 if (! $ pack ) {
176161 throw new DisplayException ('The requested service pack does not seem to exist for this combination. ' );
@@ -188,7 +173,7 @@ public function create(array $data)
188173
189174 // Is the variable required?
190175 if (! isset ($ data ['env_ ' . $ variable ->env_variable ])) {
191- if ($ variable ->required === 1 ) {
176+ if ($ variable ->required ) {
192177 throw new DisplayException ('A required service option variable field (env_ ' . $ variable ->env_variable . ') was missing from the request. ' );
193178 }
194179 $ variableList [] = [
@@ -271,7 +256,7 @@ public function create(array $data)
271256 'pack_id ' => $ data ['pack_id ' ],
272257 'startup ' => $ data ['startup ' ],
273258 'daemonSecret ' => $ uuid ->generate ('servers ' , 'daemonSecret ' ),
274- 'image ' => (isset ($ data ['custom_image_name ' ])) ? $ data ['custom_image_name ' ] : $ option ->docker_image ,
259+ 'image ' => (isset ($ data ['custom_container ' ])) ? $ data ['custom_container ' ] : $ option ->docker_image ,
275260 'username ' => $ this ->generateSFTPUsername ($ data ['name ' ], $ genShortUuid ),
276261 'sftp_password ' => Crypt::encrypt ('not set ' ),
277262 ]);
@@ -281,6 +266,19 @@ public function create(array $data)
281266 $ allocation ->server_id = $ server ->id ;
282267 $ allocation ->save ();
283268
269+ // Add Additional Allocations
270+ if (isset ($ data ['allocation_additional ' ]) && is_array ($ data ['allocation_additional ' ])) {
271+ foreach ($ data ['allocation_additional ' ] as $ allocation ) {
272+ $ model = Models \Allocation::where ('id ' , $ allocation )->where ('node_id ' , $ data ['node_id ' ])->whereNull ('server_id ' )->first ();
273+ if (! $ model ) {
274+ continue ;
275+ }
276+
277+ $ model ->server_id = $ server ->id ;
278+ $ model ->save ();
279+ }
280+ }
281+
284282 // Add Variables
285283 $ environmentVariables = [
286284 'STARTUP ' => $ data ['startup ' ],
@@ -296,25 +294,26 @@ public function create(array $data)
296294 ]);
297295 }
298296
297+ $ server ->load ('allocation ' , 'allocations ' );
299298 $ node ->guzzleClient (['X-Access-Token ' => $ node ->daemonSecret ])->request ('POST ' , '/servers ' , [
300299 'json ' => [
301300 'uuid ' => (string ) $ server ->uuid ,
302301 'user ' => $ server ->username ,
303302 'build ' => [
304303 'default ' => [
305- 'ip ' => $ allocation ->ip ,
306- 'port ' => (int ) $ allocation ->port ,
307- ],
308- 'ports ' => [
309- (string ) $ allocation ->ip => [(int ) $ allocation ->port ],
304+ 'ip ' => $ server ->allocation ->ip ,
305+ 'port ' => $ server ->allocation ->port ,
310306 ],
307+ 'ports ' => $ server ->allocations ->groupBy ('ip ' )->map (function ($ item ) {
308+ return $ item ->pluck ('port ' );
309+ })->toArray (),
311310 'env ' => $ environmentVariables ,
312311 'memory ' => (int ) $ server ->memory ,
313312 'swap ' => (int ) $ server ->swap ,
314313 'io ' => (int ) $ server ->io ,
315314 'cpu ' => (int ) $ server ->cpu ,
316315 'disk ' => (int ) $ server ->disk ,
317- 'image ' => (isset ($ data ['custom_image_name ' ])) ? $ data ['custom_image_name ' ] : $ option ->docker_image ,
316+ 'image ' => (isset ($ data ['custom_container ' ])) ? $ data ['custom_container ' ] : $ option ->docker_image ,
318317 ],
319318 'service ' => [
320319 'type ' => $ service ->file ,
0 commit comments