Skip to content

Commit 7710e7f

Browse files
committed
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
1 parent 2cb3db3 commit 7710e7f

7 files changed

Lines changed: 69 additions & 117 deletions

File tree

clean.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ if [[ -r $TOP_DIR/.stackenv ]]; then
2626
fi
2727

2828
# 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``
3030
# and ``DISTRO``
3131
GetDistro
3232

functions-common

Lines changed: 64 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -274,112 +274,71 @@ function warn {
274274
# ================
275275

276276
# 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+
#
279286
# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
280287
# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
281-
# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
282288
# ``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+
}
289317

290318
# GetOSVersion
319+
# Set the following variables:
320+
# - os_RELEASE
321+
# - os_CODENAME
322+
# - os_VENDOR
323+
# - os_PACKAGE
291324
function GetOSVersion {
325+
# We only support distros that provide a sane lsb_release
326+
_ensure_lsb_release
292327

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)
365331

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
378333
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"
381336
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
383342
}
384343

385344
# Translate the OS version values into common nomenclature
@@ -389,34 +348,31 @@ declare DISTRO
389348
function GetDistro {
390349
GetOSVersion
391350
if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
392-
# 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
351+
# 'Everyone' refers to Ubuntu / Debian releases by
352+
# the code name adjective
393353
DISTRO=$os_CODENAME
394354
elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
395355
# For Fedora, just use 'f' and the release
396356
DISTRO="f$os_RELEASE"
397357
elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
398358
DISTRO="opensuse-$os_RELEASE"
399359
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) || \
360+
# just use major release
361+
DISTRO="sle${os_RELEASE%.*}"
362+
elif [[ "$os_VENDOR" =~ (Red.*Hat) || \
407363
"$os_VENDOR" =~ (CentOS) || \
408364
"$os_VENDOR" =~ (OracleLinux) ]]; then
409365
# Drop the . release as we assume it's compatible
366+
# XXX re-evaluate when we get RHEL10
410367
DISTRO="rhel${os_RELEASE::1}"
411368
elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
412-
DISTRO="xs$os_RELEASE"
369+
DISTRO="xs${os_RELEASE%.*}"
413370
elif [[ "$os_VENDOR" =~ (kvmibm) ]]; then
414371
DISTRO="${os_VENDOR}${os_RELEASE::1}"
415372
else
416-
# Catch-all for now is Vendor + Release + Update
417-
DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
373+
die $LINENO "Unable to determine DISTRO"
418374
fi
419-
export DISTRO
375+
typeset -xr DISTRO
420376
}
421377

422378
# Utility function for checking machine architecture

stack.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ source $TOP_DIR/inc/meta-config
136136
source $TOP_DIR/lib/stack
137137

138138
# 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``
140140
# and ``DISTRO``
141141
GetDistro
142142

tools/create-stack-user.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ TOP_DIR=$(cd $(dirname "$0")/.. && pwd)
2424
source $TOP_DIR/functions
2525

2626
# 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``
2828
# and ``DISTRO``
2929
GetDistro
3030

tools/info.sh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ GetDistro
5252
echo "os|distro=$DISTRO"
5353
echo "os|vendor=$os_VENDOR"
5454
echo "os|release=$os_RELEASE"
55-
if [ -n "$os_UPDATE" ]; then
56-
echo "os|version=$os_UPDATE"
57-
fi
58-
5955

6056
# Repos
6157
# -----

tools/install_prereqs.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ if [[ -z "$TOP_DIR" ]]; then
2828
source $TOP_DIR/functions
2929

3030
# 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``
3232
# and ``DISTRO``
3333
GetDistro
3434

unstack.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ fi
8484
load_plugin_settings
8585

8686
# 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``
8888
GetOSVersion
8989

9090
# Run extras

0 commit comments

Comments
 (0)