tools.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/bin/bash
  2. set -e
  3. function install_ffmpeg() {
  4. # install a specific version of ffmpeg
  5. FFMPEG_VER="4.4.1"
  6. FFMPEG_PATH="$(pwd)/ffmpeg-$FFMPEG_VER"
  7. PATH=$FFMPEG_PATH:$PATH
  8. if ! [[ -d "$FFMPEG_PATH" ]]; then
  9. mkdir "$FFMPEG_PATH"
  10. fi
  11. pushd "$FFMPEG_PATH" >/dev/null
  12. if [[ -x "$FFMPEG_PATH/ffmpeg" ]]; then
  13. ffmpeg_version=$("$FFMPEG_PATH/ffmpeg" -version | awk -F 'ffmpeg version' '{print $2}' | awk 'NR==1{print $1}')
  14. if [[ "$ffmpeg_version" == "$FFMPEG_VER-static" ]]; then
  15. popd >/dev/null
  16. return 0
  17. else
  18. mv "$FFMPEG_PATH/ffmpeg" "$FFMPEG_PATH/ffmpeg.bk" || rm -f "$FFMPEG_PATH/ffmpeg"
  19. fi
  20. fi
  21. rm -f ffmpeg.zip
  22. curl -sL --fail https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v${FFMPEG_VER}/ffmpeg-${FFMPEG_VER}-linux-64.zip --output ffmpeg.zip >/dev/null
  23. unzip -o ffmpeg.zip >/dev/null && rm -f ffmpeg.zip
  24. chmod +x ffmpeg
  25. PATH=$FFMPEG_PATH:$PATH
  26. popd >/dev/null
  27. }
  28. function start_owncast() {
  29. # Build and run owncast from source
  30. echo "Building owncast..."
  31. pushd "$(git rev-parse --show-toplevel)" >/dev/null
  32. go build -o owncast main.go
  33. echo "Running owncast..."
  34. ./owncast -database "$TEMP_DB" &
  35. SERVER_PID=$!
  36. popd >/dev/null
  37. sleep 5
  38. }
  39. function start_stream() {
  40. # Start streaming the test file over RTMP to the local owncast instance.
  41. ../../ocTestStream.sh &
  42. STREAM_PID=$!
  43. echo "Waiting for stream to start..."
  44. sleep 12
  45. }
  46. function update_storage_config() {
  47. echo "Configuring external storage to use ${S3_BUCKET}..."
  48. # Hard-coded to admin:abc123 for auth
  49. curl --fail 'http://localhost:8080/api/admin/config/s3' \
  50. -H 'Authorization: Basic YWRtaW46YWJjMTIz' \
  51. --data-raw "{\"value\":{\"accessKey\":\"${S3_ACCESS_KEY}\",\"acl\":\"\",\"bucket\":\"${S3_BUCKET}\",\"enabled\":true,\"endpoint\":\"${S3_ENDPOINT}\",\"region\":\"${S3_REGION}\",\"secret\":\"${S3_SECRET}\",\"servingEndpoint\":\"\"}}"
  52. }
  53. function kill_with_kids() {
  54. # kill a process and all its children (by pid)! return no error.
  55. if [[ -n $1 ]]; then
  56. mapfile -t CHILDREN_PID_LIST < <(ps --ppid "$1" -o pid= &>/dev/null || true)
  57. for child_pid in "${CHILDREN_PID_LIST[@]}"; do
  58. kill "$child_pid" &>/dev/null || true
  59. wait "$child_pid" &>/dev/null || true
  60. done
  61. kill "$1" &>/dev/null || true
  62. wait "$1" &>/dev/null || true
  63. fi
  64. }
  65. function finish() {
  66. echo "Cleaning up..."
  67. kill_with_kids "$STREAM_PID"
  68. kill "$SERVER_PID" &>/dev/null || true
  69. wait "$SERVER_PID" &>/dev/null || true
  70. rm -fr "$TEMP_DB"
  71. }
  72. trap finish EXIT
  73. TEMP_DB=$(mktemp)