git.lirion.de

Of git, get, and gud

summaryrefslogtreecommitdiffstats
path: root/scripts/config_version-lirion.sh
blob: 8f0d1e1e9a99d80e5bc806c955e9148847e7fc12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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