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