tpsgi.sh 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/bin/bash
  2. USERNAME=$(bin/tpsgi-config --user)
  3. [[ -z $USERNAME ]] && USERNAME=$USER
  4. echo "tPSGI running as user $USERNAME"
  5. EGID=$(id -nu)
  6. GROUP=$(bin/tpsgi-config --http_user)
  7. [[ -z $GROUP ]] && GROUP=$EGID
  8. echo "tPSGI running with EGID $GROUP";
  9. [[ -e run/tpsgi.pid ]] && sudo pkill -F run/tpsgi.pid
  10. # Hand the application whatever systemd put in the credential store. It has to
  11. # happen here: tarbaby chroots the workers into this directory, and the store is
  12. # a ramdisk at /run/credentials which is not inside it. So the file is read
  13. # while there is still a path to it, and what was in it goes on in the
  14. # environment, which survives the chroot.
  15. #
  16. # The application is expected to take it back out of its own environment as it
  17. # starts, so that nothing it forks later inherits it.
  18. if [[ -n ${CREDENTIALS_DIRECTORY:-} && -r "$CREDENTIALS_DIRECTORY/tpsgi-vault" ]]; then
  19. export TPSGI_VAULT_KEY=$(cat "$CREDENTIALS_DIRECTORY/tpsgi-vault")
  20. echo "Loaded the tpsgi-vault credential"
  21. fi
  22. # Bind the various dirs we need for chroot to work
  23. readarray -t BIND_DIRS <<< $(bin/tpsgi-config --binds)
  24. for bind in "${BIND_DIRS[@]}"; do
  25. if [[ -z $bind ]]; then
  26. continue;
  27. fi;
  28. bn=$(basename $bind)
  29. to_bind=$(pwd)/$bn;
  30. if [[ -d $bind ]]; then
  31. echo "Bind $bind to $to_bind";
  32. mkdir -p $to_bind;
  33. [[ ! $(mountpoint -q $to_bind) ]] && mount --bind $bind $to_bind
  34. else
  35. echo "Refusing to bind nonexistant mountpoint $bind to $bn!"
  36. exit 1;
  37. fi;
  38. done
  39. # Daemonizing points stderr at /dev/null, and stderr is where the server puts
  40. # whatever an application dies with. --error-log points it at a file instead,
  41. # which the server opens before it chroots or drops privileges, so the path is
  42. # one outside the chroot. It is the directory tPSGI's own log goes in, which
  43. # the workers write as $USERNAME, so that is who it is made for.
  44. LOG_DIR=$(bin/tpsgi-config --log_dir)
  45. [[ -d $LOG_DIR ]] || install -d -o "$USERNAME" -g "$GROUP" "$LOG_DIR"
  46. # We should obey the PATH set by this user, whose homedir is right here, ideally.
  47. bin/tarbaby --listen run/tpsgi.sock --workers 20 --user "$USERNAME" --group "$GROUP" --daemonize --error-log "$LOG_DIR/error.log" --pid run/tpsgi.pid --chroot $(pwd) bin/tpsgi
  48. if [ $? -ne 0 ]
  49. then
  50. echo "Could not start service!"
  51. exit 1;
  52. fi
  53. # Wait until the socket file is ready
  54. until [ -e run/tpsgi.sock ]
  55. do
  56. echo "Waiting for sock to be ready..."
  57. sleep 1
  58. done
  59. # Fix ownership of socket so nginx can see it
  60. sudo chown $USERNAME:www-data run/tpsgi.sock
  61. sudo chmod 0770 run/tpsgi.sock
  62. echo "tPSGI running as PID "`cat run/tpsgi.pid`