Skip to content

Add a function to compare the OS version #972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1997,6 +1997,20 @@ 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) # When OS is below the given version.
- Boolean(1) # When OS is equal to or greater 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).
Expand Down
20 changes: 20 additions & 0 deletions lib/puppet/functions/os_version_gte.rb
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions spec/functions/os_version_gte_spec.rb
Original file line number Diff line number Diff line change
@@ -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