start.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. # Note that this method is deprecated since this app now supports optionalSso
  14. 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';")
  15. [[ -z "${ldap_status}" ]] && ldap_status="1"
  16. if mysql -u"${MYSQL_USERNAME}" -p"${MYSQL_PASSWORD}" -h mysql --database="${MYSQL_DATABASE}" \
  17. -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
  18. echo "=> LDAP Authentication was setup with status ${ldap_status}"
  19. else
  20. echo "=> Failed to setup LDAP authentication"
  21. exit 1
  22. fi
  23. }
  24. # SSH_PORT can be unset to disable SSH
  25. disable_ssh="false"
  26. if [[ -z "${SSH_PORT:-}" ]]; then
  27. echo "=> SSH disabled"
  28. SSH_PORT=29418 # arbitrary port to keep sshd happy
  29. disable_ssh="true"
  30. fi
  31. if [[ ! -f "/app/data/sshd/ssh_host_ed25519_key" ]]; then
  32. echo "=> Generating ssh host keys"
  33. mkdir -p /app/data/sshd
  34. ssh-keygen -qt rsa -N '' -f /app/data/sshd/ssh_host_rsa_key
  35. ssh-keygen -qt dsa -N '' -f /app/data/sshd/ssh_host_dsa_key
  36. ssh-keygen -qt ecdsa -N '' -f /app/data/sshd/ssh_host_ecdsa_key
  37. ssh-keygen -qt ed25519 -N '' -f /app/data/sshd/ssh_host_ed25519_key
  38. else
  39. echo "=> Reusing existing host keys"
  40. fi
  41. chmod 0600 /app/data/sshd/*_key
  42. chmod 0644 /app/data/sshd/*.pub
  43. sed -e "s/^Port .*/Port ${SSH_PORT}/" \
  44. -e "s/^#ListenAddress .*/ListenAddress 0.0.0.0/" \
  45. -e "s,^HostKey /etc/ssh/,HostKey /app/data/sshd/," \
  46. /etc/ssh/sshd_config > /run/gogs/sshd_config
  47. cp /home/git/app.ini.template "/run/gogs/app.ini"
  48. # create default user config file
  49. if ! [ -f /app/data/app.ini ]; then
  50. cp /home/git/app.ini.template /app/data/app.ini
  51. fi
  52. if [ "$(crudini --get /app/data/app.ini security SECRET_KEY)" == "##SECRET_KEY" ]; then
  53. echo "=> Generating new SECRET_KEY"
  54. crudini --set "/app/data/app.ini" security SECRET_KEY $(pwgen -1 -s)
  55. fi
  56. # merge user config file
  57. crudini --merge "/run/gogs/app.ini" < "/app/data/app.ini"
  58. # override important values
  59. crudini --set "/run/gogs/app.ini" database DB_TYPE mysql
  60. crudini --set "/run/gogs/app.ini" database HOST "${MYSQL_HOST}:${MYSQL_PORT}"
  61. crudini --set "/run/gogs/app.ini" database NAME "${MYSQL_DATABASE}"
  62. crudini --set "/run/gogs/app.ini" database USER "${MYSQL_USERNAME}"
  63. crudini --set "/run/gogs/app.ini" database PASSWD "${MYSQL_PASSWORD}"
  64. crudini --set "/run/gogs/app.ini" database SSL_MODE "disable"
  65. crudini --set "/run/gogs/app.ini" server PROTOCOL "http"
  66. crudini --set "/run/gogs/app.ini" server DOMAIN "${APP_DOMAIN}"
  67. crudini --set "/run/gogs/app.ini" server ROOT_URL "https://%(DOMAIN)s/"
  68. crudini --set "/run/gogs/app.ini" server HTTP_ADDR ""
  69. crudini --set "/run/gogs/app.ini" server HTTP_PORT "3000"
  70. crudini --set "/run/gogs/app.ini" server DISABLE_SSH "${disable_ssh}"
  71. crudini --set "/run/gogs/app.ini" server SSH_PORT "${SSH_PORT}"
  72. crudini --set "/run/gogs/app.ini" server APP_DATA_PATH "/app/data/appdata"
  73. crudini --set "/run/gogs/app.ini" repository ROOT "/app/data/repository"
  74. crudini --set "/run/gogs/app.ini" repository.upload TEMP_PATH "/run/gogs/tmp/uploads"
  75. # note that gogs relies SMTPS_PORT ending with 465 to determine SMTPS
  76. crudini --set "/run/gogs/app.ini" mailer HOST "${MAIL_SMTP_SERVER}:${MAIL_SMTPS_PORT}"
  77. crudini --set "/run/gogs/app.ini" mailer USER "${MAIL_SMTP_USERNAME}"
  78. crudini --set "/run/gogs/app.ini" mailer PASSWD "${MAIL_SMTP_PASSWORD}"
  79. crudini --set "/run/gogs/app.ini" mailer FROM "${MAIL_FROM}"
  80. crudini --set "/run/gogs/app.ini" mailer SKIP_VERIFY "true"
  81. crudini --set "/run/gogs/app.ini" security INSTALL_LOCK "true"
  82. crudini --set "/run/gogs/app.ini" log MODE "console"
  83. crudini --set "/run/gogs/app.ini" log ROOT_PATH "/run/gogs"
  84. mkdir -p /app/data/repository /app/data/ssh
  85. chown -R git:git /app/data /run/gogs
  86. if [[ -n "${LDAP_SERVER:-}" ]]; then
  87. ( setup_ldap_source ) &
  88. fi
  89. exec /usr/bin/supervisord --configuration /etc/supervisor/supervisord.conf --nodaemon -i Gogs