Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

refactor: Simplify SSH key synchronization logic

Andras Bacsai6dd3adba4d6b
diff --git a/other/nightly/install.sh b/other/nightly/install.sh
index 61acbd333b..020e7d45ba 100755
--- a/other/nightly/install.sh
+++ b/other/nightly/install.sh
@@ -398,90 +398,10 @@ if [ ! -f ~/.ssh/authorized_keys ]; then
chmod 600 ~/.ssh/authorized_keys
fi
-checkSshKeyInAuthorizedKeys() {
- grep -qw "root@coolify" ~/.ssh/authorized_keys
- return $?
-}
-
-checkSshKeyInCoolifyData() {
- [ -s /data/coolify/ssh/keys/[email protected] ]
- return $?
-}
-
-generateAuthorizedKeys() {
- sed -i "/root@coolify/d" ~/.ssh/authorized_keys
- cat /data/coolify/ssh/keys/[email protected] >> ~/.ssh/authorized_keys
- rm -f /data/coolify/ssh/keys/[email protected]
-}
-generateSshKey() {
- echo " - Generating SSH key."
- ssh-keygen -t ed25519 -a 100 -f /data/coolify/ssh/keys/[email protected] -q -N "" -C root@coolify
- chown 9999 /data/coolify/ssh/keys/[email protected]
- generateAuthorizedKeys
-}
-
-syncSshKeys() {
- DB_RUNNING=$(docker inspect coolify-db --format '{{ .State.Status }}' 2>/dev/null)
- # Check if SSH key exists in Coolify data but not in authorized_keys
- if checkSshKeyInCoolifyData && ! checkSshKeyInAuthorizedKeys; then
- # Add the existing Coolify SSH key to authorized_keys
- cat /data/coolify/ssh/keys/[email protected] >> ~/.ssh/authorized_keys
- # Check if SSH key exists in authorized_keys but not in Coolify data
- elif checkSshKeyInAuthorizedKeys && ! checkSshKeyInCoolifyData; then
- # Ensure Coolify DB is running before proceeding
- if [ "$DB_RUNNING" = "running" ]; then
- # Retrieve DB user and SSH key from Coolify database
- DB_USER=$(docker inspect coolify-db --format '{{ .Config.Env }}' | grep -oP 'POSTGRES_USER=\K[^ ]+')
- DB_SSH_KEY=$(docker exec coolify-db psql -U $DB_USER -d coolify -t -c "SELECT \"private_key\" FROM \"private_keys\" WHERE id = 0 AND team_id = 0 LIMIT 1;" -A -t)
-
- if [ -z "$DB_SSH_KEY" ]; then
- # If no key found in DB, generate a new one
- echo " - SSH key not found in database. Generating new key."
- generateSshKey
- else
- # If key found in DB, save it and update authorized_keys
- echo " - SSH key found in database. Saving to file."
- echo "$DB_SSH_KEY" > /data/coolify/ssh/keys/[email protected]
- chmod 600 /data/coolify/ssh/keys/[email protected]
- chown 9999 /data/coolify/ssh/keys/[email protected]
-
- # Generate public key from private key and update authorized_keys
- ssh-keygen -y -f /data/coolify/ssh/keys/[email protected] -C root@coolify > /data/coolify/ssh/keys/[email protected]
- sed -i "/root@coolify/d" ~/.ssh/authorized_keys
- cat /data/coolify/ssh/keys/[email protected] >> ~/.ssh/authorized_keys
- rm -f /data/coolify/ssh/keys/[email protected]
- chmod 600 ~/.ssh/authorized_keys
- fi
- fi
- # If SSH key doesn't exist in either location
- elif ! checkSshKeyInAuthorizedKeys && ! checkSshKeyInCoolifyData; then
- # Ensure Coolify DB is running before proceeding
- if [ "$DB_RUNNING" = "running" ]; then
- # Retrieve DB user and SSH key from Coolify database
- DB_USER=$(docker inspect coolify-db --format '{{ .Config.Env }}' | grep -oP 'POSTGRES_USER=\K[^ ]+')
- DB_SSH_KEY=$(docker exec coolify-db psql -U $DB_USER -d coolify -t -c "SELECT \"private_key\" FROM \"private_keys\" WHERE id = 0 AND team_id = 0 LIMIT 1;" -A -t)
- if [ -z "$DB_SSH_KEY" ]; then
- # If no key found in DB, generate a new one
- echo " - SSH key not found in database. Generating new key."
- generateSshKey
- else
- # If key found in DB, save it and update authorized_keys
- echo " - SSH key found in database. Saving to file."
- echo "$DB_SSH_KEY" > /data/coolify/ssh/keys/[email protected]
- chmod 600 /data/coolify/ssh/keys/[email protected]
- ssh-keygen -y -f /data/coolify/ssh/keys/[email protected] -C root@coolify > /data/coolify/ssh/keys/[email protected]
- sed -i "/root@coolify/d" ~/.ssh/authorized_keys
- cat /data/coolify/ssh/keys/[email protected] >> ~/.ssh/authorized_keys
- fi
- else
- generateSshKey
- fi
- fi
-}
-
set +e
IS_COOLIFY_VOLUME_EXISTS=$(docker volume ls | grep coolify-db | wc -l)
set -e
+
if [ "$IS_COOLIFY_VOLUME_EXISTS" -eq 0 ]; then
echo " - Generating SSH key."
ssh-keygen -t ed25519 -a 100 -f /data/coolify/ssh/keys/[email protected] -q -N "" -C coolify