Browse code

Only use lsb_release for distro tags

The existing GetOSVersion has a lot of unused code which is wrong in
several ways

- the only path tested in upstream CI is with lsb_release, because
it's pre-installed on all nodes

- the /etc/redhat-release checking probably still works, but is
unnecessary

- If using lsb_release, os_UPDATE has never actually been set.

- the /etc/SuSE-release branch checking is broken if the lsb package
is actually installed. lsb checking does not set os_UPDATE but yet
the SuSE DISTRO setting relies on this to set a patch level (and so
does some of the rpm tags). SuSE 11 is up to update 3, but the rpm
matching is stuck hard-coded to update 2. I'm guessing
installation is actually broken there.

- the debian checking branch is broken. The VERSION tags have been
removed and were not supposed to be relied on anyway (see notes in
[1])

This simplifies things:

- remove OSX checking (moved here after discussions in
I31d0fdd30928ecc8d959a95838b1d3affd28ac6f)

- only use the output of lsb_release.

- A small best-effort check to pre-install lsb packages if not
detected (that avoids chicken-egg-problem of package-install
wrappers relying on os_* flags).

- The unset os_UPDATE is removed. It's only previous use was for
setting separate suse versions in the DISTRO element for matching
during package installs (since removed)

- DISTRO setting is modified to use the parts of os_RELEASE it wants.
Per-above, this is the correct place to parse out specifics.

- Call out the is_* functions, which are a better way to detect
platforms

- Export the variables as read-only, since they shouldn't be reset

[1] http://sources.debian.net/src/base-files/7.5/debian/changelog/

Change-Id: I46a2c36d95327087085df07cb797eb91249a893c

Ian Wienand authored on 2014/08/27 15:15:32
Showing 7 changed files
... ...
@@ -26,7 +26,7 @@ if [[ -r $TOP_DIR/.stackenv ]]; then
26 26
 fi
27 27
 
28 28
 # Determine what system we are running on.  This provides ``os_VENDOR``,
29
-# ``os_RELEASE``, ``os_UPDATE``, ``os_PACKAGE``, ``os_CODENAME``
29
+# ``os_RELEASE``, ``os_PACKAGE``, ``os_CODENAME``
30 30
 # and ``DISTRO``
31 31
 GetDistro
32 32
 
