Skip to content

Commit

Permalink
Merge pull request #10 from liberu-genealogy/main
Browse files Browse the repository at this point in the history
Email and docker compose for domain
  • Loading branch information
curtisdelicata authored Jun 16, 2024
2 parents bad36bd + bf1e77e commit d5f2cda
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 2 deletions.
36 changes: 36 additions & 0 deletions app/Filament/Admin/Resources/DomainResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ public static function form(Form $form): Form
->required(),
Forms\Components\DatePicker::make('expiration_date')
->required(),
Forms\Components\TextInput::make('virtual_host')
->required()
->maxLength(255),
Forms\Components\TextInput::make('letsencrypt_host')
->required()
->maxLength(255),
Forms\Components\TextInput::make('letsencrypt_email')
->email()
->required()
->maxLength(255),
]);
}

Expand Down Expand Up @@ -88,4 +98,30 @@ public static function getPages(): array
'edit' => Pages\EditDomain::route('/{record}/edit'),
];
}

protected function generateDockerComposeContent($data): 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
networks:
nginx-proxy:
external:
name: nginx-proxy
EOT;
}
}
16 changes: 16 additions & 0 deletions app/Filament/Admin/Resources/DomainResource/Pages/CreateDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,23 @@
use Filament\Actions;
use Filament\Resources\Pages\CreateRecord;

use Illuminate\Support\Facades\Storage;
use Symfony\Component\Process\Process;

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

protected function handleRecordCreation(array $data): Domain
{
$domain = static::getModel()::create($data);

$composeContent = $this->generateDockerComposeContent($data);
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();

return $domain;
}
}
16 changes: 16 additions & 0 deletions app/Filament/Admin/Resources/DomainResource/Pages/EditDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;

use Illuminate\Support\Facades\Storage;
use Symfony\Component\Process\Process;

class EditDomain extends EditRecord
{
protected static string $resource = DomainResource::class;
Expand All @@ -16,4 +19,17 @@ protected function getHeaderActions(): array
Actions\DeleteAction::make(),
];
}

protected function handleRecordUpdate(Domain $record, array $data): Domain
{
$record->update($data);

$composeContent = $this->generateDockerComposeContent($data);
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();

return $record;
}
}
129 changes: 129 additions & 0 deletions app/Filament/Admin/Resources/EmailResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

namespace App\Filament\Admin\Resources;

use App\Filament\Admin\Resources\EmailResource\Pages;
use App\Models\Email;
use Filament\Forms;
use Filament\Resources\Form;
use Filament\Resources\Resource;
use Filament\Resources\Table;
use Filament\Tables;

class EmailResource extends Resource
{
protected static ?string $model = Email::class;

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

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

public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('email')
->searchable(),
Tables\Columns\TextColumn::make('created_at')
->dateTime(),
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\DeleteBulkAction::make(),
]);
}

public static function getPages(): array
{
return [
'index' => Pages\ManageEmails::route('/'),
];
}

protected function handleRecordCreation(array $data): Email
{
$email = Email::create($data);

// Generate Dovecot configuration
$dovecotConfig = $this->generateDovecotConfig($email);
Storage::disk('dovecot')->put($email->email, $dovecotConfig);

// Generate Postfix configuration
$postfixConfig = $this->generatePostfixConfig($email);
Storage::disk('postfix')->put($email->email, $postfixConfig);

// Restart Dovecot and Postfix containers
$this->restartContainers();

return $email;
}

protected function handleRecordUpdate(Email $email, array $data): Email
{
$email->update($data);

// Update Dovecot configuration
$dovecotConfig = $this->generateDovecotConfig($email);
Storage::disk('dovecot')->put($email->email, $dovecotConfig);

// Update Postfix configuration
$postfixConfig = $this->generatePostfixConfig($email);
Storage::disk('postfix')->put($email->email, $postfixConfig);

// Restart Dovecot and Postfix containers
$this->restartContainers();

return $email;
}

protected function handleRecordDeletion(Email $email)
{
// Remove Dovecot configuration
Storage::disk('dovecot')->delete($email->email);

// Remove Postfix configuration
Storage::disk('postfix')->delete($email->email);

// Restart Dovecot and Postfix containers
$this->restartContainers();

$email->delete();
}

protected function generateDovecotConfig(Email $email): string
{
// Generate Dovecot configuration based on $email
// ...
}

protected function generatePostfixConfig(Email $email): string
{
// Generate Postfix configuration based on $email
// ...
}

protected function restartContainers()
{
// Restart Dovecot and Postfix containers
// ...
}
}
37 changes: 35 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ services:
- ./html:/usr/share/nginx/html
labels:
- com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy

letsencrypt:
image: jrcs/letsencrypt-nginx-proxy-companion
restart: always
Expand All @@ -23,4 +23,37 @@ services:
- ./html:/usr/share/nginx/html:rw
- /var/run/docker.sock:/var/run/docker.sock:ro
environment:
- NGINX_PROXY_CONTAINER=nginx-proxy
- NGINX_PROXY_CONTAINER=nginx-proxy

postfix:
image: boky/postfix
restart: always
environment:
- SMTP_SERVER=example.com
- [email protected]
- SMTP_PASSWORD=password
ports:
- "25:25"
- "587:587"

dovecot:
image: dovecot/dovecot
restart: always
ports:
- "110:110"
- "143:143"
- "993:993"
- "995:995"
volumes:
- ./mail:/var/mail

bind9:
image: internetsystemsconsortium/bind9:9.16
restart: always
ports:
- "53:53/udp"
- "53:53/tcp"
volumes:
- ./bind:/etc/bind
- ./bind/cache:/var/cache/bind
- ./bind/records:/var/lib/bind

0 comments on commit d5f2cda

Please sign in to comment.