Experimental script to automatically deploy test instance for each proposed PR.
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.

74 lines
2.5 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #!/bin/bash
  2. set -x
  3. currently_running=$(docker container ls | grep -Eo 'plume-pr-[0-9]+' | cut -c10- | tr '\n' ',')
  4. cat Caddyfile.base *.caddy > Caddyfile
  5. [ -z "$currently_running" ] &&\
  6. echo '[]' > static/up.json ||\
  7. echo '['"${currently_running::-1}"']' > static/up.json
  8. caddy &
  9. caddy_pid=$!
  10. inotifywait -m ./ -e create -e moved_to |
  11. while read path action file; do
  12. if [[ ! $file =~ ^[0-9]*\.tar\.gz$ ]]; then
  13. continue
  14. fi;
  15. id=$(basename -s .tar.gz $file)
  16. if [ -d ../plume_deploy/$id ]; then
  17. rm -rf ../plume_deploy/$id
  18. fi;
  19. mkdir ../plume_deploy/$id
  20. tar -C ../plume_deploy/$id -xvzf $file
  21. env_temp=$(pwd)/.env.template
  22. log_dir=$(pwd)/logs
  23. pushd ../plume_deploy/$id
  24. secret=$(openssl rand -base64 32)
  25. domain=pr-$id.joinplu.me
  26. found=0
  27. port=8000
  28. while [ $found -ne 1 ]; do
  29. if [ $(netstat -tlnu | grep :$port | wc -l) == "0" ]; then
  30. found=1
  31. else
  32. port=$((port + 1))
  33. fi;
  34. done;
  35. sed -e "s;%BASE_URL%;$domain;g" -e "s;%SECRET%;$secret;g" $env_temp | tee .env
  36. # Kill old instance, if there are more than 5 running
  37. if [ $(sudo docker ps | grep plume-pr | wc -l) -ge 5 ]; then
  38. to_kill=$(sudo docker ps | grep plume-pr | tail -n 1 | awk 'NF>1{print $NF}')
  39. docker stop $to_kill
  40. fi;
  41. cont=plume-pr-$id
  42. docker container rm $cont || true
  43. docker run -td --name $cont --rm -p 127.0.0.1:$port:7878 --mount type=bind,src=$(pwd),dst=/app plumeorg/plume-buildenv:v0.0.5
  44. docker exec -w /app $cont ls -al
  45. docker exec -w /app $cont /app/bin/diesel migration run
  46. docker exec -w /app $cont /app/bin/plm instance new -n "PR #$id"
  47. docker exec -w /app $cont /app/bin/plm users new -a -n admin -p admin123 -N "Admin #$id" -e "admin@$domain"
  48. docker exec -w /app $cont /app/bin/plm search init
  49. docker container logs -f --since 0m $cont > $log_dir/$id &
  50. docker exec -w /app -d $cont /app/bin/plume
  51. popd
  52. #get comma separated list of running containers
  53. currently_running=$(docker container ls | grep -Eo 'plume-pr-[0-9]+' | cut -c10- | tr '\n' ',')
  54. #remove trailing ',' and convert to json
  55. echo '['"${currently_running::-1}"']' > static/up.json
  56. sed -e "s;%BASE_URL%;$domain;g" -e "s;%PORT%;$port;g" Caddyfile.template > $id.caddy
  57. cat Caddyfile.base *.caddy > Caddyfile
  58. kill $caddy_pid
  59. caddy &
  60. caddy_pid=$!
  61. done