From be8573c8285ccb6be3fd97681767cfca69f3fb48 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 20 Sep 2024 11:04:26 +0200 Subject: [PATCH] chore: Update SSH key generation in install.sh script --- ...ulate_ssh_keys_and_clear_mux_directory.php | 24 ++--- database/seeders/ProductionSeeder.php | 97 ++++++++++--------- other/nightly/install.sh | 15 ++- 3 files changed, 77 insertions(+), 59 deletions(-) diff --git a/database/migrations/2024_09_16_170001_populate_ssh_keys_and_clear_mux_directory.php b/database/migrations/2024_09_16_170001_populate_ssh_keys_and_clear_mux_directory.php index 6b5d09fd80..aece86c885 100644 --- a/database/migrations/2024_09_16_170001_populate_ssh_keys_and_clear_mux_directory.php +++ b/database/migrations/2024_09_16_170001_populate_ssh_keys_and_clear_mux_directory.php @@ -8,18 +8,18 @@ class PopulateSshKeysAndClearMuxDirectory extends Migration { public function up() { - Storage::disk('ssh-keys')->deleteDirectory(''); - Storage::disk('ssh-keys')->makeDirectory(''); + // Storage::disk('ssh-keys')->deleteDirectory(''); + // Storage::disk('ssh-keys')->makeDirectory(''); - Storage::disk('ssh-mux')->deleteDirectory(''); - Storage::disk('ssh-mux')->makeDirectory(''); - PrivateKey::chunk(100, function ($keys) { - foreach ($keys as $key) { - $key->storeInFileSystem(); - if ($key->id === 0) { - Storage::disk('ssh-keys')->put('id.root@host.docker.internal', $key->private_key); - } - } - }); + // Storage::disk('ssh-mux')->deleteDirectory(''); + // Storage::disk('ssh-mux')->makeDirectory(''); + // PrivateKey::chunk(100, function ($keys) { + // foreach ($keys as $key) { + // $key->storeInFileSystem(); + // if ($key->id === 0) { + // Storage::disk('ssh-keys')->put('id.root@host.docker.internal', $key->private_key); + // } + // } + // }); } } diff --git a/database/seeders/ProductionSeeder.php b/database/seeders/ProductionSeeder.php index 8b1733c0df..ce56169b09 100644 --- a/database/seeders/ProductionSeeder.php +++ b/database/seeders/ProductionSeeder.php @@ -64,27 +64,67 @@ public function run(): void 'team_id' => 0, ]); } + // Add Coolify host (localhost) as Server if it doesn't exist + if (Server::find(0) == null) { + $server_details = [ + 'id' => 0, + 'name' => 'localhost', + 'description' => "This is the server where Coolify is running on. Don't delete this!", + 'user' => 'root', + 'ip' => 'host.docker.internal', + 'team_id' => 0, + 'private_key_id' => 0, + ]; + $server_details['proxy'] = ServerMetadata::from([ + 'type' => ProxyTypes::TRAEFIK->value, + 'status' => ProxyStatus::EXITED->value, + ]); + $server = Server::create($server_details); + $server->settings->is_reachable = true; + $server->settings->is_usable = true; + $server->settings->save(); + } else { + $server = Server::find(0); + $server->settings->is_reachable = true; + $server->settings->is_usable = true; + $server->settings->save(); + } + if (StandaloneDocker::find(0) == null) { + StandaloneDocker::create([ + 'id' => 0, + 'name' => 'localhost-coolify', + 'network' => 'coolify', + 'server_id' => 0, + ]); + } if (! isCloud() && config('coolify.is_windows_docker_desktop') == false) { echo "Checking localhost key.\n"; + $coolify_key_name = '@host.docker.internal'; + $ssh_keys_directory = Storage::disk('ssh-keys')->files(); + $coolify_key = collect($ssh_keys_directory)->firstWhere(fn ($item) => str($item)->contains($coolify_key_name)); $found = PrivateKey::find(0); if ($found) { echo 'Private Key found in database.'; + if ($coolify_key) { + echo "SSH key found for the Coolify host machine (localhost).\n"; + Storage::disk('ssh-keys')->delete($coolify_key); + } } else { - $coolify_key_name = 'id.root@host.docker.internal'; - $coolify_key = Storage::disk('ssh-keys')->get("{$coolify_key_name}"); - if ($coolify_key) { - PrivateKey::create( - [ - 'id' => 0, - 'team_id' => 0, - 'name' => 'localhost\'s key', - 'description' => 'The private key for the Coolify host machine (localhost).', - 'private_key' => $coolify_key, - ] - ); + $coolify_key = Storage::disk('ssh-keys')->get($coolify_key); + $user = str($coolify_key)->before('@')->after('id.'); + PrivateKey::create([ + 'id' => 0, + 'team_id' => 0, + 'name' => 'localhost\'s key', + 'description' => 'The private key for the Coolify host machine (localhost).', + 'private_key' => $coolify_key, + ]); + $server->update(['user' => $user]); + echo "SSH key found for the Coolify host machine (localhost).\n"; + Storage::disk('ssh-keys')->delete($coolify_key); } else { PrivateKey::create( [ @@ -101,39 +141,6 @@ public function run(): void } } - // Add Coolify host (localhost) as Server if it doesn't exist - if (Server::find(0) == null) { - $server_details = [ - 'id' => 0, - 'name' => 'localhost', - 'description' => "This is the server where Coolify is running on. Don't delete this!", - 'user' => 'root', - 'ip' => 'host.docker.internal', - 'team_id' => 0, - 'private_key_id' => 0, - ]; - $server_details['proxy'] = ServerMetadata::from([ - 'type' => ProxyTypes::TRAEFIK->value, - 'status' => ProxyStatus::EXITED->value, - ]); - $server = Server::create($server_details); - $server->settings->is_reachable = true; - $server->settings->is_usable = true; - $server->settings->save(); - } else { - $server = Server::find(0); - $server->settings->is_reachable = true; - $server->settings->is_usable = true; - $server->settings->save(); - } - if (StandaloneDocker::find(0) == null) { - StandaloneDocker::create([ - 'id' => 0, - 'name' => 'localhost-coolify', - 'network' => 'coolify', - 'server_id' => 0, - ]); - } } if (config('coolify.is_windows_docker_desktop')) { PrivateKey::updateOrCreate( diff --git a/other/nightly/install.sh b/other/nightly/install.sh index 09613e12b4..9c446e2cdb 100755 --- a/other/nightly/install.sh +++ b/other/nightly/install.sh @@ -10,6 +10,8 @@ DATE=$(date +"%Y%m%d-%H%M%S") VERSION="1.5" DOCKER_VERSION="26.0" +# TODO: Ask for a user +CURRENT_USER=$USER mkdir -p /data/coolify/{source,ssh,applications,databases,backups,services,proxy,webhooks-during-maintenance,metrics,logs} mkdir -p /data/coolify/ssh/{keys,mux} @@ -23,7 +25,7 @@ INSTALLATION_LOG_WITH_DATE="/data/coolify/source/installation-${DATE}.log" exec > >(tee -a $INSTALLATION_LOG_WITH_DATE) 2>&1 getAJoke() { - JOKES=$(curl -s --max-time 2 https://v2.jokeapi.dev/joke/Programming?format=txt&type=single&amount=1 || true) + JOKES=$(curl -s --max-time 2 "https://v2.jokeapi.dev/joke/Programming?blacklistFlags=nsfw,religious,political,racist,sexist,explicit&format=txt&type=single" || true) if [ "$JOKES" != "" ]; then echo -e " - Until then, here's a joke for you:\n" echo -e "$JOKES\n" @@ -477,7 +479,16 @@ syncSshKeys() { fi } -syncSshKeys || true +IS_COOLIFY_VOLUME_EXISTS=$(docker volume inspect coolify-db 2>/dev/null) + +if [ -z "$IS_COOLIFY_VOLUME_EXISTS" ]; then + echo " - Generating SSH key." + ssh-keygen -t ed25519 -a 100 -f /data/coolify/ssh/keys/id.$CURRENT_USER@host.docker.internal -q -N "" -C coolify + chown 9999 /data/coolify/ssh/keys/id.$CURRENT_USER@host.docker.internal + sed -i "/coolify/d" ~/.ssh/authorized_keys + cat /data/coolify/ssh/keys/id.$CURRENT_USER@host.docker.internal.pub >> ~/.ssh/authorized_keys + rm -f /data/coolify/ssh/keys/id.$CURRENT_USER@host.docker.internal.pub +fi chown -R 9999:root /data/coolify chmod -R 700 /data/coolify