2
0

start.sh 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. # only change ownership if needed, if using an nfs mount this could be expensive
  32. if [ "$USER:$USER" != "$(stat /data -c '%U:%G')" ]
  33. then
  34. # Modify the owner of /data dir, make $USER(git) user have permission to create sub-dir in /data.
  35. chown -R "$USER:$USER" /data
  36. fi
  37. # Create VOLUME subfolder
  38. for f in /data/gogs/data /data/gogs/conf /data/gogs/log /data/git /data/ssh; do
  39. if ! test -d $f; then
  40. gosu "$USER" mkdir -p $f
  41. fi
  42. done
  43. }
  44. setids() {
  45. export USER=git
  46. PUID=${PUID:-1000}
  47. PGID=${PGID:-1000}
  48. groupmod -o -g "$PGID" $USER
  49. usermod -o -u "$PUID" $USER
  50. }
  51. setids
  52. cleanup
  53. create_volume_subfolder
  54. LINK=$(echo "$SOCAT_LINK" | tr '[:upper:]' '[:lower:]')
  55. if [ "$LINK" = "false" ] || [ "$LINK" = "0" ]; then
  56. echo "init:socat | Will not try to create socat links as requested" 1>&2
  57. else
  58. create_socat_links
  59. fi
  60. CROND=$(echo "$RUN_CROND" | tr '[:upper:]' '[:lower:]')
  61. if [ "$CROND" = "true" ] || [ "$CROND" = "1" ]; then
  62. echo "init:crond | Cron Daemon (crond) will be run as requested by s6" 1>&2
  63. rm -f /app/gogs/docker/s6/crond/down
  64. /bin/sh /app/gogs/docker/runtime/backup-init.sh "${PUID}"
  65. else
  66. # Tell s6 not to run the crond service
  67. touch /app/gogs/docker/s6/crond/down
  68. fi
  69. # Exec CMD or S6 by default if nothing present
  70. if [ $# -gt 0 ];then
  71. exec "$@"
  72. else
  73. exec /bin/s6-svscan /app/gogs/docker/s6/
  74. fi