Skip to content

Commit 85e3945

Browse files
committed
Add support for client-side server reinstallation
1 parent 86de737 commit 85e3945

File tree

7 files changed

+169
-47
lines changed

7 files changed

+169
-47
lines changed

app/Http/Controllers/Api/Client/Servers/SettingsController.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
use Illuminate\Http\Response;
66
use Pterodactyl\Models\Server;
7+
use Illuminate\Http\JsonResponse;
78
use Pterodactyl\Repositories\Eloquent\ServerRepository;
9+
use Pterodactyl\Services\Servers\ReinstallServerService;
810
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
911
use Pterodactyl\Http\Requests\Api\Client\Servers\Settings\RenameServerRequest;
12+
use Pterodactyl\Http\Requests\Api\Client\Servers\Settings\ReinstallServerRequest;
1013

1114
class SettingsController extends ClientApiController
1215
{
@@ -15,24 +18,33 @@ class SettingsController extends ClientApiController
1518
*/
1619
private $repository;
1720

21+
/**
22+
* @var \Pterodactyl\Services\Servers\ReinstallServerService
23+
*/
24+
private $reinstallServerService;
25+
1826
/**
1927
* SettingsController constructor.
2028
*
2129
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository
30+
* @param \Pterodactyl\Services\Servers\ReinstallServerService $reinstallServerService
2231
*/
23-
public function __construct(ServerRepository $repository)
24-
{
32+
public function __construct(
33+
ServerRepository $repository,
34+
ReinstallServerService $reinstallServerService
35+
) {
2536
parent::__construct();
2637

2738
$this->repository = $repository;
39+
$this->reinstallServerService = $reinstallServerService;
2840
}
2941

3042
/**
3143
* Renames a server.
3244
*
3345
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Settings\RenameServerRequest $request
3446
* @param \Pterodactyl\Models\Server $server
35-
* @return \Illuminate\Http\Response
47+
* @return \Illuminate\Http\JsonResponse
3648
*
3749
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
3850
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
@@ -43,6 +55,22 @@ public function rename(RenameServerRequest $request, Server $server)
4355
'name' => $request->input('name'),
4456
]);
4557

46-
return Response::create('', Response::HTTP_NO_CONTENT);
58+
return JsonResponse::create([], Response::HTTP_NO_CONTENT);
59+
}
60+
61+
/**
62+
* Reinstalls the server on the daemon.
63+
*
64+
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Settings\ReinstallServerRequest $request
65+
* @param \Pterodactyl\Models\Server $server
66+
* @return \Illuminate\Http\JsonResponse
67+
*
68+
* @throws \Throwable
69+
*/
70+
public function reinstall(ReinstallServerRequest $request, Server $server)
71+
{
72+
$this->reinstallServerService->reinstall($server);
73+
74+
return JsonResponse::create([], Response::HTTP_ACCEPTED);
4775
}
4876
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Requests\Api\Client\Servers\Settings;
4+
5+
use Pterodactyl\Models\Permission;
6+
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
7+
8+
class ReinstallServerRequest extends ClientApiRequest
9+
{
10+
/**
11+
* @return string
12+
*/
13+
public function permission()
14+
{
15+
return Permission::ACTION_SETTINGS_REINSTALL;
16+
}
17+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import http from '@/api/http';
2+
3+
export default (uuid: string): Promise<void> => {
4+
return new Promise((resolve, reject) => {
5+
http.post(`/api/client/servers/${uuid}/settings/reinstall`)
6+
.then(() => resolve())
7+
.catch(reject);
8+
});
9+
}

resources/scripts/components/elements/Can.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ interface Props {
1111
const Can = ({ action, matchAny = false, renderOnError, children }: Props) => {
1212
const can = usePermissions(action);
1313

14-
if (matchAny) {
15-
console.log('Can.tsx', can);
16-
}
17-
1814
return (
1915
<>
2016
{
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import React, { useState } from 'react';
2+
import { ServerContext } from '@/state/server';
3+
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
4+
import TitledGreyBox from '@/components/elements/TitledGreyBox';
5+
import ConfirmationModal from '@/components/elements/ConfirmationModal';
6+
import reinstallServer from '@/api/server/reinstallServer';
7+
import { Actions, useStoreActions } from 'easy-peasy';
8+
import { ApplicationStore } from '@/state';
9+
import { httpErrorToHuman } from '@/api/http';
10+
11+
export default () => {
12+
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
13+
const [ isSubmitting, setIsSubmitting ] = useState(false);
14+
const [ modalVisible, setModalVisible ] = useState(false);
15+
const { addFlash, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
16+
17+
const reinstall = () => {
18+
clearFlashes('settings');
19+
setIsSubmitting(true);
20+
reinstallServer(uuid)
21+
.then(() => {
22+
addFlash({ key: 'settings', type: 'success', message: 'Your server has begun the reinstallation process.' });
23+
})
24+
.catch(error => {
25+
console.error(error);
26+
27+
addFlash({ key: 'settings', type: 'error', message: httpErrorToHuman(error) });
28+
})
29+
.then(() => {
30+
setIsSubmitting(false);
31+
setModalVisible(false);
32+
});
33+
}
34+
35+
return (
36+
<TitledGreyBox title={'Reinstall Server'} className={'relative'}>
37+
<ConfirmationModal
38+
title={'Confirm server reinstallation'}
39+
buttonText={'Yes, reinstall server'}
40+
onConfirmed={() => reinstall()}
41+
showSpinnerOverlay={isSubmitting}
42+
visible={modalVisible}
43+
onDismissed={() => setModalVisible(false)}
44+
>
45+
Your server will be stopped and some files may be deleted or modified during this process, are you sure you wish to continue?
46+
</ConfirmationModal>
47+
<p className={'text-sm'}>
48+
Reinstalling your server will stop it, and then re-run the installation script that initially
49+
set it up.<strong className={'font-bold'}>Some files may be deleted or modified during this process,
50+
please back up your data before continuing.</strong>
51+
</p>
52+
<div className={'mt-6 text-right'}>
53+
<button
54+
type={'button'}
55+
className={'btn btn-sm btn-secondary btn-red'}
56+
onClick={() => setModalVisible(true)}
57+
>
58+
Reinstall Server
59+
</button>
60+
</div>
61+
</TitledGreyBox>
62+
);
63+
};

resources/scripts/components/server/settings/SettingsContainer.tsx

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { UserData } from '@/state/user';
77
import RenameServerBox from '@/components/server/settings/RenameServerBox';
88
import FlashMessageRender from '@/components/FlashMessageRender';
99
import Can from '@/components/elements/Can';
10+
import ReinstallServerBox from '@/components/server/settings/ReinstallServerBox';
1011

1112
export default () => {
1213
const user = useStoreState<ApplicationStore, UserData>(state => state.user.data!);
@@ -17,49 +18,56 @@ export default () => {
1718
<FlashMessageRender byKey={'settings'} className={'mb-4'}/>
1819
<div className={'md:flex'}>
1920
<Can action={'file.sftp'}>
20-
<TitledGreyBox title={'SFTP Details'} className={'w-full md:flex-1 md:max-w-1/2 md:mr-6'}>
21-
<div>
22-
<label className={'input-dark-label'}>Server Address</label>
23-
<input
24-
type={'text'}
25-
className={'input-dark'}
26-
value={`sftp://${server.sftpDetails.ip}:${server.sftpDetails.port}`}
27-
readOnly={true}
28-
/>
29-
</div>
30-
<div className={'mt-6'}>
31-
<label className={'input-dark-label'}>Username</label>
32-
<input
33-
type={'text'}
34-
className={'input-dark'}
35-
value={`${user.username}.${server.id}`}
36-
readOnly={true}
37-
/>
38-
</div>
39-
<div className={'mt-6 flex items-center'}>
40-
<div className={'flex-1'}>
41-
<div className={'border-l-4 border-cyan-500 p-3'}>
42-
<p className={'text-xs text-neutral-200'}>
43-
Your SFTP password is the same as the password you use to access this panel.
44-
</p>
45-
</div>
21+
<div className={'w-full md:flex-1 md:max-w-1/2 md:mr-10'}>
22+
<TitledGreyBox title={'SFTP Details'}>
23+
<div>
24+
<label className={'input-dark-label'}>Server Address</label>
25+
<input
26+
type={'text'}
27+
className={'input-dark'}
28+
value={`sftp://${server.sftpDetails.ip}:${server.sftpDetails.port}`}
29+
readOnly={true}
30+
/>
4631
</div>
47-
<div className={'ml-4'}>
48-
<a
49-
href={`sftp://${user.username}.${server.id}@${server.sftpDetails.ip}:${server.sftpDetails.port}`}
50-
className={'btn btn-sm btn-secondary'}
51-
>
52-
Launch SFTP
53-
</a>
32+
<div className={'mt-6'}>
33+
<label className={'input-dark-label'}>Username</label>
34+
<input
35+
type={'text'}
36+
className={'input-dark'}
37+
value={`${user.username}.${server.id}`}
38+
readOnly={true}
39+
/>
5440
</div>
55-
</div>
56-
</TitledGreyBox>
57-
</Can>
58-
<Can action={'settings.rename'}>
59-
<div className={'w-full mt-6 md:flex-1 md:max-w-1/2 md:mt-0'}>
60-
<RenameServerBox/>
41+
<div className={'mt-6 flex items-center'}>
42+
<div className={'flex-1'}>
43+
<div className={'border-l-4 border-cyan-500 p-3'}>
44+
<p className={'text-xs text-neutral-200'}>
45+
Your SFTP password is the same as the password you use to access this panel.
46+
</p>
47+
</div>
48+
</div>
49+
<div className={'ml-4'}>
50+
<a
51+
href={`sftp://${user.username}.${server.id}@${server.sftpDetails.ip}:${server.sftpDetails.port}`}
52+
className={'btn btn-sm btn-secondary'}
53+
>
54+
Launch SFTP
55+
</a>
56+
</div>
57+
</div>
58+
</TitledGreyBox>
6159
</div>
6260
</Can>
61+
<div className={'w-full mt-6 md:flex-1 md:max-w-1/2 md:mt-0'}>
62+
<Can action={'settings.rename'}>
63+
<div className={'mb-6 md:mb-10'}>
64+
<RenameServerBox/>
65+
</div>
66+
</Can>
67+
<Can action={'settings.reinstall'}>
68+
<ReinstallServerBox/>
69+
</Can>
70+
</div>
6371
</div>
6472
</div>
6573
);

routes/api-client.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,6 @@
8989

9090
Route::group(['prefix' => '/settings'], function () {
9191
Route::post('/rename', 'Servers\SettingsController@rename');
92+
Route::post('/reinstall', 'Servers\SettingsController@reinstall');
9293
});
9394
});

0 commit comments

Comments
 (0)