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
72
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
|