2016-11-28 18:04:05 +00:00
|
|
|
#!/opt/puppetlabs/puppet/bin/ruby
|
|
|
|
|
|
|
|
require 'json'
|
|
|
|
|
|
|
|
param_environment_path = ARGV[0]
|
|
|
|
param_environment = ARGV[1]
|
2015-08-25 18:47:19 +00:00
|
|
|
|
2016-11-28 18:04:05 +00:00
|
|
|
if (param_environment_path && param_environment)
|
|
|
|
# Use the parameters, if passed.
|
|
|
|
environment_path = File.join(param_environment_path, param_environment)
|
|
|
|
else
|
|
|
|
# Or use the parent directory.
|
|
|
|
environment_path = File.dirname(File.expand_path(File.dirname(__FILE__)))
|
|
|
|
end
|
2015-08-25 18:47:19 +00:00
|
|
|
|
2016-11-28 18:04:05 +00:00
|
|
|
environment_r10k_deploy_file = File.join(environment_path, '.r10k-deploy.json')
|
|
|
|
environment_dot_git = File.join(environment_path, '.git')
|
|
|
|
pe_version_file = '/opt/puppetlabs/server/pe_version'
|
|
|
|
environment_version = nil
|
2015-08-25 18:47:19 +00:00
|
|
|
|
2016-11-28 18:04:05 +00:00
|
|
|
if (File.file?(environment_r10k_deploy_file))
|
|
|
|
# Use the r10k deploy file, if using Code Manager.
|
|
|
|
environment_version = JSON.parse(File.read(environment_r10k_deploy_file))['signature']
|
2015-08-25 18:47:19 +00:00
|
|
|
|
2016-11-28 18:04:05 +00:00
|
|
|
elsif (File.file?(pe_version_file))
|
|
|
|
# Or use rugged, if using Puppet Enterprise.
|
|
|
|
begin
|
|
|
|
require 'rugged'
|
|
|
|
rescue LoadError => e
|
|
|
|
# It's okay.
|
|
|
|
else
|
|
|
|
environment_version = Rugged::Repository.discover(environment_path).head.target_id
|
|
|
|
end
|
2015-08-25 18:47:19 +00:00
|
|
|
|
2016-11-28 18:04:05 +00:00
|
|
|
elsif (File.exist?(environment_dot_git))
|
|
|
|
# Or use git, just use git.
|
|
|
|
if (File.file?(environment_dot_git))
|
|
|
|
# It's a file, created via --separate-git-dir.
|
|
|
|
git_dir = File.read(environment_dot_git).split(': ')[1]
|
|
|
|
else
|
|
|
|
git_dir = environment_dot_git
|
|
|
|
end
|
|
|
|
git_rev_parse_head = %x( /usr/bin/git --git-dir #{git_dir} rev-parse HEAD 2>/dev/null )
|
|
|
|
environment_version = git_rev_parse_head if ($?.exitstatus == 0)
|
2015-08-25 18:47:19 +00:00
|
|
|
|
2015-11-20 14:12:47 +00:00
|
|
|
end
|
2016-11-28 18:04:05 +00:00
|
|
|
|
|
|
|
# Return the version, if found, or the current timestamp.
|
|
|
|
puts (environment_version || Time.now.to_i)
|