2020-01-18 23:26:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Remote\Servers;
|
|
|
|
|
2022-09-25 19:16:58 +00:00
|
|
|
use Carbon\CarbonImmutable;
|
2020-01-18 23:26:15 +00:00
|
|
|
use Illuminate\Http\Request;
|
2020-01-19 21:50:38 +00:00
|
|
|
use Illuminate\Http\Response;
|
2021-01-17 23:55:46 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2020-01-18 23:26:15 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
|
|
|
use Pterodactyl\Repositories\Eloquent\ServerRepository;
|
2021-03-06 07:52:24 +00:00
|
|
|
use Pterodactyl\Events\Server\Installed as ServerInstalled;
|
|
|
|
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
|
2021-08-16 00:20:36 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Remote\InstallationDataRequest;
|
2020-01-18 23:26:15 +00:00
|
|
|
|
|
|
|
class ServerInstallController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* ServerInstallController constructor.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function __construct(private ServerRepository $repository, private EventDispatcher $eventDispatcher)
|
2020-01-18 23:26:15 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns installation information for a server.
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
2022-09-25 19:16:58 +00:00
|
|
|
public function index(Request $request, string $uuid): JsonResponse
|
2020-01-18 23:26:15 +00:00
|
|
|
{
|
|
|
|
$server = $this->repository->getByUuid($uuid);
|
|
|
|
$egg = $server->egg;
|
|
|
|
|
2022-05-04 23:23:01 +00:00
|
|
|
return new JsonResponse([
|
2020-01-18 23:26:15 +00:00
|
|
|
'container_image' => $egg->copy_script_container,
|
|
|
|
'entrypoint' => $egg->copy_script_entry,
|
|
|
|
'script' => $egg->copy_script_install,
|
|
|
|
]);
|
|
|
|
}
|
2020-01-19 21:50:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the installation state of a server.
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
*/
|
2022-09-25 19:16:58 +00:00
|
|
|
public function store(InstallationDataRequest $request, string $uuid): JsonResponse
|
2020-01-19 21:50:38 +00:00
|
|
|
{
|
|
|
|
$server = $this->repository->getByUuid($uuid);
|
2022-11-21 20:53:54 +00:00
|
|
|
$status = null;
|
2020-01-19 21:50:38 +00:00
|
|
|
|
2022-11-21 20:53:54 +00:00
|
|
|
// Make sure the type of failure is accurate
|
|
|
|
if (!$request->boolean('successful')) {
|
|
|
|
$status = Server::STATUS_INSTALL_FAILED;
|
|
|
|
|
|
|
|
if ($request->boolean('reinstall')) {
|
|
|
|
$status = Server::STATUS_REINSTALL_FAILED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep the server suspended if it's already suspended
|
2021-01-17 23:55:46 +00:00
|
|
|
if ($server->status === Server::STATUS_SUSPENDED) {
|
2021-01-18 00:08:41 +00:00
|
|
|
$status = Server::STATUS_SUSPENDED;
|
2021-01-17 23:55:46 +00:00
|
|
|
}
|
2020-01-19 21:50:38 +00:00
|
|
|
|
2022-09-25 19:16:58 +00:00
|
|
|
$this->repository->update($server->id, ['status' => $status, 'installed_at' => CarbonImmutable::now()], true, true);
|
2021-01-17 23:55:46 +00:00
|
|
|
|
2021-03-06 07:52:24 +00:00
|
|
|
// If the server successfully installed, fire installed event.
|
2022-09-25 19:16:58 +00:00
|
|
|
// This logic allows individually disabling install and reinstall notifications separately.
|
|
|
|
$isInitialInstall = is_null($server->installed_at);
|
|
|
|
if ($isInitialInstall && config()->get('pterodactyl.email.send_install_notification', true)) {
|
|
|
|
$this->eventDispatcher->dispatch(new ServerInstalled($server));
|
2022-10-14 16:59:20 +00:00
|
|
|
} elseif (!$isInitialInstall && config()->get('pterodactyl.email.send_reinstall_notification', true)) {
|
2021-03-06 07:52:24 +00:00
|
|
|
$this->eventDispatcher->dispatch(new ServerInstalled($server));
|
|
|
|
}
|
|
|
|
|
2021-01-17 23:55:46 +00:00
|
|
|
return new JsonResponse([], Response::HTTP_NO_CONTENT);
|
2020-01-19 21:50:38 +00:00
|
|
|
}
|
2020-01-18 23:26:15 +00:00
|
|
|
}
|