start.sh 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. cleanup
  38. create_volume_subfolder
  39. LINK=$(echo "$SOCAT_LINK" | tr '[:upper:]' '[:lower:]')
  40. if [ "$LINK" = "false" -o "$LINK" = "0" ]; then
  41. echo "init:socat | Will not try to create socat links as requested" 1>&2
  42. else
  43. create_socat_links
  44. fi
  45. CROND=$(echo "$RUN_CROND" | tr '[:upper:]' '[:lower:]')
  46. if [ "$CROND" = "true" -o "$CROND" = "1" ]; then
  47. echo "init:crond | Cron Daemon (crond) will be run as requested by s6" 1>&2
  48. rm -f /app/gogs/docker/s6/crond/down
  49. else
  50. # Tell s6 not to run the crond service
  51. touch /app/gogs/docker/s6/crond/down
  52. fi
  53. # Exec CMD or S6 by default if nothing present
  54. if [ $# -gt 0 ];then
  55. exec "$@"
  56. else
  57. exec /bin/s6-svscan /app/gogs/docker/s6/
  58. fi