Files
panel-pelican-dev/app/Http/Controllers/Api/Remote/Servers/ServerInstallController.php

84 lines
2.7 KiB
PHP
Raw Normal View History

2020-01-18 15:26:15 -08:00
<?php
2024-03-12 22:39:16 -04:00
namespace App\Http\Controllers\Api\Remote\Servers;
2020-01-18 15:26:15 -08:00
use Illuminate\Http\Request;
2020-01-19 13:50:38 -08:00
use Illuminate\Http\Response;
2024-03-12 22:39:16 -04:00
use App\Models\Server;
2020-01-18 15:26:15 -08:00
use Illuminate\Http\JsonResponse;
2024-03-12 22:39:16 -04:00
use App\Http\Controllers\Controller;
use App\Repositories\Eloquent\ServerRepository;
use App\Events\Server\Installed as ServerInstalled;
2021-03-06 15:52:24 +08:00
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
2024-03-12 22:39:16 -04:00
use App\Http\Requests\Api\Remote\InstallationDataRequest;
2020-01-18 15:26:15 -08:00
class ServerInstallController extends Controller
{
/**
* ServerInstallController constructor.
*/
2024-03-16 15:01:21 -04:00
public function __construct(private EventDispatcher $eventDispatcher)
2020-01-18 15:26:15 -08:00
{
}
/**
* Returns installation information for a server.
*
2024-03-16 15:01:21 -04:00
* @throws RecordNotFoundException
2020-01-18 15:26:15 -08:00
*/
public function index(Request $request, string $uuid): JsonResponse
2020-01-18 15:26:15 -08:00
{
$server = $this->repository->getByUuid($uuid);
$egg = $server->egg;
return new JsonResponse([
2020-01-18 15:26:15 -08:00
'container_image' => $egg->copy_script_container,
'entrypoint' => $egg->copy_script_entry,
'script' => $egg->copy_script_install,
]);
}
2020-01-19 13:50:38 -08:00
/**
* Updates the installation state of a server.
*
2024-03-12 22:39:16 -04:00
* @throws \App\Exceptions\Repository\RecordNotFoundException
* @throws \App\Exceptions\Model\DataValidationException
2020-01-19 13:50:38 -08:00
*/
public function store(InstallationDataRequest $request, string $uuid): JsonResponse
2020-01-19 13:50:38 -08:00
{
2024-03-16 15:01:21 -04:00
$server = Server::findOrFailByUuid($uuid);
$status = null;
2020-01-19 13:50:38 -08: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 15:55:46 -08:00
if ($server->status === Server::STATUS_SUSPENDED) {
2021-01-17 16:08:41 -08:00
$status = Server::STATUS_SUSPENDED;
2021-01-17 15:55:46 -08:00
}
2020-01-19 13:50:38 -08:00
2024-03-16 15:01:21 -04:00
$previouslyInstalledAt = $server->installed_at;
$server->status = $status;
$server->installed_at = now();
$server->save();
2021-01-17 15:55:46 -08:00
2021-03-06 15:52:24 +08:00
// If the server successfully installed, fire installed event.
// This logic allows individually disabling install and reinstall notifications separately.
2024-03-16 15:01:21 -04:00
$isInitialInstall = is_null($previouslyInstalledAt);
2024-03-12 22:39:16 -04:00
if ($isInitialInstall && config()->get('panel.email.send_install_notification', true)) {
$this->eventDispatcher->dispatch(new ServerInstalled($server));
2024-03-12 22:39:16 -04:00
} elseif (!$isInitialInstall && config()->get('panel.email.send_reinstall_notification', true)) {
2021-03-06 15:52:24 +08:00
$this->eventDispatcher->dispatch(new ServerInstalled($server));
}
2021-01-17 15:55:46 -08:00
return new JsonResponse([], Response::HTTP_NO_CONTENT);
2020-01-19 13:50:38 -08:00
}
2020-01-18 15:26:15 -08:00
}