webhook.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. _id=$(echo "$_domain" | sed "s/\./_/g")
  53. _remote=""
  54. while [ "$_remote" = "" ]
  55. do
  56. read -p "enter teh remote git repos url to pull from ? " _remote
  57. if [ "$_remote" != "" ]; then
  58. read -p "is $_remote correcte [y|n] " validated
  59. if [ "$validated" = "y" ]; then
  60. break
  61. else
  62. _remote=""
  63. fi
  64. fi
  65. done
  66. # TODO check for /home/"$user"/www/"$_domain"
  67. if [ ! -d /home/"$user"/www/"$_domain" ]; then
  68. echo "/home/$user/www/$_domain does not exists !"
  69. exit
  70. fi
  71. # TODO check for /home/"$user"/git-repositories/"$_domain.git"
  72. if [ ! -d /home/"$user"/git-repositories/"$_domain.git" ]; then
  73. echo "/home/$user/git-repositories/$_domain.git does not exists !"
  74. exit
  75. fi
  76. apt-get install webhook
  77. # git bare repos remote
  78. git --git-dir=/home/"$user"/git-repositories/"$_domain.git" remote add origin "$_remote"
  79. # hook deploy script
  80. cp -f "$_assets"/webhook-deploy.sh /home/"$user"/webhook_deploy_"$_id".sh
  81. sed -i -r "s/DOMAIN/$_domain/g" /home/"$user"/webhook_deploy_"$_id".sh
  82. chowm $user:$user /home/"$user"/webhook_deploy_"$_id".sh
  83. chmod +x /home/"$user"/webhook_deploy_"$_id".sh
  84. # remove git bare repos hook
  85. mv /home/"$user"/git-repositories/"$_domain".git/hooks/post-receive /home/"$user"/git-repositories/"$_domain".git/hooks/post-receive.back
  86. # webhook conf
  87. touch /etc/webhooks.conf
  88. echo "
  89. - id: deploy_app_$_id
  90. execute-command: /home/$user/webhook-deploy-$_id.sh
  91. command-working-directory: /home/$user/
  92. " >> /etc/webhooks.conf
  93. # webhook service
  94. cp -f "$_assets"/webhook.service /etc/systemd/system/webhook.service
  95. systemctl enable webhook
  96. systemctl start webhook
  97. systemctl restart webhook
  98. # systemctl reload webhook
  99. ufw allow 9000
  100. echo "http://$_domain:9000/hooks/deploy_app_$_id"