queueable actions

This commit is contained in:
2025-04-07 12:15:54 +01:00
parent 9e826e5f18
commit ce8b201a1c
7 changed files with 168 additions and 50 deletions

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Actions\FirewallRules;
use App\Enums\FirewallRuleStatus;
use App\Models\FirewallRule;
use Spatie\QueueableAction\QueueableAction;
class InstallFirewallRule
{
use QueueableAction;
public function execute(
FirewallRule $firewallRule,
) {
$ssh = $firewallRule->server->sshClient();
$result = $ssh->execute($firewallRule->command());
if (! $result->isSuccessful()) {
$firewallRule->update([
'status' => FirewallRuleStatus::FAILED,
]);
return;
}
$firewallRule->update([
'status' => FirewallRuleStatus::INSTALLED,
]);
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Actions\FirewallRules;
use App\Enums\FirewallRuleStatus;
use App\Models\FirewallRule;
use Spatie\QueueableAction\QueueableAction;
class UninstallFirewallRule
{
use QueueableAction;
public function execute(
FirewallRule $firewallRule,
) {
$ssh = $firewallRule->server->sshClient();
$result = $ssh->execute($firewallRule->command(true));
if (! $result->isSuccessful()) {
$firewallRule->update([
'status' => FirewallRuleStatus::FAILED,
]);
return;
}
$firewallRule->update([
'status' => FirewallRuleStatus::UNINSTALLED,
]);
}
}

View File

@@ -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';
}

View File

@@ -0,0 +1,9 @@
<?php
namespace App\Enums;
enum FirewallRuleType: string
{
case ALLOW = 'allow';
case DENY = 'deny';
}

View File

@@ -2,7 +2,9 @@
namespace App\Models;
use App\Actions\FirewallRules\InstallFirewallRule;
use App\Enums\FirewallRuleStatus;
use App\Enums\FirewallRuleType;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
@@ -15,7 +17,7 @@ class FirewallRule extends Model
parent::boot();
static::created(function (self $firewallRule) {
$firewallRule->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;
}
}

View File

@@ -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"
},

87
composer.lock generated
View File

@@ -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",