-
Notifications
You must be signed in to change notification settings - Fork 11
Create daemons on initial site provisioning #129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Actions; | ||
|
||
use Illuminate\Support\Str; | ||
use Lorisleiva\Actions\Concerns\AsAction; | ||
|
||
class LineBreaksToArray | ||
{ | ||
use AsAction; | ||
|
||
public function handle(?string $content): ?array | ||
{ | ||
return str($content) | ||
->explode("\n") | ||
->map(Str::squish(...)) | ||
->filter() | ||
->values() | ||
->all(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace App\Actions; | ||
|
||
use App\Services\Syntax\CommandParser; | ||
use App\Traits\CommandSyntax; | ||
use App\Traits\CommandSyntaxParser; | ||
use App\Traits\Outputifier; | ||
use Lorisleiva\Actions\Concerns\AsAction; | ||
|
||
class ParseDaemonCommands | ||
{ | ||
use AsAction, | ||
Outputifier, | ||
CommandSyntax; | ||
|
||
protected string $signature = '{command* : The command you want to run} | ||
{--directory= : Working directory} | ||
{--user= : User to run the command under} | ||
{--processes=1 : How many processes to run} | ||
{--startsecs= : The total number of seconds the program must stay running in order to consider the start successful} | ||
{--stopwaitsecs=10 : The number of seconds Supervisor will allow for the daemon to gracefully stop before forced termination} | ||
{--stopsignal=SIGTERM : The signal used to kill the program when a stop is requested}'; | ||
|
||
public function handle(array $commands): array | ||
{ | ||
$definition = $this->definition($this->signature); | ||
|
||
return array_map(function (string $command) use ($definition) { | ||
$input = $this->input($command, $definition); | ||
|
||
if (blank($input->getArgument('command'))) { | ||
$this->failCommand("No command was specified for daemon creation: {$command}"); | ||
|
||
return []; | ||
} | ||
|
||
return array_filter([ | ||
'command' => implode(' ', $input->getArgument('command')), | ||
...$input->getOptions(), | ||
], fn ($value) => ! is_null($value)); | ||
}, $commands); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Services\Forge\Pipeline; | ||
|
||
use App\Actions\LineBreaksToArray; | ||
use App\Actions\ParseDaemonCommands; | ||
use App\Services\Forge\ForgeService; | ||
use App\Traits\Outputifier; | ||
use Closure; | ||
|
||
class CreateDaemons | ||
{ | ||
use Outputifier; | ||
|
||
public function __invoke(ForgeService $service, Closure $next) | ||
{ | ||
if (! $service->setting->daemons || ! $service->siteNewlyMade) { | ||
return $next($service); | ||
} | ||
|
||
$daemons = ParseDaemonCommands::run( | ||
LineBreaksToArray::run($service->setting->daemons), | ||
); | ||
|
||
$this->information('Creating daemons.'); | ||
|
||
foreach ($daemons as $daemon) { | ||
$service->forge->createDaemon( | ||
serverId: $service->server->id, | ||
data: array_merge( | ||
[ | ||
// Defaults a daemon to run under the same user and directory as the current site. | ||
'user' => $service->site->username, | ||
'directory' => $service->siteDirectory(), | ||
], | ||
$daemon | ||
) | ||
); | ||
} | ||
|
||
return $next($service); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Services\Forge\Pipeline; | ||
|
||
use App\Actions\LineBreaksToArray; | ||
use App\Actions\ParseDaemonCommands; | ||
use App\Services\Forge\ForgeService; | ||
use App\Services\Forge\ForgeSetting; | ||
use App\Traits\Outputifier; | ||
use Closure; | ||
use Illuminate\Support\Str; | ||
use Laravel\Forge\Resources\Daemon; | ||
|
||
class RemoveDaemons | ||
{ | ||
use Outputifier; | ||
|
||
public function __invoke(ForgeService $service, Closure $next) | ||
{ | ||
$daemons = $service->forge->daemons($service->server->id); | ||
|
||
$this->information('Deleting daemons'); | ||
|
||
foreach ($daemons as $daemon) { | ||
// If the daemon is running under the same user as the site in user isolation mode. | ||
if ($service->setting->siteIsolationRequired && $daemon->user === $service->site->username) { | ||
$service->forge->deleteDaemon($service->server->id, $daemon->id); | ||
$this->information("--> Deleted daemon under user: {$daemon->command}"); | ||
|
||
continue; | ||
} | ||
|
||
// If the daemon is running from the same directory as the site. | ||
if (Str::contains(haystack: $daemon->directory, needles: $service->siteDirectory())) { | ||
$service->forge->deleteDaemon($service->server->id, $daemon->id); | ||
$this->information("--> Deleted daemon under directory: {$daemon->command}"); | ||
|
||
continue; | ||
} | ||
|
||
// If a daemon can be detected as being one that was configured by us. | ||
if ($this->daemonWasAddedForThisSite($daemon, $service->setting)) { | ||
$service->forge->deleteDaemon($service->server->id, $daemon->id); | ||
$this->information("--> Deleted daemon created with site: {$daemon->command}"); | ||
} | ||
} | ||
|
||
return $next($service); | ||
} | ||
|
||
protected function daemonWasAddedForThisSite(Daemon $daemon, ForgeSetting $setting): bool | ||
{ | ||
if (empty($setting->daemons)) { | ||
return false; | ||
} | ||
|
||
$configuredDaemons = ParseDaemonCommands::run( | ||
LineBreaksToArray::run($setting->daemons), | ||
); | ||
|
||
return collect($configuredDaemons)->contains( | ||
fn (array $configuredDaemon) => $configuredDaemon['command'] === $daemon->command | ||
&& $configuredDaemon['user'] === $daemon->user | ||
&& $configuredDaemon['directory'] === $daemon->directory | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace App\Traits; | ||
|
||
use Illuminate\Console\Parser; | ||
use Symfony\Component\Console\Input\InputDefinition; | ||
use Symfony\Component\Console\Input\StringInput; | ||
|
||
trait CommandSyntax | ||
{ | ||
protected function input(string $command, InputDefinition $definition): StringInput | ||
{ | ||
$input = new StringInput($command); | ||
$input->bind($definition); | ||
|
||
return $input; | ||
} | ||
|
||
protected function definition(string $signature): InputDefinition | ||
{ | ||
[, $arguments, $options] = Parser::parse($signature); | ||
|
||
$definition = new InputDefinition(); | ||
$definition->setArguments($arguments); | ||
$definition->setOptions($options); | ||
|
||
return $definition; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.