Support naming docker images on eggs; closes #4052

Bumps PTDL_v1 export images to PTDL_v2, updates the Minecraft specific eggs to use named images.
This commit is contained in:
DaneEveritt 2022-05-07 17:45:22 -04:00
parent 53207abcb3
commit c8faf64059
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
17 changed files with 212 additions and 261 deletions

View file

@ -74,11 +74,7 @@ class EggController extends Controller
public function store(EggFormRequest $request): RedirectResponse
{
$data = $request->normalize();
if (!empty($data['docker_images']) && !is_array($data['docker_images'])) {
$data['docker_images'] = array_map(function ($value) {
return trim($value);
}, explode("\n", $data['docker_images']));
}
$data['docker_images'] = $this->normalizeDockerImages($data['docker_images'] ?? null);
$egg = $this->creationService->handle($data);
$this->alert->success(trans('admin/nests.eggs.notices.egg_created'))->flash();
@ -91,7 +87,14 @@ class EggController extends Controller
*/
public function view(Egg $egg): View
{
return view('admin.eggs.view', ['egg' => $egg]);
return view('admin.eggs.view', [
'egg' => $egg,
'images' => array_map(
fn ($key, $value) => $key === $value ? $value : "$key|$value",
array_keys($egg->docker_images),
$egg->docker_images,
),
]);
}
/**
@ -104,11 +107,7 @@ class EggController extends Controller
public function update(EggFormRequest $request, Egg $egg): RedirectResponse
{
$data = $request->normalize();
if (!empty($data['docker_images']) && !is_array($data['docker_images'])) {
$data['docker_images'] = array_map(function ($value) {
return trim($value);
}, explode("\n", $data['docker_images']));
}
$data['docker_images'] = $this->normalizeDockerImages($data['docker_images'] ?? null);
$this->updateService->handle($egg, $data);
$this->alert->success(trans('admin/nests.eggs.notices.updated'))->flash();
@ -129,4 +128,22 @@ class EggController extends Controller
return redirect()->route('admin.nests.view', $egg->nest_id);
}
/**
* Normalizes a string of docker image data into the expected egg format.
*/
protected function normalizeDockerImages(string $input = null): array
{
$data = array_map(fn ($value) => trim($value), explode("\n", $input ?? ''));
$images = [];
// Iterate over the image data provided and convert it into a name => image
// pairing that is used to improve the display on the front-end.
foreach ($data as $value) {
$parts = explode('|', $value, 2);
$images[$parts[0]] = empty($parts[1]) ? $parts[0] : $parts[1];
}
return $images;
}
}

View file

@ -78,7 +78,7 @@ class SettingsController extends ClientApiController
*/
public function dockerImage(SetDockerImageRequest $request, Server $server)
{
if (!in_array($server->image, $server->egg->docker_images)) {
if (!in_array($server->image, array_values($server->egg->docker_images))) {
throw new BadRequestHttpException('This server\'s Docker image has been manually set by an administrator and cannot be updated.');
}

View file

@ -27,7 +27,7 @@ class SetDockerImageRequest extends ClientApiRequest implements ClientPermission
Assert::isInstanceOf($server, Server::class);
return [
'docker_image' => ['required', 'string', Rule::in($server->egg->docker_images)],
'docker_image' => ['required', 'string', Rule::in(array_values($server->egg->docker_images))],
];
}
}

View file

