You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.3 KiB

  1. #!/bin/sh
  2. # Cleanup SOCAT services and s6 event folder
  3. # On start and on shutdown in case container has been killed
  4. rm -rf $(find /app/gogs/docker/s6/ -name 'event')
  5. rm -rf /app/gogs/docker/s6/SOCAT_*
  6. # Create VOLUME subfolder
  7. for f in /data/gogs/data /data/gogs/conf /data/gogs/log /data/git /data/ssh; do
  8. if ! test -d $f; then
  9. mkdir -p $f
  10. fi
  11. done
  12. # Bind linked docker container to localhost socket using socat
  13. USED_PORT="3000:22"
  14. while read NAME ADDR PORT; do
  15. if test -z "$NAME$ADDR$PORT"; then
  16. continue
  17. elif echo $USED_PORT | grep -E "(^|:)$PORT($|:)" > /dev/null; then
  18. echo "init:socat | Can't bind linked container ${NAME} to localhost, port ${PORT} already in use" 1>&2
  19. else
  20. SERV_FOLDER=/app/gogs/docker/s6/SOCAT_${NAME}_${PORT}
  21. mkdir -p ${SERV_FOLDER}
  22. CMD="socat -ls TCP4-LISTEN:${PORT},fork,reuseaddr TCP4:${ADDR}:${PORT}"
  23. echo -e "#!/bin/sh\nexec $CMD" > ${SERV_FOLDER}/run
  24. chmod +x ${SERV_FOLDER}/run
  25. USED_PORT="${USED_PORT}:${PORT}"
  26. echo "init:socat | Linked container ${NAME} will be binded to localhost on port ${PORT}" 1>&2
  27. fi
  28. done << EOT
  29. $(env | sed -En 's|(.*)_PORT_([0-9]+)_TCP=tcp://(.*):([0-9]+)|\1 \3 \4|p')
  30. EOT
  31. # Exec CMD or S6 by default if nothing present
  32. if [ $# -gt 0 ];then
  33. exec "$@"
  34. else
  35. exec /usr/bin/s6-svscan /app/gogs/docker/s6/
  36. fi