2023-07-26 11:23:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Livewire\Project\New;
|
|
|
|
|
2023-10-11 08:19:03 +00:00
|
|
|
use App\Models\Project;
|
2023-07-26 11:23:47 +00:00
|
|
|
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;
|
2023-09-25 13:48:43 +00:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2023-07-26 11:23:47 +00:00
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
class Select extends Component
|
|
|
|
{
|
|
|
|
public $current_step = 'type';
|
2023-08-30 09:06:44 +00:00
|
|
|
public ?int $server = null;
|
2023-07-26 11:23:47 +00:00
|
|
|
public string $type;
|
|
|
|
public string $server_id;
|
|
|
|
public string $destination_uuid;
|
2023-09-23 09:47:24 +00:00
|
|
|
public Countable|array|Server $servers = [];
|
2023-08-30 09:26:46 +00:00
|
|
|
public Collection|array $standaloneDockers = [];
|
|
|
|
public Collection|array $swarmDockers = [];
|
2023-07-26 11:23:47 +00:00
|
|
|
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-09-25 13:48:43 +00:00
|
|
|
public bool $loadingServices = true;
|
2023-09-27 19:51:06 +00:00
|
|
|
public bool $loading = false;
|
2023-10-11 08:19:03 +00:00
|
|
|
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 = [
|
|
|
|
'server',
|
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
|
|
|
|
2023-07-26 11:23:47 +00:00
|
|
|
public function mount()
|
|
|
|
{
|
2023-08-09 13:57:53 +00:00
|
|
|
$this->parameters = get_route_parameters();
|
2023-09-05 10:14:31 +00:00
|
|
|
if (isDev()) {
|
|
|
|
$this->existingPostgresqlUrl = 'postgres://coolify:password@coolify-db:5432';
|
|
|
|
}
|
2023-10-11 08:19:03 +00:00
|
|
|
$projectUuid = data_get($this->parameters, 'project_uuid');
|
|
|
|
$this->environments = Project::whereUuid($projectUuid)->first()->environments;
|
|
|
|
$this->selectedEnvironment = data_get($this->parameters, 'environment_name');
|
2023-07-26 11:23:47 +00:00
|
|
|
}
|
2023-10-24 10:33:49 +00:00
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
return view('livewire.project.new.select');
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-10-11 08:19:03 +00:00
|
|
|
public function updatedSelectedEnvironment()
|
|
|
|
{
|
|
|
|
return redirect()->route('project.resources.new', [
|
|
|
|
'project_uuid' => $this->parameters['project_uuid'],
|
|
|
|
'environment_name' => $this->selectedEnvironment,
|
|
|
|
]);
|
|
|
|
}
|
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'");
|
|
|
|
// $this->emit('success', 'Successfully connected to the database.');
|
2023-09-11 15:36:30 +00:00
|
|
|
// } catch (\Throwable $e) {
|
2023-09-15 13:34:25 +00:00
|
|
|
// return handleError($e, $this);
|
2023-09-05 10:14:31 +00:00
|
|
|
// }
|
|
|
|
// }
|
2023-09-26 12:45:52 +00:00
|
|
|
|
2023-10-24 10:33:49 +00:00
|
|
|
public function loadServices(bool $force = false)
|
2023-09-25 13:48:43 +00:00
|
|
|
{
|
|
|
|
try {
|
2023-10-24 10:33:49 +00:00
|
|
|
if (count($this->allServices) > 0 && !$force) {
|
|
|
|
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 = getServiceTemplates();
|
|
|
|
$this->services = $this->allServices->filter(function ($service, $key) {
|
|
|
|
return str_contains(strtolower($key), strtolower($this->search));
|
|
|
|
});;
|
|
|
|
$this->emit('success', 'Successfully loaded services.');
|
2023-09-25 13:48:43 +00:00
|
|
|
}
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
return handleError($e, $this);
|
|
|
|
} finally {
|
|
|
|
$this->loadingServices = false;
|
|
|
|
}
|
|
|
|
}
|
2023-09-05 10:14:31 +00:00
|
|
|
public function setType(string $type)
|
2023-07-26 11:23:47 +00:00
|
|
|
{
|
|
|
|
$this->type = $type;
|
2023-09-27 19:51:06 +00:00
|
|
|
if ($this->loading) return;
|
|
|
|
$this->loading = true;
|
2023-09-05 10:14:31 +00:00
|
|
|
if ($type === "existing-postgresql") {
|
|
|
|
$this->current_step = $type;
|
|
|
|
return;
|
|
|
|
}
|
2023-08-23 08:14:39 +00:00
|
|
|
if (count($this->servers) === 1) {
|
|
|
|
$server = $this->servers->first();
|
2023-09-05 10:14:31 +00:00
|
|
|
$this->setServer($server);
|
2023-08-23 08:14:39 +00:00
|
|
|
if (count($server->destinations()) === 1) {
|
2023-09-05 10:14:31 +00:00
|
|
|
$this->setDestination($server->destinations()->first()->uuid);
|
2023-08-23 08:14:39 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-30 09:26:46 +00:00
|
|
|
if (!is_null($this->server)) {
|
2023-08-30 09:06:44 +00:00
|
|
|
$foundServer = $this->servers->where('id', $this->server)->first();
|
|
|
|
if ($foundServer) {
|
2023-09-05 10:14:31 +00:00
|
|
|
return $this->setServer($foundServer);
|
2023-08-30 09:06:44 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-26 11:23:47 +00:00
|
|
|
$this->current_step = 'servers';
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-09-05 10:14:31 +00:00
|
|
|
public function setServer(Server $server)
|
2023-07-26 11:23:47 +00:00
|
|
|
{
|
|
|
|
$this->server_id = $server->id;
|
2023-08-30 09:26:46 +00:00
|
|
|
$this->standaloneDockers = $server->standaloneDockers;
|
|
|
|
$this->swarmDockers = $server->swarmDockers;
|
2023-07-26 11:23:47 +00:00
|
|
|
$this->current_step = 'destinations';
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-09-05 10:14:31 +00:00
|
|
|
public function setDestination(string $destination_uuid)
|
2023-07-26 11:23:47 +00:00
|
|
|
{
|
|
|
|
$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 15:48:31 +00:00
|
|
|
'server_id' => $this->server_id,
|
2023-07-26 11:23:47 +00:00
|
|
|
]);
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-09-25 13:48:43 +00:00
|
|
|
public function loadServers()
|
2023-07-26 11:23:47 +00:00
|
|
|
{
|
2023-08-21 08:18:11 +00:00
|
|
|
$this->servers = Server::isUsable()->get();
|
2023-07-26 11:23:47 +00:00
|
|
|
}
|
|
|
|
}
|