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

72 lines
2.2 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
2024-04-18 03:50:20 -04:00
use App\Enums\ServerState;
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\Events\Server\Installed as ServerInstalled;
use App\Http\Requests\Api\Remote\InstallationDataRequest;
2020-01-18 15:26:15 -08:00
class ServerInstallController extends Controller
{
/**
* Returns installation information for a server.
*/
2024-03-17 20:57:06 -04:00
public function index(Server $server): JsonResponse
2020-01-18 15:26:15 -08:00
{
$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\Model\DataValidationException
2020-01-19 13:50:38 -08:00
*/
2024-03-17 20:57:06 -04:00
public function store(InstallationDataRequest $request, Server $server): JsonResponse
2020-01-19 13:50:38 -08:00
{
$status = null;
2020-01-19 13:50:38 -08:00
// Make sure the type of failure is accurate
if (!$request->boolean('successful')) {
2024-04-18 03:50:20 -04:00
$status = ServerState::InstallFailed;
if ($request->boolean('reinstall')) {
2024-04-18 03:50:20 -04:00
$status = ServerState::ReinstallFailed;
}
}
// Keep the server suspended if it's already suspended
2024-04-18 03:50:20 -04:00
if ($server->status === ServerState::Suspended) {
$status = ServerState::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)) {
2024-03-23 17:25:10 -04:00
event(new ServerInstalled($server));
}
if (!$isInitialInstall && config()->get('panel.email.send_reinstall_notification', true)) {
event(new ServerInstalled($server));
2021-03-06 15:52:24 +08:00
}
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
}