|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Livewire; |
| 4 | + |
| 5 | +use App\Models\PrivateKey; |
| 6 | +use App\Models\Project; |
| 7 | +use App\Models\Server; |
| 8 | +use Livewire\Component; |
| 9 | + |
| 10 | +class Boarding extends Component |
| 11 | +{ |
| 12 | + |
| 13 | + public string $currentState = 'create-private-key'; |
| 14 | + // public ?string $serverType = null; |
| 15 | + |
| 16 | + public ?string $privateKeyType = null; |
| 17 | + public ?string $privateKey = null; |
| 18 | + public ?string $privateKeyName = null; |
| 19 | + public ?string $privateKeyDescription = null; |
| 20 | + public ?PrivateKey $createdPrivateKey = null; |
| 21 | + |
| 22 | + public ?string $remoteServerName = null; |
| 23 | + public ?string $remoteServerDescription = null; |
| 24 | + public ?string $remoteServerHost = null; |
| 25 | + public ?int $remoteServerPort = 22; |
| 26 | + public ?string $remoteServerUser = 'root'; |
| 27 | + public ?Server $createdServer = null; |
| 28 | + |
| 29 | + public function mount() |
| 30 | + { |
| 31 | + $this->privateKeyName = generate_random_name(); |
| 32 | + $this->remoteServerName = generate_random_name(); |
| 33 | + if (is_dev()) { |
| 34 | + $this->privateKey = '-----BEGIN OPENSSH PRIVATE KEY----- |
| 35 | +b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW |
| 36 | +QyNTUxOQAAACBbhpqHhqv6aI67Mj9abM3DVbmcfYhZAhC7ca4d9UCevAAAAJi/QySHv0Mk |
| 37 | +hwAAAAtzc2gtZWQyNTUxOQAAACBbhpqHhqv6aI67Mj9abM3DVbmcfYhZAhC7ca4d9UCevA |
| 38 | +AAAECBQw4jg1WRT2IGHMncCiZhURCts2s24HoDS0thHnnRKVuGmoeGq/pojrsyP1pszcNV |
| 39 | +uZx9iFkCELtxrh31QJ68AAAAEXNhaWxANzZmZjY2ZDJlMmRkAQIDBA== |
| 40 | +-----END OPENSSH PRIVATE KEY-----'; |
| 41 | + $this->privateKeyDescription = 'Created by Coolify'; |
| 42 | + $this->remoteServerDescription = 'Created by Coolify'; |
| 43 | + $this->remoteServerHost = 'coolify-testing-host'; |
| 44 | + } |
| 45 | + } |
| 46 | + public function restartBoarding() |
| 47 | + { |
| 48 | + if ($this->createdServer) { |
| 49 | + $this->createdServer->delete(); |
| 50 | + } |
| 51 | + if ($this->createdPrivateKey) { |
| 52 | + $this->createdPrivateKey->delete(); |
| 53 | + } |
| 54 | + return redirect()->route('boarding'); |
| 55 | + } |
| 56 | + public function skipBoarding() |
| 57 | + { |
| 58 | + currentTeam()->update([ |
| 59 | + 'show_boarding' => false |
| 60 | + ]); |
| 61 | + refreshSession(); |
| 62 | + return redirect()->route('dashboard'); |
| 63 | + } |
| 64 | + public function setServer(string $type) |
| 65 | + { |
| 66 | + if ($type === 'localhost') { |
| 67 | + $this->currentState = 'create-project'; |
| 68 | + } elseif ($type === 'remote') { |
| 69 | + $this->currentState = 'private-key'; |
| 70 | + } |
| 71 | + } |
| 72 | + public function setPrivateKey(string $type) |
| 73 | + { |
| 74 | + $this->privateKeyType = $type; |
| 75 | + $this->currentState = 'create-private-key'; |
| 76 | + } |
| 77 | + public function savePrivateKey() |
| 78 | + { |
| 79 | + $this->validate([ |
| 80 | + 'privateKeyName' => 'required', |
| 81 | + 'privateKey' => 'required', |
| 82 | + ]); |
| 83 | + $this->currentState = 'create-server'; |
| 84 | + } |
| 85 | + public function saveServer() |
| 86 | + { |
| 87 | + $this->validate([ |
| 88 | + 'remoteServerName' => 'required', |
| 89 | + 'remoteServerHost' => 'required', |
| 90 | + 'remoteServerPort' => 'required', |
| 91 | + 'remoteServerUser' => 'required', |
| 92 | + ]); |
| 93 | + if ($this->privateKeyType === 'create') { |
| 94 | + $this->createNewPrivateKey(); |
| 95 | + } |
| 96 | + $this->privateKey = formatPrivateKey($this->privateKey); |
| 97 | + $this->createdPrivateKey = PrivateKey::create([ |
| 98 | + 'name' => $this->privateKeyName, |
| 99 | + 'description' => $this->privateKeyDescription, |
| 100 | + 'private_key' => $this->privateKey, |
| 101 | + 'team_id' => currentTeam()->id |
| 102 | + ]); |
| 103 | + $this->createdServer = Server::create([ |
| 104 | + 'name' => $this->remoteServerName, |
| 105 | + 'ip' => $this->remoteServerHost, |
| 106 | + 'port' => $this->remoteServerPort, |
| 107 | + 'user' => $this->remoteServerUser, |
| 108 | + 'description' => $this->remoteServerDescription, |
| 109 | + 'private_key_id' => $this->createdPrivateKey->id, |
| 110 | + 'team_id' => currentTeam()->id |
| 111 | + ]); |
| 112 | + try { |
| 113 | + ['uptime' => $uptime, 'dockerVersion' => $dockerVersion] = validateServer($this->createdServer); |
| 114 | + if (!$uptime) { |
| 115 | + $this->createdServer->delete(); |
| 116 | + $this->createdPrivateKey->delete(); |
| 117 | + throw new \Exception('Server is not reachable.'); |
| 118 | + } else { |
| 119 | + $this->createdServer->settings->update([ |
| 120 | + 'is_reachable' => true, |
| 121 | + ]); |
| 122 | + $this->emit('success', 'Server is reachable.'); |
| 123 | + } |
| 124 | + if ($dockerVersion) { |
| 125 | + $this->emit('error', 'Docker is not installed on the server.'); |
| 126 | + $this->currentState = 'install-docker'; |
| 127 | + return; |
| 128 | + } |
| 129 | + ray($uptime, $dockerVersion); |
| 130 | + } catch (\Exception $e) { |
| 131 | + return general_error_handler(customErrorMessage: "Server is not reachable. Reason: {$e->getMessage()}", that: $this); |
| 132 | + } |
| 133 | + } |
| 134 | + private function createNewPrivateKey() |
| 135 | + { |
| 136 | + $this->privateKeyName = generate_random_name(); |
| 137 | + $this->privateKeyDescription = 'Created by Coolify'; |
| 138 | + $this->privateKey = generateSSHKey(); |
| 139 | + } |
| 140 | + public function createNewProject() |
| 141 | + { |
| 142 | + Project::create([ |
| 143 | + 'name' => generate_random_name(), |
| 144 | + 'team_id' => currentTeam()->id |
| 145 | + ]); |
| 146 | + } |
| 147 | +} |
0 commit comments