Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Http/Controllers/Api/Remote/Servers/ServerInstallController.php
14056 views
1
<?php
2
3
namespace Pterodactyl\Http\Controllers\Api\Remote\Servers;
4
5
use Carbon\CarbonImmutable;
6
use Illuminate\Http\Request;
7
use Illuminate\Http\Response;
8
use Pterodactyl\Models\Server;
9
use Illuminate\Http\JsonResponse;
10
use Pterodactyl\Http\Controllers\Controller;
11
use Pterodactyl\Exceptions\Http\HttpForbiddenException;
12
use Pterodactyl\Repositories\Eloquent\ServerRepository;
13
use Pterodactyl\Events\Server\Installed as ServerInstalled;
14
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
15
use Pterodactyl\Http\Requests\Api\Remote\InstallationDataRequest;
16
17
class ServerInstallController extends Controller
18
{
19
/**
20
* ServerInstallController constructor.
21
*/
22
public function __construct(private ServerRepository $repository, private EventDispatcher $eventDispatcher)
23
{
24
}
25
26
/**
27
* Returns installation information for a server.
28
*
29
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
30
*/
31
public function index(Request $request, string $uuid): JsonResponse
32
{
33
$server = $this->repository->getByUuid($uuid);
34
$egg = $server->egg;
35
36
if (! $server->node->is($request->attributes->get('node'))) {
37
throw new HttpForbiddenException('Requesting node does not have permission to access this server.');
38
}
39
40
return new JsonResponse([
41
'container_image' => $egg->copy_script_container,
42
'entrypoint' => $egg->copy_script_entry,
43
'script' => $egg->copy_script_install,
44
]);
45
}
46
47
/**
48
* Updates the installation state of a server.
49
*
50
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
51
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
52
*/
53
public function store(InstallationDataRequest $request, string $uuid): JsonResponse
54
{
55
$server = $this->repository->getByUuid($uuid);
56
$status = null;
57
58
if (! $server->node->is($request->attributes->get('node'))) {
59
throw new HttpForbiddenException('Requesting node does not have permission to access this server.');
60
}
61
62
// Make sure the type of failure is accurate
63
if (!$request->boolean('successful')) {
64
$status = Server::STATUS_INSTALL_FAILED;
65
66
if ($request->boolean('reinstall')) {
67
$status = Server::STATUS_REINSTALL_FAILED;
68
}
69
}
70
71
// Keep the server suspended if it's already suspended
72
if ($server->status === Server::STATUS_SUSPENDED) {
73
$status = Server::STATUS_SUSPENDED;
74
}
75
76
$this->repository->update($server->id, ['status' => $status, 'installed_at' => CarbonImmutable::now()], true, true);
77
78
// If the server successfully installed, fire installed event.
79
// This logic allows individually disabling install and reinstall notifications separately.
80
$isInitialInstall = is_null($server->installed_at);
81
if ($isInitialInstall && config()->get('pterodactyl.email.send_install_notification', true)) {
82
$this->eventDispatcher->dispatch(new ServerInstalled($server));
83
} elseif (!$isInitialInstall && config()->get('pterodactyl.email.send_reinstall_notification', true)) {
84
$this->eventDispatcher->dispatch(new ServerInstalled($server));
85
}
86
87
return new JsonResponse([], Response::HTTP_NO_CONTENT);
88
}
89
}
90
91