start.sh 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/bin/sh
  2. create_socat_links() {
  3. # Bind linked docker container to localhost socket using socat
  4. USED_PORT="3000:22"
  5. while read -r NAME ADDR PORT; do
  6. if test -z "$NAME$ADDR$PORT"; then
  7. continue
  8. elif echo $USED_PORT | grep -E "(^|:)$PORT($|:)" > /dev/null; then
  9. echo "init:socat | Can't bind linked container ${NAME} to localhost, port ${PORT} already in use" 1>&2
  10. else
  11. SERV_FOLDER=/app/gogs/docker/s6/SOCAT_${NAME}_${PORT}
  12. mkdir -p "${SERV_FOLDER}"
  13. CMD="socat -ls TCP4-LISTEN:${PORT},fork,reuseaddr TCP4:${ADDR}:${PORT}"
  14. # shellcheck disable=SC2039,SC3037
  15. echo -e "#!/bin/sh\nexec $CMD" > "${SERV_FOLDER}"/run
  16. chmod +x "${SERV_FOLDER}"/run
  17. USED_PORT="${USED_PORT}:${PORT}"
  18. echo "init:socat | Linked container ${NAME} will be binded to localhost on port ${PORT}" 1>&2
  19. fi
  20. done << EOT
  21. $(env | sed -En 's|(.*)_PORT_([0-9]+)_TCP=tcp://(.*):([0-9]+)|\1 \3 \4|p')
  22. EOT
  23. }
  24. cleanup() {
  25. # Cleanup SOCAT services and s6 event folder
  26. # On start and on shutdown in case container has been killed
  27. rm -rf "$(find /app/gogs/docker/s6/ -name 'event')"
  28. rm -rf /app/gogs/docker/s6/SOCAT_*
  29. }
  30. create_volume_subfolder() {
  31. # Modify the owner of /data dir, make $USER(git) user have permission to create sub-dir in /data.
  32. chown -R "$USER:$USER" /data
  33. # Create VOLUME subfolder
  34. for f in /data/gogs/data /data/gogs/conf /data/gogs/log /data/git /data/ssh; do
  35. if ! test -d $f; then
  36. gosu "$USER" mkdir -p $f
  37. fi
  38. done
  39. }
  40. setids() {
  41. export USER=git
  42. PUID=${PUID:-1000}
  43. PGID=${PGID:-1000}
  44. groupmod -o -g "$PGID" $USER
  45. usermod -o -u "$PUID" $USER
  46. }
  47. setids
  48. cleanup
  49. create_volume_subfolder
  50. LINK=$(echo "$SOCAT_LINK" | tr '[:upper:]' '[:lower:]')
  51. if [ "$LINK" = "false" ] || [ "$LINK" = "0" ]; then
  52. echo "init:socat | Will not try to create socat links as requested" 1>&2
  53. else
  54. create_socat_links
  55. fi
  56. CROND=$(echo "$RUN_CROND" | tr '[:upper:]' '[:lower:]')
  57. if [ "$CROND" = "true" ] || [ "$CROND" = "1" ]; then
  58. echo "init:crond | Cron Daemon (crond) will be run as requested by s6" 1>&2
  59. rm -f /app/gogs/docker/s6/crond/down
  60. /bin/sh /app/gogs/docker/runtime/backup-init.sh "${PUID}"
  61. else
  62. # Tell s6 not to run the crond service
  63. touch /app/gogs/docker/s6/crond/down
  64. fi
  65. # Exec CMD or S6 by default if nothing present
  66. if [ $# -gt 0 ];then
  67. exec "$@"
  68. else
  69. exec /bin/s6-svscan /app/gogs/docker/s6/
  70. fi