2012-03-08 11 views
5

i creare lo spoglio repo @come controllare la proprietà dei file trasferiti automaticamente su un repository di destinazione git tramite hook di commit?

/srv/repos/test 

i impostare la proprietà a wwwrun: www con SUID + bit GUID impostato

chown -R wwwrun:www /srv/repos/hub 
chmod ug+s   /srv/repos/hub 
ls -ald /srv/repos/test 
    drwsrws---+ 10 wwwrun www 4.0K Mar 7 21:28 /srv/repos/hub/ 

i clonato il repository per un webroot e cambiato la sua proprietà,

git clone /srv/repos/hub /srv/www/siteA 
chown -R wwwrun:www  /srv/www/siteA 

per comodità, definisco un telecomando

cd /srv/www/siteA 
git remote add HUB /srv/repos/hub 

quindi creare post-commit e post-aggiornamento ganci per mantenere le cose in sincronia,

vi /srv/www/siteA/.git/hooks/post-commit 
    #!/bin/sh 
    git push HUB 

vi /srv/repos/hub/hooks/post-update 
    #!/bin/sh 
    cd /srv/www/siteA || exit 
    unset GIT_DIR 
    git pull HUB master 
    exec git-update-server-info 

come il mio utente normale, ho checkout HUB

whoami 
    locuse 
cd ~ 
git clone /srv/repos/hub WORK 
ls -ald WORK 
    drwxr-xr-x 10 locuse users 4.0K Mar 7 21:44 WORK/ 

fare un cambiamento, si impegnano e spingere,

cd WORK 
touch touch_file 
ls -al touch_file 
    -rw-r--r-- 1 locuse users 0 Mar 7 21:44 touch_file 
git add -A 
git commit -m "add test" 
git push 

poi controllo per vedere che il gancio sparato e l'aggiornamento è stato spinto al webroot,

ls -al /srv/www/siteA/touch_file 
    -rw-rw----+ 1 locuse www 0 Mar 7 21:45 /srv/www/siteA/touch_file 

il file è lì - come previsto.

ma, non è la proprietà dell'utente che voglio, vale a dire utente = 'locuse' non utente = 'wwwrun'.

in questo specifico caso d'uso, qual è il modo giusto per fare in modo che io, invece, avrei fine-up sempre automaticamente,

ls -al /srv/www/siteA/touch_file 
    -rw-rw----+ 1 wwwrun www 0 Mar 7 21:45 /srv/www/siteA/touch_file 

? Ad esempio, tutto viene sempre promosso su/srv/www/siteA solo come wwwrun: www.

qualcosa in un gancio, sto indovinando?

So che potrei aggiungere

chown -R wwwrun:www /srv/www/siteA 

al post-commit hook, che funziona bene per un piccolo albero, ma torbiere in ogni commit/aggiornamento verso il basso se si tratta di grandi dimensioni (che sarà).

forse se potessi in modo efficiente chown solo il commit corrente ...?

+0

Si prega di inserire la soluzione in una risposta. A tempo debito puoi accettare la risposta. –

+0

al momento in cui l'ho modificato non mi permetteva di farlo, richiedendo di aspettare altre 6 ore ... –

risposta

7

questo funziona,

vi /srv/repos/hub/hooks/post-update 
    #!/bin/sh 
    cd /srv/www/siteA || exit 
    unset GIT_DIR 
- git pull HUB master 
+ git fetch HUB master 
+ files=`git diff ..FETCH_HEAD --name-only --diff-filter=ACMRTUXB` 
+ git merge FETCH_HEAD 
+ for file in $files 
+ do 
+  sudo chown wwwrun:www $file 
+ done 
    exec git-update-server-info 

dirigenti chown su solo i file identificati come nel commit set - piccole & veloce.

2

Avevo circa lo stesso problema e dopo alcune ricerche ho scoperto che non c'è una soluzione se si aggiorna il repository (HUB) nudo tramite SSH, perché gli hook vengono eseguiti dall'utente che si è connesso al repository HUB (in il nostro caso git). Se si vuole evitare di dare il permesso chown/chmod a git user (problema di sicurezza), l'unica soluzione è creare un cron job per mantenere aggiornato il repository. Ho fatto questo script (il mio primo script bash) modificando la precedente e l'aggiunta anche un'email di notifica aggiornata di successo:

#!/bin/sh 
# web repository directory 
REPO_DIR="/srv/www/siteA" 
# remote repository 
REMOTE_REPO="HUB" 
# public branch of the remote repository (only this branch well be public accessible trough the web server) 
REMOTE_REPO_BRANCH="master" 
# email address where receive notification 
EMAIL_TO="[email protected]" 
# sender email address and name 
SENDER_ADDR="[email protected]" 
SENDER_NAME="sender name" 
# tmp file that contain email body 
TMP_MSG_FILE="emailmessage.txt" 

cd $REPO_DIR || exit 
unset GIT_DIR 
echo "fetching changes..." 
git fetch $REMOTE_REPO $REMOTE_REPO_BRANCH 
files=`git diff ..FETCH_HEAD --name-only --diff-filter=ACDMRTUXB` 
if [[ -n $files ]]; then 
echo "changes found for the following file:" 
echo ${files[*]} 
echo "merging changes" 
git merge FETCH_HEAD 
echo "sending email" 
SUBJECT="GIT: Web Directory Updated" 
TMP_MSG_FILE="emailmessage.txt" 
echo "following file are been updated/created/deleted: "> $TMP_MSG_FILE 
echo ${files[*]} >>$TMP_MSG_FILE 
echo "Working directory:" >>$TMP_MSG_FILE 
echo "$repo_dir" >>$TMP_MSG_FILE 
mutt -e "unmy_hdr from; my_hdr From: $SENDER_ADDR" -e "set realname='$SENDER_NAME' " -s "$SUBJECT" $EMAIL_TO < $TMP_MSG_FILE 
else 
echo "no changes found" 
fi 
Problemi correlati