Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update various #16

Merged
merged 23 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
2cb8ede
Update handleRecordUpdate method in EditDomain
sweep-ai[bot] Jun 16, 2024
dd7e588
Merge pull request #43 from liberu-genealogy/sweep/fix_in_editdomainp…
curtisdelicata Jun 17, 2024
18e62b6
Add DockerComposeService, update DomainResource
sweep-ai[bot] Jun 17, 2024
263a91b
Merge pull request #46 from liberu-genealogy/sweep/psr_refactor_domain
curtisdelicata Jun 17, 2024
62f1c90
Add new config generators and container restarter
sweep-ai[bot] Jun 17, 2024
3597966
Merge pull request #48 from liberu-genealogy/sweep/refactor_psr_email
curtisdelicata Jun 17, 2024
5cdbc28
Refactor DnsSettingResource and pages
sweep-ai[bot] Jun 17, 2024
890c327
Merge pull request #50 from liberu-genealogy/sweep/psr_refactor_bind_…
curtisdelicata Jun 17, 2024
bb3fbd0
Add email server config updates and command
sweep-ai[bot] Jun 17, 2024
8717793
Renamed to app/Filament/Admin/Resources/DatabaseResource.php
sweep-ai[bot] Jun 17, 2024
e183768
Renamed to app/Filament/Admin/Resources/DatabaseResource.php
sweep-ai[bot] Jun 17, 2024
25e9c7a
Merge pull request #53 from liberu-genealogy/sweep/enable_email_accou…
curtisdelicata Jun 17, 2024
d946aa6
Add DockerCompose to domain create and update
sweep-ai[bot] Jun 17, 2024
683b481
Merge pull request #54 from liberu-genealogy/sweep/create_filament_3_…
curtisdelicata Jun 17, 2024
e7877d4
Merge pull request #56 from liberu-genealogy/sweep/refactor_to_fix_fi…
curtisdelicata Jun 17, 2024
4543494
Add DockerComposeService, integrate in CreateDomai
sweep-ai[bot] Jun 17, 2024
b9f6bde
Merge pull request #59 from liberu-genealogy/sweep/in_createdomainphp…
curtisdelicata Jun 17, 2024
3b87613
Update config generators and add container restart
sweep-ai[bot] Jun 17, 2024
1ed472c
Merge pull request #62 from liberu-genealogy/sweep/enable_email_accou…
curtisdelicata Jun 17, 2024
822c645
Rename ContainerRestarter, add DnsSettingService,
sweep-ai[bot] Jun 17, 2024
b284017
Merge pull request #63 from liberu-genealogy/sweep/refactor_to_fix_fi…
curtisdelicata Jun 17, 2024
629f2ec
Update EditDomain to handle Docker Compose limit
sweep-ai[bot] Jun 17, 2024
9e35be9
Merge pull request #65 from liberu-genealogy/sweep/in_editdomainphp_l…
curtisdelicata Jun 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected function schedule(Schedule $schedule): void
protected function commands(): void
{
$this->load(__DIR__.'/Commands');

require base_path('routes/console.php');
}
}
23 changes: 23 additions & 0 deletions app/Console/UpdateEmailServersCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Console\Commands;

use App\Filament\Admin\Resources\EmailResource\ContainerRestarter;
use Illuminate\Console\Command;

