1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gameap\Http\Controllers\API; |
4
|
|
|
|
5
|
|
|
use Gameap\Exceptions\Repositories\GdaemonTaskRepository\EmptyServerStartCommandException; |
6
|
|
|
use Gameap\Exceptions\Repositories\GdaemonTaskRepository\GdaemonTaskRepositoryException; |
7
|
|
|
use Gameap\Exceptions\Repositories\GdaemonTaskRepository\InvalidServerStartCommandException; |
8
|
|
|
use Gameap\Exceptions\Repositories\RecordExistExceptions; |
9
|
|
|
use Gameap\Http\Controllers\AuthController; |
10
|
|
|
use Gameap\Repositories\ServerRepository; |
11
|
|
|
use Gameap\Repositories\GdaemonTaskRepository; |
12
|
|
|
use Gameap\Models\Server; |
13
|
|
|
use Gameap\Models\GdaemonTask; |
14
|
|
|
use Gameap\Services\ServerService; |
15
|
|
|
use Gameap\Http\Requests\API\ServerConsoleCommandRequest; |
16
|
|
|
use Illuminate\Http\Request; |
17
|
|
|
use Illuminate\Http\Response; |
18
|
|
|
use Exception; |
19
|
|
|
use Illuminate\Support\Facades\Auth; |
20
|
|
|
|
21
|
|
|
class ServersController extends AuthController |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* The ServerRepository instance. |
25
|
|
|
* |
26
|
|
|
* @var \Gameap\Repositories\ServerRepository |
27
|
|
|
*/ |
28
|
|
|
public $repository; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The GdaemonTaskRepository instance. |
32
|
|
|
* |
33
|
|
|
* @var GdaemonTaskRepository |
34
|
|
|
*/ |
35
|
|
|
public $gdaemonTaskRepository; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var \Gameap\Services\ServerService |
39
|
|
|
*/ |
40
|
|
|
public $serverService; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* ServersController constructor. |
44
|
|
|
* @param ServerRepository $repository |
45
|
|
|
*/ |
46
|
|
|
public function __construct( |
47
|
|
|
ServerRepository $repository, |
48
|
|
|
GdaemonTaskRepository $gdaemonTaskRepository, |
49
|
|
|
ServerService $serverService |
50
|
|
|
) { |
51
|
|
|
parent::__construct(); |
52
|
|
|
|
53
|
|
|
$this->repository = $repository; |
54
|
|
|
$this->gdaemonTaskRepository = $gdaemonTaskRepository; |
55
|
|
|
$this->serverService = $serverService; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param Server $server |
60
|
|
|
* |
61
|
|
|
* @return array|\Illuminate\Http\JsonResponse |
62
|
|
|
* |
63
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
64
|
|
|
*/ |
65
|
|
|
public function start(Server $server) |
66
|
|
|
{ |
67
|
|
|
$this->authorize('server-control', $server); |
68
|
|
|
|
69
|
|
|
try { |
70
|
|
|
$gdaemonTaskId = $this->gdaemonTaskRepository->addServerStart($server); |
71
|
|
|
} catch (GdaemonTaskRepositoryException $exception) { |
72
|
|
|
return $this->handleException($exception); |
73
|
|
|
} catch (RecordExistExceptions $exception) { |
74
|
|
|
$gdaemonTaskId = $this->gdaemonTaskRepository->getOneWorkingTaskId( |
75
|
|
|
$server->id, |
76
|
|
|
GdaemonTask::TASK_SERVER_START |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
if (!$gdaemonTaskId) { |
80
|
|
|
return $this->makeErrorResponse($exception->getMessage()); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return [ |
85
|
|
|
'gdaemonTaskId' => $gdaemonTaskId |
86
|
|
|
]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param Server $server |
91
|
|
|
* |
92
|
|
|
* @return array|\Illuminate\Http\JsonResponse |
93
|
|
|
* |
94
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
95
|
|
|
*/ |
96
|
|
|
public function stop(Server $server) |
97
|
|
|
{ |
98
|
|
|
$this->authorize('server-control', $server); |
99
|
|
|
|
100
|
|
|
try { |
101
|
|
|
$gdaemonTaskId = $this->gdaemonTaskRepository->addServerStop($server); |
102
|
|
|
} catch (RecordExistExceptions $exception) { |
103
|
|
|
$gdaemonTaskId = $this->gdaemonTaskRepository->getOneWorkingTaskId( |
104
|
|
|
$server->id, |
105
|
|
|
GdaemonTask::TASK_SERVER_STOP |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
if (!$gdaemonTaskId) { |
109
|
|
|
return $this->makeErrorResponse($exception->getMessage()); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return [ |
114
|
|
|
'gdaemonTaskId' => $gdaemonTaskId |
115
|
|
|
]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param Server $server |
120
|
|
|
* @return array|\Illuminate\Http\JsonResponse |
121
|
|
|
* |
122
|
|
|
* @throws \Gameap\Exceptions\Repositories\RecordExistExceptions |
123
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
124
|
|
|
*/ |
125
|
|
|
public function restart(Server $server) |
126
|
|
|
{ |
127
|
|
|
$this->authorize('server-control', $server); |
128
|
|
|
|
129
|
|
|
try { |
130
|
|
|
$gdaemonTaskId = $this->gdaemonTaskRepository->addServerRestart($server); |
131
|
|
|
} catch (GdaemonTaskRepositoryException $exception) { |
132
|
|
|
return $this->handleException($exception); |
133
|
|
|
} catch (RecordExistExceptions $exception) { |
134
|
|
|
$gdaemonTaskId = $this->gdaemonTaskRepository->getOneWorkingTaskId( |
135
|
|
|
$server->id, |
136
|
|
|
GdaemonTask::TASK_SERVER_RESTART |
137
|
|
|
); |
138
|
|
|
|
139
|
|
|
if (!$gdaemonTaskId) { |
140
|
|
|
return $this->makeErrorResponse($exception->getMessage()); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return [ |
145
|
|
|
'gdaemonTaskId' => $gdaemonTaskId |
146
|
|
|
]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param Server $server |
151
|
|
|
* @return array|\Illuminate\Http\JsonResponse |
152
|
|
|
* |
153
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
154
|
|
|
*/ |
155
|
|
|
public function update(Server $server) |
156
|
|
|
{ |
157
|
|
|
$this->authorize('server-control', $server); |
158
|
|
|
|
159
|
|
|
try { |
160
|
|
|
$gdaemonTaskId = $this->gdaemonTaskRepository->addServerUpdate($server); |
161
|
|
|
} catch (RecordExistExceptions $exception) { |
162
|
|
|
$gdaemonTaskId = $this->gdaemonTaskRepository->getOneWorkingTaskId( |
163
|
|
|
$server->id, |
164
|
|
|
GdaemonTask::TASK_SERVER_UPDATE |
165
|
|
|
); |
166
|
|
|
|
167
|
|
|
if (!$gdaemonTaskId) { |
168
|
|
|
return $this->makeErrorResponse($exception->getMessage()); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return [ |
173
|
|
|
'gdaemonTaskId' => $gdaemonTaskId |
174
|
|
|
]; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param Server $server |
179
|
|
|
* @return array|\Illuminate\Http\JsonResponse |
180
|
|
|
* |
181
|
|
|
* @throws \Gameap\Exceptions\Repositories\RecordExistExceptions |
182
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
183
|
|
|
*/ |
184
|
|
|
public function reinstall(Server $server) |
185
|
|
|
{ |
186
|
|
|
$this->authorize('server-control', $server); |
187
|
|
|
|
188
|
|
|
try { |
189
|
|
|
$deleteTaskId = $this->gdaemonTaskRepository->addServerDelete($server); |
190
|
|
|
$gdaemonTaskId = $this->gdaemonTaskRepository->addServerUpdate($server, $deleteTaskId); |
191
|
|
|
} catch (RecordExistExceptions $exception) { |
192
|
|
|
return $this->makeErrorResponse($exception->getMessage()); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return [ |
196
|
|
|
'gdaemonTaskId' => $gdaemonTaskId |
197
|
|
|
]; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Get server status |
202
|
|
|
* @param Server $server |
203
|
|
|
* @return array |
204
|
|
|
* |
205
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
206
|
|
|
*/ |
207
|
|
|
public function getStatus(Server $server) |
208
|
|
|
{ |
209
|
|
|
$this->authorize('server-control', $server); |
210
|
|
|
|
211
|
|
|
return [ |
212
|
|
|
'processActive' => $server->processActive() |
213
|
|
|
]; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param Server $server |
218
|
|
|
* @return array |
219
|
|
|
* |
220
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
221
|
|
|
*/ |
222
|
|
|
public function query(Server $server) |
223
|
|
|
{ |
224
|
|
|
$this->authorize('server-control', $server); |
225
|
|
|
$query = $this->serverService->query($server); |
226
|
|
|
|
227
|
|
|
return $query; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param Server $server |
232
|
|
|
* @return array |
233
|
|
|
* |
234
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
235
|
|
|
*/ |
236
|
|
|
public function consoleLog(Server $server) |
237
|
|
|
{ |
238
|
|
|
$this->authorize('server-control', $server); |
239
|
|
|
return [ |
240
|
|
|
'console' => $this->serverService->getConsoleLog($server) |
241
|
|
|
]; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @param ServerConsoleCommandRequest $request |
246
|
|
|
* @param Server $server |
247
|
|
|
* @return array |
248
|
|
|
* |
249
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
250
|
|
|
*/ |
251
|
|
|
public function sendCommand(ServerConsoleCommandRequest $request, Server $server) |
252
|
|
|
{ |
253
|
|
|
$this->authorize('server-control', $server); |
254
|
|
|
|
255
|
|
|
$command = $request->input('command'); |
256
|
|
|
$this->serverService->sendConsoleCommand($server, $command); |
257
|
|
|
|
258
|
|
|
return ['message' => 'success']; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @param Request $request |
263
|
|
|
* |
264
|
|
|
* TODO: Create admin part and move this |
265
|
|
|
* |
266
|
|
|
* @return mixed |
267
|
|
|
*/ |
268
|
|
|
public function search(Request $request) |
269
|
|
|
{ |
270
|
|
|
$query = $request->input('q'); |
271
|
|
|
return $this->repository->search($query); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @param Exception $exception |
276
|
|
|
* |
277
|
|
|
* @return \Illuminate\Http\JsonResponse |
278
|
|
|
*/ |
279
|
|
|
private function handleException(Exception $exception) |
280
|
|
|
{ |
281
|
|
|
if (Auth::user()->can('admin roles & permissions')) { |
282
|
|
|
$extraMessage = $this->getDocMessage($exception); |
283
|
|
|
} else { |
284
|
|
|
$extraMessage = __('main.common_admin_error'); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
return $this->makeErrorResponse($exception->getMessage() . $extraMessage); |
|
|
|
|
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @param Exception $exception |
292
|
|
|
* @return string |
293
|
|
|
*/ |
294
|
|
|
private function getDocMessage(Exception $exception) |
295
|
|
|
{ |
296
|
|
|
$msg = ''; |
297
|
|
|
|
298
|
|
|
if ($exception instanceof EmptyServerStartCommandException) { |
299
|
|
|
$msg = __('gdaemon_tasks.empty_server_start_command_doc'); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
return $msg; |
|
|
|
|
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @param $message |
307
|
|
|
* @param int $code |
308
|
|
|
* @return \Illuminate\Http\JsonResponse |
309
|
|
|
*/ |
310
|
|
|
private function makeErrorResponse($message, $code = Response::HTTP_UNPROCESSABLE_ENTITY) |
|
|
|
|
311
|
|
|
{ |
312
|
|
|
return response()->json([ |
313
|
|
|
'message' => $message, |
314
|
|
|
'http_code' => Response::HTTP_UNPROCESSABLE_ENTITY |
315
|
|
|
], Response::HTTP_UNPROCESSABLE_ENTITY); |
316
|
|
|
} |
317
|
|
|
} |