coolify/app/Http/Livewire/Project/New/Select.php

126 lines
3.8 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Livewire\Project\New;
use App\Models\Server;
2023-08-23 10:14:39 +02:00
use Countable;
2023-08-30 11:26:46 +02:00
use Illuminate\Support\Collection;
2023-09-25 15:48:43 +02:00
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Livewire\Component;
2023-09-25 15:48:43 +02:00
use Illuminate\Support\Str;
class Select extends Component
{
public $current_step = 'type';
2023-08-30 11:06:44 +02:00
public ?int $server = null;
public string $type;
public string $server_id;
public string $destination_uuid;
2023-09-23 11:47:24 +02:00
public Countable|array|Server $servers = [];
2023-08-30 11:26:46 +02:00
public Collection|array $standaloneDockers = [];
public Collection|array $swarmDockers = [];
public array $parameters;
2023-09-25 15:48:43 +02:00
public Collection|array $services = [];
public bool $loadingServices = true;
2023-09-05 12:14:31 +02:00
public ?string $existingPostgresqlUrl = null;
2023-08-30 11:06:44 +02:00
protected $queryString = [
'server',
];
public function mount()
{
$this->parameters = get_route_parameters();
2023-09-05 12:14:31 +02:00
if (isDev()) {
$this->existingPostgresqlUrl = 'postgres://coolify:password@coolify-db:5432';
}
}
2023-09-05 12:14:31 +02:00
// public function addExistingPostgresql()
// {
// try {
// instantCommand("psql {$this->existingPostgresqlUrl} -c 'SELECT 1'");
// $this->emit('success', 'Successfully connected to the database.');
2023-09-11 17:36:30 +02:00
// } catch (\Throwable $e) {
// return handleError($e, $this);
2023-09-05 12:14:31 +02:00
// }
// }
2023-09-25 15:48:43 +02:00
public function loadThings()
{
$this->loadServices();
$this->loadServers();
}
public function loadServices(bool $forceReload = false)
{
try {
if ($forceReload) {
Cache::forget('services');
}
$cached = Cache::remember('services', 3600, function () {
$services = Http::get(config('constants.services.offical'));
if ($services->failed()) {
throw new \Exception($services->body());
}
$services = collect($services->json());
$this->emit('success', 'Successfully reloaded services from the internet.');
return $services;
});
$this->services = $cached;
} catch (\Throwable $e) {
ray($e);
return handleError($e, $this);
} finally {
$this->loadingServices = false;
}
}
2023-09-05 12:14:31 +02:00
public function setType(string $type)
{
$this->type = $type;
2023-09-05 12:14:31 +02:00
if ($type === "existing-postgresql") {
$this->current_step = $type;
return;
}
2023-08-23 10:14:39 +02:00
if (count($this->servers) === 1) {
$server = $this->servers->first();
2023-09-05 12:14:31 +02:00
$this->setServer($server);
2023-08-23 10:14:39 +02:00
if (count($server->destinations()) === 1) {
2023-09-05 12:14:31 +02:00
$this->setDestination($server->destinations()->first()->uuid);
2023-08-23 10:14:39 +02:00
}
}
2023-08-30 11:26:46 +02:00
if (!is_null($this->server)) {
2023-08-30 11:06:44 +02:00
$foundServer = $this->servers->where('id', $this->server)->first();
if ($foundServer) {
2023-09-05 12:14:31 +02:00
return $this->setServer($foundServer);
2023-08-30 11:06:44 +02:00
}
}
$this->current_step = 'servers';
}
2023-09-05 12:14:31 +02:00
public function setServer(Server $server)
{
$this->server_id = $server->id;
2023-08-30 11:26:46 +02:00
$this->standaloneDockers = $server->standaloneDockers;
$this->swarmDockers = $server->swarmDockers;
$this->current_step = 'destinations';
}
2023-09-05 12:14:31 +02:00
public function setDestination(string $destination_uuid)
{
$this->destination_uuid = $destination_uuid;
redirect()->route('project.resources.new', [
'project_uuid' => $this->parameters['project_uuid'],
'environment_name' => $this->parameters['environment_name'],
'type' => $this->type,
'destination' => $this->destination_uuid,
2023-09-21 17:48:31 +02:00
'server_id' => $this->server_id,
]);
}
2023-09-25 15:48:43 +02:00
public function loadServers()
{
2023-08-21 10:18:11 +02:00
$this->servers = Server::isUsable()->get();
}
}