diff --git a/bin/add-cron-job.sh b/bin/cron-job-add.sh similarity index 96% rename from bin/add-cron-job.sh rename to bin/cron-job-add.sh index 3cc58fd..e2c1e4b 100644 --- a/bin/add-cron-job.sh +++ b/bin/cron-job-add.sh @@ -1,3 +1,5 @@ +#!/bin/bash + username=$1 schedule=$2 command=$3 diff --git a/bin/delete-cron-job.sh b/bin/cron-job-delete.sh similarity index 94% rename from bin/delete-cron-job.sh rename to bin/cron-job-delete.sh index fb60d8c..e10d8ae 100644 --- a/bin/delete-cron-job.sh +++ b/bin/cron-job-delete.sh @@ -1,3 +1,5 @@ +#!/bin/bash + username=$1 schedule=$2 command=$3 diff --git a/bin/list-cron-jobs.sh b/bin/cron-jobs-list.sh similarity index 96% rename from bin/list-cron-jobs.sh rename to bin/cron-jobs-list.sh index b236c90..a31153c 100644 --- a/bin/list-cron-jobs.sh +++ b/bin/cron-jobs-list.sh @@ -1,3 +1,5 @@ +#!/bin/bash + # Replace 'username' with the actual username you want to retrieve cron jobs for username=$1 diff --git a/bin/website-create.sh b/bin/website-create.sh new file mode 100644 index 0000000..6f42bca --- /dev/null +++ b/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!" diff --git a/bin/website-delete.sh b/bin/website-delete.sh new file mode 100644 index 0000000..9161aea --- /dev/null +++ b/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!" diff --git a/bin/websites-list.sh b/bin/websites-list.sh new file mode 100644 index 0000000..d1e5340 --- /dev/null +++ b/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" diff --git a/samples/ubuntu/nginx.conf.sample b/samples/ubuntu/nginx.conf.sample new file mode 100644 index 0000000..f5c7c6d --- /dev/null +++ b/samples/ubuntu/nginx.conf.sample @@ -0,0 +1,11 @@ +server { + + server_name %SERVER_NAME% www.%SERVER_NAME%; + + root %SERVER_ROOT%; + charset utf-8; + + location / { + + } +} diff --git a/web/app/Filament/Resources/DomainResource/Pages/CreateDomain.php b/web/app/Filament/Resources/DomainResource/Pages/CreateDomain.php deleted file mode 100644 index c3f65ef..0000000 --- a/web/app/Filament/Resources/DomainResource/Pages/CreateDomain.php +++ /dev/null @@ -1,12 +0,0 @@ -schema([ - // + Forms\Components\TextInput::make('server_name') + ->autofocus() + ->required() + ->unique() + ->placeholder('example.com'), ]); } @@ -31,9 +37,12 @@ class DomainResource extends Resource { return $table ->columns([ - Tables\Columns\TextColumn::make('label') + Tables\Columns\TextColumn::make('server_name') ->searchable() - ->sortable() + ->sortable(), + Tables\Columns\TextColumn::make('root') + ->searchable() + ->sortable(), ]) ->filters([ // @@ -58,9 +67,9 @@ class DomainResource extends Resource public static function getPages(): array { 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'), ]; } } diff --git a/web/app/Filament/Resources/WebsiteResource/Pages/CreateWebsite.php b/web/app/Filament/Resources/WebsiteResource/Pages/CreateWebsite.php new file mode 100644 index 0000000..1d4e851 --- /dev/null +++ b/web/app/Filament/Resources/WebsiteResource/Pages/CreateWebsite.php @@ -0,0 +1,12 @@ + 1, 'label' => 'admin'], - ['id' => 2, 'label' => 'manager'], - ['id' => 3, 'label' => 'user'], + ]; } } diff --git a/web/app/Models/CronJob.php b/web/app/Models/CronJob.php index 8fad471..6dc4faa 100644 --- a/web/app/Models/CronJob.php +++ b/web/app/Models/CronJob.php @@ -28,7 +28,7 @@ class CronJob extends Model static::creating(function ($model) { $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)) { return false; } @@ -38,7 +38,7 @@ class CronJob extends Model $args = escapeshellarg($model->user) .' '. escapeshellarg($model->schedule) . ' ' . escapeshellarg($model->command); $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); if (empty($deleteCron)) { return false; @@ -55,7 +55,7 @@ class CronJob extends Model public function getRows() { $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 = []; if (!empty($cronList)) { diff --git a/web/app/Models/Domain.php b/web/app/Models/Domain.php deleted file mode 100644 index 1032c6f..0000000 --- a/web/app/Models/Domain.php +++ /dev/null @@ -1,22 +0,0 @@ - 1, 'label' => 'admin'], - ['id' => 2, 'label' => 'manager'], - ['id' => 3, 'label' => 'user'], - ]; - } - -} diff --git a/web/app/Models/Website.php b/web/app/Models/Website.php new file mode 100644 index 0000000..8f950e7 --- /dev/null +++ b/web/app/Models/Website.php @@ -0,0 +1,82 @@ + '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; + } +}