Faidon Liambotis has submitted this change and it was merged.

Change subject: wmflib: add os_version() & requires_os()
......................................................................


wmflib: add os_version() & requires_os()

Since we're going to introduce Debian Jessie to our production
environment at some point, ubuntu_version() & requires_ubuntu should be
replaced with something more generic. So add os_version(), which has a
similar syntax, but which can handle compound predicates like:

    if os_version('ubuntu >= trusty || debian >= jessie') {
        ...
    }

Change-Id: I2382035ff90c7c4423c5e2b2130a9c0de6c8c707
---
M modules/mediawiki/manifests/init.pp
M modules/wmflib/README.md
A modules/wmflib/lib/puppet/parser/functions/os_version.rb
A modules/wmflib/lib/puppet/parser/functions/requires_os.rb
4 files changed, 154 insertions(+), 8 deletions(-)

Approvals:
  Faidon Liambotis: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/modules/mediawiki/manifests/init.pp 
b/modules/mediawiki/manifests/init.pp
index 4e94cd8..d12256e 100644
--- a/modules/mediawiki/manifests/init.pp
+++ b/modules/mediawiki/manifests/init.pp
@@ -28,7 +28,7 @@
 
     include ::ssh::server
 
-    if ubuntu_version('>= trusty') {
+    if os_version('ubuntu >= trusty') {
         include ::mediawiki::hhvm
     }
 
diff --git a/modules/wmflib/README.md b/modules/wmflib/README.md
index 882a17a..440fc52 100644
--- a/modules/wmflib/README.md
+++ b/modules/wmflib/README.md
@@ -129,6 +129,33 @@
     }
 
 
+## os_version
+
+`os_version( string $version_predicate )`
+
+Performs semantic OS version comparison.
+
+Takes one or more string arguments, each containing one or more predicate
+expressions. Each expression consts of a distribution name, followed by a
+comparison operator, followed by a release name or number. Multiple clauses
+are OR'd together. The arguments are case-insensitive.
+
+The host's OS version will be compared to to the comparison target
+using the specified operator, returning a boolean. If no operator is
+present, the equality operator is assumed.
+
+### Examples
+
+    # True if Ubuntu Trusty or newer or Debian Jessie or newer
+    os_version('ubuntu >= trusty || debian >= Jessie')
+
+    # Same, but with each clause as a separate argument
+    os_version('ubuntu >= trusty', 'debian >= Jessie')
+
+    # True if exactly Debian Jessie
+    os_version('debian jessie')
+
+
 ## php_ini
 
 `php_ini( hash $ini_settings [, hash $... ] )`
@@ -177,23 +204,23 @@
     requires_realm('labs')
 
 
-## requires_ubuntu
+## requires_os
 
-`requires_ubuntu( string $version_predicate )`
+`requires_os( string $version_predicate )`
 
-Validate that the host Ubuntu version satisfies a version
+Validate that the host OS version satisfies a version
 check. Abort catalog compilation if not.
 
-See the documentation for ubuntu_version() for supported
+See the documentation for os_version() for supported
 predicate syntax.
 
 ### Examples
 
-    # Fail unless version is Trusty
-    requires_ubuntu('trusty')
+    # Fail unless version is Trusty or Jessie
+    requires_os('ubuntu trusty || debian jessie')
 
     # Fail unless Trusty or newer
-    requires_ubuntu('> trusty')
+    requires_os('ubuntu >= trusty')
 
 
 
@@ -292,6 +319,8 @@
 comparison target can be provided as two separate arguments, if you
 prefer.
 
+This function is DEPRECATED. Please see os_version() instead.
+
 ### Examples
 
     # True if Precise or newer
