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 ;
2025-09-24 13:34:19 +02:00
use App\Events\Server\Installed as ServerInstalled ;
2026-04-20 17:25:54 +02:00
use App\Exceptions\Http\HttpForbiddenException ;
2025-09-24 13:34:19 +02:00
use App\Exceptions\Model\DataValidationException ;
use App\Http\Controllers\Controller ;
use App\Http\Requests\Api\Remote\InstallationDataRequest ;
2024-03-12 22:39:16 -04:00
use App\Models\Server ;
2020-01-18 15:26:15 -08:00
use Illuminate\Http\JsonResponse ;
2026-04-20 17:25:54 +02:00
use Illuminate\Http\Request ;
2025-09-24 13:34:19 +02:00
use Illuminate\Http\Response ;
2020-01-18 15:26:15 -08:00
class ServerInstallController extends Controller
{
/**
* Returns installation information for a server .
*/
2026-04-20 17:25:54 +02:00
public function index ( Request $request , Server $server ) : JsonResponse
2020-01-18 15:26:15 -08:00
{
2026-07-09 09:55:11 -04:00
throw_unless ( $server -> node -> is ( $request -> attributes -> get ( 'node' )), new HttpForbiddenException ( 'Requesting node does not have permission to access this server.' ));
2026-04-20 17:25:54 +02:00
$egg = $server -> egg ;
2022-05-04 19:23:01 -04:00
return new JsonResponse ([
2026-04-20 17:25:54 +02:00
'container_image' => $egg -> copy_script_container ,
'entrypoint' => $egg -> copy_script_entry ,
'script' => $egg -> copy_script_install ,
2020-01-18 15:26:15 -08:00
]);
}
2020-01-19 13:50:38 -08:00
/**
* Updates the installation state of a server .
*
2025-09-08 13:12:33 -04:00
* @ throws 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
{
2022-11-21 15:53:54 -05:00
$status = null ;
2020-01-19 13:50:38 -08:00
2026-07-09 09:55:11 -04:00
throw_unless ( $server -> node -> is ( $request -> attributes -> get ( 'node' )), new HttpForbiddenException ( 'Requesting node does not have permission to access this server.' ));
2026-04-20 17:25:54 +02:00
2024-12-12 14:38:45 +01:00
$successful = $request -> boolean ( 'successful' );
2022-11-21 15:53:54 -05:00
2024-12-12 14:38:45 +01:00
// Make sure the type of failure is accurate
if ( ! $successful ) {
$status = $request -> boolean ( 'reinstall' ) ? ServerState :: ReinstallFailed : ServerState :: InstallFailed ;
2022-11-21 15:53:54 -05:00
}
// 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
2024-03-16 15:01:21 -04:00
$isInitialInstall = is_null ( $previouslyInstalledAt );
2024-12-12 14:38:45 +01:00
event ( new ServerInstalled ( $server , $successful , $isInitialInstall ));
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
}