webhook.sh 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. while [ "$user" = "" ]
  21. do
  22. read -p "enter an existing user name ? " user
  23. if [ "$user" != "" ]; then
  24. # check if user already exists
  25. if id "$user" >/dev/null 2>&1; then
  26. read -p "is user name $user correcte [y|n] " validated
  27. if [ "$validated" = "y" ]; then
  28. break
  29. else
  30. user=""
  31. fi
  32. else
  33. echo "user $user doesn't exists, you must provide an existing user"
  34. user=""
  35. fi
  36. fi
  37. done
  38. while [ "$_domain" = "" ]
  39. do
  40. read -p "enter a domain name ? " _domain
  41. if [ "$_domain" != "" ]; then
  42. read -p "is domain $_domain correcte [y|n] " validated
  43. if [ "$validated" = "y" ]; then
  44. break
  45. else
  46. _domain=""
  47. fi
  48. fi
  49. done
  50. # TODO check for /home/"$user"/www/"$_domain"
  51. if [ ! -d /home/"$user"/www/"$_domain" ]; then
  52. echo "/home/$user/www/$_domain does not exists !"
  53. exit
  54. fi
  55. # TODO check for /home/"$user"/git-repositories/"$_domain.git"
  56. if [ ! -d /home/"$user"/git-repositories/"$_domain.git" ]; then
  57. echo "/home/$user/git-repositories/$_domain.git does not exists !"
  58. exit
  59. fi
  60. apt-get install webhook
  61. # hook deploy script
  62. cp "$_assets"/webhook-deploy.sh /"$user"/webhook-deploy-"$_domain".sh
  63. sed -i -r "s/DOMAIN/$_domain/g" /"$user"/webhook-deploy-"$_domain".sh
  64. chmod +x /"$user"/webhook-deploy-"$_domain".sh
  65. # remove git bare repos hook
  66. mv /home/"$user"/git-repositories/"$_domain".git/hooks/post-receive /home/"$user"/git-repositories/"$_domain".git/hooks/post-receive.back
  67. # webhook conf
  68. touch /etc/webhook.conf
  69. echo "
  70. - id: deploy-app-$_domain
  71. execute-command: /$user/webhook-deploy-$_domain.sh
  72. command-working-directory: /home/$user/
  73. " >> /etc/webhook.conf
  74. # webhook service
  75. cp "$_assets"/webhook.service /etc/systemd/system/webhook.service
  76. systemctl enable webhook
  77. systemctl start webhook
  78. systemctl reload webhook
  79. ufw allow 9000
  80. echo "http://$_domain:9000/hooks/deploy-app-$_domain"