Browse code

Extract common functions into a separate file

This is the start of an effort to organize devstack's code
to better document the requirements for configuring the individual
components.

Change-Id: I3476b76b9d1f9ee63687fb9898a98729118cbd84

Dean Troyer authored on 2012/02/01 03:11:56
Showing 9 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,91 @@
0
+# functions - Common functions used by DevStack components
1
+
2
+
3
+# apt-get wrapper to set arguments correctly
4
+# apt_get package [package ...]
5
+function apt_get() {
6
+    [[ "$OFFLINE" = "True" ]] && return
7
+    local sudo="sudo"
8
+    [[ "$(id -u)" = "0" ]] && sudo="env"
9
+    $sudo DEBIAN_FRONTEND=noninteractive \
10
+        http_proxy=$http_proxy https_proxy=$https_proxy \
11
+        apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
12
+}
13
+
14
+
15
+# Gracefully cp only if source file/dir exists
16
+# cp_it source destination
17
+function cp_it {
18
+    if [ -e $1 ] || [ -d $1 ]; then
19
+        cp -pRL $1 $2
20
+    fi
21
+}
22
+
23
+
24
+# git clone only if directory doesn't exist already.  Since ``DEST`` might not
25
+# be owned by the installation user, we create the directory and change the
26
+# ownership to the proper user.
27
+# Set global RECLONE=yes to simulate a clone when dest-dir exists
28
+# git_clone remote dest-dir branch
29
+function git_clone {
30
+    [[ "$OFFLINE" = "True" ]] && return
31
+
32
+    GIT_REMOTE=$1
33
+    GIT_DEST=$2
34
+    GIT_BRANCH=$3
35
+
36
+    if echo $GIT_BRANCH | egrep -q "^refs"; then
37
+        # If our branch name is a gerrit style refs/changes/...
38
+        if [[ ! -d $GIT_DEST ]]; then
39
+            git clone $GIT_REMOTE $GIT_DEST
40
+        fi
41
+        cd $GIT_DEST
42
+        git fetch $GIT_REMOTE $GIT_BRANCH && git checkout FETCH_HEAD
43
+    else
44
+        # do a full clone only if the directory doesn't exist
45
+        if [[ ! -d $GIT_DEST ]]; then
46
+            git clone $GIT_REMOTE $GIT_DEST
47
+            cd $GIT_DEST
48
+            # This checkout syntax works for both branches and tags
49
+            git checkout $GIT_BRANCH
50
+        elif [[ "$RECLONE" == "yes" ]]; then
51
+            # if it does exist then simulate what clone does if asked to RECLONE
52
+            cd $GIT_DEST
53
+            # set the url to pull from and fetch
54
+            git remote set-url origin $GIT_REMOTE
55
+            git fetch origin
56
+            # remove the existing ignored files (like pyc) as they cause breakage
57
+            # (due to the py files having older timestamps than our pyc, so python
58
+            # thinks the pyc files are correct using them)
59
+            find $GIT_DEST -name '*.pyc' -delete
60
+            git checkout -f origin/$GIT_BRANCH
61
+            # a local branch might not exist
62
+            git branch -D $GIT_BRANCH || true
63
+            git checkout -b $GIT_BRANCH
64
+        fi
65
+    fi
66
+}
67
+
68
+
69
+# pip install wrapper to set cache and proxy environment variables
70
+# pip_install package [package ...]
71
+function pip_install {
72
+    [[ "$OFFLINE" = "True" ]] && return
73
+    sudo PIP_DOWNLOAD_CACHE=/var/cache/pip \
74
+        HTTP_PROXY=$http_proxy \
75
+        HTTPS_PROXY=$https_proxy \
76
+        pip install --use-mirrors $@
77
+}
78
+
79
+
80
+# Normalize config values to True or False
81
+# VAR=`trueorfalse default-value test-value`
82
+function trueorfalse() {
83
+    local default=$1
84
+    local testval=$2
85
+
86
+    [[ -z "$testval" ]] && { echo "$default"; return; }
87
+    [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
88
+    [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
89
+    echo "$default"
90
+}
... ...
@@ -35,6 +35,9 @@ fi
35 35
 # Keep track of the current devstack directory.
36 36
 TOP_DIR=$(cd $(dirname "$0") && pwd)
37 37
 
38
+# Import common functions
39
+. $TOP_DIR/functions
40
+
38 41
 # stack.sh keeps the list of **apt** and **pip** dependencies in external
39 42
 # files, along with config templates and other useful files.  You can find these
40 43
 # in the ``files`` directory (next to this script).  We will reference this
... ...
@@ -86,16 +89,6 @@ source ./stackrc
86 86
 # Destination path for installation ``DEST``
87 87
 DEST=${DEST:-/opt/stack}
88 88
 
89
-# apt-get wrapper to just get arguments set correctly
90
-function apt_get() {
91
-    [[ "$OFFLINE" = "True" ]] && return
92
-    local sudo="sudo"
93
-    [ "$(id -u)" = "0" ] && sudo="env"
94
-    $sudo DEBIAN_FRONTEND=noninteractive \
95
-        http_proxy=$http_proxy https_proxy=$https_proxy \
96
-        apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
97
-}
98
-
99 89
 # Check to see if we are already running a stack.sh
100 90
 if screen -ls | egrep -q "[0-9].stack"; then
101 91
     echo "You are already running a stack.sh session."
... ...
@@ -155,18 +148,6 @@ else
155 155
     sudo mv $TEMPFILE /etc/sudoers.d/stack_sh_nova
156 156
 fi
157 157
 
158
-# Normalize config values to True or False
159
-# VAR=`trueorfalse default-value test-value`
160
-function trueorfalse() {
161
-    local default=$1
162
-    local testval=$2
163
-
164
-    [[ -z "$testval" ]] && { echo "$default"; return; }
165
-    [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
166
-    [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
167
-    echo "$default"
168
-}
169
-
170 158
 # Set True to configure stack.sh to run cleanly without Internet access.
171 159
 # stack.sh must have been previously run with Internet access to install
172 160
 # prerequisites and initialize $DEST.
... ...
@@ -542,14 +523,6 @@ function get_packages() {
542 542
     done
543 543
 }
544 544
 
545
-function pip_install {
546
-    [[ "$OFFLINE" = "True" ]] && return
547
-    sudo PIP_DOWNLOAD_CACHE=/var/cache/pip \
548
-        HTTP_PROXY=$http_proxy \
549
-        HTTPS_PROXY=$https_proxy \
550
-        pip install --use-mirrors $@
551
-}
552
-
553 545
 # install apt requirements
554 546
 apt_get update
555 547
 apt_get install $(get_packages)
... ...
@@ -557,48 +530,6 @@ apt_get install $(get_packages)
557 557
 # install python requirements
558 558
 pip_install `cat $FILES/pips/* | uniq`
559 559
 
560
-# git clone only if directory doesn't exist already.  Since ``DEST`` might not
561
-# be owned by the installation user, we create the directory and change the
562
-# ownership to the proper user.
563
-function git_clone {
564
-    [[ "$OFFLINE" = "True" ]] && return
565
-
566
-    GIT_REMOTE=$1
567
-    GIT_DEST=$2
568
-    GIT_BRANCH=$3
569
-
570
-    if echo $GIT_BRANCH | egrep -q "^refs"; then
571
-        # If our branch name is a gerrit style refs/changes/...
572
-        if [ ! -d $GIT_DEST ]; then
573
-            git clone $GIT_REMOTE $GIT_DEST
574
-        fi
575
-        cd $GIT_DEST
576
-        git fetch $GIT_REMOTE $GIT_BRANCH && git checkout FETCH_HEAD
577
-    else
578
-        # do a full clone only if the directory doesn't exist
579
-        if [ ! -d $GIT_DEST ]; then
580
-            git clone $GIT_REMOTE $GIT_DEST
581
-            cd $GIT_DEST
582
-            # This checkout syntax works for both branches and tags
583
-            git checkout $GIT_BRANCH
584
-        elif [[ "$RECLONE" == "yes" ]]; then
585
-            # if it does exist then simulate what clone does if asked to RECLONE
586
-            cd $GIT_DEST
587
-            # set the url to pull from and fetch
588
-            git remote set-url origin $GIT_REMOTE
589
-            git fetch origin
590
-            # remove the existing ignored files (like pyc) as they cause breakage
591
-            # (due to the py files having older timestamps than our pyc, so python
592
-            # thinks the pyc files are correct using them)
593
-            find $GIT_DEST -name '*.pyc' -delete
594
-            git checkout -f origin/$GIT_BRANCH
595
-            # a local branch might not exist
596
-            git branch -D $GIT_BRANCH || true
597
-            git checkout -b $GIT_BRANCH
598
-        fi
599
-    fi
600
-}
601
-
602 560
 # compute service
603 561
 git_clone $NOVA_REPO $NOVA_DIR $NOVA_BRANCH
604 562
 # python client library to nova that horizon (and others) use
... ...
@@ -47,7 +47,10 @@ IMG_FILE=$1
47 47
 
48 48
 # Keep track of the current directory
49 49
 TOOLS_DIR=$(cd $(dirname "$0") && pwd)
50
-TOP_DIR=`cd $TOOLS_DIR/..; pwd`
50
+TOP_DIR=$(cd $TOOLS_DIR/..; pwd)
51
+
52
+# Import common functions
53
+. $TOP_DIR/functions
51 54
 
52 55
 # Store cwd
53 56
 CWD=`pwd`
... ...
@@ -170,35 +173,6 @@ if [ ! -r "`ls $MNTDIR/boot/vmlinuz-*-generic | head -1`" ]; then
170 170
     chroot $MNTDIR apt-get install -y linux-generic
171 171
 fi
172 172
 
173
-# git clone only if directory doesn't exist already.  Since ``DEST`` might not
174
-# be owned by the installation user, we create the directory and change the
175
-# ownership to the proper user.
176
-function git_clone {
177
-
178
-    # clone new copy or fetch latest changes
179
-    CHECKOUT=${MNTDIR}$2
180
-    if [ ! -d $CHECKOUT ]; then
181
-        mkdir -p $CHECKOUT
182
-        git clone $1 $CHECKOUT
183
-    else
184
-        pushd $CHECKOUT
185
-        git fetch
186
-        popd
187
-    fi
188
-
189
-    # FIXME(ja): checkout specified version (should works for branches and tags)
190
-
191
-    pushd $CHECKOUT
192
-    # checkout the proper branch/tag
193
-    git checkout $3
194
-    # force our local version to be the same as the remote version
195
-    git reset --hard origin/$3
196
-    popd
197
-
198
-    # give ownership to the stack user
199
-    chroot $MNTDIR chown -R stack $2
200
-}
201
-
202 173
 git_clone $NOVA_REPO $DEST/nova $NOVA_BRANCH
203 174
 git_clone $GLANCE_REPO $DEST/glance $GLANCE_BRANCH
204 175
 git_clone $KEYSTONE_REPO $DEST/keystone $KEYSTONE_BRANCH
... ...
@@ -26,7 +26,10 @@ trap cleanup SIGHUP SIGINT SIGTERM SIGQUIT EXIT
26 26
 
27 27
 # Keep track of the current directory
28 28
 TOOLS_DIR=$(cd $(dirname "$0") && pwd)
29
-TOP_DIR=`cd $TOOLS_DIR/..; pwd`
29
+TOP_DIR=$(cd $TOOLS_DIR/..; pwd)
30
+
31
+# Import common functions
32
+. $TOP_DIR/functions
30 33
 
31 34
 # Abort if localrc is not set
32 35
 if [ ! -e $TOP_DIR/localrc ]; then
... ...
@@ -43,42 +46,8 @@ DEST=${DEST:-/opt/stack}
43 43
 
44 44
 TEMPEST_DIR=$DEST/tempest
45 45
 
46
-DIST_NAME=${DIST_NAME:-oneiric}
47
-
48
-# git clone only if directory doesn't exist already.  Since ``DEST`` might not
49
-# be owned by the installation user, we create the directory and change the
50
-# ownership to the proper user.
51
-function git_clone {
52
-
53
-    GIT_REMOTE=$1
54
-    GIT_DEST=$2
55
-    GIT_BRANCH=$3
56
-
57
-    # do a full clone only if the directory doesn't exist
58
-    if [ ! -d $GIT_DEST ]; then
59
-        git clone $GIT_REMOTE $GIT_DEST
60
-        cd $2
61
-        # This checkout syntax works for both branches and tags
62
-        git checkout $GIT_BRANCH
63
-    elif [[ "$RECLONE" == "yes" ]]; then
64
-        # if it does exist then simulate what clone does if asked to RECLONE
65
-        cd $GIT_DEST
66
-        # set the url to pull from and fetch
67
-        git remote set-url origin $GIT_REMOTE
68
-        git fetch origin
69
-        # remove the existing ignored files (like pyc) as they cause breakage
70
-        # (due to the py files having older timestamps than our pyc, so python
71
-        # thinks the pyc files are correct using them)
72
-        find $GIT_DEST -name '*.pyc' -delete
73
-        git checkout -f origin/$GIT_BRANCH
74
-        # a local branch might not exist
75
-        git branch -D $GIT_BRANCH || true
76
-        git checkout -b $GIT_BRANCH
77
-    fi
78
-}
79
-
80 46
 # Install tests and prerequisites
81
-sudo PIP_DOWNLOAD_CACHE=/var/cache/pip pip install --use-mirrors `cat $TOP_DIR/files/pips/tempest`
47
+pip_install `cat $TOP_DIR/files/pips/tempest`
82 48
 
83 49
 git_clone $TEMPEST_REPO $TEMPEST_DIR $TEMPEST_BRANCH
84 50
 
... ...
@@ -8,7 +8,10 @@ fi
8 8
 
9 9
 # Keep track of the current directory
10 10
 TOOLS_DIR=$(cd $(dirname "$0") && pwd)
11
-TOP_DIR=`cd $TOOLS_DIR/..; pwd`
11
+TOP_DIR=$(cd $TOOLS_DIR/..; pwd)
12
+
13
+# Import common functions
14
+. $TOP_DIR/functions
12 15
 
13 16
 cd $TOP_DIR
14 17
 
... ...
@@ -34,7 +37,7 @@ fi
34 34
 
35 35
 # Install deps if needed
36 36
 DEPS="kvm libvirt-bin kpartx cloud-utils curl"
37
-apt-get install -y --force-yes $DEPS || true # allow this to fail gracefully for concurrent builds
37
+apt_get install -y --force-yes $DEPS || true # allow this to fail gracefully for concurrent builds
38 38
 
39 39
 # Where to store files and instances
40 40
 WORK_DIR=${WORK_DIR:-/opt/uecstack}
... ...
@@ -40,7 +40,10 @@ DEST_FILE=$1
40 40
 
41 41
 # Keep track of the current directory
42 42
 TOOLS_DIR=$(cd $(dirname "$0") && pwd)
43
-TOP_DIR=`cd $TOOLS_DIR/..; pwd`
43
+TOP_DIR=$(cd $TOOLS_DIR/..; pwd)
44
+
45
+# Import common functions
46
+. $TOP_DIR/functions
44 47
 
45 48
 cd $TOP_DIR
46 49
 
... ...
@@ -68,7 +71,7 @@ fi
68 68
 
69 69
 # Install deps if needed
70 70
 DEPS="kvm libvirt-bin kpartx cloud-utils curl"
71
-apt-get install -y --force-yes $DEPS
71
+apt_get install -y --force-yes $DEPS
72 72
 
73 73
 # Where to store files and instances
74 74
 CACHEDIR=${CACHEDIR:-/opt/stack/cache}
... ...
@@ -113,35 +116,6 @@ if [ ! -r "`ls $MNT_DIR/boot/vmlinuz-*-generic | head -1`" ]; then
113 113
     chroot $MNT_DIR apt-get install -y linux-generic
114 114
 fi
115 115
 
116
-# git clone only if directory doesn't exist already.  Since ``DEST`` might not
117
-# be owned by the installation user, we create the directory and change the
118
-# ownership to the proper user.
119
-function git_clone {
120
-
121
-    # clone new copy or fetch latest changes
122
-    CHECKOUT=${MNT_DIR}$2
123
-    if [ ! -d $CHECKOUT ]; then
124
-        mkdir -p $CHECKOUT
125
-        git clone $1 $CHECKOUT
126
-    else
127
-        pushd $CHECKOUT
128
-        git fetch
129
-        popd
130
-    fi
131
-
132
-    # FIXME(ja): checkout specified version (should works for branches and tags)
133
-
134
-    pushd $CHECKOUT
135
-    # checkout the proper branch/tag
136
-    git checkout $3
137
-    # force our local version to be the same as the remote version
138
-    git reset --hard origin/$3
139
-    popd
140
-
141
-    # give ownership to the stack user
142
-    chroot $MNT_DIR chown -R stack $2
143
-}
144
-
145 116
 git_clone $NOVA_REPO $DEST/nova $NOVA_BRANCH
146 117
 git_clone $GLANCE_REPO $DEST/glance $GLANCE_BRANCH
147 118
 git_clone $KEYSTONE_REPO $DEST/keystone $KEYSTONE_BRANCH
... ...
@@ -30,7 +30,10 @@ trap cleanup SIGHUP SIGINT SIGTERM SIGQUIT EXIT
30 30
 
31 31
 # Keep track of the current directory
32 32
 TOOLS_DIR=$(cd $(dirname "$0") && pwd)
33
-TOP_DIR=`cd $TOOLS_DIR/..; pwd`
33
+TOP_DIR=$(cd $TOOLS_DIR/..; pwd)
34
+
35
+# Import common functions
36
+. $TOP_DIR/functions
34 37
 
35 38
 # Abort if localrc is not set
36 39
 if [ ! -e $TOP_DIR/localrc ]; then
... ...
@@ -8,7 +8,10 @@ set -o errexit
8 8
 
9 9
 # Keep track of the current directory
10 10
 TOOLS_DIR=$(cd $(dirname "$0") && pwd)
11
-TOP_DIR=`cd $TOOLS_DIR/..; pwd`
11
+TOP_DIR=$(cd $TOOLS_DIR/..; pwd)
12
+
13
+# Import common functions
14
+. $TOP_DIR/functions
12 15
 
13 16
 # Change dir to top of devstack
14 17
 cd $TOP_DIR
... ...
@@ -47,13 +50,6 @@ echo stack:pass | chroot $STAGING_DIR chpasswd
47 47
 ( umask 226 && echo "stack ALL=(ALL) NOPASSWD:ALL" \
48 48
     > $STAGING_DIR/etc/sudoers.d/50_stack_sh )
49 49
 
50
-# Gracefully cp only if source file/dir exists
51
-function cp_it {
52
-    if [ -e $1 ] || [ -d $1 ]; then
53
-        cp -pRL $1 $2
54
-    fi
55
-}
56
-
57 50
 # Copy over your ssh keys and env if desired
58 51
 cp_it ~/.ssh $STAGING_DIR/$DEST/.ssh
59 52
 cp_it ~/.ssh/id_rsa.pub $STAGING_DIR/$DEST/.ssh/authorized_keys
... ...
@@ -6,7 +6,10 @@ ROOTSIZE=${ROOTSIZE:-2000}
6 6
 
7 7
 # Keep track of the current directory
8 8
 TOOLS_DIR=$(cd $(dirname "$0") && pwd)
9
-TOP_DIR=`cd $TOOLS_DIR/..; pwd`
9
+TOP_DIR=$(cd $TOOLS_DIR/..; pwd)
10
+
11
+# Import common functions
12
+. $TOP_DIR/functions
10 13
 
11 14
 # exit on error to stop unexpected errors
12 15
 set -o errexit