... ...
@@ -274,112 +274,71 @@ function warn {
274 274
 # ================
275 275
 
276 276
 # Determine OS Vendor, Release and Update
277
-# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
278
-# Returns results in global variables:
277
+
278
+#
279
+# NOTE : For portability, you almost certainly do not want to use
280
+# these variables directly!  The "is_*" functions defined below this
281
+# bundle up compatible platforms under larger umbrellas that we have
282
+# determinted are compatible enough (e.g. is_ubuntu covers Ubuntu &
283
+# Debian, is_fedora covers RPM-based distros).  Higher-level functions
284
+# such as "install_package" further abstract things in better ways.
285
+#
279 286
 # ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
280 287
 # ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
281
-# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
282 288
 # ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
283
-# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
284
-os_VENDOR=""
285
-os_RELEASE=""
286
-os_UPDATE=""
287
-os_PACKAGE=""
288
-os_CODENAME=""
289
+# ``os_CODENAME`` - vendor's codename for release: ``trusty``
290
+
291
+declare os_VENDOR os_RELEASE os_PACKAGE os_CODENAME
292
+
293
+# Make a *best effort* attempt to install lsb_release packages for the
294
+# user if not available.  Note can't use generic install_package*
295
+# because they depend on this!
296
+function _ensure_lsb_release {
297
+    if [[ -x $(which lsb_release 2>/dev/null) ]]; then
298
+        return
299
+    fi
300
+
301
+    if [[ -x $(which apt-get 2>/dev/null) ]]; then
302
+        sudo apt-get install -y lsb-release
303
+    elif [[ -x $(which zypper 2>/dev/null) ]]; then
304
+        # XXX: old code paths seem to have assumed SUSE platforms also
305
+        # had "yum".  Keep this ordered above yum so we don't try to
306
+        # install the rh package.  suse calls it just "lsb"
307
+        sudo zypper -y install lsb
308
+    elif [[ -x $(which dnf 2>/dev/null) ]]; then
309
+        sudo dnf install -y redhat-lsb-core
310
+    elif [[ -x $(which yum 2>/dev/null) ]]; then
311
+        # all rh patforms (fedora, centos, rhel) have this pkg
312
+        sudo yum install -y redhat-lsb-core
313
+    else
314
+        die $LINENO "Unable to find or auto-install lsb_release"
315
+    fi
316
+}
289 317
 
290 318
 # GetOSVersion
319
+#  Set the following variables:
320
+#  - os_RELEASE
321
+#  - os_CODENAME
322
+#  - os_VENDOR
323
+#  - os_PACKAGE
291 324
 function GetOSVersion {
325
+    # We only support distros that provide a sane lsb_release
326
+    _ensure_lsb_release
292 327
 
293
-    # Figure out which vendor we are
294
-    if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
295
-        # OS/X
296
-        os_VENDOR=`sw_vers -productName`
297
-        os_RELEASE=`sw_vers -productVersion`
298
-        os_UPDATE=${os_RELEASE##*.}
299
-        os_RELEASE=${os_RELEASE%.*}
300
-        os_PACKAGE=""
301
-        if [[ "$os_RELEASE" =~ "10.7" ]]; then
302
-            os_CODENAME="lion"
303
-        elif [[ "$os_RELEASE" =~ "10.6" ]]; then
304
-            os_CODENAME="snow leopard"
305
-        elif [[ "$os_RELEASE" =~ "10.5" ]]; then
306
-            os_CODENAME="leopard"
307
-        elif [[ "$os_RELEASE" =~ "10.4" ]]; then
308
-            os_CODENAME="tiger"
309
-        elif [[ "$os_RELEASE" =~ "10.3" ]]; then
310
-            os_CODENAME="panther"
311
-        else
312
-            os_CODENAME=""
313
-        fi
314
-    elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
315
-        os_VENDOR=$(lsb_release -i -s)
316
-        os_RELEASE=$(lsb_release -r -s)
317
-        os_UPDATE=""
318
-        os_PACKAGE="rpm"
319
-        if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
320
-            os_PACKAGE="deb"
321
-        elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
322
-            lsb_release -d -s | grep -q openSUSE
323
-            if [[ $? -eq 0 ]]; then
324
-                os_VENDOR="openSUSE"
325
-            fi
326
-        elif [[ $os_VENDOR == "openSUSE project" ]]; then
327
-            os_VENDOR="openSUSE"
328
-        elif [[ $os_VENDOR =~ Red.*Hat ]]; then
329
-            os_VENDOR="Red Hat"
330
-        fi
331
-        os_CODENAME=$(lsb_release -c -s)
332
-    elif [[ -r /etc/redhat-release ]]; then
333
-        # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
334
-        # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
335
-        # CentOS release 5.5 (Final)
336
-        # CentOS Linux release 6.0 (Final)
337
-        # Fedora release 16 (Verne)
338
-        # XenServer release 6.2.0-70446c (xenenterprise)
339
-        # Oracle Linux release 7
340
-        # CloudLinux release 7.1
341
-        os_CODENAME=""
342
-        for r in "Red Hat" CentOS Fedora XenServer CloudLinux; do
343
-            os_VENDOR=$r
344
-            if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
345
-                ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
346
-                os_CODENAME=${ver#*|}
347
-                os_RELEASE=${ver%|*}
348
-                os_UPDATE=${os_RELEASE##*.}
349
-                os_RELEASE=${os_RELEASE%.*}
350
-                break
351
-            fi
352
-            os_VENDOR=""
353
-        done
354
-        if [ "$os_VENDOR" = "Red Hat" ] && [[ -r /etc/oracle-release ]]; then
355
-            os_VENDOR=OracleLinux
356
-        fi
357
-        os_PACKAGE="rpm"
358
-    elif [[ -r /etc/SuSE-release ]]; then
359
-        for r in openSUSE "SUSE Linux"; do
360
-            if [[ "$r" = "SUSE Linux" ]]; then
361
-                os_VENDOR="SUSE LINUX"
362
-            else
363
-                os_VENDOR=$r
364
-            fi
328
+    os_RELEASE=$(lsb_release -r -s)
329
+    os_CODENAME=$(lsb_release -c -s)
330
+    os_VENDOR=$(lsb_release -i -s)
365 331
 
366
-            if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
367
-                os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
368
-                os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
369
-                os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
370
-                break
371
-            fi
372
-            os_VENDOR=""
373
-        done
374
-        os_PACKAGE="rpm"
375
-    # If lsb_release is not installed, we should be able to detect Debian OS
376
-    elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
377
-        os_VENDOR="Debian"
332
+    if [[ $os_VENDOR =~ (Debian|Ubuntu|LinuxMint) ]]; then
378 333
         os_PACKAGE="deb"
379
-        os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
380
-        os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
334
+    else
335
+        os_PACKAGE="rpm"
381 336
     fi
382
-    export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
337
+
338
+    typeset -xr os_VENDOR
339
+    typeset -xr os_RELEASE
340
+    typeset -xr os_PACKAGE
341
+    typeset -xr os_CODENAME
383 342
 }
384 343
 
385 344
 # Translate the OS version values into common nomenclature
... ...
@@ -389,7 +348,8 @@ declare DISTRO
389 389
 function GetDistro {
390 390
     GetOSVersion
391 391
     if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
392
-        # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
392
+        # 'Everyone' refers to Ubuntu / Debian releases by
393
+        # the code name adjective
393 394
         DISTRO=$os_CODENAME
394 395
     elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
395 396
         # For Fedora, just use 'f' and the release
... ...
@@ -397,26 +357,22 @@ function GetDistro {
397 397
     elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
398 398
         DISTRO="opensuse-$os_RELEASE"
399 399
     elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
400
-        # For SLE, also use the service pack
401
-        if [[ -z "$os_UPDATE" ]]; then
402
-            DISTRO="sle${os_RELEASE}"
403
-        else
404
-            DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
405
-        fi
406
-    elif [[ "$os_VENDOR" =~ (Red Hat) || \
400
+        # just use major release
401
+        DISTRO="sle${os_RELEASE%.*}"
402
+    elif [[ "$os_VENDOR" =~ (Red.*Hat) || \
407 403
         "$os_VENDOR" =~ (CentOS) || \
408 404
         "$os_VENDOR" =~ (OracleLinux) ]]; then
409 405
         # Drop the . release as we assume it's compatible
406
+        # XXX re-evaluate when we get RHEL10
410 407
         DISTRO="rhel${os_RELEASE::1}"
411 408
     elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
412
-        DISTRO="xs$os_RELEASE"
409
+        DISTRO="xs${os_RELEASE%.*}"
413 410
     elif [[ "$os_VENDOR" =~ (kvmibm) ]]; then
414 411
         DISTRO="${os_VENDOR}${os_RELEASE::1}"
415 412
     else
416
-        # Catch-all for now is Vendor + Release + Update
417
-        DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
413
+        die $LINENO "Unable to determine DISTRO"
418 414
     fi
419
-    export DISTRO
415
+    typeset -xr DISTRO
420 416
 }
421 417
 
422 418
 # Utility function for checking machine architecture
... ...
@@ -136,7 +136,7 @@ source $TOP_DIR/inc/meta-config
136 136
 source $TOP_DIR/lib/stack
137 137
 
138 138
 # Determine what system we are running on.  This provides ``os_VENDOR``,
139
-# ``os_RELEASE``, ``os_UPDATE``, ``os_PACKAGE``, ``os_CODENAME``
139
+# ``os_RELEASE``, ``os_PACKAGE``, ``os_CODENAME``
140 140
 # and ``DISTRO``
141 141
 GetDistro
142 142
 
... ...
@@ -24,7 +24,7 @@ TOP_DIR=$(cd $(dirname "$0")/.. && pwd)
24 24
 source $TOP_DIR/functions
25 25
 
26 26
 # Determine what system we are running on.  This provides ``os_VENDOR``,
27
-# ``os_RELEASE``, ``os_UPDATE``, ``os_PACKAGE``, ``os_CODENAME``
27
+# ``os_RELEASE``, ``os_PACKAGE``, ``os_CODENAME``
28 28
 # and ``DISTRO``
29 29
 GetDistro
30 30
 
... ...
@@ -52,10 +52,6 @@ GetDistro
52 52
 echo "os|distro=$DISTRO"
53 53
 echo "os|vendor=$os_VENDOR"
54 54
 echo "os|release=$os_RELEASE"
55
-if [ -n "$os_UPDATE" ]; then
56
-    echo "os|version=$os_UPDATE"
57
-fi
58
-
59 55
 
60 56
 # Repos
61 57
 # -----
... ...
@@ -28,7 +28,7 @@ if [[ -z "$TOP_DIR" ]]; then
28 28
     source $TOP_DIR/functions
29 29
 
30 30
     # Determine what system we are running on.  This provides ``os_VENDOR``,
31
-    # ``os_RELEASE``, ``os_UPDATE``, ``os_PACKAGE``, ``os_CODENAME``
31
+    # ``os_RELEASE``, ``os_PACKAGE``, ``os_CODENAME``
32 32
     # and ``DISTRO``
33 33
     GetDistro
34 34
 
... ...
@@ -84,7 +84,7 @@ fi
84 84
 load_plugin_settings
85 85
 
86 86
 # Determine what system we are running on.  This provides ``os_VENDOR``,
87
-# ``os_RELEASE``, ``os_UPDATE``, ``os_PACKAGE``, ``os_CODENAME``
87
+# ``os_RELEASE``, ``os_PACKAGE``, ``os_CODENAME``
88 88
 GetOSVersion
89 89
 
90 90
 # Run extras