Bozhidar Slaveykov il y a 1 an
Parent
commit
92428a8c33

+ 2 - 0
bin/add-cron-job.sh → bin/cron-job-add.sh

@@ -1,3 +1,5 @@
+#!/bin/bash
+
 username=$1
 username=$1
 schedule=$2
 schedule=$2
 command=$3
 command=$3

+ 2 - 0
bin/delete-cron-job.sh → bin/cron-job-delete.sh

@@ -1,3 +1,5 @@
+#!/bin/bash
+
 username=$1
 username=$1
 schedule=$2
 schedule=$2
 command=$3
 command=$3

+ 2 - 0
bin/list-cron-jobs.sh → bin/cron-jobs-list.sh

@@ -1,3 +1,5 @@
+#!/bin/bash
+
 # Replace 'username' with the actual username you want to retrieve cron jobs for
 # Replace 'username' with the actual username you want to retrieve cron jobs for
 username=$1
 username=$1
 
 

+ 30 - 0
bin/website-create.sh

@@ -0,0 +1,30 @@
+#!/bin/bash
+
+DOMAIN=$1
+USER=$2
+
+# Path to NGINX sites-available directory
+SITES_AVAILABLE_DIR="/etc/nginx/sites-available"
+SITES_ENABLED_DIR="/etc/nginx/sites-enabled"
+
+# Create the site
+SERVER_ROOT="/var/www/$DOMAIN/public_html"
+
+cp -f /usr/local/phyre/samples/ubuntu/nginx.conf.sample $SITES_AVAILABLE_DIR/$DOMAIN.conf
+ln -s $SITES_AVAILABLE_DIR/$DOMAIN.conf $SITES_ENABLED_DIR/$DOMAIN.conf
+
+mkdir -p $SERVER_ROOT
+chown -R www-data:www-data $SERVER_ROOT
+
+# Replace the domain name in the NGINX config
+sed -i "s/%SERVER_NAME%/${DOMAIN}/g" $SITES_AVAILABLE_DIR/$DOMAIN.conf
+sed -i "s/%USER%/${USER}/g" $SITES_AVAILABLE_DIR/$DOMAIN.conf
+
+SERVER_ROOT_ESCAPED=$(printf '%s\n' "$SERVER_ROOT" | sed -e 's/[\/&]/\\&/g')
+sed -i "s#%SERVER_ROOT%#${SERVER_ROOT_ESCAPED}#g" $SITES_AVAILABLE_DIR/$DOMAIN.conf
+
+# Reload NGINX
+service nginx reload
+
+echo "Created site $DOMAIN"
+echo "done!"

+ 15 - 0
bin/website-delete.sh

@@ -0,0 +1,15 @@
+#!/bin/bash
+
+# Path to NGINX sites-available directory
+sites_available_dir="/etc/nginx/sites-available"
+sites_enabled_dir="/etc/nginx/sites-enabled"
+
+# Delete the site
+rm -rf $sites_available_dir/$1
+rm -rf $sites_enabled_dir/$1
+
+# Reload NGINX
+service nginx reload
+
+echo "Deleted site $1"
+echo "done!"

+ 24 - 0
bin/websites-list.sh

