From 2def33c9d7d2929a1c949b043be72ba0315ab1be Mon Sep 17 00:00:00 2001 From: prplwtf Date: Sat, 22 Jun 2024 14:38:49 +0200 Subject: [PATCH] feat `core` `lib` `app` `build`: Prepare schedules support --- .../Console/KernelExtensions.php | 11 -- .../GetExtensionSchedules.php | 15 +++ app/BlueprintFramework/Schedules/.gitkeep | 0 app/Console/Kernel.php | 4 +- blueprint.sh | 117 ++++++++++++------ .../extensions/console/ScheduleConstructor | 1 + blueprint/lib/misc.sh | 12 ++ tests/Console.yml | 8 +- 8 files changed, 112 insertions(+), 56 deletions(-) delete mode 100644 app/BlueprintFramework/Console/KernelExtensions.php create mode 100644 app/BlueprintFramework/GetExtensionSchedules.php create mode 100644 app/BlueprintFramework/Schedules/.gitkeep create mode 100644 blueprint/extensions/blueprint/private/build/extensions/console/ScheduleConstructor diff --git a/app/BlueprintFramework/Console/KernelExtensions.php b/app/BlueprintFramework/Console/KernelExtensions.php deleted file mode 100644 index 973faed..0000000 --- a/app/BlueprintFramework/Console/KernelExtensions.php +++ /dev/null @@ -1,11 +0,0 @@ -getExtension() == 'php') { + require $file->getPathname(); + } + } + } +}; diff --git a/app/BlueprintFramework/Schedules/.gitkeep b/app/BlueprintFramework/Schedules/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 58770a1..f512ea8 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -8,7 +8,7 @@ use Illuminate\Console\Scheduling\Schedule; use Illuminate\Database\Console\PruneCommand; use Pterodactyl\Repositories\Eloquent\SettingsRepository; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; -use Pterodactyl\BlueprintFramework\Console\KernelExtensions; +use Pterodactyl\BlueprintFramework\GetExtensionSchedules; use Pterodactyl\Services\Telemetry\TelemetryCollectionService; use Pterodactyl\Console\Commands\Schedule\ProcessRunnableCommand; use Pterodactyl\Console\Commands\Maintenance\PruneOrphanedBackupsCommand; @@ -49,7 +49,7 @@ class Kernel extends ConsoleKernel $this->registerTelemetry($schedule); } - KernelExtensions::schedules($schedule); + GetExtensionSchedules::schedules($schedule); } /** diff --git a/blueprint.sh b/blueprint.sh index 06b117b..8f6ce7a 100644 --- a/blueprint.sh +++ b/blueprint.sh @@ -820,22 +820,15 @@ if [[ ( $2 == "-i" ) || ( $2 == "-install" ) || ( $2 == "-add" ) ]]; then VCMD=" eval "$(parse_yaml .blueprint/tmp/"$n"/"$data_console"/Console.yml Console_)" if [[ $DUPLICATE == "y" ]]; then eval "$(parse_yaml .blueprint/extensions/"${identifier}"/private/.store/Console.yml OldConsole_)"; fi - # TODO: read through every console entry - # tests/Console.yml - # Print warning if console configuration is empty - otherwise go through all options. if [[ $Console__ == "" ]]; then PRINT WARNING "Console configuration (Console.yml) is empty!" else PRINT INFO "Creating and linking console commands and schedules.." - ArtisanCommandConstructor="$__BuildDir/extensions/console/ArtisanCommandConstructor.bak" - - { - cp "$__BuildDir/extensions/console/ArtisanCommandConstructor" "$ArtisanCommandConstructor" - } 2>> "$BLUEPRINT__DEBUG" - - sed -i "s~\[id\^]~""${identifier^}""~g" $ArtisanCommandConstructor + # Create (and replace) schedules file + touch "app/BlueprintFramework/Schedules/${identifier^}Schedules.php" 2>> "$BLUEPRINT__DEBUG" + echo -e " "app/BlueprintFramework/Schedules/${identifier^}Schedules.php" for parent in $Console__; do parent="${parent}_" @@ -850,6 +843,16 @@ if [[ ( $2 == "-i" ) || ( $2 == "-install" ) || ( $2 == "-add" ) ]]; then VCMD=" if [[ $child == "Console_"+([0-9])"_Interval" ]]; then CONSOLE_ENTRY_INTE="${!child}"; fi done + ArtisanCommandConstructor="$__BuildDir/extensions/console/ArtisanCommandConstructor.bak" + ScheduleConstructor="$__BuildDir/extensions/console/ScheduleConstructor.bak" + + { + cp "$__BuildDir/extensions/console/ArtisanCommandConstructor" "$ArtisanCommandConstructor" + cp "$__BuildDir/extensions/console/ScheduleConstructor" "$ScheduleConstructor" + } 2>> "$BLUEPRINT__DEBUG" + + sed -i "s~\[id\^]~""${identifier^}""~g" $ArtisanCommandConstructor + CONSOLE_ENTRY_SIGN="${CONSOLE_ENTRY_SIGN//&/\\&}" CONSOLE_ENTRY_DESC="${CONSOLE_ENTRY_DESC//&/\\&}" CONSOLE_ENTRY_SIGN="${CONSOLE_ENTRY_SIGN//\'/\\\'}" @@ -862,22 +865,6 @@ if [[ ( $2 == "-i" ) || ( $2 == "-install" ) || ( $2 == "-add" ) ]]; then VCMD=" echo -e "SIGN: $CONSOLE_ENTRY_SIGN\nDESC: $CONSOLE_ENTRY_DESC\nPATH: $CONSOLE_ENTRY_PATH\nINTE: $CONSOLE_ENTRY_INTE\nIDEN: $CONSOLE_ENTRY_IDEN" >> "$BLUEPRINT__DEBUG" - # Return error if interval is not defined correctly. - # -> We should really allow more than just hourly/daily, but for now - # this is just here for testing. We're probably gonna go with - # minutely/hourly/bihourly/ - # daily/bidaily/weekly/ - # biweekly/monthly/bimonthly - if [[ - ( $CONSOLE_ENTRY_INTE != "hourly" ) && - ( $CONSOLE_ENTRY_INTE != "daily" ) && - ( $CONSOLE_ENTRY_INTE != "" ) - ]]; then - rm -R ".blueprint/tmp/$n" - PRINT FATAL "Console entry intervals can only be empty, 'hourly' or 'daily'." - exit 1 - fi - # Prevent escaping console folder. if [[ ( ${CONSOLE_ENTRY_PATH} == "/"* ) || @@ -926,7 +913,7 @@ if [[ ( $2 == "-i" ) || ( $2 == "-install" ) || ( $2 == "-add" ) ]]; then VCMD=" # Assign value to certain variables if empty/invalid if [[ $CONSOLE_ENTRY_INTE == "" ]]; then CONSOLE_ENTRY_INTE="false"; fi - # Apply variables to contructor. + # Apply variables to contructors. sed -i \ -e "s~\[IDENTIFIER\]~$identifier~g" \ -e "s~\[SIGNATURE\]~$CONSOLE_ENTRY_SIGN~g" \ @@ -934,20 +921,78 @@ if [[ ( $2 == "-i" ) || ( $2 == "-install" ) || ( $2 == "-add" ) ]]; then VCMD=" -e "s~\[FILENAME\]~$CONSOLE_ENTRY_PATH~g" \ -e "s~__ArtisanCommand__~${CONSOLE_ENTRY_IDEN}Command~g" \ "$ArtisanCommandConstructor" + sed -i \ + -e "s~\[IDENTIFIER\]~$identifier~g" \ + -e "s~\[SIGNATURE\]~$CONSOLE_ENTRY_SIGN~g" \ + "$ScheduleConstructor" cp "$ArtisanCommandConstructor" "app/Console/Commands/BlueprintFramework/Extensions/${identifier^}/${CONSOLE_ENTRY_IDEN}Command.php" + # Detect schedule definition and apply it + SCHEDULE_SET=false + if [[ + ( $CONSOLE_ENTRY_INTE != "" ) && + ( $CONSOLE_ENTRY_INTE != "false" ) + ]]; then + # Check if interval has valid cron syntax + if eval is_valid_cron "$CONSOLE_ENTRY_INTE"; then + sed -i "s~\[SCHEDULE\]~cron('$CONSOLE_ENTRY_INTE')~g" "$ScheduleConstructor" + else + SCHEDULE_SET=false + ApplyConsoleInterval() { + sed -i "s~\[SCHEDULE\]~${1}()~g" "$ScheduleConstructor" + SCHEDULE_SET=true + } + if [[ $CONSOLE_ENTRY_INTE == "everySecond" ]]; then ApplyConsoleInterval "everySecond"; fi + if [[ $CONSOLE_ENTRY_INTE == "everyTwoSeconds" ]]; then ApplyConsoleInterval "everyTwoSeconds"; fi + if [[ $CONSOLE_ENTRY_INTE == "everyFiveSeconds" ]]; then ApplyConsoleInterval "everyFiveSeconds"; fi + if [[ $CONSOLE_ENTRY_INTE == "everyTenSeconds" ]]; then ApplyConsoleInterval "everyTenSeconds"; fi + if [[ $CONSOLE_ENTRY_INTE == "everyFifteenSeconds" ]]; then ApplyConsoleInterval "everyFifteenSeconds"; fi + if [[ $CONSOLE_ENTRY_INTE == "everyTwentySeconds" ]]; then ApplyConsoleInterval "everyTwentySeconds"; fi + if [[ $CONSOLE_ENTRY_INTE == "everyThirtySeconds" ]]; then ApplyConsoleInterval "everyThirtySeconds"; fi + if [[ $CONSOLE_ENTRY_INTE == "everyMinute" ]]; then ApplyConsoleInterval "everyMinute"; fi + if [[ $CONSOLE_ENTRY_INTE == "everyTwoMinutes" ]]; then ApplyConsoleInterval "everyTwoMinutes"; fi + if [[ $CONSOLE_ENTRY_INTE == "everyThreeMinutes" ]]; then ApplyConsoleInterval "everyThreeMinutes"; fi + if [[ $CONSOLE_ENTRY_INTE == "everyFourMinutes" ]]; then ApplyConsoleInterval "everyFourMinutes"; fi + if [[ $CONSOLE_ENTRY_INTE == "everyFiveMinutes" ]]; then ApplyConsoleInterval "everyFiveMinutes"; fi + if [[ $CONSOLE_ENTRY_INTE == "everyTenMinutes" ]]; then ApplyConsoleInterval "everyTenMinutes"; fi + if [[ $CONSOLE_ENTRY_INTE == "everyFifteenMinutes" ]]; then ApplyConsoleInterval "everyFifteenMinutes"; fi + if [[ $CONSOLE_ENTRY_INTE == "everyThirtyMinutes" ]]; then ApplyConsoleInterval "everyThirtyMinutes"; fi + if [[ $CONSOLE_ENTRY_INTE == "hourly" ]]; then ApplyConsoleInterval "hourly"; fi + if [[ $CONSOLE_ENTRY_INTE == "daily" ]]; then ApplyConsoleInterval "daily"; fi + if [[ $CONSOLE_ENTRY_INTE == "weekdays" ]]; then ApplyConsoleInterval "daily()->weekdays"; fi + if [[ $CONSOLE_ENTRY_INTE == "weekends" ]]; then ApplyConsoleInterval "daily()->weekends"; fi + if [[ $CONSOLE_ENTRY_INTE == "sundays" ]]; then ApplyConsoleInterval "daily()->sundays"; fi + if [[ $CONSOLE_ENTRY_INTE == "mondays" ]]; then ApplyConsoleInterval "daily()->mondays"; fi + if [[ $CONSOLE_ENTRY_INTE == "tuesdays" ]]; then ApplyConsoleInterval "daily()->tuesdays"; fi + if [[ $CONSOLE_ENTRY_INTE == "wednesdays" ]]; then ApplyConsoleInterval "daily()->wednesdays"; fi + if [[ $CONSOLE_ENTRY_INTE == "thursdays" ]]; then ApplyConsoleInterval "daily()->thursdays"; fi + if [[ $CONSOLE_ENTRY_INTE == "fridays" ]]; then ApplyConsoleInterval "daily()->fridays"; fi + if [[ $CONSOLE_ENTRY_INTE == "saturdays" ]]; then ApplyConsoleInterval "daily()->saturdays"; fi + if [[ $CONSOLE_ENTRY_INTE == "weekly" ]]; then ApplyConsoleInterval "weekly"; fi + if [[ $CONSOLE_ENTRY_INTE == "monthly" ]]; then ApplyConsoleInterval "monthly"; fi + if [[ $CONSOLE_ENTRY_INTE == "quarterly" ]]; then ApplyConsoleInterval "quarterly"; fi + if [[ $CONSOLE_ENTRY_INTE == "yearly" ]]; then ApplyConsoleInterval "yearly"; fi + fi + if $SCHEDULE_SET; then + cat "$ScheduleConstructor" >> "app/BlueprintFramework/Schedules/${identifier^}Schedules.php" + else + PRINT WARNING "Unknown interval provided for console command ($CONSOLE_ENTRY_SIGN), this command will not have an interval!" + fi + fi + # Clear variables after doing all console entry stuff for a defined entry. CONSOLE_ENTRY_SIGN="" CONSOLE_ENTRY_DESC="" CONSOLE_ENTRY_PATH="" CONSOLE_ENTRY_INTE="" CONSOLE_ENTRY_IDEN="" - done - { - rm "$ArtisanCommandConstructor" - } 2>> "$BLUEPRINT__DEBUG" + rm \ + "$ArtisanCommandConstructor" \ + "$ScheduleConstructor" \ + 2>> "$BLUEPRINT__DEBUG" + done fi fi @@ -1266,11 +1311,11 @@ if [[ ( $2 == "-i" ) || ( $2 == "-install" ) || ( $2 == "-add" ) ]]; then VCMD=" # Fix line breaks by removing all of them. sed -i -E "s~~~g" "resources/scripts/blueprint/extends/routers/routes.ts" - { - rm "$ImportConstructor" - rm "$AccountRouteConstructor" - rm "$ServerRouteConstructor" - } 2>> "$BLUEPRINT__DEBUG" + rm \ + "$ImportConstructor" \ + "$AccountRouteConstructor" \ + "$ServerRouteConstructor" \ + 2>> "$BLUEPRINT__DEBUG" fi else # warn about missing components.yml file diff --git a/blueprint/extensions/blueprint/private/build/extensions/console/ScheduleConstructor b/blueprint/extensions/blueprint/private/build/extensions/console/ScheduleConstructor new file mode 100644 index 0000000..7b55b2e --- /dev/null +++ b/blueprint/extensions/blueprint/private/build/extensions/console/ScheduleConstructor @@ -0,0 +1 @@ +$schedule->command("[IDENTIFIER]:[SIGNATURE]")->[SCHEDULE]; \ No newline at end of file diff --git a/blueprint/lib/misc.sh b/blueprint/lib/misc.sh index 0b14b4a..7840f7a 100644 --- a/blueprint/lib/misc.sh +++ b/blueprint/lib/misc.sh @@ -24,6 +24,7 @@ sendTelemetry() { curl --location --silent --connect-timeout 3 "http://api.blueprint.zip:50000/send/$key/$1" & } + # === CACHEREMINDER === updateCacheReminder() { cd "${BLUEPRINT__FOLDER}" || exit @@ -35,4 +36,15 @@ updateCacheReminder() { sed -i "s~I0TWHOPKAB-$oldClassName~I0TWHOPKAB-$newClassName~g" resources/views/blueprint/admin/admin.blade.php sed -i "s~I0TWHOPKAB-$oldClassName~I0TWHOPKAB-$newClassName~g" .blueprint/extensions/blueprint/assets/misc/cacheOverlay-"${newClassName}".css echo "$newClassName" > .blueprint/extensions/blueprint/private/db/randomclassname +} + + +# === IS_VALID_CRON === +function is_valid_cron() { + local cron_expr="$1" + if [[ $(echo "$cron_expr" | crontab 2>/dev/null) ]]; then + echo true + else + echo false + fi } \ No newline at end of file diff --git a/tests/Console.yml b/tests/Console.yml index de597eb..ad8d4d2 100644 --- a/tests/Console.yml +++ b/tests/Console.yml @@ -1,12 +1,6 @@ -#- { -# Signature: "", -# Description: "", -# Path: "", -# Interval: "" -#} - { Signature: "check-latest", # this should become "blueprint:check-latest" Description: "check if blueprint is running the latest version", Path: "./Command.php", - Interval: "hourly/daily" # optional + Interval: "* * * * *" # optional } \ No newline at end of file