123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- #!/bin/bash
- # bachir soussi chiadmi
- # get the current position
- _cwd="$(pwd)"
- echo -e '\033[35m
- __ __ _ _ _ _
- \ \ / /__| |__| || |___ ___| |__
- \ \/\/ / -_) `_ \ __ / _ \/ _ \ / /
- \_/\_/\___|_.__/_||_\___/\___/_\_\
- \033[0m'
- # check for assets folder
- _assets="$_cwd/assets"
- if [ ! -d "$_assets" ]; then
- _assets="$_cwd/../assets"
- if [ ! -d "$_assets" ]; then
- echo "!! can't find assets directory !!"
- exit
- fi
- fi
- user=""
- while [ "$user" = "" ]
- do
- read -p "enter an existing user name ? " user
- if [ "$user" != "" ]; then
- # check if user already exists
- if id "$user" >/dev/null 2>&1; then
- read -p "is user name $user correcte [y|n] " validated
- if [ "$validated" = "y" ]; then
- break
- else
- user=""
- fi
- else
- echo "user $user doesn't exists, you must provide an existing user"
- user=""
- fi
- fi
- done
- _domain=""
- while [ "$_domain" = "" ]
- do
- read -p "enter a domain name ? " _domain
- if [ "$_domain" != "" ]; then
- read -p "is domain $_domain correcte [y|n] " validated
- if [ "$validated" = "y" ]; then
- break
- else
- _domain=""
- fi
- fi
- done
- _id=$(echo "$_domain" | sed "s/\./_/g")
- _remote=""
- while [ "$_remote" = "" ]
- do
- read -p "enter teh remote git repos url to pull from ? " _remote
- if [ "$_remote" != "" ]; then
- read -p "is $_remote correcte [y|n] " validated
- if [ "$validated" = "y" ]; then
- break
- else
- _remote=""
- fi
- fi
- done
- # TODO check for /home/"$user"/www/"$_domain"
- if [ ! -d /home/"$user"/www/"$_domain" ]; then
- echo "/home/$user/www/$_domain does not exists !"
- exit
- fi
- # TODO check for /home/"$user"/git-repositories/"$_domain.git"
- if [ ! -d /home/"$user"/git-repositories/"$_domain.git" ]; then
- echo "/home/$user/git-repositories/$_domain.git does not exists !"
- exit
- fi
- apt-get install webhook
- # git bare repos remote
- git --git-dir=/home/"$user"/git-repositories/"$_domain.git" remote add origin "$_remote"
- # hook deploy script
- cp -f "$_assets"/webhook-deploy.sh /home/"$user"/webhook-deploy-"$_id".sh
- sed -i -r "s/DOMAIN/$_domain/g" /home/"$user"/webhook-deploy-"$_id".sh
- sed -i -r "s/USER/$user/g" /home/"$user"/webhook-deploy-"$_id".sh
- chowm $user:$user /home/"$user"/webhook-deploy-"$_id".sh
- chmod +x /home/"$user"/webhook-deploy-"$_id".sh
- # remove git bare repos hook
- mv /home/"$user"/git-repositories/"$_domain".git/hooks/post-receive /home/"$user"/git-repositories/"$_domain".git/hooks/post-receive.back
- # webhook conf
- touch /etc/webhooks.conf
- echo "
- - id: deploy_app_$_id
- execute-command: /home/$user/webhook-deploy-$_id.sh
- command-working-directory: /home/$user/
- " >> /etc/webhooks.conf
- # webhook service
- cp -f "$_assets"/webhook.service /etc/systemd/system/webhook.service
- systemctl enable webhook
- systemctl start webhook
- systemctl restart webhook
- # systemctl reload webhook
- ufw allow 9000
- echo "webhook done"
- echo "you can configure your webhook trigger with the following url :"
- echo "http://$_domain:9000/hooks/deploy_app_$_id"
|