@@ -77,7 +77,7 @@ public function create(array $data)
7777
7878 // Verify IP & Port are a.) free and b.) assigned to the node.
7979 // We know the node exists because of 'exists:nodes,id' in the validation
80- $ node = Models \Node::find ($ data ['node ' ]);
80+ $ node = Models \Node::getByID ($ data ['node ' ]);
8181 $ allocation = Models \Allocation::where ('ip ' , $ data ['ip ' ])->where ('port ' , $ data ['port ' ])->where ('node ' , $ data ['node ' ])->whereNull ('assigned_to ' )->first ();
8282
8383 // Something failed in the query, either that combo doesn't exist, or it is in use.
@@ -94,6 +94,9 @@ public function create(array $data)
9494 throw new DisplayException ('The requested service option does not exist for the specified service. ' );
9595 }
9696
97+ // Load up the Service Information
98+ $ service = Models \Service::find ($ option ->parent_service );
99+
97100 // Check those Variables
98101 $ variables = Models \ServiceVariables::where ('option_id ' , $ data ['option ' ])->get ();
99102 $ variableList = [];
@@ -105,12 +108,11 @@ public function create(array $data)
105108 if ($ variable ->required === 1 ) {
106109 throw new DisplayException ('A required service option variable field (env_ ' . $ variable ->env_variable . ') was missing from the request. ' );
107110 }
108-
109111 $ variableList = array_merge ($ variableList , [[
110- 'var_id ' => $ variable ->id ,
111- 'var_val ' => $ variable ->default_value
112+ 'id ' => $ variable ->id ,
113+ 'env ' => $ variable ->env_variable ,
114+ 'val ' => $ variable ->default_value
112115 ]]);
113-
114116 continue ;
115117 }
116118
@@ -120,10 +122,10 @@ public function create(array $data)
120122 }
121123
122124 $ variableList = array_merge ($ variableList , [[
123- 'var_id ' => $ variable ->id ,
124- 'var_val ' => $ data ['env_ ' . $ variable ->env_variable ]
125+ 'id ' => $ variable ->id ,
126+ 'env ' => $ variable ->env_variable ,
127+ 'val ' => $ data ['env_ ' . $ variable ->env_variable ]
125128 ]]);
126-
127129 continue ;
128130 }
129131 }
@@ -188,25 +190,78 @@ public function create(array $data)
188190 $ allocation ->save ();
189191
190192 // Add Variables
193+ $ environmentVariables = [];
194+ $ environmentVariables = array_merge ($ environmentVariables , [
195+ 'STARTUP ' => $ data ['startup ' ]
196+ ]);
191197 foreach ($ variableList as $ item ) {
198+ $ environmentVariables = array_merge ($ environmentVariables , [
199+ $ item ['env ' ] => $ item ['val ' ]
200+ ]);
192201 Models \ServerVariables::create ([
193202 'server_id ' => $ server ->id ,
194- 'variable_id ' => $ item ['var_id ' ],
195- 'variable_value ' => $ item ['var_val ' ]
203+ 'variable_id ' => $ item ['id ' ],
204+ 'variable_value ' => $ item ['val ' ]
196205 ]);
197206 }
198207
199208 try {
200209
201- // Add logic for communicating with Wings to make the server in here.
202- // We should add the server regardless of the Wings response, but
203- // handle the error and then allow the server to be re-deployed.
210+ $ client = Models \Node::guzzleRequest ($ node ->id );
211+ $ client ->request ('POST ' , '/servers ' , [
212+ 'headers ' => [
213+ 'X-Access-Token ' => $ node ->daemonSecret
214+ ],
215+ 'json ' => [
216+ 'uuid ' => (string ) $ server ->uuid ,
217+ 'user ' => $ server ->username ,
218+ 'build ' => [
219+ 'default ' => [
220+ 'ip ' => $ server ->ip ,
221+ 'port ' => (int ) $ server ->port
222+ ],
223+ 'ports ' => [
224+ (string ) $ server ->ip => [ (int ) $ server ->port ]
225+ ],
226+ 'env ' => $ environmentVariables ,
227+ 'memory ' => (int ) $ server ->memory ,
228+ 'swap ' => (int ) $ server ->swap ,
229+ 'io ' => (int ) $ server ->io ,
230+ 'cpu ' => (int ) $ server ->cpu ,
231+ 'disk ' => (int ) $ server ->disk ,
232+ 'image ' => (isset ($ data ['custom_image_name ' ])) ? $ data ['custom_image_name ' ] : $ option ->docker_image
233+ ],
234+ 'service ' => [
235+ 'type ' => $ service ->file ,
236+ 'option ' => $ option ->tag
237+ ],
238+ 'keys ' => [
239+ (string ) $ server ->daemonSecret => [
240+ 's:get ' ,
241+ 's:power ' ,
242+ 's:console ' ,
243+ 's:command ' ,
244+ 's:files:get ' ,
245+ 's:files:read ' ,
246+ 's:files:post ' ,
247+ 's:files:delete ' ,
248+ 's:files:upload ' ,
249+ 's:set-password '
250+ ]
251+ ],
252+ 'rebuild ' => false
253+ ]
254+ ]);
204255
205256 DB ::commit ();
206257 return $ server ->id ;
207- } catch (\Exception $ e ) {
258+ } catch (\GuzzleHttp \Exception \TransferException $ ex ) {
259+ DB ::rollBack ();
260+ throw new DisplayException ('An error occured while attempting to update the configuration: ' . $ ex ->getMessage ());
261+ } catch (\Exception $ ex ) {
208262 DB ::rollBack ();
209- throw $ e ;
263+ Log:error ($ ex );
264+ throw $ ex ;
210265 }
211266
212267 }
0 commit comments