b1873: Alter post-receive and sync-repo scripts to the new git-branch approach for httpd config updates.

This commit is contained in:
Thomas Stokes
2024-03-14 16:39:39 +01:00
parent 6a8869fdd3
commit 605971239c
3 changed files with 49 additions and 14 deletions
@@ -5,22 +5,55 @@
# after the merge completes. The user, in which the bare repo and hook are installed, must have aws credentials, that
# don't need mfa. (These may need to be installed manually -- within the correct user -- if starting from a clean instance.)
TAG="ReverseProxy"
DISPOSABLE_TAG="DisposableProxy"
CENTRAL_TAG="CentralReverseProxy"
DIR="/etc/httpd"
COMMAND="sudo service httpd reload"
MAIN_BRANCH_NAME="main"
DISPOSABLE_BRANCH_NAME="disposable"
CENTRAL_BRANCH_NAME="central"
CHECKED_OUT_WORKSPACE_NAME="checked-out" # Assumes it is in the root user.
sync_repo() {
#$1 tag key to use
#$2 branch to check out
# Gets all public ips for the instances with the chosen tag and iterates over the IPs.
for IP in $(aws ec2 describe-instances --filters Name=tag-key,Values="${1}" | jq -r '.Reservations[].Instances[].PublicIpAddress'); do
# strictHostKey... means no authenticity check. The sync-repo... script must be installed in the root user's home.
echo $IP
ssh -o "StrictHostKeyChecking=no" root@${IP} "cd ~ && sync-repo-and-execute-cmd.sh '${DIR}' '${COMMAND}' '${2}'";
done;
}
merge_main_into_branch() {
#$1 branch name. Assumes you are in the git repo.
git checkout "${1}"
git pull
echo "merging ${MAIN_NAME} into ${1}"
git merge ${MAIN_NAME}
git push
}
while read oldrev newrev refname; do # These vars are passed on stdin to this hook.
# Check if the update is on a branch
if [[ $refname == "refs/heads/"* ]]; then
branch_name=$(echo $refname | sed "s|refs/heads/\(.*\)$|\1|")
echo "Update on branch: $branch_name"
if [[ "$branch_name" == "$MAIN_BRANCH_NAME" ]]; then
# Gets all public ips for the instances with the chosen tag and iterates over the IPs.
for IP in $(aws ec2 describe-instances --filters Name=tag-key,Values="${TAG}" | jq -r '.Reservations[].Instances[].PublicIpAddress'); do
# strictHostKey... means no authenticity check. The sync-repo... script must be installed in the root user's home.
echo "Connecting to $IP"
ssh -o "StrictHostKeyChecking=no" root@${IP} "cd ~ && /usr/local/bin/sync-repo-and-execute-cmd.sh '${DIR}' '${COMMAND}' ";
done
if [[ "$branch_name" == "$DISPOSABLE_BRANCH_NAME" ]]; then
echo "All the disposables pull the disposable branch"
sync_repo "${DISPOSABLE_TAG}" "${DISPOSABLE_BRANCH_NAME}"
elif [[ "$branch_name" == "$CENTRAL_BRANCH_NAME" ]]; then
echo "The central pulls the central branch"
sync_repo "${CENTRAL_TAG}" "${CENTRAL_BRANCH_NAME}"
elif [[ "$branch_name" == "$MAIN_BRANCH_NAME" ]]; then
cd ~
unset GIT_DIR
cd ${CHECKED_OUT_WORKSPACE_NAME}
# get the latest main
git checkout ${MAIN_NAME}
git pull
# get the latest diposable branch and merge in changes
merge_main_into_branch "$DISPOSABLE_NAME"
# get the latest central branch and merge in changes
merge_main_into_branch "$CENTRAL_NAME"
git checkout ${MAIN_NAME}
fi
fi
done
@@ -1,7 +1,7 @@
#!/bin/bash
# Purpose: This script goes to a given git dir (eg. httpd); fetches any new commits to
# the repo; and - if new commits are found - merges them into the branch and runs a command.
# Purpose: This script goes to a given git dir (eg. httpd); checks out a given branch; fetches any new commits to
# that given branch; and - if new commits are found - merges them into the branch and runs a command.
if [ $# -eq 0 ]; then
echo "$0 PATH_TO_GIT_REPO COMMAND_TO_RUN_ON_COMPLETION_IN_REPO"
@@ -14,7 +14,9 @@ fi
GIT_PATH=$1
COMMAND_ON_COMPLETION=$2
GIT_BRANCH=$3
cd ${GIT_PATH}
git checkout ${GIT_BRANCH} >/dev/null
# Rev-parse gets the commit hash of given reference.
CURRENT_HEAD=$(git rev-parse HEAD)
GIT_SSH_COMMAND="ssh -o StrictHostKeyChecking=no" git fetch
@@ -22,17 +24,17 @@ if [[ "$?" -ne 0 ]]; then
echo "Error encountered: issue with fetching or there is no repo."
exit 1
fi
if [[ $CURRENT_HEAD != $(git rev-parse origin/main) ]] # Checks if there are new commits
if [[ $CURRENT_HEAD != "$(git rev-parse origin/${GIT_BRANCH})" ]] # Checks if there are new commits
then
logger -t httpd "Changes found; merging now"
cd ${GIT_PATH} && git merge origin/main
cd ${GIT_PATH} && git merge origin/${GIT_BRANCH}
if [[ $? -eq 0 ]]; then
logger -t httpdMerge "Merge succeeded: different files edited."
else
logger -t httpdMerge "First merge unsuccessful: same file modified."
git merge --abort # Returns to pre-merge state.
git stash
git merge origin/main # This should be a fast-forward merge.
git merge origin/${GIT_BRANCH} # This should be a fast-forward merge.
git stash apply # Keeps stash on top of stack, in case the apply fails.
if [[ $? -eq 0 ]]; then
logger -t httpdMerge "Second merge success: merge of httpd remote to local successful, and previous working directory changes restored."
@@ -11,4 +11,4 @@ WantedBy=httpd.service
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/usr/local/bin/sync-repo-and-execute-cmd.sh "/etc/httpd" "echo synced"
ExecStart=/usr/local/bin/sync-repo-and-execute-cmd.sh "/etc/httpd" "echo synced" disposable