git.lirion.de

Of git, get, and gud

summaryrefslogtreecommitdiffstats
path: root/scripts/config_version-lirion.sh
diff options
context:
space:
mode:
authormail_redacted_for_web 2025-08-15 07:21:01 +0200
committermail_redacted_for_web 2025-08-15 07:21:01 +0200
commitd2221daeb81ad9f627d2499fa7817ea1cf3379c5 (patch)
treef815df477601390809f06204ab50b98626f02325 /scripts/config_version-lirion.sh
parent565607dc6b2cbab99e0c9a66ea361757678b5fb7 (diff)
downloadcontrol-repo-template-d2221daeb81ad9f627d2499fa7817ea1cf3379c5.tar.bz2
+ config_version implementations with commit messageconfig_version_lirion
Diffstat (limited to 'scripts/config_version-lirion.sh')
-rwxr-xr-xscripts/config_version-lirion.sh71
1 files changed, 71 insertions, 0 deletions
diff --git a/scripts/config_version-lirion.sh b/scripts/config_version-lirion.sh
new file mode 100755
index 0000000..8f0d1e1
--- /dev/null
+++ b/scripts/config_version-lirion.sh
@@ -0,0 +1,71 @@
+#!/usr/bin/env bash
+# Little helper script to display latest commit as "Applying configuration version xxx" message
+# instead of the bare "servername-branch-commit_id" message (which is also a great idea,
+# we just started differently :-) )
+
+# Syntax is: `$script environment`
+# ...or `$script environment environmentpath`
+# The former tries to find the standard paths for Puppet and PE, the latter
+# lets you specify the path yourself.
+
+if [ -z "$1" ]; then
+ printf 'No env. USAGE: %b ENVIRONMENT [ENVIRONMENTS_PATH]\n' "$(basename "$0")" >&2
+ exit 101
+fi
+if [ -n "$2" ]; then
+ CODEDIRS="$2"
+else
+ CODEDIRS=(
+ # The actual code directory. In case of PE, you may want to adapt -
+ # since PE 2023.x, they have introduced an internal git deployer between
+ # the code pulled from git repos and what is then deployed to the agents,
+ # for reasons unknown and for raised complexity.
+ # If the directory does not exist, this will be ignored.
+ '/etc/puppetlabs/code'
+ # PE originally staged here - if you stage, too, check this directory.
+ # If it doesn't exist, it will be ignored.
+ '/etc/puppetlabs/code-staging'
+ )
+fi
+# I.e.: if none of the above exist or yield a (valid) .git folder underneath
+# $VARIABLE/.git, AND/OR we have nop git binary on the resp. puppet compiler,
+# we will have no version+commitmsg statement in agent runs.
+# ...
+# ...yay, code!
+# Check if /usr/bin/git exists, otherwise, yield a generic message.
+if [ -x /usr/bin/git ]; then
+ # iterate over codestagedir and codedir. First appearance with
+ # a .git folder underneath = break out of loop.
+ for cdir in "${CODEDIRS[@]}"; do
+ # Check if directory exists.
+ if [ -d "$cdir" ]; then
+ # Check if ./environments/ENVNAME/.git exists.
+ # If so, we have a winner and break the loop.
+ if [ -d "${cdir}/environments/${1}/.git" ]; then
+ ENVGITDIR="${cdir}/environments/${1}/.git"
+ break
+ # Check if ./ENVNAME/.git exists. If so, ...
+ elif [ -d "${cdir}/${1}/.git" ]; then
+ ENVGITDIR="${cdir}/${1}/.git"
+ break
+ fi
+ fi
+ done
+ # Did all of the above yield a value in $ENVGITDIR? If so: have a nice line
+ # with last committer and commit. If not: Standard output with env. name.
+ if [ -n "$ENVGITDIR" ]; then
+ if ! mycomm="$(/usr/bin/git --git-dir "$ENVGITDIR" log --pretty=format:'%h - %an, %ad : %s' -1 2>/dev/null)"; then
+ printf 'Environment: %b (.git content invalid inside deployed environment)\n' "$1"
+ else
+ printf '%b -- %b\n' "$1" "$mycomm"
+ fi
+ else
+ # In case the code deployments have no git folder.
+ # TODO: Maybe implement a git hook that places a file with the commit message somewhere where
+ # we can read it?
+ printf 'Environment: %b (no git directory in code deployments)\n' "$1"
+ fi
+else
+ printf 'Environment: %b (no git binary on server)\n' "$1"
+fi
+exit 0