start.sh 2.4 KB

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