From ffc1769d2e9ab6057d44660e2ef4430a70652836 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Tarti=C3=A8re?= Date: Sat, 7 Apr 2018 11:13:41 +0200 Subject: [PATCH 1/6] Improve portability - Fix shebang: `bash` is not always in `/bin/`, and since the script does not have bashism, rely on `sh` which is always in `/bin/`; - Use `/opt/puppetlabs/puppet/bin/ruby` if this file exist and is executable, otherwise use `ruby` from $PATH; - Use `code_manager_config_version.rb` if `.r10k-deploy.json` is found, and `config_version.rb` in all other cases. --- scripts/config_version.sh | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/scripts/config_version.sh b/scripts/config_version.sh index 8dd8086..0378952 100755 --- a/scripts/config_version.sh +++ b/scripts/config_version.sh @@ -1,12 +1,14 @@ -#!/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 +#!/bin/sh + +ruby=ruby +script="$1/$2/scripts/config_version.rb" + +if [ -x /opt/puppetlabs/puppet/bin/ruby ]; then + ruby=/opt/puppetlabs/puppet/bin/ruby fi + +if [ -e $1/$2/.r10k-deploy.json ]; then + script="$1/$2/scripts/code_manager_config_version.rb" +fi + +"${ruby}" "${script}" "$1" "$2" From 61d0147e494521aff0c4d80abda52abde774c27e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Tarti=C3=A8re?= Date: Sat, 7 Apr 2018 11:18:26 +0200 Subject: [PATCH 2/6] Remove execute permission and shebangs The appropriate ruby interpreter is determined by the config_version.sh shell script which explicitely use it to start these ruby scripts. Removing the execute bit ensure users will not run these script with the wrong Ruby version. --- scripts/code_manager_config_version.rb | 1 - scripts/config_version.rb | 1 - 2 files changed, 2 deletions(-) mode change 100755 => 100644 scripts/code_manager_config_version.rb mode change 100755 => 100644 scripts/config_version.rb diff --git a/scripts/code_manager_config_version.rb b/scripts/code_manager_config_version.rb old mode 100755 new mode 100644 index beacabc..521bcfb --- a/scripts/code_manager_config_version.rb +++ b/scripts/code_manager_config_version.rb @@ -1,4 +1,3 @@ -#!/opt/puppetlabs/puppet/bin/ruby require 'json' require 'socket' diff --git a/scripts/config_version.rb b/scripts/config_version.rb old mode 100755 new mode 100644 index abb3390..5652226 --- a/scripts/config_version.rb +++ b/scripts/config_version.rb @@ -1,4 +1,3 @@ -#!/opt/puppetlabs/puppet/bin/ruby begin require 'rugged' require 'socket' From 211ecc58b5b650ed158f0c36158a85a289ce2640 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Tarti=C3=A8re?= Date: Sat, 7 Apr 2018 11:34:07 +0200 Subject: [PATCH 3/6] Do sanity checking early Fail before doing anything when passed parameters are invalid. --- scripts/config_version.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/scripts/config_version.sh b/scripts/config_version.sh index 0378952..7ccd2f4 100755 --- a/scripts/config_version.sh +++ b/scripts/config_version.sh @@ -1,5 +1,10 @@ #!/bin/sh +if [ $# -ne 2 -o ! -d "$1" -o ! -d "$1/$2" ]; then + echo "usage: $0 " >&2 + exit 1 +fi + ruby=ruby script="$1/$2/scripts/config_version.rb" From 415a71dd59c17093684efd3a1376afe1bfb8824d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Tarti=C3=A8re?= Date: Sat, 7 Apr 2018 11:59:18 +0200 Subject: [PATCH 4/6] Remove code dead for more than 2 years r10k generates a .r10k-deploy.json file since version 2.1.0 which was released on October 28, 2015. New users of the control-repo are not likely to have a so old version of r10k, so remove this dead code. --- scripts/config_version.rb | 24 ------------------------ scripts/config_version.sh | 7 +------ 2 files changed, 1 insertion(+), 30 deletions(-) delete mode 100644 scripts/config_version.rb diff --git a/scripts/config_version.rb b/scripts/config_version.rb deleted file mode 100644 index 5652226..0000000 --- a/scripts/config_version.rb +++ /dev/null @@ -1,24 +0,0 @@ -begin - require 'rugged' - require 'socket' -rescue LoadError - t = Time.new - puts t.to_i -else - environmentpath = ARGV[0] - environment = ARGV[1] - - # 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 - - # Get the path to the environment being compiled. - repo = Rugged::Repository.discover(File.join(environmentpath, environment)) - head = repo.head - - # First 12 characters of the sha1 hash of the newest commit. - commit_id = head.target_id[0...11] - - # Show the compiling master, environment name, and commit ID. - puts "#{compiling_master}-#{environment}-#{commit_id}" -end diff --git a/scripts/config_version.sh b/scripts/config_version.sh index 7ccd2f4..1dc54f7 100755 --- a/scripts/config_version.sh +++ b/scripts/config_version.sh @@ -6,14 +6,9 @@ if [ $# -ne 2 -o ! -d "$1" -o ! -d "$1/$2" ]; then fi ruby=ruby -script="$1/$2/scripts/config_version.rb" if [ -x /opt/puppetlabs/puppet/bin/ruby ]; then ruby=/opt/puppetlabs/puppet/bin/ruby fi -if [ -e $1/$2/.r10k-deploy.json ]; then - script="$1/$2/scripts/code_manager_config_version.rb" -fi - -"${ruby}" "${script}" "$1" "$2" +"${ruby}" "$1/$2/scripts/code_manager_config_version.rb" "$1" "$2" From ada94157a42e1d20112eaa74618e2f22db4ed560 Mon Sep 17 00:00:00 2001 From: Reid Vandewiele Date: Wed, 16 Oct 2019 10:16:34 -0700 Subject: [PATCH 5/6] Restore and clarify config_version functionality This branch is intended as a portability fix. Some functionailty had been inadvertently removed as unused, but testing revealed that it had a purpose. Because the purpose was unclear, this commit restores the functionality AND clarifies it in the script names and comments in config_version.sh. --- ...nfig_version.rb => config_version-r10k.rb} | 1 + scripts/config_version-rugged.rb | 25 +++++++++++++++ scripts/config_version.sh | 31 ++++++++++++++++--- 3 files changed, 52 insertions(+), 5 deletions(-) rename scripts/{code_manager_config_version.rb => config_version-r10k.rb} (95%) mode change 100644 => 100755 create mode 100755 scripts/config_version-rugged.rb diff --git a/scripts/code_manager_config_version.rb b/scripts/config_version-r10k.rb old mode 100644 new mode 100755 similarity index 95% rename from scripts/code_manager_config_version.rb rename to scripts/config_version-r10k.rb index 521bcfb..beacabc --- a/scripts/code_manager_config_version.rb +++ b/scripts/config_version-r10k.rb @@ -1,3 +1,4 @@ +#!/opt/puppetlabs/puppet/bin/ruby require 'json' require 'socket' diff --git a/scripts/config_version-rugged.rb b/scripts/config_version-rugged.rb new file mode 100755 index 0000000..abb3390 --- /dev/null +++ b/scripts/config_version-rugged.rb @@ -0,0 +1,25 @@ +#!/opt/puppetlabs/puppet/bin/ruby +begin + require 'rugged' + require 'socket' +rescue LoadError + t = Time.new + puts t.to_i +else + environmentpath = ARGV[0] + environment = ARGV[1] + + # 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 + + # Get the path to the environment being compiled. + repo = Rugged::Repository.discover(File.join(environmentpath, environment)) + head = repo.head + + # First 12 characters of the sha1 hash of the newest commit. + commit_id = head.target_id[0...11] + + # Show the compiling master, environment name, and commit ID. + puts "#{compiling_master}-#{environment}-#{commit_id}" +end diff --git a/scripts/config_version.sh b/scripts/config_version.sh index 1dc54f7..d6981a1 100755 --- a/scripts/config_version.sh +++ b/scripts/config_version.sh @@ -1,14 +1,35 @@ #!/bin/sh +# Usage if [ $# -ne 2 -o ! -d "$1" -o ! -d "$1/$2" ]; then echo "usage: $0 " >&2 exit 1 fi -ruby=ruby +# For portability, identify a preferred ruby executable to use +ruby() { + [ -x /opt/puppetlabs/puppet/bin/ruby ] \ + && /opt/puppetlabs/puppet/bin/ruby "$@" \ + || /usr/bin/env ruby "$@" +} + +# Determine how best to calculate a config_version +if [ -e $1/$2/.r10k-deploy.json ]; then + # The environment was deployed using r10k. We will calculate the config + # version using the r10k data. + ruby $1/$2/scripts/config_version-r10k.rb $1 $2 + +elif [ -e /opt/puppetlabs/server/pe_version ]; then + # This is a Puppet Enterprise system and we can rely on the rugged ruby gem + # being available. + ruby $1/$2/scripts/config_version-rugged.rb $1 $2 + +elif [ -x /usr/bin/git ]; then + # The git command is available. + /usr/bin/git --git-dir $1/$2/.git rev-parse HEAD + +else + # Nothing else available; just use the date. + date +%s -if [ -x /opt/puppetlabs/puppet/bin/ruby ]; then - ruby=/opt/puppetlabs/puppet/bin/ruby fi - -"${ruby}" "$1/$2/scripts/code_manager_config_version.rb" "$1" "$2" From e46d209f19ad94c89652bec8064a8b6ce880a752 Mon Sep 17 00:00:00 2001 From: Reid Vandewiele Date: Wed, 16 Oct 2019 12:46:30 -0700 Subject: [PATCH 6/6] Don't assume git is in /usr/bin; expect it on PATH --- scripts/config_version.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/config_version.sh b/scripts/config_version.sh index d6981a1..bdbc511 100755 --- a/scripts/config_version.sh +++ b/scripts/config_version.sh @@ -24,9 +24,9 @@ elif [ -e /opt/puppetlabs/server/pe_version ]; then # being available. ruby $1/$2/scripts/config_version-rugged.rb $1 $2 -elif [ -x /usr/bin/git ]; then +elif type git >/dev/null; then # The git command is available. - /usr/bin/git --git-dir $1/$2/.git rev-parse HEAD + git --git-dir $1/$2/.git rev-parse HEAD else # Nothing else available; just use the date.