Add root team + localhost (coolify host) in prod seeder
This commit is contained in:
		@@ -6,6 +6,7 @@ use App\Models\Team;
 | 
			
		||||
use App\Models\User;
 | 
			
		||||
use Illuminate\Support\Facades\DB;
 | 
			
		||||
use Illuminate\Support\Facades\Hash;
 | 
			
		||||
use Illuminate\Support\Facades\Log;
 | 
			
		||||
use Illuminate\Support\Facades\Validator;
 | 
			
		||||
use Illuminate\Validation\Rule;
 | 
			
		||||
use Laravel\Fortify\Contracts\CreatesNewUsers;
 | 
			
		||||
@@ -33,23 +34,38 @@ class CreateNewUser implements CreatesNewUsers
 | 
			
		||||
            'password' => $this->passwordRules(),
 | 
			
		||||
        ])->validate();
 | 
			
		||||
 | 
			
		||||
        if (User::count() == 0) {
 | 
			
		||||
            // If this is the first user, make them the root user
 | 
			
		||||
            // Team is already created in the database/seeders/ProductionSeeder.php
 | 
			
		||||
            $team = Team::find(0);
 | 
			
		||||
            $user = User::create([
 | 
			
		||||
                'id' => 0,
 | 
			
		||||
                'name' => $input['name'],
 | 
			
		||||
                'email' => $input['email'],
 | 
			
		||||
                'password' => Hash::make($input['password']),
 | 
			
		||||
                'is_root_user' => true,
 | 
			
		||||
            ]);
 | 
			
		||||
        } else {
 | 
			
		||||
            $team = Team::create([
 | 
			
		||||
                'name' => explode(' ', $input['name'], 2)[0] . "'s Team",
 | 
			
		||||
                'personal_team' => true,
 | 
			
		||||
            ]);
 | 
			
		||||
 | 
			
		||||
            $user = User::create([
 | 
			
		||||
                'name' => $input['name'],
 | 
			
		||||
                'email' => $input['email'],
 | 
			
		||||
                'password' => Hash::make($input['password']),
 | 
			
		||||
            'is_root_user' => User::count() == 0 ? true : false,
 | 
			
		||||
                'is_root_user' => false,
 | 
			
		||||
            ]);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Add user to team
 | 
			
		||||
        DB::table('team_user')->insert([
 | 
			
		||||
            'user_id' => $user->id,
 | 
			
		||||
            'team_id' => $team->id,
 | 
			
		||||
            'role' => 'admin',
 | 
			
		||||
        ]);
 | 
			
		||||
 | 
			
		||||
        // Set session variable
 | 
			
		||||
        session(['currentTeam' => $user->currentTeam = $team]);
 | 
			
		||||
        return $user;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -7,6 +7,11 @@ class HomeController extends Controller
 | 
			
		||||
    public function show()
 | 
			
		||||
    {
 | 
			
		||||
        $projects = session('currentTeam')->load(['projects'])->projects;
 | 
			
		||||
        return view('home', ['projects' => $projects]);
 | 
			
		||||
        $servers = session('currentTeam')->load(['servers'])->servers;
 | 
			
		||||
 | 
			
		||||
        return view('home', [
 | 
			
		||||
            'servers' => $servers,
 | 
			
		||||
            'projects' => $projects
 | 
			
		||||
        ]);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -8,6 +8,7 @@ class Team extends BaseModel
 | 
			
		||||
        'personal_team' => 'boolean',
 | 
			
		||||
    ];
 | 
			
		||||
    protected $fillable = [
 | 
			
		||||
        'id',
 | 
			
		||||
        'name',
 | 
			
		||||
        'personal_team'
 | 
			
		||||
    ];
 | 
			
		||||
 
 | 
			
		||||
@@ -19,6 +19,7 @@ class User extends Authenticatable
 | 
			
		||||
     * @var array<int, string>
 | 
			
		||||
     */
 | 
			
		||||
    protected $fillable = [
 | 
			
		||||
        'id',
 | 
			
		||||
        'name',
 | 
			
		||||
        'email',
 | 
			
		||||
        'password',
 | 
			
		||||
 
 | 
			
		||||
@@ -15,7 +15,7 @@ class GithubAppSeeder extends Seeder
 | 
			
		||||
     */
 | 
			
		||||
    public function run(): void
 | 
			
		||||
    {
 | 
			
		||||
        $root_team = Team::find(1);
 | 
			
		||||
        $root_team = Team::find(0);
 | 
			
		||||
        $private_key_1 = PrivateKey::find(1);
 | 
			
		||||
        $private_key_2 = PrivateKey::find(2);
 | 
			
		||||
        GithubApp::create([
 | 
			
		||||
 
 | 
			
		||||
@@ -15,7 +15,7 @@ class GitlabAppSeeder extends Seeder
 | 
			
		||||
     */
 | 
			
		||||
    public function run(): void
 | 
			
		||||
    {
 | 
			
		||||
        $root_team = Team::find(1);
 | 
			
		||||
        $root_team = Team::find(0);
 | 
			
		||||
        $private_key_3 = PrivateKey::find(3);
 | 
			
		||||
        GitlabApp::create([
 | 
			
		||||
            'id' => 1,
 | 
			
		||||
 
 | 
			
		||||
@@ -4,6 +4,7 @@ namespace Database\Seeders;
 | 
			
		||||
 | 
			
		||||
use App\Models\PrivateKey;
 | 
			
		||||
use App\Models\Project;
 | 
			
		||||
use App\Models\Server;
 | 
			
		||||
use App\Models\Team;
 | 
			
		||||
use Illuminate\Database\Seeder;
 | 
			
		||||
use Illuminate\Support\Facades\Storage;
 | 
			
		||||
@@ -12,6 +13,7 @@ class ProductionSeeder extends Seeder
 | 
			
		||||
{
 | 
			
		||||
    public function run(): void
 | 
			
		||||
    {
 | 
			
		||||
        // Save SSH Keys for the Coolify Host
 | 
			
		||||
        $coolify_key_name = "id.root@host.docker.internal";
 | 
			
		||||
        $coolify_key = Storage::disk('local')->get("ssh-keys/{$coolify_key_name}");
 | 
			
		||||
        $coolify_key_in_database = PrivateKey::where('name', 'Coolify Host');
 | 
			
		||||
@@ -21,10 +23,32 @@ class ProductionSeeder extends Seeder
 | 
			
		||||
        }
 | 
			
		||||
        if ($coolify_key && !$coolify_key_in_database->exists()) {
 | 
			
		||||
            PrivateKey::create([
 | 
			
		||||
                'name' => 'Coolify Host',
 | 
			
		||||
                'description' => 'The private key for the Coolify host machine.',
 | 
			
		||||
                'id' => 0,
 | 
			
		||||
                'name' => 'localhost\'s key',
 | 
			
		||||
                'description' => 'The private key for the Coolify host machine (localhost).',
 | 
			
		||||
                'private_key' => $coolify_key,
 | 
			
		||||
            ]);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Add first Team if it doesn't exist
 | 
			
		||||
        if (Team::find(0) == null) {
 | 
			
		||||
            Team::create([
 | 
			
		||||
                'id' => 0,
 | 
			
		||||
                'name' => "Root's Team",
 | 
			
		||||
                'personal_team' => true,
 | 
			
		||||
            ]);
 | 
			
		||||
        }
 | 
			
		||||
        // Add Coolify host (localhost) as Server if it doesn't exist
 | 
			
		||||
        if (Server::find(0) == null) {
 | 
			
		||||
            Server::create([
 | 
			
		||||
                'id' => 0,
 | 
			
		||||
                'name' => "localhost",
 | 
			
		||||
                'description' => "This is the local machine",
 | 
			
		||||
                'user' => 'root',
 | 
			
		||||
                'ip' => "host.docker.internal",
 | 
			
		||||
                'team_id' => 0,
 | 
			
		||||
                'private_key_id' => 0,
 | 
			
		||||
            ]);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -10,7 +10,7 @@ class ProjectSeeder extends Seeder
 | 
			
		||||
{
 | 
			
		||||
    public function run(): void
 | 
			
		||||
    {
 | 
			
		||||
        $root_team = Team::find(1);
 | 
			
		||||
        $root_team = Team::find(0);
 | 
			
		||||
        Project::create([
 | 
			
		||||
            'id' => 1,
 | 
			
		||||
            'name' => "My first project",
 | 
			
		||||
 
 | 
			
		||||
@@ -14,10 +14,10 @@ class ServerSeeder extends Seeder
 | 
			
		||||
     */
 | 
			
		||||
    public function run(): void
 | 
			
		||||
    {
 | 
			
		||||
        $root_team = Team::find(1);
 | 
			
		||||
        $root_team = Team::find(0);
 | 
			
		||||
        $private_key_1 = PrivateKey::find(1);
 | 
			
		||||
        Server::create([
 | 
			
		||||
            'id' => 1,
 | 
			
		||||
            'id' => 0,
 | 
			
		||||
            'name' => "localhost",
 | 
			
		||||
            'description' => "This is the local machine",
 | 
			
		||||
            'user' => 'root',
 | 
			
		||||
@@ -26,7 +26,7 @@ class ServerSeeder extends Seeder
 | 
			
		||||
            'private_key_id' => $private_key_1->id,
 | 
			
		||||
        ]);
 | 
			
		||||
        Server::create([
 | 
			
		||||
            'id' => 2,
 | 
			
		||||
            'id' => 1,
 | 
			
		||||
            'name' => "testing-local-docker-container",
 | 
			
		||||
            'description' => "This is a test docker container",
 | 
			
		||||
            'ip' => "coolify-testing-host",
 | 
			
		||||
@@ -34,7 +34,7 @@ class ServerSeeder extends Seeder
 | 
			
		||||
            'private_key_id' => $private_key_1->id,
 | 
			
		||||
        ]);
 | 
			
		||||
        Server::create([
 | 
			
		||||
            'id' => 3,
 | 
			
		||||
            'id' => 2,
 | 
			
		||||
            'name' => "testing-local-docker-container-2",
 | 
			
		||||
            'description' => "This is a test docker container",
 | 
			
		||||
            'ip' => "coolify-testing-host-2",
 | 
			
		||||
 
 | 
			
		||||
@@ -15,11 +15,11 @@ class StandaloneDockerSeeder extends Seeder
 | 
			
		||||
     */
 | 
			
		||||
    public function run(): void
 | 
			
		||||
    {
 | 
			
		||||
        $server_1 = Server::find(1);
 | 
			
		||||
        $server_0 = Server::find(0);
 | 
			
		||||
        StandaloneDocker::create([
 | 
			
		||||
            'id' => 1,
 | 
			
		||||
            'network' => 'coolify',
 | 
			
		||||
            'server_id' => $server_1->id,
 | 
			
		||||
            'server_id' => $server_0->id,
 | 
			
		||||
        ]);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -16,10 +16,10 @@ class SwarmDockerSeeder extends Seeder
 | 
			
		||||
     */
 | 
			
		||||
    public function run(): void
 | 
			
		||||
    {
 | 
			
		||||
        $server_2 = Server::find(2);
 | 
			
		||||
        $server_1 = Server::find(1);
 | 
			
		||||
        SwarmDocker::create([
 | 
			
		||||
            'id' => 1,
 | 
			
		||||
            'server_id' => $server_2->id,
 | 
			
		||||
            'server_id' => $server_1->id,
 | 
			
		||||
        ]);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -15,6 +15,7 @@ class TeamSeeder extends Seeder
 | 
			
		||||
        $normal_user = User::find(2);
 | 
			
		||||
 | 
			
		||||
        $root_user_personal_team = Team::create([
 | 
			
		||||
            'id' => 0,
 | 
			
		||||
            'name' => "Root Team",
 | 
			
		||||
            'personal_team' => true,
 | 
			
		||||
        ]);
 | 
			
		||||
 
 | 
			
		||||
@@ -1,8 +1,8 @@
 | 
			
		||||
<x-layout>
 | 
			
		||||
    <form action="/register" method="POST">
 | 
			
		||||
        @csrf
 | 
			
		||||
        <input type="text" name="name" placeholder="name" @env('local') value="Andras Bacsai" @endenv />
 | 
			
		||||
        <input type="text" name="email" placeholder="email" @env('local') value="andras@bacsai.com" @endenv />
 | 
			
		||||
        <input type="text" name="name" placeholder="name" @env('local') value="Root" @endenv />
 | 
			
		||||
        <input type="text" name="email" placeholder="email" @env('local') value="test@example.com" @endenv />
 | 
			
		||||
        <input type="password" name="password" placeholder="Password" @env('local') value="password" @endenv />
 | 
			
		||||
        <input type="password" name="password_confirmation" placeholder="Password"
 | 
			
		||||
            @env('local') value="password" @endenv />
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,10 @@
 | 
			
		||||
<x-layout>
 | 
			
		||||
    <h1>Servers</h1>
 | 
			
		||||
    @forelse ($servers as $server)
 | 
			
		||||
        <a href="{{ route('project.environments', [$server->uuid]) }}">{{ data_get($server, 'name') }}</a>
 | 
			
		||||
    @empty
 | 
			
		||||
        <p>No projects found.</p>
 | 
			
		||||
    @endforelse
 | 
			
		||||
    <h1>Projects</h1>
 | 
			
		||||
    @forelse ($projects as $project)
 | 
			
		||||
        <a href="{{ route('project.environments', [$project->uuid]) }}">{{ data_get($project, 'name') }}</a>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user