From 4509b5cdaca319a7cd1586ea324edcbcba5d8c8c Mon Sep 17 00:00:00 2001 From: Ewoud Kohl van Wijngaarden Date: Fri, 23 Nov 2018 12:54:15 +0100 Subject: [PATCH 1/4] Add a function to compare the OS version This function aims to reduce the boiler plate that a lot of modules have to compare versions: if $facts['operatingsystem'] == 'Ubuntu' && versioncmp(facts['operatingsystemmajrelease'], '16.04') >= 0 { Can now be reduced to: if os_version_gte('Ubuntu', '16.04') { --- lib/puppet/functions/os_version_gte.rb | 20 ++++++++++++++++ spec/functions/os_version_gte_spec.rb | 33 ++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 lib/puppet/functions/os_version_gte.rb create mode 100644 spec/functions/os_version_gte_spec.rb diff --git a/lib/puppet/functions/os_version_gte.rb b/lib/puppet/functions/os_version_gte.rb new file mode 100644 index 000000000..e3089a858 --- /dev/null +++ b/lib/puppet/functions/os_version_gte.rb @@ -0,0 +1,20 @@ +# Checks if the OS version is at least a certain version. Note that only the +# major version is taken into account. +# +# Example usage: +# +# if os_version_gte('Debian', '9') { } +# if os_version_gte('Ubuntu', '18.04') { } +Puppet::Functions.create_function(:os_version_gte) do + dispatch :os_version_gte do + param 'String[1]', :os + param 'String[1]', :version + return_type 'Boolean' + end + + def os_version_gte(os, version) + facts = closure_scope['facts'] + (facts['operatingsystem'] == os && + Puppet::Util::Package.versioncmp(version, facts['operatingsystemmajrelease']) >= 0) + end +end diff --git a/spec/functions/os_version_gte_spec.rb b/spec/functions/os_version_gte_spec.rb new file mode 100644 index 000000000..4abfd7ef3 --- /dev/null +++ b/spec/functions/os_version_gte_spec.rb @@ -0,0 +1,33 @@ +require 'spec_helper' + +describe 'os_version_gte' do + context 'on Debian 9' do + let(:facts) do + { + :operatingsystem => 'Debian', + :operatingsystemmajrelease => '9', + } + end + + it { is_expected.to run.with_params('Debian', '9').and_return(true) } + it { is_expected.to run.with_params('Debian', '8').and_return(false) } + it { is_expected.to run.with_params('Debian', '8.0').and_return(false) } + it { is_expected.to run.with_params('Ubuntu', '16.04').and_return(false) } + it { is_expected.to run.with_params('Fedora', '29').and_return(false) } + end + + context 'on Ubuntu 16.04' do + let(:facts) do + { + :operatingsystem => 'Ubuntu', + :operatingsystemmajrelease => '16.04', + } + end + + it { is_expected.to run.with_params('Debian', '9').and_return(false) } + it { is_expected.to run.with_params('Ubuntu', '16').and_return(false) } + it { is_expected.to run.with_params('Ubuntu', '16.04').and_return(true) } + it { is_expected.to run.with_params('Ubuntu', '18.04').and_return(true) } + it { is_expected.to run.with_params('Fedora', '29').and_return(false) } + end +end From 2883c60fe48ec6e2c12b94162fb789788a423f1c Mon Sep 17 00:00:00 2001 From: David Swan Date: Tue, 27 Nov 2018 12:05:34 +0000 Subject: [PATCH 2/4] Readme Updated --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index bd596a8bc..6ad3ff38d 100644 --- a/README.md +++ b/README.md @@ -1997,6 +1997,18 @@ function in Puppet for the many available type conversions. *Type*: rvalue. +#### 'os_version_gte' + +Checks to see if the OS version is at least a certain version. Note that only the major version is taken into account. + +Example usage: + if os_version_gte('Debian', '9') { } + if os_version_gte('Ubuntu', '18.04') { } + +Returns: + Boolean(0) # OS is below the given version. + Boolean(1) # OS is equal to or grater than the given version. + #### `parsejson` Converts a string of JSON into the correct Puppet structure (as a hash, array, string, integer, or a combination of such). From d2a2f5235fbafbfcf16d961cb16cbbebdbb0de43 Mon Sep 17 00:00:00 2001 From: Ewoud Kohl van Wijngaarden Date: Tue, 27 Nov 2018 13:37:07 +0100 Subject: [PATCH 3/4] Fix typo in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6ad3ff38d..bdd4bc42e 100644 --- a/README.md +++ b/README.md @@ -2007,7 +2007,7 @@ Example usage: Returns: Boolean(0) # OS is below the given version. - Boolean(1) # OS is equal to or grater than the given version. + Boolean(1) # OS is equal to or greater than the given version. #### `parsejson` From 03329464ad6f7f8a95cb3f79bde3da18d3b7d295 Mon Sep 17 00:00:00 2001 From: David Swan Date: Tue, 27 Nov 2018 13:19:34 +0000 Subject: [PATCH 4/4] Readme format update --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index bdd4bc42e..0023c2cc3 100644 --- a/README.md +++ b/README.md @@ -1997,17 +1997,19 @@ function in Puppet for the many available type conversions. *Type*: rvalue. -#### 'os_version_gte' +#### `os_version_gte` Checks to see if the OS version is at least a certain version. Note that only the major version is taken into account. Example usage: +``` if os_version_gte('Debian', '9') { } if os_version_gte('Ubuntu', '18.04') { } +``` Returns: - Boolean(0) # OS is below the given version. - Boolean(1) # OS is equal to or greater than the given version. + - Boolean(0) # When OS is below the given version. + - Boolean(1) # When OS is equal to or greater than the given version. #### `parsejson`