Skip to content

Commit

Permalink
Merge branch 'pelican-dev:main' into feature/admin-roles
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy132 authored Sep 1, 2024
2 parents 8de90f2 + d7b5966 commit 8daac73
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 32 deletions.
3 changes: 3 additions & 0 deletions app/Filament/Pages/Installer/PanelInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ public function submit()
$variables = array_get($inputs, 'env');
$this->writeToEnvironment($variables);

// Clear config cache
Artisan::call('config:clear');

// Run migrations
Artisan::call('migrate', [
'--force' => true,
Expand Down
9 changes: 3 additions & 6 deletions app/Filament/Pages/Installer/Steps/DatabaseStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Filament\Forms\Get;
use Filament\Notifications\Notification;
use Filament\Support\Exceptions\Halt;
use Illuminate\Database\DatabaseManager;
use Illuminate\Support\Facades\DB;
use PDOException;

class DatabaseStep
Expand Down Expand Up @@ -61,9 +61,6 @@ public static function make(): Step
->afterValidation(function (Get $get) {
$driver = $get('env.DB_CONNECTION');
if ($driver !== 'sqlite') {
/** @var DatabaseManager $database */
$database = app(DatabaseManager::class);

try {
config()->set('database.connections._panel_install_test', [
'driver' => $driver,
Expand All @@ -77,15 +74,15 @@ public static function make(): Step
'strict' => true,
]);

$database->connection('_panel_install_test')->getPdo();
DB::connection('_panel_install_test')->getPdo();
} catch (PDOException $exception) {
Notification::make()
->title('Database connection failed')
->body($exception->getMessage())
->danger()
->send();

$database->disconnect('_panel_install_test');
DB::disconnect('_panel_install_test');

throw new Halt('Database connection failed');
}
Expand Down
27 changes: 26 additions & 1 deletion app/Filament/Pages/Installer/Steps/RedisStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

namespace App\Filament\Pages\Installer\Steps;

use Exception;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Wizard\Step;
use Filament\Forms\Get;
use Filament\Notifications\Notification;
use Filament\Support\Exceptions\Halt;
use Illuminate\Support\Facades\Redis;

class RedisStep
{
Expand Down Expand Up @@ -37,6 +42,26 @@ public static function make(): Step
->password()
->revealable()
->default(config('database.redis.default.password')),
]);
])
->afterValidation(function (Get $get) {
try {
config()->set('database.redis._panel_install_test', [
'host' => $get('env.REDIS_HOST'),
'username' => $get('env.REDIS_USERNAME'),
'password' => $get('env.REDIS_PASSWORD'),
'port' => $get('env.REDIS_PORT'),
]);

Redis::connection('_panel_install_test')->command('ping');
} catch (Exception $exception) {
Notification::make()
->title('Redis connection failed')
->body($exception->getMessage())
->danger()
->send();

throw new Halt('Redis connection failed');
}
});
}
}
2 changes: 1 addition & 1 deletion app/Filament/Pages/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ private function generalSettings(): array
TextInput::make('APP_NAME')
->label('App Name')
->required()
->alphaNum()
->default(env('APP_NAME', 'Pelican')),
TextInput::make('APP_FAVICON')
->label('App Favicon')
Expand Down Expand Up @@ -307,7 +308,6 @@ private function mailSettings(): array
->default(env('MAIL_PORT', config('mail.mailers.smtp.port'))),
TextInput::make('MAIL_USERNAME')
->label('Username')
->required()
->default(env('MAIL_USERNAME', config('mail.mailers.smtp.username'))),
TextInput::make('MAIL_PASSWORD')
->label('Password')
Expand Down
65 changes: 45 additions & 20 deletions app/Filament/Resources/ServerResource/Pages/CreateServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use App\Models\Allocation;
use App\Models\Egg;
use App\Models\Node;
use App\Models\ServerVariable;
use App\Models\User;
use App\Services\Allocations\AssignmentService;
use App\Services\Servers\RandomWordService;
Expand Down Expand Up @@ -307,9 +306,9 @@ public function form(Form $form): Form
->completedIcon('tabler-check')
->columns([
'default' => 1,
'sm' => 2,
'md' => 2,
'lg' => 4,
'sm' => 4,
'md' => 4,
'lg' => 6,
])
->schema([
Forms\Components\Select::make('egg_id')
Expand All @@ -319,7 +318,7 @@ public function form(Form $form): Form
'default' => 1,
'sm' => 2,
'md' => 2,
'lg' => 3,
'lg' => 4,
])
->searchable()
->preload()
Expand Down Expand Up @@ -376,29 +375,51 @@ public function form(Form $form): Form
->inline()
->required(),

Forms\Components\ToggleButtons::make('start_on_completion')
->label('Start Server After Install?')
->default(true)
->required()
->columnSpan([
'default' => 1,
'sm' => 1,
'md' => 1,
'lg' => 1,
])
->options([
true => 'Yes',
false => 'No',
])
->colors([
true => 'primary',
false => 'danger',
])
->icons([
true => 'tabler-code',
false => 'tabler-code-off',
])
->inline(),

