start.sh 3.4 KB

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