2018-01-28 17:14:14 -06:00
|
|
|
<?php
|
|
|
|
|
|
2024-03-12 22:39:16 -04:00
|
|
|
namespace App\Services\Deployment;
|
2018-01-28 17:14:14 -06:00
|
|
|
|
2024-03-12 22:39:16 -04:00
|
|
|
use App\Models\Node;
|
2022-10-14 10:59:20 -06:00
|
|
|
use Illuminate\Support\Collection;
|
2018-01-28 17:14:14 -06:00
|
|
|
|
|
|
|
|
class FindViableNodesService
|
|
|
|
|
{
|
|
|
|
|
/**
|
2024-05-21 21:02:11 -04:00
|
|
|
* Returns a collection of nodes that meet the provided requirements and can then
|
2018-01-28 17:14:14 -06:00
|
|
|
* be passed to the AllocationSelectionService to return a single allocation.
|
|
|
|
|
*
|
|
|
|
|
* This functionality is used for automatic deployments of servers and will
|
2024-05-22 08:34:43 +02:00
|
|
|
* attempt to find all nodes in the defined locations that meet the memory, disk
|
|
|
|
|
* and cpu availability requirements. Any nodes not meeting those requirements
|
2018-01-28 17:14:14 -06:00
|
|
|
* are tossed out, as are any nodes marked as non-public, meaning automatic
|
2018-05-13 10:50:56 -04:00
|
|
|
* deployments should not be done against them.
|
2018-01-28 17:14:14 -06:00
|
|
|
*/
|
2024-05-30 00:57:30 +02:00
|
|
|
public function handle(int $memory = 0, int $disk = 0, int $cpu = 0, $tags = []): Collection
|
2018-01-28 17:14:14 -06:00
|
|
|
{
|
2024-05-21 21:44:49 -04:00
|
|
|
$nodes = Node::query()
|
|
|
|
|
->withSum('servers', 'memory')
|
2024-05-30 00:57:30 +02:00
|
|
|
->withSum('servers', 'disk')
|
2024-05-21 21:44:49 -04:00
|
|
|
->withSum('servers', 'cpu')
|
|
|
|
|
->where('public', true)
|
|
|
|
|
->get();
|
|
|
|
|
|
|
|
|
|
return $nodes
|
|
|
|
|
->filter(fn (Node $node) => !$tags || collect($node->tags)->intersect($tags))
|
2024-05-30 00:57:30 +02:00
|
|
|
->filter(fn (Node $node) => $node->isViable($memory, $disk, $cpu));
|
2018-01-28 17:14:14 -06:00
|
|
|
}
|
|
|
|
|
}
|