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.6 KiB

  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 stop $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 > $log_dir/$id
  45. docker exec -t -w /app $cont /app/bin/plm migration run >> $log_dir/$id
  46. docker exec -t -w /app $cont /app/bin/plm instance new -n "PR #$id" >> $log_dir/$id
  47. docker exec -t -w /app $cont /app/bin/plm users new -a -n admin -p admin123 -N "Admin #$id" -e "admin@$domain" >> $log_dir/$id
  48. docker exec -t -w /app $cont /app/bin/plm search init >> $log_dir/$id
  49. docker exec -t -e ROCKET_ENV=dev -w /app $cont /app/bin/plume >> $log_dir/$id &
  50. popd
  51. #get comma separated list of running containers
  52. currently_running=$(docker container ls | grep -Eo 'plume-pr-[0-9]+' | cut -c10- | tr '\n' ',' | sort -r)
  53. #remove trailing ',' and convert to json
  54. echo '['"${currently_running::-1}"']' > static/up.json
  55. sed -e "s;%BASE_URL%;$domain;g" -e "s;%PORT%;$port;g" Caddyfile.template > $id.caddy
  56. cat Caddyfile.base *.caddy > Caddyfile
  57. kill $caddy_pid
  58. caddy &
  59. caddy_pid=$!
  60. done