@@ -0,0 +1,24 @@
+#!/bin/bash
+
+# Path to NGINX sites-available directory
+sites_available_dir="/etc/nginx/sites-available"
+
+# Array to hold site configurations
+declare -a sites_array=()
+
+# Loop through NGINX site configuration files and collect data
+for file in "$sites_available_dir"/*; do
+    if [ -f "$file" ] && [ "$(basename "$file")" != "default" ]; then
+        server_name=$(awk '$1 == "server_name" {gsub(/;/, "", $2); print $2; exit}' "$file")
+        root=$(awk '$1 == "root" {gsub(/;/, "", $2); print $2; exit}' "$file")
+
+        # Append site data to the array
+        sites_array+=("{\"file\": \"$file\", \"server_name\": \"$server_name\", \"root\": \"$root\"}")
+    fi
+done
+
+# Convert array to JSON
+json_output=$(printf '%s\n' "${sites_array[@]}" | jq -s '.')
+
+# Output JSON
+echo "$json_output"

+ 11 - 0
samples/ubuntu/nginx.conf.sample

@@ -0,0 +1,11 @@
+server {
+
+    server_name %SERVER_NAME% www.%SERVER_NAME%;
+
+    root        %SERVER_ROOT%;
+    charset     utf-8;
+
+    location / {
+
+    }
+}

+ 0 - 12
web/app/Filament/Resources/DomainResource/Pages/CreateDomain.php

@@ -1,12 +0,0 @@
-<?php
-
-namespace App\Filament\Resources\DomainResource\Pages;
-
-use App\Filament\Resources\DomainResource;
-use Filament\Actions;
-use Filament\Resources\Pages\CreateRecord;
-
-class CreateDomain extends CreateRecord
-{
-    protected static string $resource = DomainResource::class;
-}

+ 20 - 11
web/app/Filament/Resources/DomainResource.php → web/app/Filament/Resources/WebsiteResource.php

@@ -2,9 +2,9 @@
 
 
 namespace App\Filament\Resources;
 namespace App\Filament\Resources;
 
 
-use App\Filament\Resources\DomainResource\Pages;
-use App\Filament\Resources\DomainResource\RelationManagers;
-use App\Models\Domain;
+use App\Filament\Resources\WebsiteResource\Pages;
+use App\Filament\Resources\WebsiteResource\RelationManagers;
+use App\Models\Website;
 use Filament\Forms;
 use Filament\Forms;
 use Filament\Forms\Form;
 use Filament\Forms\Form;
 use Filament\Resources\Resource;
 use Filament\Resources\Resource;
@@ -13,17 +13,23 @@ use Filament\Tables\Table;
 use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Database\Eloquent\SoftDeletingScope;
 use Illuminate\Database\Eloquent\SoftDeletingScope;
 
 
-class DomainResource extends Resource
+class WebsiteResource extends Resource
 {
 {
-    protected static ?string $model = Domain::class;
+    protected static ?string $model = Website::class;
 
 
     protected static ?string $navigationIcon = 'heroicon-o-globe-europe-africa';
     protected static ?string $navigationIcon = 'heroicon-o-globe-europe-africa';
 
 
+    protected static ?int $navigationSort = 4;
+
     public static function form(Form $form): Form
     public static function form(Form $form): Form
     {
     {
         return $form
         return $form
             ->schema([
             ->schema([
-                //
+                Forms\Components\TextInput::make('server_name')
+                    ->autofocus()
+                    ->required()
+                    ->unique()
+                    ->placeholder('example.com'),
             ]);
             ]);
     }
     }
 
 
@@ -31,9 +37,12 @@ class DomainResource extends Resource
     {
     {
         return $table
         return $table
             ->columns([
             ->columns([
-                Tables\Columns\TextColumn::make('label')
+                Tables\Columns\TextColumn::make('server_name')
+                    ->searchable()
+                    ->sortable(),
+                Tables\Columns\TextColumn::make('root')
                     ->searchable()
                     ->searchable()
-                    ->sortable()
+                    ->sortable(),
             ])
             ])
             ->filters([
             ->filters([
                 //
                 //
@@ -58,9 +67,9 @@ class DomainResource extends Resource
     public static function getPages(): array
     public static function getPages(): array
     {
     {
         return [
         return [
-            'index' => Pages\ListDomains::route('/'),
-            'create' => Pages\CreateDomain::route('/create'),
-            'edit' => Pages\EditDomain::route('/{record}/edit'),
+            'index' => Pages\ListWebsites::route('/'),
+            'create' => Pages\CreateWebsite::route('/create'),
+            'edit' => Pages\EditWebsite::route('/{record}/edit'),
         ];
         ];
     }
     }
 }
 }

+ 12 - 0
web/app/Filament/Resources/WebsiteResource/Pages/CreateWebsite.php

@@ -0,0 +1,12 @@
+<?php
+
+namespace App\Filament\Resources\WebsiteResource\Pages;
+
+use App\Filament\Resources\WebsiteResource;
+use Filament\Actions;
+use Filament\Resources\Pages\CreateRecord;
+
+class CreateWebsite extends CreateRecord
+{
+    protected static string $resource = WebsiteResource::class;
+}

+ 4 - 4
web/app/Filament/Resources/DomainResource/Pages/EditDomain.php → web/app/Filament/Resources/WebsiteResource/Pages/EditWebsite.php

@@ -1,14 +1,14 @@
 <?php
 <?php
 
 
-namespace App\Filament\Resources\DomainResource\Pages;
+namespace App\Filament\Resources\WebsiteResource\Pages;
 
 
-use App\Filament\Resources\DomainResource;
+use App\Filament\Resources\WebsiteResource;
 use Filament\Actions;
 use Filament\Actions;
 use Filament\Resources\Pages\EditRecord;
 use Filament\Resources\Pages\EditRecord;
 
 
-class EditDomain extends EditRecord
+class EditWebsite extends EditRecord
 {
 {
-    protected static string $resource = DomainResource::class;
+    protected static string $resource = WebsiteResource::class;
 
 
     protected function getHeaderActions(): array
     protected function getHeaderActions(): array
     {
     {

+ 4 - 4
web/app/Filament/Resources/DomainResource/Pages/ListDomains.php → web/app/Filament/Resources/WebsiteResource/Pages/ListWebsites.php

@@ -1,14 +1,14 @@
 <?php
 <?php
 
 
-namespace App\Filament\Resources\DomainResource\Pages;
+namespace App\Filament\Resources\WebsiteResource\Pages;
 
 
-use App\Filament\Resources\DomainResource;
+use App\Filament\Resources\WebsiteResource;
 use Filament\Actions;
 use Filament\Actions;
 use Filament\Resources\Pages\ListRecords;
 use Filament\Resources\Pages\ListRecords;
 
 
-class ListDomains extends ListRecords
+class ListWebsites extends ListRecords
 {
 {
-    protected static string $resource = DomainResource::class;
+    protected static string $resource = WebsiteResource::class;
 
 
     protected function getHeaderActions(): array
     protected function getHeaderActions(): array
     {
     {

+ 1 - 3
web/app/Models/Backup.php

@@ -13,9 +13,7 @@ class Backup extends Model
     public function getRows()
     public function getRows()
     {
     {
         return [
         return [
-            ['id' => 1, 'label' => 'admin'],
-            ['id' => 2, 'label' => 'manager'],
-            ['id' => 3, 'label' => 'user'],
+
         ];
         ];
     }
     }
 }
 }

+ 3 - 3
web/app/Models/CronJob.php

@@ -28,7 +28,7 @@ class CronJob extends Model
 
 
         static::creating(function ($model) {
         static::creating(function ($model) {
             $args = escapeshellarg($model->user) .' '. escapeshellarg($model->schedule) . ' ' . escapeshellarg($model->command);
             $args = escapeshellarg($model->user) .' '. escapeshellarg($model->schedule) . ' ' . escapeshellarg($model->command);
-            $addCron = shell_exec('/usr/local/phyre/bin/add-cron-job.sh ' . $args);
+            $addCron = shell_exec('/usr/local/phyre/bin/cron-job-add.sh ' . $args);
             if (empty($addCron)) {
             if (empty($addCron)) {
                 return false;
                 return false;
             }
             }
@@ -38,7 +38,7 @@ class CronJob extends Model
 
 
             $args = escapeshellarg($model->user) .' '. escapeshellarg($model->schedule) . ' ' . escapeshellarg($model->command);
             $args = escapeshellarg($model->user) .' '. escapeshellarg($model->schedule) . ' ' . escapeshellarg($model->command);
             $args = str_replace(PHP_EOL, '', $args);
             $args = str_replace(PHP_EOL, '', $args);
-            $command = '/usr/local/phyre/bin/delete-cron-job.sh ' . $args;
+            $command = '/usr/local/phyre/bin/cron-job-delete.sh ' . $args;
             $deleteCron = shell_exec($command);
             $deleteCron = shell_exec($command);
             if (empty($deleteCron)) {
             if (empty($deleteCron)) {
                 return false;
                 return false;
@@ -55,7 +55,7 @@ class CronJob extends Model
     public function getRows()
     public function getRows()
     {
     {
         $user = shell_exec('whoami');
         $user = shell_exec('whoami');
-        $cronList = shell_exec('/usr/local/phyre/bin/list-cron-jobs.sh ' . $user);
+        $cronList = shell_exec('/usr/local/phyre/bin/cron-jobs-list.sh ' . $user);
 
 
         $rows = [];
         $rows = [];
         if (!empty($cronList)) {
         if (!empty($cronList)) {

+ 0 - 22
web/app/Models/Domain.php

@@ -1,22 +0,0 @@
-<?php
-
-namespace App\Models;
-
-use Illuminate\Database\Eloquent\Factories\HasFactory;
-use Illuminate\Database\Eloquent\Model;
-use Sushi\Sushi;
-
-class Domain extends Model
-{
-    use Sushi;
-
-    public function getRows()
-    {
-        return [
-            ['id' => 1, 'label' => 'admin'],
-            ['id' => 2, 'label' => 'manager'],
-            ['id' => 3, 'label' => 'user'],
-        ];
-    }
-
-}

+ 82 - 0
web/app/Models/Website.php

@@ -0,0 +1,82 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+use Sushi\Sushi;
+
+class Website extends Model
+{
+    use Sushi;
+
+    protected $fillable = [
+        'file',
+        'server_name',
+        'root'
+    ];
+
+    protected $schema = [
+        'file' => 'string',
+        'server_name' => 'string',
+        'root' => 'string'
+    ];
+
+    public static function boot()
+    {
+        parent::boot();
+
+        static::creating(function ($model) {
+
+            $args =  escapeshellarg($model->server_name) . ' ' . escapeshellarg('bobi');
+            $args = str_replace(PHP_EOL, '', $args);
+            $command = '/usr/local/phyre/bin/website-create.sh ' . $args;
+            $createWebsite = shell_exec($command);
+            if (empty($createWebsite)) {
+                return false;
+            }
+
+            dd($createWebsite);
+
+        });
+
+        static::deleting(function ($model) {
+
+            $args =  escapeshellarg($model->server_name);
+            $args = str_replace(PHP_EOL, '', $args);
+            $command = '/usr/local/phyre/bin/website-delete.sh ' . $args;
+            $deleteWebsite = shell_exec($command);
+            if (empty($deleteWebsite)) {
+                return false;
+            }
+
+        });
+    }
+
+    protected function sushiShouldCache()
+    {
+        return true;
+    }
+
+    public function getRows()
+    {
+        $websitesList = shell_exec('/usr/local/phyre/bin/websites-list.sh');
+        $rows = [];
+        if (!empty($websitesList)) {
+            $websitesList = json_decode($websitesList, true);
+            if (!empty($websitesList)) {
+                foreach ($websitesList as $website) {
+                    if (isset($website['file'])) {
+                        $rows[] = [
+                            'file' => $website['file'],
+                            'server_name' => $website['server_name'],
+                            'root' => $website['root']
+                        ];
+                    }
+                }
+            }
+        }
+
+        return $rows;
+    }
+}