lasthourcloud/app/Livewire/Project/New/Select.php

193 lines
6.7 KiB
PHP
Raw Normal View History

<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Project\New;
use App\Models\Project;
use App\Models\Server;
2023-08-23 08:14:39 +00:00
use Countable;
2023-08-30 09:26:46 +00:00
use Illuminate\Support\Collection;
use Livewire\Component;
class Select extends Component
{
public $current_step = 'type';
2023-12-15 14:48:01 +00:00
public ?Server $server = null;
public string $type;
public string $server_id;
public string $destination_uuid;
2024-05-24 15:37:51 +00:00
public Collection|null|Server $allServers;
public Collection|null|Server $servers;
public ?Collection $standaloneDockers;
public ?Collection $swarmDockers;
public array $parameters;
2023-09-25 13:48:43 +00:00
public Collection|array $services = [];
2023-10-24 10:33:49 +00:00
public Collection|array $allServices = [];
2023-12-18 13:01:25 +00:00
public bool $isDatabase = false;
public bool $includeSwarm = true;
2023-10-24 10:33:49 +00:00
2023-09-25 13:48:43 +00:00
public bool $loadingServices = true;
2023-09-27 19:51:06 +00:00
public bool $loading = false;
public $environments = [];
public ?string $selectedEnvironment = null;
2023-09-05 10:14:31 +00:00
public ?string $existingPostgresqlUrl = null;
2023-10-24 10:33:49 +00:00
public ?string $search = null;
2023-08-30 09:06:44 +00:00
protected $queryString = [
2023-12-15 14:48:01 +00:00
'server_id',
2023-10-24 10:33:49 +00:00
'search'
2023-08-30 09:06:44 +00:00
];
2023-09-26 12:45:52 +00:00
public function mount()
{
$this->parameters = get_route_parameters();
2023-09-05 10:14:31 +00:00
if (isDev()) {
$this->existingPostgresqlUrl = 'postgres://coolify:password@coolify-db:5432';
}
$projectUuid = data_get($this->parameters, 'project_uuid');
$this->environments = Project::whereUuid($projectUuid)->first()->environments;
$this->selectedEnvironment = data_get($this->parameters, 'environment_name');
}
2023-10-24 10:33:49 +00:00
public function render()
{
return view('livewire.project.new.select');
}
public function updatedSelectedEnvironment()
{
2024-01-07 15:23:41 +00:00
return redirect()->route('project.resource.create', [
'project_uuid' => $this->parameters['project_uuid'],
'environment_name' => $this->selectedEnvironment,
2023-12-27 15:45:01 +00:00
]);
}
2023-10-24 10:33:49 +00:00
2023-09-05 10:14:31 +00:00
// public function addExistingPostgresql()
// {
// try {
// instantCommand("psql {$this->existingPostgresqlUrl} -c 'SELECT 1'");
2023-12-07 18:06:32 +00:00
// $this->dispatch('success', 'Successfully connected to the database.');
2023-09-11 15:36:30 +00:00
// } catch (\Throwable $e) {
// return handleError($e, $this);
2023-09-05 10:14:31 +00:00
// }
// }
2023-09-26 12:45:52 +00:00
2024-03-28 14:18:05 +00:00
public function updatedSearch()
{
$this->loadServices();
}
2024-02-20 14:07:12 +00:00
public function loadServices(bool $force = false)
2023-09-25 13:48:43 +00:00
{
try {
2024-03-27 13:50:56 +00:00
$this->loadingServices = true;
2024-02-20 14:07:12 +00:00
if (count($this->allServices) > 0 && !$force) {
2023-10-24 10:33:49 +00:00
if (!$this->search) {
$this->services = $this->allServices;
return;
}
$this->services = $this->allServices->filter(function ($service, $key) {
$tags = collect(data_get($service, 'tags', []));
return str_contains(strtolower($key), strtolower($this->search)) || $tags->contains(function ($tag) {
return str_contains(strtolower($tag), strtolower($this->search));
});
});
} else {
$this->search = null;
$this->allServices = get_service_templates($force);
2023-10-24 10:33:49 +00:00
$this->services = $this->allServices->filter(function ($service, $key) {
return str_contains(strtolower($key), strtolower($this->search));
});
2023-09-25 13:48:43 +00:00
}
} catch (\Throwable $e) {
return handleError($e, $this);
} finally {
$this->loadingServices = false;
}
}
2023-12-18 13:01:25 +00:00
public function instantSave()
{
if ($this->includeSwarm) {
$this->servers = $this->allServers;
} else {
2024-05-24 15:37:51 +00:00
if ($this->allServers instanceof Collection) {
$this->servers = $this->allServers->where('settings.is_swarm_worker', false)->where('settings.is_swarm_manager', false)->where('settings.is_build_server', false);
} else {
$this->servers = $this->allServers;
}
2023-12-18 13:01:25 +00:00
}
}
2023-09-05 10:14:31 +00:00
public function setType(string $type)
{
2023-09-27 19:51:06 +00:00
if ($this->loading) return;
$this->loading = true;
2023-12-18 13:01:25 +00:00
$this->type = $type;
switch ($type) {
case 'postgresql':
case 'mysql':
case 'mariadb':
case 'redis':
2024-04-10 13:00:46 +00:00
case 'keydb':
case 'dragonfly':
case 'clickhouse':
2023-12-18 13:01:25 +00:00
case 'mongodb':
$this->isDatabase = true;
$this->includeSwarm = false;
2024-05-24 15:37:51 +00:00
if ($this->allServers instanceof Collection) {
$this->servers = $this->allServers->where('settings.is_swarm_worker', false)->where('settings.is_swarm_manager', false)->where('settings.is_build_server', false);
} else {
$this->servers = $this->allServers;
}
2023-12-18 13:01:25 +00:00
break;
}
2023-12-21 07:46:48 +00:00
if (str($type)->startsWith('one-click-service') || str($type)->startsWith('docker-compose-empty')) {
2023-12-18 13:01:25 +00:00
$this->isDatabase = true;
$this->includeSwarm = false;
2024-05-24 15:37:51 +00:00
if ($this->allServers instanceof Collection) {
$this->servers = $this->allServers->where('settings.is_swarm_worker', false)->where('settings.is_swarm_manager', false)->where('settings.is_build_server', false);
} else {
$this->servers = $this->allServers;
}
2023-12-18 13:01:25 +00:00
}
2023-09-05 10:14:31 +00:00
if ($type === "existing-postgresql") {
$this->current_step = $type;
return;
}
2023-12-18 13:01:25 +00:00
// if (count($this->servers) === 1) {
// $server = $this->servers->first();
// $this->setServer($server);
// }
2023-08-30 09:26:46 +00:00
if (!is_null($this->server)) {
2023-12-15 14:48:01 +00:00
$foundServer = $this->servers->where('id', $this->server->id)->first();
2023-08-30 09:06:44 +00:00
if ($foundServer) {
2023-09-05 10:14:31 +00:00
return $this->setServer($foundServer);
2023-08-30 09:06:44 +00:00
}
}
$this->current_step = 'servers';
}
2023-09-05 10:14:31 +00:00
public function setServer(Server $server)
{
$this->server_id = $server->id;
2023-12-15 14:48:01 +00:00
$this->server = $server;
2023-08-30 09:26:46 +00:00
$this->standaloneDockers = $server->standaloneDockers;
$this->swarmDockers = $server->swarmDockers;
$this->current_step = 'destinations';
}
2023-09-05 10:14:31 +00:00
public function setDestination(string $destination_uuid)
{
$this->destination_uuid = $destination_uuid;
2024-01-07 15:23:41 +00:00
return redirect()->route('project.resource.create', [
'project_uuid' => $this->parameters['project_uuid'],
'environment_name' => $this->parameters['environment_name'],
'type' => $this->type,
'destination' => $this->destination_uuid,
2023-09-21 15:48:31 +00:00
'server_id' => $this->server_id,
]);
}
2023-09-25 13:48:43 +00:00
public function loadServers()
{
2023-08-21 08:18:11 +00:00
$this->servers = Server::isUsable()->get();
2023-12-18 13:01:25 +00:00
$this->allServers = $this->servers;
}
}