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
10284 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\Repositories\Eloquent\ServerRepository;
12
use Pterodactyl\Events\Server\Installed as ServerInstalled;
13
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
14
use Pterodactyl\Http\Requests\Api\Remote\InstallationDataRequest;
15
16
class ServerInstallController extends Controller
17
{
18
/**
19
* ServerInstallController constructor.
20
*/
21
public function __construct(private ServerRepository $repository, private EventDispatcher $eventDispatcher)
22
{
23
}
24
25
/**
26
* Returns installation information for a server.
27
*
28
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
29
*/
30
public function index(Request $request, string $uuid): JsonResponse
31
{
32
$server = $this->repository->getByUuid($uuid);
33
$egg = $server->egg;
34
35
return new JsonResponse([
36
'container_image' => $egg->copy_script_container,
37
'entrypoint' => $egg->copy_script_entry,
38
'script' => $egg->copy_script_install,
39
]);
40
}
41
42
/**
43
* Updates the installation state of a server.
44
*
45
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
46
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
47
*/
48
public function store(InstallationDataRequest $request, string $uuid): JsonResponse
49
{
50
$server = $this->repository->getByUuid($uuid);
51
$status = null;
52
53
// Make sure the type of failure is accurate
54
if (!$request->boolean('successful')) {
55
$status = Server::STATUS_INSTALL_FAILED;
56
57
if ($request->boolean('reinstall')) {
58
$status = Server::STATUS_REINSTALL_FAILED;
59
}
60
}
61
62
// Keep the server suspended if it's already suspended
63
if ($server->status === Server::STATUS_SUSPENDED) {
64
$status = Server::STATUS_SUSPENDED;
65
}
66
67
$this->repository->update($server->id, ['status' => $status, 'installed_at' => CarbonImmutable::now()], true, true);
68
69
// If the server successfully installed, fire installed event.
70
// This logic allows individually disabling install and reinstall notifications separately.
71
$isInitialInstall = is_null($server->installed_at);
72
if ($isInitialInstall && config()->get('pterodactyl.email.send_install_notification', true)) {
73
$this->eventDispatcher->dispatch(new ServerInstalled($server));
74
} elseif (!$isInitialInstall && config()->get('pterodactyl.email.send_reinstall_notification', true)) {
75
$this->eventDispatcher->dispatch(new ServerInstalled($server));
76
}
77
78
return new JsonResponse([], Response::HTTP_NO_CONTENT);
79
}
80
}
81
82