lasthourcloud/app/Http/Livewire/Boarding/Index.php

285 lines
10 KiB
PHP
Raw Normal View History

2023-08-29 14:31:46 +00:00
<?php
namespace App\Http\Livewire\Boarding;
2023-08-29 18:34:01 +00:00
2023-08-29 14:31:46 +00:00
use App\Actions\Server\InstallDocker;
use App\Models\PrivateKey;
use App\Models\Project;
use App\Models\Server;
2023-09-08 16:33:26 +00:00
use App\Models\Team;
2023-08-30 09:06:44 +00:00
use Illuminate\Support\Collection;
2023-08-29 14:31:46 +00:00
use Livewire\Component;
class Index extends Component
2023-08-29 18:34:01 +00:00
{
public string $currentState = 'welcome';
2023-08-29 14:31:46 +00:00
public ?string $selectedServerType = null;
2023-08-29 18:34:01 +00:00
public ?Collection $privateKeys = null;
public ?int $selectedExistingPrivateKey = null;
2023-08-29 14:31:46 +00:00
public ?string $privateKeyType = null;
public ?string $privateKey = null;
public ?string $publicKey = null;
public ?string $privateKeyName = null;
public ?string $privateKeyDescription = null;
public ?PrivateKey $createdPrivateKey = null;
2023-08-30 09:06:44 +00:00
public ?Collection $servers = null;
public ?int $selectedExistingServer = null;
2023-08-29 14:31:46 +00:00
public ?string $remoteServerName = null;
public ?string $remoteServerDescription = null;
public ?string $remoteServerHost = null;
public ?int $remoteServerPort = 22;
public ?string $remoteServerUser = 'root';
public ?Server $createdServer = null;
2023-08-30 09:06:44 +00:00
public Collection|array $projects = [];
public ?int $selectedExistingProject = null;
2023-08-29 14:31:46 +00:00
public ?Project $createdProject = null;
2023-09-18 09:21:10 +00:00
public bool $dockerInstallationStarted = false;
2023-09-18 12:41:31 +00:00
public string $serverPublicKey;
public bool $serverReachable = true;
2023-08-29 14:31:46 +00:00
public function mount()
{
$this->privateKeyName = generate_random_name();
$this->remoteServerName = generate_random_name();
if (isDev()) {
$this->privateKey = '-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACBbhpqHhqv6aI67Mj9abM3DVbmcfYhZAhC7ca4d9UCevAAAAJi/QySHv0Mk
hwAAAAtzc2gtZWQyNTUxOQAAACBbhpqHhqv6aI67Mj9abM3DVbmcfYhZAhC7ca4d9UCevA
AAAECBQw4jg1WRT2IGHMncCiZhURCts2s24HoDS0thHnnRKVuGmoeGq/pojrsyP1pszcNV
uZx9iFkCELtxrh31QJ68AAAAEXNhaWxANzZmZjY2ZDJlMmRkAQIDBA==
-----END OPENSSH PRIVATE KEY-----';
$this->privateKeyDescription = 'Created by Coolify';
$this->remoteServerDescription = 'Created by Coolify';
$this->remoteServerHost = 'coolify-testing-host';
}
}
public function explanation()
{
2023-08-31 07:56:37 +00:00
if (isCloud()) {
2023-08-30 16:35:20 +00:00
return $this->setServerType('remote');
}
$this->currentState = 'select-server-type';
}
2023-09-14 08:39:05 +00:00
2023-08-29 14:31:46 +00:00
public function restartBoarding()
{
return redirect()->route('boarding');
}
public function skipBoarding()
{
2023-09-08 16:33:26 +00:00
Team::find(currentTeam()->id)->update([
2023-08-29 14:31:46 +00:00
'show_boarding' => false
]);
refreshSession();
return redirect()->route('dashboard');
}
2023-08-30 09:06:44 +00:00
public function setServerType(string $type)
2023-08-29 14:31:46 +00:00
{
$this->selectedServerType = $type;
if ($this->selectedServerType === 'localhost') {
2023-08-29 14:31:46 +00:00
$this->createdServer = Server::find(0);
if (!$this->createdServer) {
return $this->emit('error', 'Localhost server is not found. Something went wrong during installation. Please try to reinstall or contact support.');
}
2023-09-18 12:41:31 +00:00
$this->serverPublicKey = $this->createdServer->privateKey->publicKey();
2023-09-16 14:49:33 +00:00
return $this->validateServer('localhost');
} elseif ($this->selectedServerType === 'remote') {
2023-08-30 09:06:44 +00:00
$this->privateKeys = PrivateKey::ownedByCurrentTeam(['name'])->where('id', '!=', 0)->get();
if ($this->privateKeys->count() > 0) {
$this->selectedExistingPrivateKey = $this->privateKeys->first()->id;
}
$this->servers = Server::ownedByCurrentTeam(['name'])->where('id', '!=', 0)->get();
if ($this->servers->count() > 0) {
$this->selectedExistingServer = $this->servers->first()->id;
$this->currentState = 'select-existing-server';
return;
}
2023-08-29 14:31:46 +00:00
$this->currentState = 'private-key';
}
}
2023-08-30 09:06:44 +00:00
public function selectExistingServer()
{
$this->createdServer = Server::find($this->selectedExistingServer);
if (!$this->createdServer) {
$this->emit('error', 'Server is not found.');
$this->currentState = 'private-key';
return;
}
$this->selectedExistingPrivateKey = $this->createdServer->privateKey->id;
2023-09-18 12:41:31 +00:00
$this->serverPublicKey = $this->createdServer->privateKey->publicKey();
2023-08-30 09:06:44 +00:00
$this->validateServer();
}
public function getProxyType()
{
2023-08-30 09:06:44 +00:00
$proxyTypeSet = $this->createdServer->proxy->type;
if (!$proxyTypeSet) {
$this->currentState = 'select-proxy';
return;
}
$this->getProjects();
}
2023-08-29 18:34:01 +00:00
public function selectExistingPrivateKey()
{
2023-09-18 09:21:10 +00:00
$this->createdPrivateKey = PrivateKey::find($this->selectedExistingPrivateKey);
2023-09-28 11:46:53 +00:00
$this->privateKey = $this->createdPrivateKey->private_key;
2023-08-30 09:06:44 +00:00
$this->currentState = 'create-server';
}
public function createNewServer()
{
$this->selectedExistingServer = null;
$this->currentState = 'private-key';
2023-08-29 18:34:01 +00:00
}
2023-08-29 14:31:46 +00:00
public function setPrivateKey(string $type)
{
2023-08-30 09:06:44 +00:00
$this->selectedExistingPrivateKey = null;
2023-08-29 14:31:46 +00:00
$this->privateKeyType = $type;
if ($type === 'create') {
2023-08-29 14:31:46 +00:00
$this->createNewPrivateKey();
}
$this->currentState = 'create-private-key';
}
public function savePrivateKey()
{
$this->validate([
'privateKeyName' => 'required',
'privateKey' => 'required',
]);
2023-09-18 09:21:10 +00:00
$this->createdPrivateKey = PrivateKey::create([
'name' => $this->privateKeyName,
'description' => $this->privateKeyDescription,
'private_key' => $this->privateKey,
'team_id' => currentTeam()->id
]);
$this->createdPrivateKey->save();
2023-08-29 14:31:46 +00:00
$this->currentState = 'create-server';
}
public function saveServer()
{
$this->validate([
'remoteServerName' => 'required',
'remoteServerHost' => 'required',
'remoteServerPort' => 'required|integer',
2023-08-29 14:31:46 +00:00
'remoteServerUser' => 'required',
]);
$this->privateKey = formatPrivateKey($this->privateKey);
$foundServer = Server::whereIp($this->remoteServerHost)->first();
if ($foundServer) {
return $this->emit('error', 'IP address is already in use by another team.');
}
2023-09-18 09:21:10 +00:00
$this->createdServer = Server::create([
'name' => $this->remoteServerName,
'ip' => $this->remoteServerHost,
'port' => $this->remoteServerPort,
'user' => $this->remoteServerUser,
'description' => $this->remoteServerDescription,
'private_key_id' => $this->createdPrivateKey->id,
'team_id' => currentTeam()->id,
]);
$this->createdServer->save();
2023-08-30 09:06:44 +00:00
$this->validateServer();
}
public function validateServer()
{
2023-08-29 14:31:46 +00:00
try {
$customErrorMessage = "Server is not reachable:";
config()->set('coolify.mux_enabled', false);
2023-09-18 09:21:10 +00:00
instant_remote_process(['uptime'], $this->createdServer, true);
2023-09-18 09:21:10 +00:00
2023-09-18 12:41:31 +00:00
$this->createdServer->settings()->update([
2023-09-18 09:21:10 +00:00
'is_reachable' => true,
]);
} catch (\Throwable $e) {
2023-09-18 12:41:31 +00:00
$this->serverReachable = false;
return handleError(error: $e, customErrorMessage: $customErrorMessage, livewire: $this);
}
2023-09-18 09:21:10 +00:00
try {
$dockerVersion = instant_remote_process(["docker version|head -2|grep -i version| awk '{print $2}'"], $this->createdServer, true);
$dockerVersion = checkMinimumDockerEngineVersion($dockerVersion);
if (is_null($dockerVersion)) {
2023-09-18 09:21:10 +00:00
$this->currentState = 'install-docker';
throw new \Exception('Docker version is not supported or not installed.');
2023-09-16 14:49:33 +00:00
}
2023-09-18 12:41:31 +00:00
$this->createdServer->settings()->update([
'is_usable' => true,
]);
$this->getProxyType();
2023-09-11 15:36:30 +00:00
} catch (\Throwable $e) {
2023-10-20 16:15:25 +00:00
// $this->dockerInstallationStarted = false;
return handleError(error: $e, customErrorMessage: $customErrorMessage, livewire: $this);
2023-08-29 14:31:46 +00:00
}
}
public function installDocker()
{
2023-09-18 09:21:10 +00:00
$this->dockerInstallationStarted = true;
2023-10-09 09:00:18 +00:00
$activity = InstallDocker::run($this->createdServer);
2023-08-29 14:31:46 +00:00
$this->emit('newMonitorActivity', $activity->id);
2023-09-18 09:21:10 +00:00
}
public function dockerInstalledOrSkipped()
{
2023-09-18 12:41:31 +00:00
$this->validateServer();
2023-08-29 14:31:46 +00:00
}
public function selectProxy(string|null $proxyType = null)
{
if (!$proxyType) {
2023-08-30 09:06:44 +00:00
return $this->getProjects();
2023-08-29 14:31:46 +00:00
}
$this->createdServer->proxy->type = $proxyType;
$this->createdServer->proxy->status = 'exited';
$this->createdServer->save();
2023-08-30 09:06:44 +00:00
$this->getProjects();
}
public function getProjects()
{
2023-08-30 09:06:44 +00:00
$this->projects = Project::ownedByCurrentTeam(['name'])->get();
if ($this->projects->count() > 0) {
$this->selectedExistingProject = $this->projects->first()->id;
}
2023-08-29 14:31:46 +00:00
$this->currentState = 'create-project';
}
public function selectExistingProject()
{
2023-08-30 09:06:44 +00:00
$this->createdProject = Project::find($this->selectedExistingProject);
$this->currentState = 'create-resource';
}
2023-08-29 14:31:46 +00:00
public function createNewProject()
{
$this->createdProject = Project::create([
'name' => "My first project",
'team_id' => currentTeam()->id
]);
$this->currentState = 'create-resource';
}
public function showNewResource()
{
$this->skipBoarding();
return redirect()->route(
'project.resources.new',
[
'project_uuid' => $this->createdProject->uuid,
'environment_name' => 'production',
'server' => $this->createdServer->id,
2023-08-29 14:31:46 +00:00
]
);
}
private function createNewPrivateKey()
{
$this->privateKeyName = generate_random_name();
$this->privateKeyDescription = 'Created by Coolify';
2023-08-29 18:34:01 +00:00
['private' => $this->privateKey, 'public' => $this->publicKey] = generateSSHKey();
2023-08-29 14:31:46 +00:00
}
public function render()
{
return view('livewire.boarding.index')->layout('layouts.boarding');
}
}