start.sh 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. # Create VOLUME subfolder
  31. for f in /data/gogs/data /data/gogs/conf /data/gogs/log /data/git /data/ssh; do
  32. if ! test -d $f; then
  33. mkdir -p $f
  34. fi
  35. done
  36. }
  37. setids() {
  38. PUID=${PUID:-1000}
  39. PGID=${PGID:-1000}
  40. groupmod -o -g "$PGID" git
  41. usermod -o -u "$PUID" git
  42. }
  43. setids
  44. cleanup
  45. create_volume_subfolder
  46. LINK=$(echo "$SOCAT_LINK" | tr '[:upper:]' '[:lower:]')
  47. if [ "$LINK" = "false" -o "$LINK" = "0" ]; then
  48. echo "init:socat | Will not try to create socat links as requested" 1>&2
  49. else
  50. create_socat_links
  51. fi
  52. CROND=$(echo "$RUN_CROND" | tr '[:upper:]' '[:lower:]')
  53. if [ "$CROND" = "true" -o "$CROND" = "1" ]; then
  54. echo "init:crond | Cron Daemon (crond) will be run as requested by s6" 1>&2
  55. rm -f /app/gogs/docker/s6/crond/down
  56. else
  57. # Tell s6 not to run the crond service
  58. touch /app/gogs/docker/s6/crond/down
  59. fi
  60. # Exec CMD or S6 by default if nothing present
  61. if [ $# -gt 0 ];then
  62. exec "$@"
  63. else
  64. exec /bin/s6-svscan /app/gogs/docker/s6/
  65. fi