Comparing package versions
Package version numbers are odd things. They look like decimal numbers, but they're not: a version number is often in the form of 2.6.4
, for example. If you need to compare one version number with another, you can't do a straightforward string comparison: 2.6.4
would be interpreted as greater than 2.6.12
. And a numeric comparison won't work because they're not valid numbers.
Puppet's versioncmp
function comes to the rescue. If you pass two things that look like version numbers, it will compare them and return a value indicating which is greater:
versioncmp( A, B )
returns:
- 0 if A and B are equal
- Greater than 1 if A is higher than B
- Less than 0 if A is less than B
How to do it…
Here's an example using the versioncmp
function:
- Modify your
site.pp
file as follows:node 'cookbook' { $app_version = '1.2.2' $min_version = '1.2.10' if versioncmp($app_version, $min_version) >= 0 { notify { 'Version OK': } } else { notify { 'Upgrade needed': } } }
- Run Puppet:
[root@cookbook ~]# puppet agent -t Info: Caching catalog for cookbook.example.com Notice: Upgrade needed
- Now change the value of
$app_version
:$app_version = '1.2.14'
- Run Puppet again:
[root@cookbook ~]# puppet agent -t Info: Caching catalog for cookbook.example.com Notice: Version OK
How it works…
We've specified that the minimum acceptable version ($min_version
) is 1.2.10
. So, in the first example, we want to compare it with $app_version
of 1.2.2
. A simple alphabetic comparison of these two strings (in Ruby, for example) would give the wrong result, but versioncmp
correctly determines that 1.2.2
is less than 1.2.10
and alerts us that we need to upgrade.
In the second example, $app_version
is now 1.2.14
, which versioncmp
correctly recognizes as greater than $min_version
and so we get the message Version OK.
- 大數據戰爭:人工智能時代不能不說的事
- 嵌入式系統應用
- Cinema 4D R13 Cookbook
- Managing Mission:Critical Domains and DNS
- PyTorch Deep Learning Hands-On
- 完全掌握AutoCAD 2008中文版:機械篇
- Kubernetes for Developers
- Windows Server 2008 R2活動目錄內幕
- Working with Linux:Quick Hacks for the Command Line
- Practical Network Automation
- 超好玩的Python少兒編程
- Deep Learning with PyTorch Quick Start Guide
- 仿蛛機器人的設計與制作
- 亮劍.NET:圖解ASP.NET網站開發實戰
- 新手學Photoshop CS6數碼照片處理