From ce8b201a1c1a9dc2135543b7cecbb463871a73a4 Mon Sep 17 00:00:00 2001 From: "Harry (hjbdev)" Date: Mon, 7 Apr 2025 12:15:54 +0100 Subject: [PATCH] queueable actions --- .../FirewallRules/InstallFirewallRule.php | 30 +++++++ .../FirewallRules/UninstallFirewallRule.php | 30 +++++++ app/Enums/FirewallRuleStatus.php | 4 +- app/Enums/FirewallRuleType.php | 9 ++ app/Models/FirewallRule.php | 57 +++--------- composer.json | 1 + composer.lock | 87 ++++++++++++++++++- 7 files changed, 168 insertions(+), 50 deletions(-) create mode 100644 app/Actions/FirewallRules/InstallFirewallRule.php create mode 100644 app/Actions/FirewallRules/UninstallFirewallRule.php create mode 100644 app/Enums/FirewallRuleType.php diff --git a/app/Actions/FirewallRules/InstallFirewallRule.php b/app/Actions/FirewallRules/InstallFirewallRule.php new file mode 100644 index 0000000..a4b3f25 --- /dev/null +++ b/app/Actions/FirewallRules/InstallFirewallRule.php @@ -0,0 +1,30 @@ +server->sshClient(); + $result = $ssh->execute($firewallRule->command()); + + if (! $result->isSuccessful()) { + $firewallRule->update([ + 'status' => FirewallRuleStatus::FAILED, + ]); + return; + } + + $firewallRule->update([ + 'status' => FirewallRuleStatus::INSTALLED, + ]); + } +} diff --git a/app/Actions/FirewallRules/UninstallFirewallRule.php b/app/Actions/FirewallRules/UninstallFirewallRule.php new file mode 100644 index 0000000..f0d1880 --- /dev/null +++ b/app/Actions/FirewallRules/UninstallFirewallRule.php @@ -0,0 +1,30 @@ +server->sshClient(); + $result = $ssh->execute($firewallRule->command(true)); + + if (! $result->isSuccessful()) { + $firewallRule->update([ + 'status' => FirewallRuleStatus::FAILED, + ]); + return; + } + + $firewallRule->update([ + 'status' => FirewallRuleStatus::UNINSTALLED, + ]); + } +} diff --git a/app/Enums/FirewallRuleStatus.php b/app/Enums/FirewallRuleStatus.php index 34e1775..f545284 100644 --- a/app/Enums/FirewallRuleStatus.php +++ b/app/Enums/FirewallRuleStatus.php @@ -8,8 +8,8 @@ enum FirewallRuleStatus: string { use Arrayable; - case NOT_APPLIED = 'not-applied'; - case APPLIED = 'applied'; + case UNINSTALLED = 'uninstalled'; + case INSTALLED = 'installed'; case FAILED = 'failed'; case REMOVED = 'removed'; } \ No newline at end of file diff --git a/app/Enums/FirewallRuleType.php b/app/Enums/FirewallRuleType.php new file mode 100644 index 0000000..de080c4 --- /dev/null +++ b/app/Enums/FirewallRuleType.php @@ -0,0 +1,9 @@ +install(); + app(InstallFirewallRule::class)->execute($firewallRule); }); } @@ -23,6 +25,7 @@ class FirewallRule extends Model { return [ 'status' => FirewallRuleStatus::class, + 'type' => FirewallRuleType::class, ]; } @@ -31,12 +34,14 @@ class FirewallRule extends Model return $this->belongsTo(Server::class); } - public function install(): void + public function command(bool $delete = false): string { - $ssh = $this->server->sshClient(); - $command = "ufw"; + if ($delete) { + $command .= " delete"; + } + if ($this->type === 'allow') { $command .= " allow"; } elseif ($this->type === 'deny') { @@ -50,48 +55,6 @@ class FirewallRule extends Model $command .= " {$this->ports}"; - $result = $ssh->execute($command); - - if (! $result->isSuccessful()) { - $this->update([ - 'status' => FirewallRuleStatus::FAILED, - ]); - return; - } - $this->update([ - 'status' => FirewallRuleStatus::APPLIED, - ]); - } - - public function remove(): void - { - $ssh = $this->server->sshClient(); - - $command = "ufw"; - - if ($this->type === 'allow') { - $command .= " delete allow"; - } elseif ($this->type === 'deny') { - $command .= " delete deny"; - } - - if ($this->from) { - $command .= " from {$this->from}"; - $command .= " to any port"; - } - - $command .= " {$this->ports}"; - - $result = $ssh->execute($command); - - if (! $result->isSuccessful()) { - $this->update([ - 'status' => FirewallRuleStatus::FAILED, - ]); - return; - } - $this->update([ - 'status' => FirewallRuleStatus::REMOVED, - ]); + return $command; } } diff --git a/composer.json b/composer.json index ce8e87e..f37af37 100644 --- a/composer.json +++ b/composer.json @@ -14,6 +14,7 @@ "laravel/framework": "^12.0", "laravel/tinker": "^2.10.1", "saloonphp/saloon": "3.0", + "spatie/laravel-queueable-action": "^2.16", "spatie/ssh": "^1.13", "tightenco/ziggy": "^2.4" }, diff --git a/composer.lock b/composer.lock index 5e0f03e..67eeb9e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "9d020e23e3b4787ce468533e44c3783c", + "content-hash": "0ad069ff3461e30a7d92cfea145f68e7", "packages": [ { "name": "brick/math", @@ -3441,6 +3441,91 @@ ], "time": "2023-10-01T08:13:44+00:00" }, + { + "name": "spatie/laravel-queueable-action", + "version": "2.16.2", + "source": { + "type": "git", + "url": "https://github.com/spatie/laravel-queueable-action.git", + "reference": "6ec68f85ebd771c4c047d1422a57e481abea38e6" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/spatie/laravel-queueable-action/zipball/6ec68f85ebd771c4c047d1422a57e481abea38e6", + "reference": "6ec68f85ebd771c4c047d1422a57e481abea38e6", + "shasum": "" + }, + "require": { + "laravel/framework": "^8.0|^9.0|^10.0|^11.0|^12.0", + "php": "^8.0" + }, + "require-dev": { + "mockery/mockery": "^1.4", + "orchestra/testbench": "^6.23|^7.0|^8.0|^9.0|^10.0", + "pestphp/pest": "^1.22|^2.34|^3.7", + "pestphp/pest-plugin-laravel": "^1.3|^2.3|^3.1" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Spatie\\QueueableAction\\QueueableActionServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "Spatie\\QueueableAction\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Brent Roose", + "email": "brent@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + }, + { + "name": "Alex Vanderbist", + "email": "alex@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + }, + { + "name": "Sebastian De Deyne", + "email": "sebastian@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + }, + { + "name": "Freek Van der Herten", + "email": "freek@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + } + ], + "description": "Queueable action support in Laravel", + "homepage": "https://github.com/spatie/laravel-queueable-action", + "keywords": [ + "laravel-queueable-action", + "spatie" + ], + "support": { + "issues": "https://github.com/spatie/laravel-queueable-action/issues", + "source": "https://github.com/spatie/laravel-queueable-action/tree/2.16.2" + }, + "funding": [ + { + "url": "https://spatie.be/open-source/support-us", + "type": "custom" + } + ], + "time": "2025-02-21T09:31:01+00:00" + }, { "name": "spatie/ssh", "version": "1.13.0",