webhook.sh 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/bin/bash
  2. # bachir soussi chiadmi
  3. # get the current position
  4. _cwd="$(pwd)"
  5. echo -e '\033[35m
  6. __ __ _ _ _ _
  7. \ \ / /__| |__| || |___ ___| |__
  8. \ \/\/ / -_) `_ \ __ / _ \/ _ \ / /
  9. \_/\_/\___|_.__/_||_\___/\___/_\_\
  10. \033[0m'
  11. # check for assets folder
  12. _assets="$_cwd/assets"
  13. if [ ! -d "$_assets" ]; then
  14. _assets="$_cwd/../assets"
  15. if [ ! -d "$_assets" ]; then
  16. echo "!! can't find assets directory !!"
  17. exit
  18. fi
  19. fi
  20. user=""
  21. while [ "$user" = "" ]
  22. do
  23. read -p "enter an existing user name ? " user
  24. if [ "$user" != "" ]; then
  25. # check if user already exists
  26. if id "$user" >/dev/null 2>&1; then
  27. read -p "is user name $user correcte [y|n] " validated
  28. if [ "$validated" = "y" ]; then
  29. break
  30. else
  31. user=""
  32. fi
  33. else
  34. echo "user $user doesn't exists, you must provide an existing user"
  35. user=""
  36. fi
  37. fi
  38. done
  39. _domain=""
  40. while [ "$_domain" = "" ]
  41. do
  42. read -p "enter a domain name ? " _domain
  43. if [ "$_domain" != "" ]; then
  44. read -p "is domain $_domain correcte [y|n] " validated
  45. if [ "$validated" = "y" ]; then
  46. break
  47. else
  48. _domain=""
  49. fi
  50. fi
  51. done
  52. # TODO check for /home/"$user"/www/"$_domain"
  53. if [ ! -d /home/"$user"/www/"$_domain" ]; then
  54. echo "/home/$user/www/$_domain does not exists !"
  55. exit
  56. fi
  57. # TODO check for /home/"$user"/git-repositories/"$_domain.git"
  58. if [ ! -d /home/"$user"/git-repositories/"$_domain.git" ]; then
  59. echo "/home/$user/git-repositories/$_domain.git does not exists !"
  60. exit
  61. fi
  62. apt-get install webhook
  63. # hook deploy script
  64. cp "$_assets"/webhook-deploy.sh /home/"$user"/webhook-deploy-"$_domain".sh
  65. sed -i -r "s/DOMAIN/$_domain/g" /home/"$user"/webhook-deploy-"$_domain".sh
  66. chmod +x /"$user"/webhook-deploy-"$_domain".sh
  67. # remove git bare repos hook
  68. mv /home/"$user"/git-repositories/"$_domain".git/hooks/post-receive /home/"$user"/git-repositories/"$_domain".git/hooks/post-receive.back
  69. # webhook conf
  70. touch /etc/webhook.conf
  71. echo "
  72. - id: deploy-app-$_domain
  73. execute-command: /$user/webhook-deploy-$_domain.sh
  74. command-working-directory: /home/$user/
  75. " >> /etc/webhook.conf
  76. # webhook service
  77. cp "$_assets"/webhook.service /etc/systemd/system/webhook.service
  78. systemctl enable webhook
  79. systemctl start webhook
  80. systemctl reload webhook
  81. ufw allow 9000
  82. echo "http://$_domain:9000/hooks/deploy-app-$_domain"