use App\Infrastructure\Entities\ServerStatus;
use App\Jobs\UpdateUserPublicKey;
use App\Mail\ServerProvisioned;
use App\Models\FirewallRule;
use App\Models\Task as TaskModel;
use App\Rules\PublicKey;
use App\Server\Firewall\RuleAction;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
class ProvisionFreshServer extends Task implements HasCallbacks
{
// ...
protected function onFinished(TaskModel $task, Request $request)
{
// Store default firewall rules in the database...
$this->server->firewallRules()->createMany([
['name' => 'SSH', 'port' => 22, 'action' => RuleAction::Allow],
['name' => 'HTTP', 'port' => 80, 'action' => RuleAction::Allow],
['name' => 'HTTPS', 'port' => 443, 'action' => RuleAction::Allow],
])->each(function (FirewallRule $firewallRule) {
$firewallRule->forceFill(['installed_at' => now()])->save();
});
$this->server->forceFill([
'provisioned_at' => now(),
'status' => ServerStatus::Running,
])->save();
dispatch(new UpdateUserPublicKey($this->server));
Mail::to($this->server->createdByUser)->queue(new ServerProvisioned($this->server));
}
// ...
}