diff --git a/add-repo-user.sh b/add-repo-user.sh index 768f3cc..79551fd 100755 --- a/add-repo-user.sh +++ b/add-repo-user.sh @@ -15,11 +15,23 @@ my_www=${my_www:-"/srv/www"} echo "" set -x +# user sudo adduser --disabled-password --gecos "Git Site Deploys" --home "$my_repos" --no-create-home $my_user >/dev/null + +# ssh sudo mkdir -p "$my_repos/.ssh" "$my_www" sudo chmod 0700 "$my_repos/.ssh" sudo ssh-keygen -t rsa -N "" -C "$my_user@$(hostname)" -f "$my_repos/.ssh/id_rsa" sudo install -m 0600 /dev/null "$my_repos/.ssh/authorized_keys" + +# git templates +sudo rsync -a git-templates/project/ "$my_repos/git-template/" +sudo install -m 0644 /dev/null "$my_repos/.gitconfig" +my_config='[init] + templatedir = '"$my_repos/git-template" +sudo bash -c "echo '$my_config' >> '$my_repos/.gitconfig'" + +# fix permissions sudo chown -R $my_user:$my_user "$my_repos" "$my_www" set +x diff --git a/git-templates/project-test.sh b/git-templates/project-test.sh new file mode 100644 index 0000000..ba31f97 --- /dev/null +++ b/git-templates/project-test.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +rsync -a project/ project-test/ +my_input='old1 new1 name1 +000 fff refs/heads/master +' +echo "$my_input" | ./project-test/hooks/pre-receive +./project-test/hooks/update ref_name old_rev new_rev +echo "$my_input" | ./project-test/hooks/post-receive diff --git a/git-templates/project-test/hooks/post-receive.d/03-fail b/git-templates/project-test/hooks/post-receive.d/03-fail new file mode 100755 index 0000000..9a97fdd --- /dev/null +++ b/git-templates/project-test/hooks/post-receive.d/03-fail @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -e +set -u + +my_input=$(cat) +echo "$my_input" | while read my_line; do + exit 159 +done diff --git a/git-templates/project-test/hooks/post-receive.d/04-fail b/git-templates/project-test/hooks/post-receive.d/04-fail new file mode 100755 index 0000000..bd51cd5 --- /dev/null +++ b/git-templates/project-test/hooks/post-receive.d/04-fail @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -e +set -u + +my_input=$(cat) +echo "$my_input" | while read my_line; do + exit 160 +done diff --git a/git-templates/project-test/hooks/post-receive.d/98-succeed b/git-templates/project-test/hooks/post-receive.d/98-succeed new file mode 100755 index 0000000..3ab2f5d --- /dev/null +++ b/git-templates/project-test/hooks/post-receive.d/98-succeed @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -u +set -e + +my_input=$(cat) +echo "$my_input" | while read my_line; do + echo "Success $my_line" +done diff --git a/git-templates/project-test/hooks/pre-receive.d/02-pass b/git-templates/project-test/hooks/pre-receive.d/02-pass new file mode 100755 index 0000000..565aff1 --- /dev/null +++ b/git-templates/project-test/hooks/pre-receive.d/02-pass @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -e +set -u + +my_input=$(cat) +echo "$my_input" | while read my_line; do + echo "$my_line" +done diff --git a/git-templates/project-test/hooks/pre-receive.d/07-fail b/git-templates/project-test/hooks/pre-receive.d/07-fail new file mode 100755 index 0000000..bd51cd5 --- /dev/null +++ b/git-templates/project-test/hooks/pre-receive.d/07-fail @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -e +set -u + +my_input=$(cat) +echo "$my_input" | while read my_line; do + exit 160 +done diff --git a/git-templates/project-test/hooks/update.d/01-hello b/git-templates/project-test/hooks/update.d/01-hello new file mode 100755 index 0000000..f1af382 --- /dev/null +++ b/git-templates/project-test/hooks/update.d/01-hello @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +echo "Hello $1 $2 $3" diff --git a/git-templates/project-test/hooks/update.d/50-fail b/git-templates/project-test/hooks/update.d/50-fail new file mode 100755 index 0000000..7412f43 --- /dev/null +++ b/git-templates/project-test/hooks/update.d/50-fail @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +echo "Fail $1 $2 $3" +exit 158 diff --git a/git-templates/project-test/hooks/update.d/99-goodbye b/git-templates/project-test/hooks/update.d/99-goodbye new file mode 100755 index 0000000..d69540a --- /dev/null +++ b/git-templates/project-test/hooks/update.d/99-goodbye @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +echo "Goodbye $1 $2 $3" diff --git a/git-templates/project/hooks/post-receive b/git-templates/project/hooks/post-receive new file mode 100755 index 0000000..b3c2f87 --- /dev/null +++ b/git-templates/project/hooks/post-receive @@ -0,0 +1,36 @@ +#!/usr/bin/env bash +set -u +set -e + +# post-receive +# this accepts multiple lines of input at a time +# it defers exit on failure until after all scripts have run + +my_input=$(cat) +my_dir=$(dirname $0) +my_success="true" + +for my_hook in ${my_dir}/post-receive.d/*; do + + # Let the user know if the hook isn't executable, but don't bail + if [ ! -x "${my_hook}" ] || [ ! -f "${my_hook}" ]; then + >&2 echo "${my_hook} is not (or does not point to) an executable file" + fi + + # Allow the script to fail so we can bubble the exit code and ensure an error message + set +e + echo "${my_input}" | ("${my_hook}") + my_exit_code=$? + set -e + + # Don't bail on script failure + if [ "0" != "${my_exit_code}" ]; then + my_success="" + >&2 echo "${my_hook} failed with exit code ${my_exit_code}" + fi +done + +if [ -z "${my_success}" ]; then + echo "some post-receives failed" + exit 156 +fi diff --git a/git-templates/project/hooks/post-receive.d/.gitkeep b/git-templates/project/hooks/post-receive.d/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/git-templates/project/hooks/pre-receive b/git-templates/project/hooks/pre-receive new file mode 100755 index 0000000..9347e78 --- /dev/null +++ b/git-templates/project/hooks/pre-receive @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +set -u +set -e + +# pre-receive +# this accepts multiple lines of input at a time +# it fails at the first of any child script failing + +my_input=$(cat) +my_dir=$(dirname $0) + +for my_hook in ${my_dir}/pre-receive.d/*; do + + # Bail and let the user know if the hook isn't executable + if [ ! -x "${my_hook}" ] || [ ! -f "${my_hook}" ]; then + >&2 echo "${my_hook} is not (or does not point to) an executable file" + exit 155 + fi + + # Allow the script to fail so we can bubble the exit code and ensure an error message + # and subscript it so that any set -e won't cause this to fail + set +e + + # foo="$(echo bar; exit 3)"; echo $?; echo $foo + #my_output="$(echo "${my_input}" | "${my_hook}")" + echo "${my_input}" | ("${my_hook}") + my_exit_code=$? + set -e + + #echo my_output: $my_output + + # Bail on the first script failure + if [ "0" != "${my_exit_code}" ]; then + >&2 echo "${my_hook} failed with exit code ${my_exit_code}" + exit ${my_exit_code} + fi +done diff --git a/git-templates/project/hooks/pre-receive.d/.gitkeep b/git-templates/project/hooks/pre-receive.d/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/git-templates/project/hooks/update b/git-templates/project/hooks/update new file mode 100755 index 0000000..1e94417 --- /dev/null +++ b/git-templates/project/hooks/update @@ -0,0 +1,31 @@ +#!/usr/bin/env bash +set -u +set -e + +# update script +# this accepts a single line up input in three args +# it fails at the first of any child script failing (and that branch fails to update) + +my_dir=$(dirname $0) +my_exit_code="" + +for my_hook in ${my_dir}/update.d/*; do + + # Bail and let the user know if the hook isn't executable + if [ ! -x "${my_hook}" ] || [ ! -f "${my_hook}" ]; then + >&2 echo "${my_hook} is not (or does not point to) an executable file" + exit 155 + fi + + # Allow the script to fail so we can bubble the exit code and ensure an error message + set +e + ("${my_hook}" $1 $2 $3) + my_exit_code=$? + set -e + + # Bail on the first script failure + if [ "0" != "${my_exit_code}" ]; then + >&2 echo "${my_hook} failed with exit code ${my_exit_code}" + exit ${my_exit_code} + fi +done diff --git a/git-templates/project/hooks/update.d/.gitkeep b/git-templates/project/hooks/update.d/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/git-templates/user/.gitconfig b/git-templates/user/.gitconfig new file mode 100644 index 0000000..e69de29 diff --git a/git-templates/user/gitconfig.symlink b/git-templates/user/gitconfig.symlink new file mode 120000 index 0000000..e717d37 --- /dev/null +++ b/git-templates/user/gitconfig.symlink @@ -0,0 +1 @@ +.gitconfig \ No newline at end of file