Skip to content

Commit 6dd3adb

Browse files
committed
refactor: Simplify SSH key synchronization logic
1 parent 08d58eb commit 6dd3adb

File tree

1 file changed

+1
-81
lines changed

1 file changed

+1
-81
lines changed

other/nightly/install.sh

Lines changed: 1 addition & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -398,90 +398,10 @@ if [ ! -f ~/.ssh/authorized_keys ]; then
398398
chmod 600 ~/.ssh/authorized_keys
399399
fi
400400

401-
checkSshKeyInAuthorizedKeys() {
402-
grep -qw "root@coolify" ~/.ssh/authorized_keys
403-
return $?
404-
}
405-
406-
checkSshKeyInCoolifyData() {
407-
[ -s /data/coolify/ssh/keys/[email protected] ]
408-
return $?
409-
}
410-
411-
generateAuthorizedKeys() {
412-
sed -i "/root@coolify/d" ~/.ssh/authorized_keys
413-
cat /data/coolify/ssh/keys/[email protected] >> ~/.ssh/authorized_keys
414-
rm -f /data/coolify/ssh/keys/[email protected]
415-
}
416-
generateSshKey() {
417-
echo " - Generating SSH key."
418-
ssh-keygen -t ed25519 -a 100 -f /data/coolify/ssh/keys/[email protected] -q -N "" -C root@coolify
419-
chown 9999 /data/coolify/ssh/keys/[email protected]
420-
generateAuthorizedKeys
421-
}
422-
423-
syncSshKeys() {
424-
DB_RUNNING=$(docker inspect coolify-db --format '{{ .State.Status }}' 2>/dev/null)
425-
# Check if SSH key exists in Coolify data but not in authorized_keys
426-
if checkSshKeyInCoolifyData && ! checkSshKeyInAuthorizedKeys; then
427-
# Add the existing Coolify SSH key to authorized_keys
428-
cat /data/coolify/ssh/keys/[email protected] >> ~/.ssh/authorized_keys
429-
# Check if SSH key exists in authorized_keys but not in Coolify data
430-
elif checkSshKeyInAuthorizedKeys && ! checkSshKeyInCoolifyData; then
431-
# Ensure Coolify DB is running before proceeding
432-
if [ "$DB_RUNNING" = "running" ]; then
433-
# Retrieve DB user and SSH key from Coolify database
434-
DB_USER=$(docker inspect coolify-db --format '{{ .Config.Env }}' | grep -oP 'POSTGRES_USER=\K[^ ]+')
435-
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)
436-
437-
if [ -z "$DB_SSH_KEY" ]; then
438-
# If no key found in DB, generate a new one
439-
echo " - SSH key not found in database. Generating new key."
440-
generateSshKey
441-
else
442-
# If key found in DB, save it and update authorized_keys
443-
echo " - SSH key found in database. Saving to file."
444-
echo "$DB_SSH_KEY" > /data/coolify/ssh/keys/[email protected]
445-
chmod 600 /data/coolify/ssh/keys/[email protected]
446-
chown 9999 /data/coolify/ssh/keys/[email protected]
447-
448-
# Generate public key from private key and update authorized_keys
449-
ssh-keygen -y -f /data/coolify/ssh/keys/[email protected] -C root@coolify > /data/coolify/ssh/keys/[email protected]
450-
sed -i "/root@coolify/d" ~/.ssh/authorized_keys
451-
cat /data/coolify/ssh/keys/[email protected] >> ~/.ssh/authorized_keys
452-
rm -f /data/coolify/ssh/keys/[email protected]
453-
chmod 600 ~/.ssh/authorized_keys
454-
fi
455-
fi
456-
# If SSH key doesn't exist in either location
457-
elif ! checkSshKeyInAuthorizedKeys && ! checkSshKeyInCoolifyData; then
458-
# Ensure Coolify DB is running before proceeding
459-
if [ "$DB_RUNNING" = "running" ]; then
460-
# Retrieve DB user and SSH key from Coolify database
461-
DB_USER=$(docker inspect coolify-db --format '{{ .Config.Env }}' | grep -oP 'POSTGRES_USER=\K[^ ]+')
462-
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)
463-
if [ -z "$DB_SSH_KEY" ]; then
464-
# If no key found in DB, generate a new one
465-
echo " - SSH key not found in database. Generating new key."
466-
generateSshKey
467-
else
468-
# If key found in DB, save it and update authorized_keys
469-
echo " - SSH key found in database. Saving to file."
470-
echo "$DB_SSH_KEY" > /data/coolify/ssh/keys/[email protected]
471-
chmod 600 /data/coolify/ssh/keys/[email protected]
472-
ssh-keygen -y -f /data/coolify/ssh/keys/[email protected] -C root@coolify > /data/coolify/ssh/keys/[email protected]
473-
sed -i "/root@coolify/d" ~/.ssh/authorized_keys
474-
cat /data/coolify/ssh/keys/[email protected] >> ~/.ssh/authorized_keys
475-
fi
476-
else
477-
generateSshKey
478-
fi
479-
fi
480-
}
481-
482401
set +e
483402
IS_COOLIFY_VOLUME_EXISTS=$(docker volume ls | grep coolify-db | wc -l)
484403
set -e
404+
485405
if [ "$IS_COOLIFY_VOLUME_EXISTS" -eq 0 ]; then
486406
echo " - Generating SSH key."
487407
ssh-keygen -t ed25519 -a 100 -f /data/coolify/ssh/keys/id.$CURRENT_USER@host.docker.internal -q -N "" -C coolify

0 commit comments

Comments
 (0)