tpsgi.sh 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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/tcms-vault" ]]; then
  19. export TCMS_VAULT_KEY=$(cat "$CREDENTIALS_DIRECTORY/tcms-vault")
  20. echo "Loaded the tcms-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. # We should obey the PATH set by this user, whose homedir is right here, ideally.
  40. bin/tarbaby --listen run/tpsgi.sock --workers 20 --user "$USERNAME" --group "$GROUP" --daemonize --pid run/tpsgi.pid --chroot $(pwd) bin/tpsgi
  41. if [ $? -ne 0 ]
  42. then
  43. echo "Could not start service!"
  44. exit 1;
  45. fi
  46. # Wait until the socket file is ready
  47. until [ -e run/tpsgi.sock ]
  48. do
  49. echo "Waiting for sock to be ready..."
  50. sleep 1
  51. done
  52. # Fix ownership of socket so nginx can see it
  53. sudo chown $USERNAME:www-data run/tpsgi.sock
  54. sudo chmod 0770 run/tpsgi.sock
  55. echo "tPSGI running as PID "`cat run/tpsgi.pid`