start.sh 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/bin/bash
  2. set -eu -o pipefail
  3. setup_ldap_source() {
  4. set -eu
  5. # Wait for gogs to finish db setup, before we insert ldap source in db
  6. while ! curl --fail http://localhost:3000/healthcheck; do
  7. echo "Waiting for gogs to come up"
  8. sleep 1
  9. done
  10. now=$(date +%s)
  11. if mysql -u"${MYSQL_USERNAME}" -p"${MYSQL_PASSWORD}" -h mysql --database="${MYSQL_DATABASE}" \
  12. -e "REPLACE INTO login_source (id, type, name, is_actived, cfg, created_unix, updated_unix) VALUES (1,2,'cloudron',1,'{\"Name\":\"cloudron\",\"Host\":\"${LDAP_SERVER}\",\"Port\":${LDAP_PORT},\"UseSSL\":false,\"SkipVerify\":true,\"BindDN\":\"${LDAP_BIND_DN}\",\"BindPassword\":\"${LDAP_BIND_PASSWORD}\",\"UserBase\":\"${LDAP_USERS_BASE_DN}\",\"AttributeUsername\":\"username\",\"AttributeName\":\"displayname\",\"AttributeSurname\":\"\",\"AttributeMail\":\"mail\",\"Filter\":\"(\\\\u007C(mail=%[1]s)(username=%[1]s))\",\"AdminFilter\":\"(memberof=cn=admins,${LDAP_GROUPS_BASE_DN})\"}','${now}','${now}');"; then
  13. echo "LDAP Authentication Setup"
  14. else
  15. echo "Failed to setup LDAP authentication"
  16. exit 1
  17. fi
  18. }
  19. # SSH_PORT can be unset to disable SSH
  20. disable_ssh="false"
  21. if [[ -z "${SSH_PORT:-}" ]]; then
  22. echo "SSH disabled"
  23. SSH_PORT=29418 # arbitrary port to keep sshd happy
  24. disable_ssh="true"
  25. fi
  26. if [[ ! -f "/app/data/sshd/ssh_host_ed25519_key" ]]; then
  27. echo "Generating ssh host keys"
  28. mkdir -p /app/data/sshd
  29. ssh-keygen -qt rsa -N '' -f /app/data/sshd/ssh_host_rsa_key
  30. ssh-keygen -qt dsa -N '' -f /app/data/sshd/ssh_host_dsa_key
  31. ssh-keygen -qt ecdsa -N '' -f /app/data/sshd/ssh_host_ecdsa_key
  32. ssh-keygen -qt ed25519 -N '' -f /app/data/sshd/ssh_host_ed25519_key
  33. else
  34. echo "Reusing existing host keys"
  35. fi
  36. chmod 0600 /app/data/sshd/*_key
  37. chmod 0644 /app/data/sshd/*.pub
  38. sed -e "s/^Port .*/Port ${SSH_PORT}/" \
  39. -e "s/^#ListenAddress .*/ListenAddress 0.0.0.0/" \
  40. -e "s,^HostKey /etc/ssh/,HostKey /app/data/sshd/," \
  41. /etc/ssh/sshd_config > /run/gogs/sshd_config
  42. sed -e "s/##DOMAIN/${APP_DOMAIN}/g" \
  43. -e "s/##SSH_PORT/${SSH_PORT}/g" \
  44. -e "s/##DISABLE_SSH/${disable_ssh}/g" \
  45. -e "s/##MYSQL_HOST/${MYSQL_HOST}/g" \
  46. -e "s/##MYSQL_PORT/${MYSQL_PORT}/g" \
  47. -e "s/##MYSQL_USERNAME/${MYSQL_USERNAME}/g" \
  48. -e "s/##MYSQL_PASSWORD/${MYSQL_PASSWORD}/g" \
  49. -e "s/##MYSQL_DATABASE/${MYSQL_DATABASE}/g" \
  50. -e "s/##MAIL_SERVER/${MAIL_SMTP_SERVER}/g" \
  51. -e "s/##MAIL_PORT/${MAIL_SMTP_PORT}/g" \
  52. -e "s/##MAIL_FROM/${MAIL_FROM}/g" \
  53. -e "s/##MAIL_SMTP_USERNAME/${MAIL_SMTP_USERNAME}/g" \
  54. -e "s/##MAIL_SMTP_PASSWORD/${MAIL_SMTP_PASSWORD}/g" \
  55. -e "s/##SECRET_KEY/$(pwgen -1 -s)/g" \
  56. /home/git/app.ini.template > "/run/gogs/app.ini"
  57. # merge any user config file
  58. [[ -f /app/data/app.ini ]] && echo "/app/data/app.ini" >> "/run/gogs/app.ini"
  59. mkdir -p /app/data/repository /app/data/ssh
  60. chown -R git:git /app/data /run/gogs
  61. ( setup_ldap_source ) &
  62. exec /usr/bin/supervisord --configuration /etc/supervisor/supervisord.conf --nodaemon -i Gogs