Skip to content

Commit

Permalink
wip: boarding
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasbacsai committed Aug 22, 2023
1 parent 2414ddd commit b39ca51
Show file tree
Hide file tree
Showing 71 changed files with 694 additions and 137 deletions.
1 change: 1 addition & 0 deletions .ssh/known_hosts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
|1|TRkudHmvsBVekjyAfpo3EWrkQSs=|XdMLpIt2l32hhdSyWnDwMMlVBSI= ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIK/Ln1b72Lc5JtHRDiZd4lYyW7F5aVuJH42HdHXuYudT
3 changes: 0 additions & 3 deletions app/Actions/Server/UpdateCoolify.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ public function __invoke(bool $force)
$settings = InstanceSettings::get();
ray('Running InstanceAutoUpdateJob');
$localhost_name = 'localhost';
if (is_dev()) {
$localhost_name = 'testing-local-docker-container';
}
$this->server = Server::where('name', $localhost_name)->firstOrFail();
$this->latest_version = get_latest_version_of_coolify();
$this->current_version = config('version');
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/ApplicationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ApplicationController extends Controller

public function configuration()
{
$project = auth()->user()->currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
$project = currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
if (!$project) {
return redirect()->route('dashboard');
}
Expand All @@ -29,7 +29,7 @@ public function configuration()

public function deployments()
{
$project = auth()->user()->currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
$project = currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
if (!$project) {
return redirect()->route('dashboard');
}
Expand All @@ -49,7 +49,7 @@ public function deployment()
{
$deploymentUuid = request()->route('deployment_uuid');

$project = auth()->user()->currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
$project = currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
if (!$project) {
return redirect()->route('dashboard');
}
Expand Down
14 changes: 10 additions & 4 deletions app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,24 @@ public function dashboard()
$resources += $project->applications->count();
$resources += $project->postgresqls->count();
}

return view('dashboard', [
'servers' => $servers->count(),
'projects' => $projects->count(),
'resources' => $resources,
's3s' => $s3s,
]);
}
public function boarding() {
if (currentTeam()->boarding || is_dev()) {
return view('boarding');
} else {
return redirect()->route('dashboard');
}
}

public function settings()
{
if (is_instance_admin()) {
if (isInstanceAdmin()) {
$settings = InstanceSettings::get();
$database = StandalonePostgresql::whereName('coolify-db')->first();
if ($database) {
Expand All @@ -89,7 +95,7 @@ public function team()
{
$invitations = [];
if (auth()->user()->isAdminFromSession()) {
$invitations = TeamInvitation::whereTeamId(auth()->user()->currentTeam()->id)->get();
$invitations = TeamInvitation::whereTeamId(currentTeam()->id)->get();
}
return view('team.show', [
'invitations' => $invitations,
Expand All @@ -116,7 +122,7 @@ public function members()
{
$invitations = [];
if (auth()->user()->isAdminFromSession()) {
$invitations = TeamInvitation::whereTeamId(auth()->user()->currentTeam()->id)->get();
$invitations = TeamInvitation::whereTeamId(currentTeam()->id)->get();
}
return view('team.members', [
'invitations' => $invitations,
Expand Down
10 changes: 5 additions & 5 deletions app/Http/Controllers/DatabaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class DatabaseController extends Controller

public function configuration()
{
$project = auth()->user()->currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
$project = currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
if (!$project) {
return redirect()->route('dashboard');
}
Expand All @@ -29,7 +29,7 @@ public function configuration()
public function executions()
{
$backup_uuid = request()->route('backup_uuid');
$project = auth()->user()->currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
$project = currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
if (!$project) {
return redirect()->route('dashboard');
}
Expand All @@ -50,13 +50,13 @@ public function executions()
'database' => $database,
'backup' => $backup,
'executions' => $executions,
's3s' => auth()->user()->currentTeam()->s3s,
's3s' => currentTeam()->s3s,
]);
}

public function backups()
{
$project = auth()->user()->currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
$project = currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
if (!$project) {
return redirect()->route('dashboard');
}
Expand All @@ -70,7 +70,7 @@ public function backups()
}
return view('project.database.backups.all', [
'database' => $database,
's3s' => auth()->user()->currentTeam()->s3s,
's3s' => currentTeam()->s3s,
]);
}
}
4 changes: 2 additions & 2 deletions app/Http/Controllers/MagicController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function newProject()
{
$project = Project::firstOrCreate(
['name' => request()->query('name') ?? generate_random_name()],
['team_id' => auth()->user()->currentTeam()->id]
['team_id' => currentTeam()->id]
);
return response()->json([
'project_uuid' => $project->uuid
Expand All @@ -68,7 +68,7 @@ public function newTeam()
],
);
auth()->user()->teams()->attach($team, ['role' => 'admin']);
session(['currentTeam' => $team]);
refreshSession();
return redirect(request()->header('Referer'));
}
}
8 changes: 4 additions & 4 deletions app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function all()
public function edit()
{
$projectUuid = request()->route('project_uuid');
$teamId = auth()->user()->currentTeam()->id;
$teamId = currentTeam()->id;
$project = Project::where('team_id', $teamId)->where('uuid', $projectUuid)->first();
if (!$project) {
return redirect()->route('dashboard');
Expand All @@ -29,7 +29,7 @@ public function edit()
public function show()
{
$projectUuid = request()->route('project_uuid');
$teamId = auth()->user()->currentTeam()->id;
$teamId = currentTeam()->id;

$project = Project::where('team_id', $teamId)->where('uuid', $projectUuid)->first();
if (!$project) {
Expand All @@ -44,7 +44,7 @@ public function new()
$type = request()->query('type');
$destination_uuid = request()->query('destination');

$project = auth()->user()->currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
$project = currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
if (!$project) {
return redirect()->route('dashboard');
}
Expand All @@ -67,7 +67,7 @@ public function new()

public function resources()
{
$project = auth()->user()->currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
$project = currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
if (!$project) {
return redirect()->route('dashboard');
}
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/ServerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ class ServerController extends Controller

public function new_server()
{
if (!is_cloud() || is_instance_admin()) {
if (!is_cloud() || isInstanceAdmin()) {
return view('server.create', [
'limit_reached' => false,
'private_keys' => PrivateKey::ownedByCurrentTeam()->get(),
]);
}
$servers = auth()->user()->currentTeam()->servers->count();
$subscription = auth()->user()->currentTeam()?->subscription->type();
$servers = currentTeam()->servers->count();
$subscription = currentTeam()?->subscription->type();
$your_limit = config('constants.limits.server')[strtolower($subscription)];
$limit_reached = $servers >= $your_limit;

Expand Down
1 change: 1 addition & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Kernel extends HttpKernel
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\CheckForcePasswordReset::class,
\App\Http\Middleware\SubscriptionValid::class,
\App\Http\Middleware\IsBoardingFlow::class,

],

Expand Down
147 changes: 147 additions & 0 deletions app/Http/Livewire/Boarding.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

namespace App\Http\Livewire;

use App\Models\PrivateKey;
use App\Models\Project;
use App\Models\Server;
use Livewire\Component;

class Boarding extends Component
{

public string $currentState = 'create-private-key';
// public ?string $serverType = null;

public ?string $privateKeyType = null;
public ?string $privateKey = null;
public ?string $privateKeyName = null;
public ?string $privateKeyDescription = null;
public ?PrivateKey $createdPrivateKey = null;

public ?string $remoteServerName = null;
public ?string $remoteServerDescription = null;
public ?string $remoteServerHost = null;
public ?int $remoteServerPort = 22;
public ?string $remoteServerUser = 'root';
public ?Server $createdServer = null;

public function mount()
{
$this->privateKeyName = generate_random_name();
$this->remoteServerName = generate_random_name();
if (is_dev()) {
$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 restartBoarding()
{
if ($this->createdServer) {
$this->createdServer->delete();
}
if ($this->createdPrivateKey) {
$this->createdPrivateKey->delete();
}
return redirect()->route('boarding');
}
public function skipBoarding()
{
currentTeam()->update([
'show_boarding' => false
]);
refreshSession();
return redirect()->route('dashboard');
}
public function setServer(string $type)
{
if ($type === 'localhost') {
$this->currentState = 'create-project';
} elseif ($type === 'remote') {
$this->currentState = 'private-key';
}
}
public function setPrivateKey(string $type)
{
$this->privateKeyType = $type;
$this->currentState = 'create-private-key';
}
public function savePrivateKey()
{
$this->validate([
'privateKeyName' => 'required',
'privateKey' => 'required',
]);
$this->currentState = 'create-server';
}
public function saveServer()
{
$this->validate([
'remoteServerName' => 'required',
'remoteServerHost' => 'required',
'remoteServerPort' => 'required',
'remoteServerUser' => 'required',
]);
if ($this->privateKeyType === 'create') {
$this->createNewPrivateKey();
}
$this->privateKey = formatPrivateKey($this->privateKey);
$this->createdPrivateKey = PrivateKey::create([
'name' => $this->privateKeyName,
'description' => $this->privateKeyDescription,
'private_key' => $this->privateKey,
'team_id' => currentTeam()->id
]);
$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
]);
try {
['uptime' => $uptime, 'dockerVersion' => $dockerVersion] = validateServer($this->createdServer);
if (!$uptime) {
$this->createdServer->delete();
$this->createdPrivateKey->delete();
throw new \Exception('Server is not reachable.');
} else {
$this->createdServer->settings->update([
'is_reachable' => true,
]);
$this->emit('success', 'Server is reachable.');
}
if ($dockerVersion) {
$this->emit('error', 'Docker is not installed on the server.');
$this->currentState = 'install-docker';
return;
}
ray($uptime, $dockerVersion);
} catch (\Exception $e) {
return general_error_handler(customErrorMessage: "Server is not reachable. Reason: {$e->getMessage()}", that: $this);
}
}
private function createNewPrivateKey()
{
$this->privateKeyName = generate_random_name();
$this->privateKeyDescription = 'Created by Coolify';
$this->privateKey = generateSSHKey();
}
public function createNewProject()
{
Project::create([
'name' => generate_random_name(),
'team_id' => currentTeam()->id
]);
}
}
2 changes: 1 addition & 1 deletion app/Http/Livewire/Destination/New/StandaloneDocker.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function submit()
'name' => $this->name,
'network' => $this->network,
'server_id' => $this->server_id,
'team_id' => auth()->user()->currentTeam()->id
'team_id' => currentTeam()->id
]);
}
$this->createNetworkAndAttachToProxy();
Expand Down
3 changes: 1 addition & 2 deletions app/Http/Livewire/Notifications/DiscordSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ public function submit()

public function saveModel()
{
ray($this->model);
$this->model->save();
if (is_a($this->model, Team::class)) {
session(['currentTeam' => $this->model]);
refreshSession();
}
$this->emit('success', 'Settings saved.');
}
Expand Down
Loading

0 comments on commit b39ca51

Please sign in to comment.