Forms\Components\Textarea::make('startup')
->hintIcon('tabler-code')
->label('Startup Command')
->hidden(fn (Forms\Get $get) => $get('egg_id') === null)
->required()
->live()
->columnSpan([
'default' => 1,
'sm' => 2,
'md' => 2,
'lg' => 4,
])
->rows(function ($state) {
return str($state)->explode("\n")->reduce(
fn (int $carry, $line) => $carry + floor(strlen($line) / 125),
1
);
}),
})
->columnSpan([
'default' => 1,
'sm' => 4,
'md' => 4,
'lg' => 6,
]),

Forms\Components\Hidden::make('environment')->default([]),

Forms\Components\Hidden::make('start_on_completion')->default(true),

Forms\Components\Section::make('Variables')
->icon('tabler-eggs')
->iconColor('primary')
Expand Down Expand Up @@ -429,7 +450,7 @@ public function form(Form $form): Form

$text = Forms\Components\TextInput::make('variable_value')
->hidden($this->shouldHideComponent(...))
->required(fn (ServerVariable $serverVariable) => $serverVariable->variable->getRequiredAttribute())
->required(fn (Forms\Get $get) => in_array('required', $get('rules')))
->rules(
fn (Forms\Get $get): Closure => function (string $attribute, $value, Closure $fail) use ($get) {
$validator = Validator::make(['validatorkey' => $value], [
Expand All @@ -456,7 +477,7 @@ public function form(Form $form): Form
->live(onBlur: true)
->hintIcon('tabler-code')
->label(fn (Forms\Get $get) => $get('name'))
->hintIconTooltip(fn (Forms\Get $get) => $get('rules'))
->hintIconTooltip(fn (Forms\Get $get) => implode('|', $get('rules')))
->prefix(fn (Forms\Get $get) => '{{' . $get('env_variable') . '}}')
->helperText(fn (Forms\Get $get) => empty($get('description')) ? '' : $get('description'))
->afterStateUpdated(function (Forms\Set $set, Forms\Get $get, $state) {
Expand Down Expand Up @@ -791,9 +812,11 @@ protected function handleRecordCreation(array $data): Model
return $service->handle($data);
}

private function shouldHideComponent(ServerVariable $serverVariable, Forms\Components\Component $component): bool
private function shouldHideComponent(Forms\Get $get, Forms\Components\Component $component): bool
{
$containsRuleIn = array_first($serverVariable->variable->rules, fn ($value) => str($value)->startsWith('in:'), false);
$containsRuleIn = collect($get('rules'))->reduce(
fn ($result, $value) => $result === true && !str($value)->startsWith('in:'), true
);

if ($component instanceof Forms\Components\Select) {
return $containsRuleIn;
Expand All @@ -806,9 +829,11 @@ private function shouldHideComponent(ServerVariable $serverVariable, Forms\Compo
throw new \Exception('Component type not supported: ' . $component::class);
}

private function getSelectOptionsFromRules(ServerVariable $serverVariable): array
private function getSelectOptionsFromRules(Forms\Get $get): array
{
$inRule = array_first($serverVariable->variable->rules, fn ($value) => str($value)->startsWith('in:'));
$inRule = collect($get('rules'))->reduce(
fn ($result, $value) => str($value)->startsWith('in:') ? $value : $result, ''
);

return str($inRule)
->after('in:')
Expand Down
17 changes: 16 additions & 1 deletion app/Filament/Resources/ServerResource/Pages/EditServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Filament\Resources\Pages\EditRecord;
use Illuminate\Support\Facades\Validator;
use Closure;
use Illuminate\Database\Eloquent\Builder;
use Webbingbrasil\FilamentCopyActions\Forms\Actions\CopyAction;

class EditServer extends EditRecord
Expand Down Expand Up @@ -473,7 +474,21 @@ public function form(Form $form): Form
->columnSpan(6),

Forms\Components\Repeater::make('server_variables')
->relationship('serverVariables')
->relationship('serverVariables', function (Builder $query) {
/** @var Server $server */
$server = $this->getRecord();

foreach ($server->variables as $variable) {
ServerVariable::query()->firstOrCreate([
'server_id' => $server->id,
'variable_id' => $variable->id,
], [
'variable_value' => $variable->server_value ?? '',
]);
}

return $query;
})
->grid()
->mutateRelationshipDataBeforeSaveUsing(function (array &$data): array {
foreach ($data as $key => $value) {
Expand Down
6 changes: 3 additions & 3 deletions app/Models/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,21 +244,21 @@ public function allocations(): HasMany
*/
public function isViable(int $memory, int $disk, int $cpu): bool
{
if ($this->memory_overallocate >= 0) {
if ($this->memory > 0 && $this->memory_overallocate >= 0) {
$memoryLimit = $this->memory * (1 + ($this->memory_overallocate / 100));
if ($this->servers_sum_memory + $memory > $memoryLimit) {
return false;
}
}

if ($this->disk_overallocate >= 0) {
if ($this->disk > 0 && $this->disk_overallocate >= 0) {
$diskLimit = $this->disk * (1 + ($this->disk_overallocate / 100));
if ($this->servers_sum_disk + $disk > $diskLimit) {
return false;
}
}

if ($this->cpu_overallocate >= 0) {
if ($this->cpu > 0 && $this->cpu_overallocate >= 0) {
$cpuLimit = $this->cpu * (1 + ($this->cpu_overallocate / 100));
if ($this->servers_sum_cpu + $cpu > $cpuLimit) {
return false;
Expand Down

0 comments on commit 8daac73

Please sign in to comment.