start.sh 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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})\",\"Enabled\":true}','${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 rsa1 -N '' -f /app/data/sshd/ssh_host_key
  30. ssh-keygen -qt rsa -N '' -f /app/data/sshd/ssh_host_rsa_key
  31. ssh-keygen -qt dsa -N '' -f /app/data/sshd/ssh_host_dsa_key
  32. ssh-keygen -qt ecdsa -N '' -f /app/data/sshd/ssh_host_ecdsa_key
  33. ssh-keygen -qt ed25519 -N '' -f /app/data/sshd/ssh_host_ed25519_key
  34. else
  35. echo "Reusing existing host keys"
  36. fi
  37. chmod 0600 /app/data/sshd/*_key
  38. chmod 0644 /app/data/sshd/*.pub
  39. sed -e "s/^Port .*/Port ${SSH_PORT}/" \
  40. -e "s/^#ListenAddress .*/ListenAddress 0.0.0.0/" \
  41. -e "s,^HostKey /etc/ssh/,HostKey /app/data/sshd/," \
  42. /etc/ssh/sshd_config > /run/gogs/sshd_config
  43. sed -e "s/##DOMAIN/${APP_DOMAIN}/g" \
  44. -e "s/##SSH_PORT/${SSH_PORT}/g" \
  45. -e "s/##DISABLE_SSH/${disable_ssh}/g" \
  46. -e "s/##MYSQL_HOST/${MYSQL_HOST}/g" \
  47. -e "s/##MYSQL_PORT/${MYSQL_PORT}/g" \
  48. -e "s/##MYSQL_USERNAME/${MYSQL_USERNAME}/g" \
  49. -e "s/##MYSQL_PASSWORD/${MYSQL_PASSWORD}/g" \
  50. -e "s/##MYSQL_DATABASE/${MYSQL_DATABASE}/g" \
  51. -e "s/##MAIL_SERVER/${MAIL_SMTP_SERVER}/g" \
  52. -e "s/##MAIL_PORT/${MAIL_SMTP_PORT}/g" \
  53. -e "s/##MAIL_FROM/${MAIL_SMTP_USERNAME}@${MAIL_DOMAIN}/g" \
  54. -e "s/##SECRET_KEY/$(pwgen -1 -s)/g" \
  55. /home/git/app.ini.template > "/run/gogs/app.ini"
  56. mkdir -p /app/data/repository /app/data/ssh
  57. chown -R git:git /app/data /run/gogs
  58. ( setup_ldap_source ) &
  59. exec /usr/bin/supervisord --configuration /etc/supervisor/supervisord.conf --nodaemon -i Gogs