Browse code

A) Add/move functions to 'functions' file

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

Rebased

Change-Id: I570dba5ed4d2b988cdd1771cf6bed0aaf8e0fe27

Dean Troyer authored on 2012/03/28 04:50:45
Showing 3 changed files
... ...
@@ -1,4 +1,7 @@
1 1
 # functions - Common functions used by DevStack components
2
+#
3
+# ENABLED_SERVICES is used by is_service_enabled()
4
+
2 5
 
3 6
 # Save trace setting
4 7
 XTRACE=$(set +o | grep xtrace)
... ...
@@ -6,7 +9,7 @@ set +o xtrace
6 6
 
7 7
 
8 8
 # apt-get wrapper to set arguments correctly
9
-# apt_get package [package ...]
9
+# apt_get operation package [package ...]
10 10
 function apt_get() {
11 11
     [[ "$OFFLINE" = "True" || -z "$@" ]] && return
12 12
     local sudo="sudo"
... ...
@@ -70,6 +73,71 @@ function get_field() {
70 70
 }
71 71
 
72 72
 
73
+# Determine OS Vendor, Release and Update
74
+# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
75
+# Returns results in global variables:
76
+# os_VENDOR - vendor name
77
+# os_RELEASE - release
78
+# os_UPDATE - update
79
+# os_PACKAGE - package type
80
+# os_CODENAME - vendor's codename for release
81
+# GetOSVersion
82
+GetOSVersion() {
83
+    # Figure out which vendor we are
84
+    if [[ -n "`which sw_vers 2>/dev/null`" ]]; then
85
+        # OS/X
86
+        os_VENDOR=`sw_vers -productName`
87
+        os_RELEASE=`sw_vers -productVersion`
88
+        os_UPDATE=${os_RELEASE##*.}
89
+        os_RELEASE=${os_RELEASE%.*}
90
+        os_PACKAGE=""
91
+        if [[ "$os_RELEASE" =~ "10.7" ]]; then
92
+            os_CODENAME="lion"
93
+        elif [[ "$os_RELEASE" =~ "10.6" ]]; then
94
+            os_CODENAME="snow leopard"
95
+        elif [[ "$os_RELEASE" =~ "10.5" ]]; then
96
+            os_CODENAME="leopard"
97
+        elif [[ "$os_RELEASE" =~ "10.4" ]]; then
98
+            os_CODENAME="tiger"
99
+        elif [[ "$os_RELEASE" =~ "10.3" ]]; then
100
+            os_CODENAME="panther"
101
+        else
102
+            os_CODENAME=""
103
+        fi
104
+    elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
105
+        os_VENDOR=$(lsb_release -i -s)
106
+        os_RELEASE=$(lsb_release -r -s)
107
+        os_UPDATE=""
108
+        if [[ "Debian,Ubuntu" =~ $os_VENDOR ]]; then
109
+            os_PACKAGE="deb"
110
+        else
111
+            os_PACKAGE="rpm"
112
+        fi
113
+        os_CODENAME=$(lsb_release -c -s)
114
+    elif [[ -r /etc/redhat-release ]]; then
115
+        # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
116
+        # CentOS release 5.5 (Final)
117
+        # CentOS Linux release 6.0 (Final)
118
+        # Fedora release 16 (Verne)
119
+        os_CODENAME=""
120
+        for r in "Red Hat" CentOS Fedora; do
121
+            os_VENDOR=$r
122
+            if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
123
+                ver=`sed -e 's/^.* \(.*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
124
+                os_CODENAME=${ver#*|}
125
+                os_RELEASE=${ver%|*}
126
+                os_UPDATE=${os_RELEASE##*.}
127
+                os_RELEASE=${os_RELEASE%.*}
128
+                break
129
+            fi
130
+            os_VENDOR=""
131
+        done
132
+        os_PACKAGE="rpm"
133
+    fi
134
+    export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
135
+}
136
+
137
+
73 138
 # git clone only if directory doesn't exist already.  Since ``DEST`` might not
74 139
 # be owned by the installation user, we create the directory and change the
75 140
 # ownership to the proper user.
... ...
@@ -115,6 +183,42 @@ function git_clone {
115 115
 }
116 116
 
117 117
 
118
+# Comment an option in an INI file
119
+# optset config-file section option
120
+function inicomment() {
121
+    local file=$1
122
+    local section=$2
123
+    local option=$3
124
+    sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file
125
+}
126
+
127
+
128
+# Get an option from an INI file
129
+# optget config-file section option
130
+function iniget() {
131
+    local file=$1
132
+    local section=$2
133
+    local option=$3
134
+    local line
135
+    line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
136
+    echo ${line#*=}
137
+}
138
+
139
+
140
+# Set an option in an INI file
141
+# This is NOT a complete option setter, it assumes that the section and
142
+# option already exist in the INI file.  If the section does not exist,
143
+# nothing happens.
144
+# optset config-file section option value
145
+function iniset() {
146
+    local file=$1
147
+    local section=$2
148
+    local option=$3
149
+    local value=$4
150
+    sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
151
+}
152
+
153
+
118 154
 # is_service_enabled() checks if the service(s) specified as arguments are
119 155
 # enabled by the user in **ENABLED_SERVICES**.
120 156
 #
... ...
@@ -138,6 +242,20 @@ function is_service_enabled() {
138 138
 }
139 139
 
140 140
 
141
+# Distro-agnostic package installer
142
+# install_package package [package ...]
143
+function install_package() {
144
+    if [[ -z "$os_PACKAGE" ]]; then
145
+        GetOSVersion
146
+    fi
147
+    if [[ "$os_PACKAGE" = "deb" ]]; then
148
+        apt_get install "$@"
149
+    else
150
+        yum_install "$@"
151
+    fi
152
+}
153
+
154
+
141 155
 # Test if the named environment variable is set and not zero length
142 156
 # is_set env-var
143 157
 function is_set() {
... ...
@@ -153,10 +271,39 @@ function is_set() {
153 153
 # pip_install package [package ...]
154 154
 function pip_install {
155 155
     [[ "$OFFLINE" = "True" || -z "$@" ]] && return
156
+    if [[ -z "$os_PACKAGE" ]]; then
157
+        GetOSVersion
158
+    fi
159
+    if [[ "$os_PACKAGE" = "deb" ]]; then
160
+        CMD_PIP=/usr/bin/pip
161
+    else
162
+        CMD_PIP=/usr/bin/pip-python
163
+    fi
156 164
     sudo PIP_DOWNLOAD_CACHE=/var/cache/pip \
157 165
         HTTP_PROXY=$http_proxy \
158 166
         HTTPS_PROXY=$https_proxy \
159
-        pip install --use-mirrors $@
167
+        $CMD_PIP install --use-mirrors $@
168
+}
169
+
170
+
171
+# Service wrapper to restart services
172
+# restart_service service-name
173
+function restart_service() {
174
+    sudo /usr/sbin/service $1 restart
175
+}
176
+
177
+
178
+# Service wrapper to start services
179
+# start_service service-name
180
+function start_service() {
181
+    sudo /usr/sbin/service $1 start
182
+}
183
+
184
+
185
+# Service wrapper to stop services
186
+# stop_service service-name
187
+function stop_service() {
188
+    sudo /usr/sbin/service $1 stop
160 189
 }
161 190
 
162 191
 
... ...
@@ -172,5 +319,17 @@ function trueorfalse() {
172 172
     echo "$default"
173 173
 }
174 174
 
175
+
176
+# yum wrapper to set arguments correctly
177
+# yum_install package [package ...]
178
+function yum_install() {
179
+    [[ "$OFFLINE" = "True" ]] && return
180
+    local sudo="sudo"
181
+    [[ "$(id -u)" = "0" ]] && sudo="env"
182
+    $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
183
+        yum install -y "$@"
184
+}
185
+
186
+
175 187
 # Restore xtrace
176 188
 $XTRACE
... ...
@@ -107,7 +107,7 @@ if [[ $EUID -eq 0 ]]; then
107 107
 
108 108
     # since this script runs as a normal user, we need to give that user
109 109
     # 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
111 111
 
112 112
     if ! getent passwd stack >/dev/null; then
113 113
         echo "Creating a user called stack"
... ...
@@ -268,6 +268,7 @@ function read_password {
268 268
     set -o xtrace
269 269
 }
270 270
 
271
+
271 272
 # Nova Network Configuration
272 273
 # --------------------------
273 274
 
... ...
@@ -590,7 +591,7 @@ function get_packages() {
590 590
 
591 591
 # install apt requirements
592 592
 apt_get update
593
-apt_get install $(get_packages $FILES/apts)
593
+install_package $(get_packages $FILES/apts)
594 594
 
595 595
 # install python requirements
596 596
 pip_install $(get_packages $FILES/pips | sort -u)
... ...
@@ -677,7 +678,7 @@ fi
677 677
 # ------
678 678
 
679 679
 if [[ $SYSLOG != "False" ]]; then
680
-    apt_get install -y rsyslog-relp
680
+    install_package rsyslog-relp
681 681
     if [[ "$SYSLOG_HOST" = "$HOST_IP" ]]; then
682 682
         # Configure the master host to receive
683 683
         cat <<EOF >/tmp/90-stack-m.conf
... ...
@@ -692,7 +693,7 @@ EOF
692 692
 EOF
693 693
         sudo mv /tmp/90-stack-s.conf /etc/rsyslog.d
694 694
     fi
695
-    sudo /usr/sbin/service rsyslog restart
695
+    restart_service rsyslog
696 696
 fi
697 697
 
698 698
 
... ...
@@ -703,7 +704,7 @@ if is_service_enabled rabbit; then
703 703
     # Install and start rabbitmq-server
704 704
     # the temp file is necessary due to LP: #878600
705 705
     tfile=$(mktemp)
706
-    apt_get install rabbitmq-server > "$tfile" 2>&1
706
+    install_package rabbitmq-server > "$tfile" 2>&1
707 707
     cat "$tfile"
708 708
     rm -f "$tfile"
709 709
     # change the rabbit password since the default is "guest"
... ...
@@ -738,13 +739,13 @@ EOF
738 738
     fi
739 739
 
740 740
     # Install and start mysql-server
741
-    apt_get install mysql-server
741
+    install_package mysql-server
742 742
     # Update the DB to give user ‘$MYSQL_USER’@’%’ full control of the all databases:
743 743
     sudo mysql -uroot -p$MYSQL_PASSWORD -h127.0.0.1 -e "GRANT ALL PRIVILEGES ON *.* TO '$MYSQL_USER'@'%' identified by '$MYSQL_PASSWORD';"
744 744
 
745 745
     # 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:
746 746
     sudo sed -i 's/127.0.0.1/0.0.0.0/g' /etc/mysql/my.cnf
747
-    sudo service mysql restart
747
+    restart_service mysql
748 748
 fi
749 749
 
750 750
 # Our screenrc file builder
... ...
@@ -801,7 +802,7 @@ screen -r stack -X hardstatus alwayslastline "%-Lw%{= BW}%50>%n%f* %t%{-}%+Lw%<
801 801
 if is_service_enabled horizon; then
802 802
 
803 803
     # Install apache2, which is NOPRIME'd
804
-    apt_get install apache2 libapache2-mod-wsgi
804
+    install_package apache2 libapache2-mod-wsgi
805 805
 
806 806
 
807 807
     # Remove stale session database.
... ...
@@ -826,7 +827,7 @@ if is_service_enabled horizon; then
826 826
         s,%GROUP%,$APACHE_GROUP,g;
827 827
         s,%HORIZON_DIR%,$HORIZON_DIR,g;
828 828
     " -i /etc/apache2/sites-enabled/000-default
829
-    sudo service apache2 restart
829
+    restart_service apache2
830 830
 fi
831 831
 
832 832
 
... ...
@@ -905,8 +906,7 @@ if is_service_enabled q-svc; then
905 905
         # Install deps
906 906
         # FIXME add to files/apts/quantum, but don't install if not needed!
907 907
         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
908
+        install_package openvswitch-switch openvswitch-datapath-dkms linux-headers-$kernel_version
910 909
         # Create database for the plugin/agent
911 910
         if is_service_enabled mysql; then
912 911
             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
1019 1019
 
1020 1020
     # Virtualization Configuration
1021 1021
     # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1022
-    apt_get install libvirt-bin
1022
+    install_package libvirt-bin
1023 1023
 
1024 1024
     # Force IP forwarding on, just on case
1025 1025
     sudo sysctl -w net.ipv4.ip_forward=1
... ...
@@ -1043,7 +1043,7 @@ if is_service_enabled n-cpu; then
1043 1043
     # to simulate multiple systems.
1044 1044
     if [[ "$LIBVIRT_TYPE" == "lxc" ]]; then
1045 1045
         if [[ "$DISTRO" > natty ]]; then
1046
-            apt_get install cgroup-lite
1046
+            install_package cgroup-lite
1047 1047
         else
1048 1048
             cgline="none /cgroup cgroup cpuacct,memory,devices,cpu,freezer,blkio 0 0"
1049 1049
             sudo mkdir -p /cgroup
... ...
@@ -1062,7 +1062,7 @@ if is_service_enabled n-cpu; then
1062 1062
     # libvirt detects various settings on startup, as we potentially changed
1063 1063
     # the system configuration (modules, filesystems), we need to restart
1064 1064
     # libvirt to detect those changes.
1065
-    sudo /etc/init.d/libvirt-bin restart
1065
+    restart_service libvirt-bin
1066 1066
 
1067 1067
 
1068 1068
     # Instance Storage
... ...
@@ -1113,7 +1113,7 @@ fi
1113 1113
 # Storage Service
1114 1114
 if is_service_enabled swift; then
1115 1115
     # Install memcached for swift.
1116
-    apt_get install memcached
1116
+    install_package memcached
1117 1117
 
1118 1118
     # We first do a bit of setup by creating the directories and
1119 1119
     # changing the permissions so we can run it as our user.
... ...
@@ -1297,7 +1297,7 @@ if is_service_enabled n-vol; then
1297 1297
     # By default, the backing file is 2G in size, and is stored in /opt/stack.
1298 1298
 
1299 1299
     # install the package
1300
-    apt_get install tgt
1300
+    install_package tgt
1301 1301
 
1302 1302
     if ! sudo vgs $VOLUME_GROUP; then
1303 1303
         VOLUME_BACKING_FILE=${VOLUME_BACKING_FILE:-$DEST/nova-volumes-backing-file}
... ...
@@ -37,3 +37,89 @@ if [[ $? = 0 ]]; then
37 37
     echo "die_if_not_set [X='' false] Failed"
38 38
 fi
39 39
 
40
+
41
+echo "Testing INI functions"
42
+
43
+cat >test.ini <<EOF
44
+[default]
45
+# comment an option
46
+#log_file=./log.conf
47
+log_file=/etc/log.conf
48
+handlers=do not disturb
49
+
50
+[aaa]
51
+# the commented option should not change
52
+#handlers=cc,dd
53
+handlers = aa, bb
54
+
55
+[bbb]
56
+handlers=ee,ff
57
+EOF
58
+
59
+# Test with spaces
60
+
61
+VAL=$(iniget test.ini aaa handlers)
62
+if [[ "$VAL" == "aa, bb" ]]; then
63
+    echo "OK: $VAL"
64
+else
65
+    echo "iniget failed: $VAL"
66
+fi
67
+
68
+iniset test.ini aaa handlers "11, 22"
69
+
70
+VAL=$(iniget test.ini aaa handlers)
71
+if [[ "$VAL" == "11, 22" ]]; then
72
+    echo "OK: $VAL"
73
+else
74
+    echo "iniget failed: $VAL"
75
+fi
76
+
77
+
78
+# Test without spaces, end of file
79
+
80
+VAL=$(iniget test.ini bbb handlers)
81
+if [[ "$VAL" == "ee,ff" ]]; then
82
+    echo "OK: $VAL"
83
+else
84
+    echo "iniget failed: $VAL"
85
+fi
86
+
87
+iniset test.ini bbb handlers "33,44"
88
+
89
+VAL=$(iniget test.ini bbb handlers)
90
+if [[ "$VAL" == "33,44" ]]; then
91
+    echo "OK: $VAL"
92
+else
93
+    echo "iniget failed: $VAL"
94
+fi
95
+
96
+
97
+# Test section not exist
98
+
99
+VAL=$(iniget test.ini zzz handlers)
100
+if [[ -z "$VAL" ]]; then
101
+    echo "OK"
102
+else
103
+    echo "iniget failed: $VAL"
104
+fi
105
+
106
+iniset test.ini zzz handlers "999"
107
+
108
+VAL=$(iniget test.ini zzz handlers)
109
+if [[ -z "$VAL" ]]; then
110
+    echo "OK"
111
+else
112
+    echo "iniget failed: $VAL"
113
+fi
114
+
115
+
116
+# Test comments
117
+
118
+inicomment test.ini aaa handlers
119
+
120
+VAL=$(iniget test.ini aaa handlers)
121
+if [[ -z "$VAL" ]]; then
122
+    echo "OK"
123
+else
124
+    echo "inicomment failed: $VAL"
125
+fi