From 45dc45f22a8909bf5757d4601dda941b4d232fc8 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 31 May 2019 18:17:28 -0600 Subject: [PATCH] initial commit --- add-origin.sh | 1 + add-repo-user.sh | 31 ++++++++++++++ hooks/post-receive | 11 +++++ hooks/update.format.full | 84 +++++++++++++++++++++++++++++++++++++ hooks/update.format.minimal | 37 ++++++++++++++++ new-deployable-site.sh | 40 ++++++++++++++++++ 6 files changed, 204 insertions(+) create mode 100755 add-origin.sh create mode 100755 add-repo-user.sh create mode 100755 hooks/post-receive create mode 100755 hooks/update.format.full create mode 100755 hooks/update.format.minimal create mode 100755 new-deployable-site.sh diff --git a/add-origin.sh b/add-origin.sh new file mode 100755 index 0000000..53144b2 --- /dev/null +++ b/add-origin.sh @@ -0,0 +1 @@ +git remote add origin www-repos@beta.therootcompany.com:rootprojects.org diff --git a/add-repo-user.sh b/add-repo-user.sh new file mode 100755 index 0000000..d4a76cd --- /dev/null +++ b/add-repo-user.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash +set -e +set -u + +read -p "Repository Username [www-repos]: " my_user +#${my_user:=www-repos} +my_user=${my_user:-"www-repos"} + +read -p "Repository Directory [/srv/$my_user]: " my_repos +my_repos=${my_repos:-"/srv/$my_user"} + +read -p "Deploy Directory [/srv/www]: " my_www +my_www=${my_www:-"/srv/www"} + +echo "" + +set -x +sudo adduser --disabled-password --gecos "Git Site Deploys" --home "$my_repos" --no-create-home $my_user >/dev/null +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" +sudo chown -R $my_user:$my_user "$my_repos" "$my_www" +set +x + +echo "" +echo "" +echo "To enable git deploys from this account you will need to add its public key to your git deployer:" +echo "" +cat "$my_repos/.ssh/id_rsa" +echo "" diff --git a/hooks/post-receive b/hooks/post-receive new file mode 100755 index 0000000..6914471 --- /dev/null +++ b/hooks/post-receive @@ -0,0 +1,11 @@ +#!/usr/bin/env bash +# always deploys from the default (master) branch +mkdir -p '/srv/www/beta.therootcompany.com' +git --work-tree='/srv/www/beta.therootcompany.com' --git-dir='/srv/www-repos/rootprojects.org.git' checkout -f +echo "Deployed master to beta.therootcompany.com" +mkdir -p '/srv/www/beta.rootprojects.org' +git --work-tree='/srv/www/beta.rootprojects.org' --git-dir='/srv/www-repos/rootprojects.org.git' checkout -f +echo "Deployed master to beta.rootprojects.org" +git remote set-url upstream git@git.rootprojects.org:root/rootprojects.org.git >/dev/null +git push -u upstream master --force >/dev/null +git push -u upstream beta --force >/dev/null diff --git a/hooks/update.format.full b/hooks/update.format.full new file mode 100755 index 0000000..9a9fac0 --- /dev/null +++ b/hooks/update.format.full @@ -0,0 +1,84 @@ +#!/usr/bin/env bash +set -u +set -e + +ref_name=$1 +new_rev=$3 + +# Here you can use whatever formatter you like +# and set up the error (or success) output as you wish +format() { + local my_work_tree=$1 + pushd $my_work_tree >/dev/null + + # briefly allow non-zero exit status + set +e + local my_diffs=$(2>&1 /usr/local/bin/prettier './**/*.{js,css,html,json,md}' --list-different | grep -v 'No matching files') + set -e + + # Show a friendly error message to the git user downstream (if there are files to fix) + if [ -n "$my_diffs" ]; then + >&2 echo "" + >&2 echo "Please run prettier on the following files (and double check your .prettierrc):" + echo "$my_diffs" | while read my_word; do + >&2 echo " $my_word" + done + >&2 echo "" + >&2 echo "Example:" + >&2 echo " npm install -g prettier" + >&2 echo " prettier './**/*.{js,css,html,json,md}' --write" + >&2 echo "" + fi + + popd >/dev/null + + # This is the "return" value + echo $my_diffs +} + +# This is all pretty generic - just checking out files to lint/format and running said formatter +case "${ref_name}" in +refs/heads/*) + # match empty commit 0000000000000000000000000000000000000000 + # all deletes should be successful (no files to check against) + if [ "$(expr "${new_rev}" : '0*$')" -ne 0 ]; then + exit 0 + fi + + # We don't want a race condition if multiple people push while a check is in progress + # For this reason we first checkout all of the files in HEAD and the copy over the changed files. + # We also need to remove any deleted files. + # + # ex: /tmp/git-work-tree.abc123xyz + my_work_tree=$(mktemp -d -t git-work-tree.XXXXXXXX) 2>/dev/null + git --work-tree="${my_work_tree}" --git-dir="." checkout -f >/dev/null + my_changes=$(git --work-tree="${my_work_tree}" --git-dir="." diff --name-status HEAD $new_rev) + if [ -n "$(echo "$my_changes" | grep -e "^A")" ]; then + echo "$my_changes" | grep -e "^A" | cut -f 2 | \ + xargs git --work-tree="${my_work_tree}" --git-dir="." checkout $new_rev -- + fi + if [ -n "$(echo "$my_changes" | grep -e "^M")" ]; then + echo "$my_changes" | grep -e "^M" | cut -f 2 | \ + xargs git --work-tree="${my_work_tree}" --git-dir="." checkout $new_rev -- + fi + if [ -n "$(echo "$my_changes" | grep -e "^D")" ]; then + echo "$my_changes" | grep -e "^D" | cut -f 2 | \ + xargs git --work-tree="${my_work_tree}" --git-dir="." rm -rf -- >/dev/null + fi + + # Now we run the formatter, do some cleanup, and error out if needed + my_failure=$(format $my_work_tree) + rm -rf "${my_work_tree}" + if [ -n "$my_failure" ]; then + exit 1 + fi +;; +refs/tags/*) + # allowing tags to be made regardless of formatting (such as old versions) + exit 0 +;; +*) + # allowing all other things (not sure what they are though, TBH) + exit 0 +;; +esac diff --git a/hooks/update.format.minimal b/hooks/update.format.minimal new file mode 100755 index 0000000..be50c34 --- /dev/null +++ b/hooks/update.format.minimal @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +set -e + +ref_name=$1 +new_rev=$3 + +# only check branches, not tags or bare commits +if [ -z $(echo $ref_name | grep "refs/heads/") ]; then + exit 0 +fi + +# don't check empty branches +if [ "$(expr "${new_rev}" : '0*$')" -ne 0 ]; then + exit 0 +fi + +# Checkout a copy of the branch (but also changes HEAD) +my_work_tree=$(mktemp -d -t git-work-tree.XXXXXXXX) 2>/dev/null +git --work-tree="${my_work_tree}" --git-dir="." checkout $new_rev -f >/dev/null + +# Do the formatter check +echo "Checking code formatting..." +pushd ${my_work_tree} >/dev/null +prettier './**/*.{js,css,html,json,md}' --list-different +my_status=$? +popd >/dev/null + +# reset HEAD to master, and cleanup +git --work-tree="${my_work_tree}" --git-dir="." checkout master -f >/dev/null +rm -rf "${my_work_tree}" + +# handle error, if any +if [ "0" != "$my_status" ]; then + echo "Please format the files listed above and re-commit." + echo "(and don't forget your .prettierrc, if you have one)" + exit 1 +fi diff --git a/new-deployable-site.sh b/new-deployable-site.sh new file mode 100755 index 0000000..96b86bc --- /dev/null +++ b/new-deployable-site.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +set -u +set -e + +my_user_tmp=$(grep -r "Git Site Deploys" /etc/passwd | cut -d':' -f1) +read -p "Repository Username [$my_user_tmp]: " my_user +my_user=${my_user:-"$my_user_tmp"} + +while [ -z "${my_site:-}" ]; do + read -p "Site name (ex: example.com): " my_site +done + +my_repo_tmp=$(eval echo "~$my_user") +read -p "Repository [$my_repo_tmp/${my_site}.git]: " my_repos +my_repo=${my_repos:-"$my_repo_tmp/${my_site}.git"} + +read -p "Deploy to [/srv/www/beta.therootcompany.com/$my_site]: " my_deploy +my_deploy=${my_www:-"/srv/www/beta.therootcompany.com/$my_site"} + +echo "" +echo "" + +set -x +sudo git init --bare --shared=group "${my_repo}" +sudo bash -c "cat << EOF > ${my_repo}/hooks/post-receive +#!/usr/bin/env bash +mkdir -p '${my_deploy}' +# always deploys from the default (master) branch +git --work-tree='${my_deploy}' --git-dir='${my_repo}' checkout -f +echo Deployed branch 'master' to '${my_deploy}' +EOF +" +sudo chmod a+x "${my_repo}/hooks/post-receive" +sudo chown -R ${my_user}:${my_user} "$my_repo" +set +x + +echo "" +echo "Authorized users can push to this repo by adding it as a remote. Example:" +echo " git remote add origin ${my_user}@$(hostname)${my_repo}" +echo ""