Skip to content

Commit 3b7c794

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "A) Add/move functions to 'functions' file"
2 parents e75e8b5 + 13dc5cc commit 3b7c794

File tree

3 files changed

+263
-18
lines changed

3 files changed

+263
-18
lines changed

functions

Lines changed: 161 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# functions - Common functions used by DevStack components
2+
#
3+
# ENABLED_SERVICES is used by is_service_enabled()
4+
25

36
# Save trace setting
47
XTRACE=$(set +o | grep xtrace)
58
set +o xtrace
69

710

811
# apt-get wrapper to set arguments correctly
9-
# apt_get package [package ...]
12+
# apt_get operation package [package ...]
1013
function apt_get() {
1114
[[ "$OFFLINE" = "True" || -z "$@" ]] && return
1215
local sudo="sudo"
@@ -70,6 +73,71 @@ function get_field() {
7073
}
7174

7275

76+
# Determine OS Vendor, Release and Update
77+
# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
78+
# Returns results in global variables:
79+
# os_VENDOR - vendor name
80+
# os_RELEASE - release
81+
# os_UPDATE - update
82+
# os_PACKAGE - package type
83+
# os_CODENAME - vendor's codename for release
84+
# GetOSVersion
85+
GetOSVersion() {
86+
# Figure out which vendor we are
87+
if [[ -n "`which sw_vers 2>/dev/null`" ]]; then
88+
# OS/X
89+
os_VENDOR=`sw_vers -productName`
90+
os_RELEASE=`sw_vers -productVersion`
91+
os_UPDATE=${os_RELEASE##*.}
92+
os_RELEASE=${os_RELEASE%.*}
93+
os_PACKAGE=""
94+
if [[ "$os_RELEASE" =~ "10.7" ]]; then
95+
os_CODENAME="lion"
96+
elif [[ "$os_RELEASE" =~ "10.6" ]]; then
97+
os_CODENAME="snow leopard"
98+
elif [[ "$os_RELEASE" =~ "10.5" ]]; then
99+
os_CODENAME="leopard"
100+
elif [[ "$os_RELEASE" =~ "10.4" ]]; then
101+
os_CODENAME="tiger"
102+
elif [[ "$os_RELEASE" =~ "10.3" ]]; then
103+
os_CODENAME="panther"
104+
else
105+
os_CODENAME=""
106+
fi
107+
elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
108+
os_VENDOR=$(lsb_release -i -s)
109+
os_RELEASE=$(lsb_release -r -s)
110+
os_UPDATE=""
111+
if [[ "Debian,Ubuntu" =~ $os_VENDOR ]]; then
112+
os_PACKAGE="deb"
113+
else
114+
os_PACKAGE="rpm"
115+
fi
116+
os_CODENAME=$(lsb_release -c -s)
117+
elif [[ -r /etc/redhat-release ]]; then
118+
# Red Hat Enterprise Linux Server release 5.5 (Tikanga)
119+
# CentOS release 5.5 (Final)
120+
# CentOS Linux release 6.0 (Final)
121+
# Fedora release 16 (Verne)
122+
os_CODENAME=""
123+
for r in "Red Hat" CentOS Fedora; do
124+
os_VENDOR=$r
125+
if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
126+
ver=`sed -e 's/^.* \(.*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
127+
os_CODENAME=${ver#*|}
128+
os_RELEASE=${ver%|*}
129+
os_UPDATE=${os_RELEASE##*.}
130+
os_RELEASE=${os_RELEASE%.*}
131+
break
132+
fi
133+
os_VENDOR=""
134+
done
135+
os_PACKAGE="rpm"
136+
fi
137+
export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
138+
}
139+
140+
73141
# git clone only if directory doesn't exist already. Since ``DEST`` might not
74142
# be owned by the installation user, we create the directory and change the
75143
# ownership to the proper user.
@@ -115,6 +183,42 @@ function git_clone {
115183
}
116184

117185

186+
# Comment an option in an INI file
187+
# optset config-file section option
188+
function inicomment() {
189+
local file=$1
190+
local section=$2
191+
local option=$3
192+
sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file
193+
}
194+
195+
196+
# Get an option from an INI file
197+
# optget config-file section option
198+
function iniget() {
199+
local file=$1
200+
local section=$2
201+
local option=$3
202+
local line
203+
line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
204+
echo ${line#*=}
205+
}
206+
207+
208+
# Set an option in an INI file
209+
# This is NOT a complete option setter, it assumes that the section and
210+
# option already exist in the INI file. If the section does not exist,
211+
# nothing happens.
212+
# optset config-file section option value
213+
function iniset() {
214+
local file=$1
215+
local section=$2
216+
local option=$3
217+
local value=$4
218+
sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
219+
}
220+
221+
118222
# is_service_enabled() checks if the service(s) specified as arguments are
119223
# enabled by the user in **ENABLED_SERVICES**.
120224
#
@@ -138,6 +242,20 @@ function is_service_enabled() {
138242
}
139243

140244

245+
# Distro-agnostic package installer
246+
# install_package package [package ...]
247+
function install_package() {
248+
if [[ -z "$os_PACKAGE" ]]; then
249+
GetOSVersion
250+
fi
251+
if [[ "$os_PACKAGE" = "deb" ]]; then
252+
apt_get install "$@"
253+
else
254+
yum_install "$@"
255+
fi
256+
}
257+
258+
141259
# Test if the named environment variable is set and not zero length
142260
# is_set env-var
143261
function is_set() {
@@ -153,10 +271,39 @@ function is_set() {
153271
# pip_install package [package ...]
154272
function pip_install {
155273
[[ "$OFFLINE" = "True" || -z "$@" ]] && return
274+
if [[ -z "$os_PACKAGE" ]]; then
275+
GetOSVersion
276+
fi
277+
if [[ "$os_PACKAGE" = "deb" ]]; then
278+
CMD_PIP=/usr/bin/pip
279+
else
280+
CMD_PIP=/usr/bin/pip-python
281+
fi
156282
sudo PIP_DOWNLOAD_CACHE=/var/cache/pip \
157283
HTTP_PROXY=$http_proxy \
158284
HTTPS_PROXY=$https_proxy \
159-
pip install --use-mirrors $@
285+
$CMD_PIP install --use-mirrors $@
286+
}
287+
288+
289+
# Service wrapper to restart services
290+
# restart_service service-name
291+
function restart_service() {
292+
sudo /usr/sbin/service $1 restart
293+
}
294+
295+
296+
# Service wrapper to start services
297+
# start_service service-name
298+
function start_service() {
299+
sudo /usr/sbin/service $1 start
300+
}
301+
302+
303+
# Service wrapper to stop services
304+
# stop_service service-name
305+
function stop_service() {
306+
sudo /usr/sbin/service $1 stop
160307
}
161308

162309

@@ -172,5 +319,17 @@ function trueorfalse() {
172319
echo "$default"
173320
}
174321

322+
323+
# yum wrapper to set arguments correctly
324+
# yum_install package [package ...]
325+
function yum_install() {
326+
[[ "$OFFLINE" = "True" ]] && return
327+
local sudo="sudo"
328+
[[ "$(id -u)" = "0" ]] && sudo="env"
329+
$sudo http_proxy=$http_proxy https_proxy=$https_proxy \
330+
yum install -y "$@"
331+
}
332+
333+
175334
# Restore xtrace
176335
$XTRACE

stack.sh

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ if [[ $EUID -eq 0 ]]; then
107107

108108
# since this script runs as a normal user, we need to give that user
109109
# ability to run sudo
110-
dpkg -l sudo || apt_get update && apt_get install sudo
110+
dpkg -l sudo || apt_get update && install_package sudo
111111

112112
if ! getent passwd stack >/dev/null; then
113113
echo "Creating a user called stack"
@@ -268,6 +268,7 @@ function read_password {
268268
set -o xtrace
269269
}
270270

271+
271272
# Nova Network Configuration
272273
# --------------------------
273274

@@ -590,7 +591,7 @@ function get_packages() {
590591

591592
# install apt requirements
592593
apt_get update
593-
apt_get install $(get_packages $FILES/apts)
594+
install_package $(get_packages $FILES/apts)
594595

595596
# install python requirements
596597
pip_install $(get_packages $FILES/pips | sort -u)
@@ -677,7 +678,7 @@ fi
677678
# ------
678679

679680
if [[ $SYSLOG != "False" ]]; then
680-
apt_get install -y rsyslog-relp
681+
install_package rsyslog-relp
681682
if [[ "$SYSLOG_HOST" = "$HOST_IP" ]]; then
682683
# Configure the master host to receive
683684
cat <<EOF >/tmp/90-stack-m.conf
@@ -692,7 +693,7 @@ EOF
692693
EOF
693694
sudo mv /tmp/90-stack-s.conf /etc/rsyslog.d
694695
fi
695-
sudo /usr/sbin/service rsyslog restart
696+
restart_service rsyslog
696697
fi
697698

698699

@@ -703,7 +704,7 @@ if is_service_enabled rabbit; then
703704
# Install and start rabbitmq-server
704705
# the temp file is necessary due to LP: #878600
705706
tfile=$(mktemp)
706-
apt_get install rabbitmq-server > "$tfile" 2>&1
707+
install_package rabbitmq-server > "$tfile" 2>&1
707708
cat "$tfile"
708709
rm -f "$tfile"
709710
# change the rabbit password since the default is "guest"
@@ -738,13 +739,13 @@ EOF
738739
fi
739740

740741
# Install and start mysql-server
741-
apt_get install mysql-server
742+
install_package mysql-server
742743
# Update the DB to give user ‘$MYSQL_USER’@’%’ full control of the all databases:
743744
sudo mysql -uroot -p$MYSQL_PASSWORD -h127.0.0.1 -e "GRANT ALL PRIVILEGES ON *.* TO '$MYSQL_USER'@'%' identified by '$MYSQL_PASSWORD';"
744745

745746
# 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:
746747
sudo sed -i 's/127.0.0.1/0.0.0.0/g' /etc/mysql/my.cnf
747-
sudo service mysql restart
748+
restart_service mysql
748749
fi
749750

750751
# Our screenrc file builder
@@ -801,7 +802,7 @@ screen -r stack -X hardstatus alwayslastline "%-Lw%{= BW}%50>%n%f* %t%{-}%+Lw%<
801802
if is_service_enabled horizon; then
802803

803804
# Install apache2, which is NOPRIME'd
804-
apt_get install apache2 libapache2-mod-wsgi
805+
install_package apache2 libapache2-mod-wsgi
805806

806807

807808
# Remove stale session database.
@@ -826,7 +827,7 @@ if is_service_enabled horizon; then
826827
s,%GROUP%,$APACHE_GROUP,g;
827828
s,%HORIZON_DIR%,$HORIZON_DIR,g;
828829
" -i /etc/apache2/sites-enabled/000-default
829-
sudo service apache2 restart
830+
restart_service apache2
830831
fi
831832

832833

@@ -905,8 +906,7 @@ if is_service_enabled q-svc; then
905906
# Install deps
906907
# FIXME add to files/apts/quantum, but don't install if not needed!
907908
kernel_version=`cat /proc/version | cut -d " " -f3`
908-
apt_get install linux-headers-$kernel_version
909-
apt_get install openvswitch-switch openvswitch-datapath-dkms
909+
install_package openvswitch-switch openvswitch-datapath-dkms linux-headers-$kernel_version
910910
# Create database for the plugin/agent
911911
if is_service_enabled mysql; then
912912
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
10191019

10201020
# Virtualization Configuration
10211021
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1022-
apt_get install libvirt-bin
1022+
install_package libvirt-bin
10231023

10241024
# Force IP forwarding on, just on case
10251025
sudo sysctl -w net.ipv4.ip_forward=1
@@ -1043,7 +1043,7 @@ if is_service_enabled n-cpu; then
10431043
# to simulate multiple systems.
10441044
if [[ "$LIBVIRT_TYPE" == "lxc" ]]; then
10451045
if [[ "$DISTRO" > natty ]]; then
1046-
apt_get install cgroup-lite
1046+
install_package cgroup-lite
10471047
else
10481048
cgline="none /cgroup cgroup cpuacct,memory,devices,cpu,freezer,blkio 0 0"
10491049
sudo mkdir -p /cgroup
@@ -1062,7 +1062,7 @@ if is_service_enabled n-cpu; then
10621062
# libvirt detects various settings on startup, as we potentially changed
10631063
# the system configuration (modules, filesystems), we need to restart
10641064
# libvirt to detect those changes.
1065-
sudo /etc/init.d/libvirt-bin restart
1065+
restart_service libvirt-bin
10661066

10671067

10681068
# Instance Storage
@@ -1113,7 +1113,7 @@ fi
11131113
# Storage Service
11141114
if is_service_enabled swift; then
11151115
# Install memcached for swift.
1116-
apt_get install memcached
1116+
install_package memcached
11171117

11181118
# We first do a bit of setup by creating the directories and
11191119
# changing the permissions so we can run it as our user.
@@ -1297,7 +1297,7 @@ if is_service_enabled n-vol; then
12971297
# By default, the backing file is 2G in size, and is stored in /opt/stack.
12981298

12991299
# install the package
1300-
apt_get install tgt
1300+
install_package tgt
13011301

13021302
if ! sudo vgs $VOLUME_GROUP; then
13031303
VOLUME_BACKING_FILE=${VOLUME_BACKING_FILE:-$DEST/nova-volumes-backing-file}

0 commit comments

Comments
 (0)