@ -12,7 +12,7 @@ namespace Pterodactyl\Models;
* @property array|null $features
* @property string $docker_image -- deprecated, use $docker_images
* @property string $update_url
* @property array $docker_images
* @property array<string, string> $docker_images
* @property array|null $file_denylist
* @property string|null $config_files
* @property string|null $config_startup
@ -50,6 +50,11 @@ class Egg extends Model
*/
public const RESOURCE_NAME = 'egg';
/**
* Defines the current egg export version.
*/
public const EXPORT_VERSION = 'PTDL_v2';
/**
* Different features that can be enabled on any given egg. These are used internally
* to determine which types of frontend functionality should be shown to the user. Eggs

View file

@ -3,6 +3,7 @@
namespace Pterodactyl\Services\Eggs\Sharing;
use Carbon\Carbon;
use Pterodactyl\Models\Egg;
use Illuminate\Support\Collection;
use Pterodactyl\Models\EggVariable;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
@ -34,7 +35,7 @@ class EggExporterService
$struct = [
'_comment' => 'DO NOT EDIT: FILE GENERATED AUTOMATICALLY BY PTERODACTYL PANEL - PTERODACTYL.IO',
'meta' => [
'version' => 'PTDL_v1',
'version' => Egg::EXPORT_VERSION,
'update_url' => $egg->update_url,
],
'exported_at' => Carbon::now()->toIso8601String(),
@ -42,7 +43,7 @@ class EggExporterService
'author' => $egg->author,
'description' => $egg->description,
'features' => $egg->features,
'images' => $egg->docker_images,
'docker_images' => $egg->docker_images,
'file_denylist' => Collection::make($egg->inherit_file_denylist)->filter(function ($value) {
return !empty($value);
}),
@ -63,6 +64,7 @@ class EggExporterService
'variables' => $egg->variables->transform(function (EggVariable $item) {
return Collection::make($item->toArray())
->except(['id', 'egg_id', 'created_at', 'updated_at'])
->merge(['field_type' => 'text'])
->toArray();
}),
];

View file

@ -10,7 +10,6 @@ use Illuminate\Support\Collection;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
use Pterodactyl\Exceptions\Service\Egg\BadJsonFormatException;
use Pterodactyl\Exceptions\Service\InvalidFileUploadException;
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
@ -56,8 +55,8 @@ class EggImporterService
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Service\Egg\BadJsonFormatException
* @throws \Pterodactyl\Exceptions\Service\InvalidFileUploadException
* @throws \JsonException
*/
public function handle(UploadedFile $file, int $nest): Egg
{
@ -66,13 +65,13 @@ class EggImporterService
}
/** @var array $parsed */
$parsed = json_decode($file->openFile()->fread($file->getSize()), true);
if (json_last_error() !== 0) {
throw new BadJsonFormatException(trans('exceptions.nest.importer.json_error', ['error' => json_last_error_msg()]));
$parsed = json_decode($file->openFile()->fread($file->getSize()), true, 512, JSON_THROW_ON_ERROR);
if (!in_array(Arr::get($parsed, 'meta.version') ?? '', ['PTDL_v1', 'PTDL_v2'])) {
throw new InvalidFileUploadException(trans('exceptions.nest.importer.invalid_json_provided'));
}
if (Arr::get($parsed, 'meta.version') !== 'PTDL_v1') {
throw new InvalidFileUploadException(trans('exceptions.nest.importer.invalid_json_provided'));
if ($parsed['meta']['version'] !== Egg::EXPORT_VERSION) {
$parsed = $this->convertV1ToV2($parsed);
}
$nest = $this->nestRepository->getWithEggs($nest);
@ -86,9 +85,7 @@ class EggImporterService
'name' => Arr::get($parsed, 'name'),
'description' => Arr::get($parsed, 'description'),
'features' => Arr::get($parsed, 'features'),
// Maintain backwards compatability for eggs that are still using the old single image
// string format. New eggs can provide an array of Docker images that can be used.
'docker_images' => Arr::get($parsed, 'images') ?? [Arr::get($parsed, 'image')],
'docker_images' => Arr::get($parsed, 'docker_images'),
'file_denylist' => Collection::make(Arr::get($parsed, 'file_denylist'))->filter(function ($value) {
return !empty($value);
}),
@ -105,6 +102,8 @@ class EggImporterService
], true, true);
Collection::make($parsed['variables'] ?? [])->each(function (array $variable) use ($egg) {
unset($variable['field_type']);
$this->eggVariableRepository->create(array_merge($variable, [
'egg_id' => $egg->id,
]));
@ -114,4 +113,33 @@ class EggImporterService
return $egg;
}
/**
* Converts a PTDL_V1 egg into the expected PTDL_V2 egg format. This just handles
* the "docker_images" field potentially not being present, and not being in the
* expected "key => value" format.
*/
protected function convertV1ToV2(array $parsed): array
{
// Maintain backwards compatability for eggs that are still using the old single image
// string format. New eggs can provide an array of Docker images that can be used.
if (!isset($parsed['images'])) {
$images = [Arr::get($parsed, 'image') ?? 'nil'];
} else {
$images = $parsed['images'];
}
unset($parsed['images'], $parsed['image']);
$parsed['docker_images'] = [];
foreach ($images as $image) {
$parsed['docker_images'][$image] = $image;
}
$parsed['variables'] = array_map(function ($value) {
return array_merge($value, ['field_type' => 'text']);
}, $parsed['variables']);
return $parsed;
}
}

View file

@ -3,6 +3,7 @@
namespace Pterodactyl\Transformers\Api\Application;
use Pterodactyl\Models\Egg;
use Illuminate\Support\Arr;
use Pterodactyl\Models\Nest;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\EggVariable;
@ -49,7 +50,7 @@ class EggTransformer extends BaseTransformer
// "docker_image" is deprecated, but left here to avoid breaking too many things at once
// in external software. We'll remove it down the road once things have gotten the chance
// to upgrade to using "docker_images".
'docker_image' => count($model->docker_images) > 0 ? $model->docker_images[0] : '',
'docker_image' => count($model->docker_images) > 0 ? Arr::first($model->docker_images) : '',
'docker_images' => $model->docker_images,
'config' => [
'files' => json_decode($model->config_files, true),