diff --git a/modules/wmflib/lib/puppet/parser/functions/os_version.rb 
b/modules/wmflib/lib/puppet/parser/functions/os_version.rb
new file mode 100644
index 0000000..1bd13bf
--- /dev/null
+++ b/modules/wmflib/lib/puppet/parser/functions/os_version.rb
@@ -0,0 +1,95 @@
+# == Function: os_version( string $version_predicate )
+#
+# Performs semantic OS version comparison.
+#
+# Takes one or more string arguments, each containing one or more predicate
+# expressions. Each expression consts of a distribution name, followed by a
+# comparison operator, followed by a release name or number. Multiple clauses
+# are OR'd together. The arguments are case-insensitive.
+#
+# The host's OS version will be compared to to the comparison target
+# using the specified operator, returning a boolean. If no operator is
+# present, the equality operator is assumed.
+#
+# === Examples
+#
+#  # True if Ubuntu Trusty or newer or Debian Jessie or newer
+#  os_version('ubuntu >= trusty || debian >= Jessie')
+#
+#  # Same, but with each clause as a separate argument
+#  os_version('ubuntu >= trusty', 'debian >= Jessie')
+#
+#  # True if exactly Debian Jessie
+#  os_version('debian jessie')
+#
+require 'puppet/util/package'
+
+module Puppet::Parser::Functions
+  os_versions = {
+    'Ubuntu' => {
+      'Hardy'    => '8.04',
+      'Intrepid' => '8.10',
+      'Jaunty'   => '9.04',
+      'Karmic'   => '9.10',
+      'Lucid'    => '10.04.4',
+      'Maverick' => '10.10',
+      'Natty'    => '11.04',
+      'Oneiric'  => '11.10',
+      'Precise'  => '12.04.4',
+      'Quantal'  => '12.10',
+      'Raring'   => '13.04',
+      'Saucy'    => '13.10',
+      'Trusty'   => '14.04',
+      'Utopic'   => '14.10'
+    },
+    'Debian' => {
+      'Jessie'  => '8.0',
+      'Wheezy'  => '7.0',
+      'Squeeze' => '6.0',
+      'Lenny'   => '5.0',
+      'Etch'    => '4.0',
+      'Sarge'   => '3.1',
+      'Woody'   => '3.0',
+      'Potato'  => '2.2',
+      'Slink'   => '2.1',
+      'Hamm'    => '2.0'
+    }
+  }
+
+  newfunction(:os_version, :type => :rvalue, :arity => -2) do |args|
+    self_release = lookupvar('lsbdistrelease').capitalize
+    self_id = lookupvar('lsbdistid').capitalize
+
+    # Multiple clauses are OR'd
+    clauses = args.join('||').split('||').map(&:strip)
+
+    clauses.any? do |clause|
+      unless /^(?<other_id>\w+) *(?<operator>[<>=]*) 
*(?<other_release>[\w\.]+)$/ =~ clause
+        fail(ArgumentError, "os_version(): invalid expression '#{clause}'")
+      end
+
+      [other_id, other_release].each(&:capitalize!)
+
+      next unless self_id == other_id
+
+      other_release = os_versions[other_id][other_release] || other_release
+
+      unless /^[\d.]+$/ =~ other_release
+        fail(ArgumentError,
+             "os_version(): unknown #{other_id} release '#{other_release}'")
+      end
+
+      cmp = Puppet::Util::Package.versioncmp(self_release, other_release)
+      case operator
+      when '', '==' then cmp == 0
+      when '!=' then cmp != 0
+      when '>'  then cmp == 1
+      when '<'  then cmp == -1
+      when '>=' then cmp >= 0
+      when '<=' then cmp <= 0
+      else fail(ArgumentError,
+                "os_version(): unknown comparison operator '#{operator}'")
+      end
+    end
+  end
+end
diff --git a/modules/wmflib/lib/puppet/parser/functions/requires_os.rb 
b/modules/wmflib/lib/puppet/parser/functions/requires_os.rb
new file mode 100644
index 0000000..d89d75b
--- /dev/null
+++ b/modules/wmflib/lib/puppet/parser/functions/requires_os.rb
@@ -0,0 +1,22 @@
+# == Function: requires_os( string $version_predicate )
+#
+# Validate that the host operating system version satisfies a version
+# check. Abort catalog compilation if not.
+#
+# See the documentation for os_version() for supported predicate syntax.
+#
+# === Examples
+#
+#  # Fail unless version is exactly Debian Jessie
+#  requires_os('debian jessie')
+#
+#  # Fail unless Ubuntu Trusty or newer or Debian Jessie or newer
+#  requires_os('ubuntu >= trusty || debian >= Jessie')
+#
+module Puppet::Parser::Functions
+  newfunction(:requires_os, :arity => -2) do |args|
+    clauses = args.join('||')
+    Puppet::Parser::Functions.function(:os_version)
+    fail(Puppet::ParseError, "OS #{clauses} required.") unless 
function_os_version(clauses)
+  end
+end

-- 
To view, visit https://gerrit.wikimedia.org/r/178394
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I2382035ff90c7c4423c5e2b2130a9c0de6c8c707
Gerrit-PatchSet: 5
Gerrit-Project: operations/puppet
Gerrit-Branch: production
Gerrit-Owner: Ori.livneh <o...@wikimedia.org>
Gerrit-Reviewer: Faidon Liambotis <fai...@wikimedia.org>
Gerrit-Reviewer: Ori.livneh <o...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to