Add reliable OS detection for ansible bootstrap

Python's platform module provides a very reliable method for determining
the Linux distribution, its version, and any applicable codenames.  This
patch adds three bash variables that can be brought into the bash scripts
to take different actions depending on the operating system that is
detected.

Implements: blueprint multi-platform-host

Change-Id: I98bcb478e99e11956618b2327399e18471aab534
This commit is contained in:
Major Hayden 2015-10-04 17:40:29 -05:00
parent 42cf693c75
commit d9b9a427aa
3 changed files with 34 additions and 4 deletions

View File

@ -43,12 +43,12 @@ info_block "Bootstrapping System with Ansible"
ssh_key_create ssh_key_create
# Install the base packages # Install the base packages
APT=`command -v apt-get` || true if [[ $HOST_DISTRO =~ ^(Ubuntu|Debian) ]]; then
YUM=`command -v yum` || true
if [[ "$APT" != "" ]]; then
apt-get update && apt-get -y install git python-all python-dev curl autoconf g++ python2.7-dev < /dev/null apt-get update && apt-get -y install git python-all python-dev curl autoconf g++ python2.7-dev < /dev/null
elif [[ "$YUM" != "" ]]; then elif [[ $HOST_DISTRO =~ ^(CentOS|Red Hat) ]]; then
yum check-update && yum -y install git python2 curl autoconf gcc-c++ python2-devel yum check-update && yum -y install git python2 curl autoconf gcc-c++ python2-devel
elif [[ $HOST_DISTRO =~ ^Fedora ]]; then
dnf -y install git python curl autoconf gcc-c++ python-devel
fi fi
# If the working directory exists remove it # If the working directory exists remove it

25
scripts/os-detection.py Executable file
View File

@ -0,0 +1,25 @@
#!/usr/bin/env python
# Copyright 2015, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Determines the operating system of a host and returns bash variables."""
import platform
template = """
HOST_DISTRO="{0}"
HOST_VERSION="{1}"
HOST_CODENAME="{2}"
""".format(*platform.linux_distribution())
print(template)

View File

@ -325,6 +325,11 @@ function get_pip {
trap "exit_fail ${LINENO} $? 'Received STOP Signal'" SIGHUP SIGINT SIGTERM trap "exit_fail ${LINENO} $? 'Received STOP Signal'" SIGHUP SIGINT SIGTERM
trap "exit_fail ${LINENO} $?" ERR trap "exit_fail ${LINENO} $?" ERR
## Determine OS --------------------------------------------------------------
# Determine the operating system of the base host
# Adds the $HOST_DISTRO, $HOST_VERSION, and $HOST_CODENAME bash variables.
eval "$(python $(dirname ${0})/os-detection.py)"
echo "Detected ${HOST_DISTRO} ${HOST_VERSION} (codename: ${HOST_CODENAME})"
## Pre-flight check ---------------------------------------------------------- ## Pre-flight check ----------------------------------------------------------
# Make sure only root can run our script # Make sure only root can run our script