git.lirion.de

Of git, get, and gud

summaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/config_version-lirion.rb73
-rwxr-xr-xscripts/config_version-lirion.sh71
2 files changed, 144 insertions, 0 deletions
diff --git a/scripts/config_version-lirion.rb b/scripts/config_version-lirion.rb
new file mode 100755
index 0000000..6edfe09
--- /dev/null
+++ b/scripts/config_version-lirion.rb
@@ -0,0 +1,73 @@
+#!/usr/bin/ruby
+# frozen_string_literal: true
+
+begin
+ require 'rugged'
+ require 'socket'
+rescue LoadError
+ t = Time.new
+ puts t.to_i
+else
+ environment = ARGV[0]
+ environmentpaths = if (defined? ARGV[1]) && !ARGV[1].nil?
+ ARGV[1]
+ else
+ [
+ # 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/environments',
+ # PE originally staged here - if you stage, too, check this directory.
+ # If it doesn't exist, it will be ignored.
+ '/etc/puppetlabs/code-staging'
+ ]
+ end
+ environmentpaths = Array(environmentpaths)
+ # Our return string.
+ myreturn = nil
+ # Loop through all path possibilities, on first git occurrence: assemble output and break out
+ # dirs_exist: Was any of the paths an actual directory on the Puppet server?
+ dirs_exist = false
+ environmentpaths.each do |checkpath|
+ next unless Dir.exist?(File.join(checkpath, environment))
+
+ dirs_exist = true
+ begin
+ # Get the path to the environment being compiled.
+ repo = Rugged::Repository.discover(File.join(checkpath, environment))
+ head = repo.head
+
+ # Leaving this - maybe we want to come back to it.
+ # # Get the hostname of the Puppet master compiling the catalog.
+ # # Sometimes the hostname is the fqdn, so we'll take the first segment.
+ # compiling_master = Socket.gethostname.split('.').first
+
+ # First 6 characters of the sha1 hash of the newest commit.
+ commit_id = head.target_id[0..5]
+
+ # Commit message:
+ commit_msg = head.target.message.delete("\n")
+
+ # Show the compiling master, environment name, and commit ID.
+ myreturn = "#{environment} -- #{commit_id} - #{commit_msg}"
+
+ # We have the message we want? Break the loop, we don't care about other
+ # folders anymore.
+ break
+ rescue Rugged::RepositoryError
+ # Folder is existing but not a git repo? Maybe check the next folder structure, then.
+ next
+ rescue Rugged::ConfigError
+ myreturn = "Environment: #{environment} (repository not usable for catalog message)"
+ break
+ end
+ end
+ if dirs_exist
+ "Environment: #{environment} (no git directory in code deployments)" if myreturn.nil?
+ else
+ "Environment: #{environment} (environment path(s) not existing)"
+ end
+ puts myreturn
+end
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