A) Add/move functions to 'functions' file

Add ini*() and tests
Add GetOSVersion()
Add install_package(), yum_install()
Add *_service()

Rebased

Change-Id: I570dba5ed4d2b988cdd1771cf6bed0aaf8e0fe27
This commit is contained in:
Dean Troyer 2012-03-27 14:50:45 -05:00
parent 60df29a234
commit 13dc5ccd13
3 changed files with 263 additions and 18 deletions

163
functions
View File

@ -1,4 +1,7 @@
# functions - Common functions used by DevStack components
#
# ENABLED_SERVICES is used by is_service_enabled()
# Save trace setting
XTRACE=$(set +o | grep xtrace)
@ -6,7 +9,7 @@ set +o xtrace
# apt-get wrapper to set arguments correctly
# apt_get package [package ...]
# apt_get operation package [package ...]
function apt_get() {
[[ "$OFFLINE" = "True" || -z "$@" ]] && return
local sudo="sudo"
@ -70,6 +73,71 @@ function get_field() {
}
# Determine OS Vendor, Release and Update
# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
# Returns results in global variables:
# os_VENDOR - vendor name
# os_RELEASE - release
# os_UPDATE - update
# os_PACKAGE - package type
# os_CODENAME - vendor's codename for release
# GetOSVersion
GetOSVersion() {
# Figure out which vendor we are
if [[ -n "`which sw_vers 2>/dev/null`" ]]; then
# OS/X
os_VENDOR=`sw_vers -productName`
os_RELEASE=`sw_vers -productVersion`
os_UPDATE=${os_RELEASE##*.}
os_RELEASE=${os_RELEASE%.*}
os_PACKAGE=""
if [[ "$os_RELEASE" =~ "10.7" ]]; then
os_CODENAME="lion"
elif [[ "$os_RELEASE" =~ "10.6" ]]; then
os_CODENAME="snow leopard"
elif [[ "$os_RELEASE" =~ "10.5" ]]; then
os_CODENAME="leopard"
elif [[ "$os_RELEASE" =~ "10.4" ]]; then
os_CODENAME="tiger"
elif [[ "$os_RELEASE" =~ "10.3" ]]; then
os_CODENAME="panther"
else
os_CODENAME=""
fi
elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
os_VENDOR=$(lsb_release -i -s)
os_RELEASE=$(lsb_release -r -s)
os_UPDATE=""
if [[ "Debian,Ubuntu" =~ $os_VENDOR ]]; then
os_PACKAGE="deb"
else
os_PACKAGE="rpm"
fi
os_CODENAME=$(lsb_release -c -s)
elif [[ -r /etc/redhat-release ]]; then
# Red Hat Enterprise Linux Server release 5.5 (Tikanga)
# CentOS release 5.5 (Final)
# CentOS Linux release 6.0 (Final)
# Fedora release 16 (Verne)
os_CODENAME=""
for r in "Red Hat" CentOS Fedora; do
os_VENDOR=$r
if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
ver=`sed -e 's/^.* \(.*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
os_CODENAME=${ver#*|}
os_RELEASE=${ver%|*}
os_UPDATE=${os_RELEASE##*.}
os_RELEASE=${os_RELEASE%.*}
break
fi
os_VENDOR=""
done
os_PACKAGE="rpm"
fi
export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
}
# git clone only if directory doesn't exist already. Since ``DEST`` might not
# be owned by the installation user, we create the directory and change the
# ownership to the proper user.
@ -115,6 +183,42 @@ function git_clone {
}
# Comment an option in an INI file
# optset config-file section option
function inicomment() {
local file=$1
local section=$2
local option=$3
sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file
}
# Get an option from an INI file
# optget config-file section option
function iniget() {
local file=$1
local section=$2
local option=$3
local line
line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
echo ${line#*=}
}
# Set an option in an INI file
# This is NOT a complete option setter, it assumes that the section and
# option already exist in the INI file. If the section does not exist,
# nothing happens.
# optset config-file section option value
function iniset() {
local file=$1
local section=$2
local option=$3
local value=$4
sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
}
# is_service_enabled() checks if the service(s) specified as arguments are
# enabled by the user in **ENABLED_SERVICES**.
#
@ -138,6 +242,20 @@ function is_service_enabled() {
}
# Distro-agnostic package installer
# install_package package [package ...]
function install_package() {
if [[ -z "$os_PACKAGE" ]]; then
GetOSVersion
fi
if [[ "$os_PACKAGE" = "deb" ]]; then
apt_get install "$@"
else
yum_install "$@"
fi
}
# Test if the named environment variable is set and not zero length
# is_set env-var
function is_set() {
@ -153,10 +271,39 @@ function is_set() {
# pip_install package [package ...]
function pip_install {
[[ "$OFFLINE" = "True" || -z "$@" ]] && return
if [[ -z "$os_PACKAGE" ]]; then
GetOSVersion
fi
if [[ "$os_PACKAGE" = "deb" ]]; then
CMD_PIP=/usr/bin/pip
else
CMD_PIP=/usr/bin/pip-python
fi
sudo PIP_DOWNLOAD_CACHE=/var/cache/pip \
HTTP_PROXY=$http_proxy \
HTTPS_PROXY=$https_proxy \
pip install --use-mirrors $@
$CMD_PIP install --use-mirrors $@
}
# Service wrapper to restart services
# restart_service service-name
function restart_service() {
sudo /usr/sbin/service $1 restart
}
# Service wrapper to start services
# start_service service-name
function start_service() {
sudo /usr/sbin/service $1 start
}
# Service wrapper to stop services
# stop_service service-name
function stop_service() {
sudo /usr/sbin/service $1 stop
}
@ -172,5 +319,17 @@ function trueorfalse() {
echo "$default"
}
# yum wrapper to set arguments correctly
# yum_install package [package ...]
function yum_install() {
[[ "$OFFLINE" = "True" ]] && return
local sudo="sudo"
[[ "$(id -u)" = "0" ]] && sudo="env"
$sudo http_proxy=$http_proxy https_proxy=$https_proxy \
yum install -y "$@"
}
# Restore xtrace
$XTRACE

View File

@ -107,7 +107,7 @@ if [[ $EUID -eq 0 ]]; then
# since this script runs as a normal user, we need to give that user
# ability to run sudo
dpkg -l sudo || apt_get update && apt_get install sudo
dpkg -l sudo || apt_get update && install_package sudo
if ! getent passwd stack >/dev/null; then
echo "Creating a user called stack"
@ -268,6 +268,7 @@ function read_password {
set -o xtrace
}
# Nova Network Configuration
# --------------------------
@ -590,7 +591,7 @@ function get_packages() {
# install apt requirements
apt_get update
apt_get install $(get_packages $FILES/apts)
install_package $(get_packages $FILES/apts)
# install python requirements
pip_install $(get_packages $FILES/pips | sort -u)
@ -677,7 +678,7 @@ fi
# ------
if [[ $SYSLOG != "False" ]]; then
apt_get install -y rsyslog-relp
install_package rsyslog-relp
if [[ "$SYSLOG_HOST" = "$HOST_IP" ]]; then
# Configure the master host to receive
cat <<EOF >/tmp/90-stack-m.conf
@ -692,7 +693,7 @@ EOF
EOF
sudo mv /tmp/90-stack-s.conf /etc/rsyslog.d
fi
sudo /usr/sbin/service rsyslog restart
restart_service rsyslog
fi
@ -703,7 +704,7 @@ if is_service_enabled rabbit; then
# Install and start rabbitmq-server
# the temp file is necessary due to LP: #878600
tfile=$(mktemp)
apt_get install rabbitmq-server > "$tfile" 2>&1
install_package rabbitmq-server > "$tfile" 2>&1
cat "$tfile"
rm -f "$tfile"
# change the rabbit password since the default is "guest"
@ -738,13 +739,13 @@ EOF
fi
# Install and start mysql-server
apt_get install mysql-server
install_package mysql-server
# Update the DB to give user $MYSQL_USER@% full control of the all databases:
sudo mysql -uroot -p$MYSQL_PASSWORD -h127.0.0.1 -e "GRANT ALL PRIVILEGES ON *.* TO '$MYSQL_USER'@'%' identified by '$MYSQL_PASSWORD';"
# Edit /etc/mysql/my.cnf to change bind-address from localhost (127.0.0.1) to any (0.0.0.0) and restart the mysql service:
sudo sed -i 's/127.0.0.1/0.0.0.0/g' /etc/mysql/my.cnf
sudo service mysql restart
restart_service mysql
fi
# Our screenrc file builder
@ -801,7 +802,7 @@ screen -r stack -X hardstatus alwayslastline "%-Lw%{= BW}%50>%n%f* %t%{-}%+Lw%<
if is_service_enabled horizon; then
# Install apache2, which is NOPRIME'd
apt_get install apache2 libapache2-mod-wsgi
install_package apache2 libapache2-mod-wsgi
# Remove stale session database.
@ -826,7 +827,7 @@ if is_service_enabled horizon; then
s,%GROUP%,$APACHE_GROUP,g;
s,%HORIZON_DIR%,$HORIZON_DIR,g;
" -i /etc/apache2/sites-enabled/000-default
sudo service apache2 restart
restart_service apache2
fi
@ -905,8 +906,7 @@ if is_service_enabled q-svc; then
# Install deps
# FIXME add to files/apts/quantum, but don't install if not needed!
kernel_version=`cat /proc/version | cut -d " " -f3`
apt_get install linux-headers-$kernel_version
apt_get install openvswitch-switch openvswitch-datapath-dkms
install_package openvswitch-switch openvswitch-datapath-dkms linux-headers-$kernel_version
# Create database for the plugin/agent
if is_service_enabled mysql; then
mysql -u$MYSQL_USER -p$MYSQL_PASSWORD -e 'DROP DATABASE IF EXISTS ovs_quantum;'
@ -1019,7 +1019,7 @@ if is_service_enabled n-cpu; then
# Virtualization Configuration
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
apt_get install libvirt-bin
install_package libvirt-bin
# Force IP forwarding on, just on case
sudo sysctl -w net.ipv4.ip_forward=1
@ -1043,7 +1043,7 @@ if is_service_enabled n-cpu; then
# to simulate multiple systems.
if [[ "$LIBVIRT_TYPE" == "lxc" ]]; then
if [[ "$DISTRO" > natty ]]; then
apt_get install cgroup-lite
install_package cgroup-lite
else
cgline="none /cgroup cgroup cpuacct,memory,devices,cpu,freezer,blkio 0 0"
sudo mkdir -p /cgroup
@ -1062,7 +1062,7 @@ if is_service_enabled n-cpu; then
# libvirt detects various settings on startup, as we potentially changed
# the system configuration (modules, filesystems), we need to restart
# libvirt to detect those changes.
sudo /etc/init.d/libvirt-bin restart
restart_service libvirt-bin
# Instance Storage
@ -1113,7 +1113,7 @@ fi
# Storage Service
if is_service_enabled swift; then
# Install memcached for swift.
apt_get install memcached
install_package memcached
# We first do a bit of setup by creating the directories and
# changing the permissions so we can run it as our user.
@ -1297,7 +1297,7 @@ if is_service_enabled n-vol; then
# By default, the backing file is 2G in size, and is stored in /opt/stack.
# install the package
apt_get install tgt
install_package tgt
if ! sudo vgs $VOLUME_GROUP; then
VOLUME_BACKING_FILE=${VOLUME_BACKING_FILE:-$DEST/nova-volumes-backing-file}

View File

@ -37,3 +37,89 @@ if [[ $? = 0 ]]; then
echo "die_if_not_set [X='' false] Failed"
fi
echo "Testing INI functions"
cat >test.ini <<EOF
[default]
# comment an option
#log_file=./log.conf
log_file=/etc/log.conf
handlers=do not disturb
[aaa]
# the commented option should not change
#handlers=cc,dd
handlers = aa, bb
[bbb]
handlers=ee,ff
EOF
# Test with spaces
VAL=$(iniget test.ini aaa handlers)
if [[ "$VAL" == "aa, bb" ]]; then
echo "OK: $VAL"
else
echo "iniget failed: $VAL"
fi
iniset test.ini aaa handlers "11, 22"
VAL=$(iniget test.ini aaa handlers)
if [[ "$VAL" == "11, 22" ]]; then
echo "OK: $VAL"
else
echo "iniget failed: $VAL"
fi
# Test without spaces, end of file
VAL=$(iniget test.ini bbb handlers)
if [[ "$VAL" == "ee,ff" ]]; then
echo "OK: $VAL"
else
echo "iniget failed: $VAL"
fi
iniset test.ini bbb handlers "33,44"
VAL=$(iniget test.ini bbb handlers)
if [[ "$VAL" == "33,44" ]]; then
echo "OK: $VAL"
else
echo "iniget failed: $VAL"
fi
# Test section not exist
VAL=$(iniget test.ini zzz handlers)
if [[ -z "$VAL" ]]; then
echo "OK"
else
echo "iniget failed: $VAL"
fi
iniset test.ini zzz handlers "999"
VAL=$(iniget test.ini zzz handlers)
if [[ -z "$VAL" ]]; then
echo "OK"
else
echo "iniget failed: $VAL"
fi
# Test comments
inicomment test.ini aaa handlers
VAL=$(iniget test.ini aaa handlers)
if [[ -z "$VAL" ]]; then
echo "OK"
else
echo "inicomment failed: $VAL"
fi