class UpdateEmailServersCommand extends Command
{
protected $signature = 'email-servers:update';

protected $description = 'Update Dovecot and Postfix Docker instances with new email account configurations';

public function handle()
{
$this->info('Updating Dovecot and Postfix Docker instances...');

$containerRestarter = new ContainerRestarter();
$containerRestarter->restart();

$this->info('Dovecot and Postfix Docker instances updated successfully.');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,32 @@

class ResourceResource extends Resource
{
protected static ?string $model = ResourceModel::class;
protected static ?string $model = Database::class;

protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

public static function form(Form $form): Form
{
return $form
->schema([
//
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
Forms\Components\TextInput::make('user_id')
->required()
->numeric(),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
//
Tables\Columns\TextColumn::make('name')
->searchable(),
Tables\Columns\TextColumn::make('user_id')
->numeric()
->sortable(),
])
->filters([
//
Expand Down
60 changes: 15 additions & 45 deletions app/Filament/Admin/Resources/DnsSettingResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@
use Illuminate\Support\Facades\Storage;
use Symfony\Component\Process\Process;

class DnsSettingResource extends Resource
{
use App\Services\DnsSettingService;

class DnsSettingResource extends Resource {
protected static ?string $model = DnsSetting::class;

protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

public static function form(Form $form): Form
public function __construct(protected DnsSettingService $dnsSettingService)
{
// ...
}

public static function form(Form $form): Form {
return $form
->schema([
Forms\Components\TextInput::make('domain_id')
Expand All @@ -47,8 +52,7 @@ public static function form(Form $form): Form
]);
}

public static function table(Table $table): Table
{
public static function table(Table $table): Table {
return $table
->columns([
Tables\Columns\TextColumn::make('domain_id')
Expand Down Expand Up @@ -88,56 +92,22 @@ public static function table(Table $table): Table
]);
}

public static function getRelations(): array
{
public static function getRelations(): array {
return [
//
];
}

public static function getPages(): array
public function __construct(protected DnsSettingService $dnsSettingService)
{
// ...
}

public static function getPages(): array {
return [
'index' => Pages\ListDnsSettings::route('/'),
'create' => Pages\CreateDnsSetting::route('/create'),
'edit' => Pages\EditDnsSetting::route('/{record}/edit'),
];
}

protected function updateBindDnsRecord(DnsSetting $dnsSetting): void
{
switch ($dnsSetting->record_type) {
case 'A':
$this->generateARecordEntry($dnsSetting);
break;
case 'MX':
$this->generateMxRecordEntry($dnsSetting);
break;
}

$this->restartBindContainer();
}

protected function generateARecordEntry(DnsSetting $dnsSetting): void
{
$entry = "{$dnsSetting->name} IN A {$dnsSetting->value}";
$zonePath = "/etc/bind/records/{$dnsSetting->domain->name}.db";

Storage::disk('bind')->append($zonePath, $entry);
}

protected function generateMxRecordEntry(DnsSetting $dnsSetting): void
{
$entry = "{$dnsSetting->name} IN MX {$dnsSetting->priority} {$dnsSetting->value}";
$zonePath = "/etc/bind/records/{$dnsSetting->domain->name}.db";

Storage::disk('bind')->append($zonePath, $entry);
}

protected function restartBindContainer(): void
{
$process = new Process(['docker-compose', 'restart', 'bind9']);
$process->setWorkingDirectory(base_path());
$process->run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,40 @@
use App\Filament\Admin\Resources\DnsSettingResource;
use Filament\Actions;
use Filament\Resources\Pages\CreateRecord;
use App\Services\DnsSettingService;
use Filament\Notifications\Notification;
use App\Models\DnsSetting;

class CreateDnsSetting extends CreateRecord
{
class CreateDnsSetting extends CreateRecord {
protected static string $resource = DnsSettingResource::class;

public function __construct(protected DnsSettingService $dnsSettingService)
{
// ...
}

protected function create(array $data): DnsSetting
{
try {
$dnsSetting = DnsSetting::create($data);

$this->dnsSettingService->updateBindDnsRecord($dnsSetting);

Notification::make()
->title('DNS Setting Created')
->body('The DNS setting has been created and BIND records have been updated.')
->success()
->send();

return $dnsSetting;
} catch (\Exception $e) {
Notification::make()
->title('Error')
->body('An error occurred while creating the DNS setting: ' . $e->getMessage())
->danger()
->send();

throw $e;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,35 @@
use App\Filament\Admin\Resources\DnsSettingResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
use App\Providers\DnsSettingService;
use Filament\Notifications\Notification;

class EditDnsSetting extends EditRecord
{
class EditDnsSetting extends EditRecord {
protected static string $resource = DnsSettingResource::class;

protected function getHeaderActions(): array
public function __construct(protected DnsSettingService $dnsSettingService)
{
// ...
}

protected function getHeaderActions(): array {
return [
Actions\DeleteAction::make(),
];
}

protected function save(array $data): DnsSetting
{
$this->record->update($data);

$this->dnsSettingService->updateBindDnsRecord($this->record);

Notification::make()
->title('DNS Setting Updated')
->body('The DNS setting has been updated and BIND records have been updated.')
->success()
->send();

return $this->record;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;

class ListDnsSettings extends ListRecords
{
class ListDnsSettings extends ListRecords {
protected static string $resource = DnsSettingResource::class;

protected function getHeaderActions(): array
{
protected function getHeaderActions(): array {
return [
Actions\CreateAction::make(),
];
Expand Down
37 changes: 5 additions & 32 deletions app/Filament/Admin/Resources/DomainResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ public static function getRelations(): array
];
}

public function __construct(protected DomainContainerRestarter $containerRestarter)
{
// ...
}

public static function getPages(): array
{
return [
Expand All @@ -99,37 +104,5 @@ public static function getPages(): array
];
}

protected function generateDockerComposeContent($data, $hostingPlan): string
{
return <<<EOT
version: '3.8'

services:
web:
image: nginx:latest
container_name: {$data['domain_name']}
environment:
- VIRTUAL_HOST={$data['virtual_host']}
- LETSENCRYPT_HOST={$data['letsencrypt_host']}
- LETSENCRYPT_EMAIL={$data['letsencrypt_email']}
volumes:
- ./html:/usr/share/nginx/html
- ./vhost.d:/etc/nginx/conf.d
networks:
- nginx-proxy
deploy:
resources:
limits:
cpus: '0.5'
memory: {$hostingPlan->disk_space}M
reservations:
cpus: '0.25'
memory: {$hostingPlan->bandwidth}M

networks:
nginx-proxy:
external:
name: nginx-proxy
EOT;
}
}
14 changes: 14 additions & 0 deletions app/Filament/Admin/Resources/DomainResource/ContainerRestarter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Filament\Admin\Resources\DomainResource;

class DomainContainerRestarter
{
public function restart(string $domainName): void
{
// Restart necessary containers for the given domain
$process = new Process(['docker-compose', 'restart', $domainName]);
$process->setWorkingDirectory(base_path());
$process->run();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Filament\Admin\Resources\EmailResource;

class DovecotConfigGenerator
{
public function generate(string $email, string $password): string
{
return "user $email {\n password = $password\n}\n";
}
}
31 changes: 21 additions & 10 deletions app/Filament/Admin/Resources/DomainResource/Pages/CreateDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,44 @@
use Filament\Actions;
use Filament\Resources\Pages\CreateRecord;

use Illuminate\Support\Facades\Storage;
use Symfony\Component\Process\Process;
use App\Models\Domain;
use App\Services\DockerComposeService;

use App\Services\DockerComposeService;
use Filament\Notifications\Notification;

class CreateDomain extends CreateRecord
{
protected static string $resource = DomainResource::class;

protected function handleRecordCreation(array $data): Domain
public function __construct(protected DockerComposeService $dockerCompose)
{
// ...
}

protected function create(array $data): Domain
{
$user = auth()->user();

if ($user->hasReachedDockerComposeLimit()) {
throw new \Exception('You have reached the limit of Docker Compose instances for your hosting plan.');
Notification::make()
->title('Docker Compose Limit Reached')
->body('You have reached the limit of Docker Compose instances for your hosting plan.')
->danger()
->send();

return null;
}

$hostingPlan = $user->currentHostingPlan();

$domain = static::getModel()::create([
$domain = Domain::create([
...$data,
'hosting_plan_id' => $hostingPlan->id,
]);

$composeContent = $this->generateDockerComposeContent($data, $hostingPlan);
Storage::disk('local')->put('docker-compose-'.$data['domain_name'].'.yml', $composeContent);

$process = new Process(['docker-compose', '-f', storage_path('app/docker-compose-'.$data['domain_name'].'.yml'), 'up', '-d']);
$process->run();
$this->dockerCompose->generateComposeFile($data, $hostingPlan);
$this->dockerCompose->startServices($data['domain_name']);

return $domain;
}
Expand Down
Loading
Loading