-
-
Notifications
You must be signed in to change notification settings - Fork 279
Expand file tree
/
Copy pathServerInstallController.php
More file actions
72 lines (58 loc) · 2.27 KB
/
ServerInstallController.php
File metadata and controls
72 lines (58 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
namespace App\Http\Controllers\Api\Remote\Servers;
use App\Enums\ServerState;
use App\Events\Server\Installed as ServerInstalled;
use App\Exceptions\Http\HttpForbiddenException;
use App\Exceptions\Model\DataValidationException;
use App\Http\Controllers\Controller;
use App\Http\Requests\Api\Remote\InstallationDataRequest;
use App\Models\Server;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class ServerInstallController extends Controller
{
/**
* Returns installation information for a server.
*/
public function index(Request $request, Server $server): JsonResponse
{
if (!$server->node->is($request->attributes->get('node'))) {
throw new HttpForbiddenException('Requesting node does not have permission to access this server.');
}
$egg = $server->egg;
return new JsonResponse([
'container_image' => $egg->copy_script_container,
'entrypoint' => $egg->copy_script_entry,
'script' => $egg->copy_script_install,
]);
}
/**
* Updates the installation state of a server.
*
* @throws DataValidationException
*/
public function store(InstallationDataRequest $request, Server $server): JsonResponse
{
$status = null;
if (!$server->node->is($request->attributes->get('node'))) {
throw new HttpForbiddenException('Requesting node does not have permission to access this server.');
}
$successful = $request->boolean('successful');
// Make sure the type of failure is accurate
if (!$successful) {
$status = $request->boolean('reinstall') ? ServerState::ReinstallFailed : ServerState::InstallFailed;
}
// Keep the server suspended if it's already suspended
if ($server->status === ServerState::Suspended) {
$status = ServerState::Suspended;
}
$previouslyInstalledAt = $server->installed_at;
$server->status = $status;
$server->installed_at = now();
$server->save();
$isInitialInstall = is_null($previouslyInstalledAt);
event(new ServerInstalled($server, $successful, $isInitialInstall));
return new JsonResponse([], Response::HTTP_NO_CONTENT);
}
}