diff --git a/environment.conf b/environment.conf index 5ba929b..4dd0b85 100644 --- a/environment.conf +++ b/environment.conf @@ -1,2 +1,2 @@ modulepath = modules:site:$basemodulepath -config_version = 'scripts/config_version.sh $environmentpath $environment' +config_version = 'scripts/config_version.rb $environmentpath $environment' diff --git a/scripts/code_manager_config_version.rb b/scripts/code_manager_config_version.rb deleted file mode 100755 index 2c9cb62..0000000 --- a/scripts/code_manager_config_version.rb +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/env ruby -require 'json' - -environmentpath = ARGV[0] -environment = ARGV[1] - -r10k_deploy_file_path = File.join(environmentpath, environment, '.r10k-deploy.json') - -#output the sha1 from the control-repo -puts JSON.parse(File.read(r10k_deploy_file_path))['signature'] diff --git a/scripts/config_version.rb b/scripts/config_version.rb index 3016a27..2142647 100755 --- a/scripts/config_version.rb +++ b/scripts/config_version.rb @@ -1,24 +1,49 @@ -#!/usr/bin/env ruby -begin - require 'rugged' -rescue LoadError => e - t = Time.new - puts t.to_i +#!/opt/puppetlabs/puppet/bin/ruby + +require 'json' + +param_environment_path = ARGV[0] +param_environment = ARGV[1] + +if (param_environment_path && param_environment) + # Use the parameters, if passed. + environment_path = File.join(param_environment_path, param_environment) else - - environmentpath = ARGV[0] - environment = ARGV[1] - - repo = Rugged::Repository.discover(File.join(environmentpath, environment)) - head = repo.head - - #sha1 hash of the newest commit - head_sha = head.target_id - - #the commit message associated the newest commit - commit = repo.lookup(head_sha) - - #add something to find the remote url - - puts head_sha + # Or use the parent directory. + environment_path = File.dirname(File.expand_path(File.dirname(__FILE__))) end + +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 + +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'] + +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 + +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) + +end + +# Return the version, if found, or the current timestamp. +puts (environment_version || Time.now.to_i) diff --git a/scripts/config_version.sh b/scripts/config_version.sh deleted file mode 100755 index bc77213..0000000 --- a/scripts/config_version.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash -if [ -e $1/$2/.r10k-deploy.json ] -then - /opt/puppetlabs/puppet/bin/ruby $1/$2/scripts/code_manager_config_version.rb $1 $2 -elif [ -e /opt/puppetlabs/server/pe_version ] -then - /opt/puppetlabs/puppet/bin/ruby $1/$2/scripts/config_version.rb $1 $2 -else - /usr/bin/git --version > /dev/null 2>&1 && - /usr/bin/git --git-dir $1/$2/.git rev-parse HEAD || - date +%s -fi