Browse code

Clean up local variable usage - misc functions

A catch-all for a bunch of smaller functions

Change-Id: I3f97a514f9964ef36bff2f24a2f0a98614871a9f

Dean Troyer authored on 2014/07/26 01:13:11
Showing 1 changed files
... ...
@@ -49,6 +49,7 @@ function iniadd {
49 49
     local section=$2
50 50
     local option=$3
51 51
     shift 3
52
+
52 53
     local values="$(iniget_multiline $file $section $option) $@"
53 54
     iniset_multiline $file $section $option $values
54 55
     $xtrace
... ...
@@ -62,6 +63,7 @@ function inicomment {
62 62
     local file=$1
63 63
     local section=$2
64 64
     local option=$3
65
+
65 66
     sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
66 67
     $xtrace
67 68
 }
... ...
@@ -75,6 +77,7 @@ function iniget {
75 75
     local section=$2
76 76
     local option=$3
77 77
     local line
78
+
78 79
     line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
79 80
     echo ${line#*=}
80 81
     $xtrace
... ...
@@ -89,6 +92,7 @@ function iniget_multiline {
89 89
     local section=$2
90 90
     local option=$3
91 91
     local values
92
+
92 93
     values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file")
93 94
     echo ${values}
94 95
     $xtrace
... ...
@@ -103,6 +107,7 @@ function ini_has_option {
103 103
     local section=$2
104 104
     local option=$3
105 105
     local line
106
+
106 107
     line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
107 108
     $xtrace
108 109
     [ -n "$line" ]
... ...
@@ -145,6 +150,7 @@ function iniset_multiline {
145 145
     local file=$1
146 146
     local section=$2
147 147
     local option=$3
148
+
148 149
     shift 3
149 150
     local values
150 151
     for v in $@; do
... ...
@@ -237,28 +243,28 @@ function die {
237 237
 # die_if_not_set $LINENO env-var "message"
238 238
 function die_if_not_set {
239 239
     local exitcode=$?
240
-    FXTRACE=$(set +o | grep xtrace)
240
+    local xtrace=$(set +o | grep xtrace)
241 241
     set +o xtrace
242 242
     local line=$1; shift
243 243
     local evar=$1; shift
244 244
     if ! is_set $evar || [ $exitcode != 0 ]; then
245 245
         die $line "$*"
246 246
     fi
247
-    $FXTRACE
247
+    $xtrace
248 248
 }
249 249
 
250 250
 # Prints line number and "message" in error format
251 251
 # err $LINENO "message"
252 252
 function err {
253 253
     local exitcode=$?
254
-    errXTRACE=$(set +o | grep xtrace)
254
+    local xtrace=$(set +o | grep xtrace)
255 255
     set +o xtrace
256 256
     local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
257 257
     echo $msg 1>&2;
258 258
     if [[ -n ${SCREEN_LOGDIR} ]]; then
259 259
         echo $msg >> "${SCREEN_LOGDIR}/error.log"
260 260
     fi
261
-    $errXTRACE
261
+    $xtrace
262 262
     return $exitcode
263 263
 }
264 264
 
... ...
@@ -268,14 +274,14 @@ function err {
268 268
 # err_if_not_set $LINENO env-var "message"
269 269
 function err_if_not_set {
270 270
     local exitcode=$?
271
-    errinsXTRACE=$(set +o | grep xtrace)
271
+    local xtrace=$(set +o | grep xtrace)
272 272
     set +o xtrace
273 273
     local line=$1; shift
274 274
     local evar=$1; shift
275 275
     if ! is_set $evar || [ $exitcode != 0 ]; then
276 276
         err $line "$*"
277 277
     fi
278
-    $errinsXTRACE
278
+    $xtrace
279 279
     return $exitcode
280 280
 }
281 281
 
... ...
@@ -304,14 +310,14 @@ function is_set {
304 304
 # warn $LINENO "message"
305 305
 function warn {
306 306
     local exitcode=$?
307
-    errXTRACE=$(set +o | grep xtrace)
307
+    local xtrace=$(set +o | grep xtrace)
308 308
     set +o xtrace
309 309
     local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
310 310
     echo $msg 1>&2;
311 311
     if [[ -n ${SCREEN_LOGDIR} ]]; then
312 312
         echo $msg >> "${SCREEN_LOGDIR}/error.log"
313 313
     fi
314
-    $errXTRACE
314
+    $xtrace
315 315
     return $exitcode
316 316
 }
317 317
 
... ...
@@ -322,13 +328,16 @@ function warn {
322 322
 # Determine OS Vendor, Release and Update
323 323
 # Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
324 324
 # Returns results in global variables:
325
-# os_VENDOR - vendor name
326
-# os_RELEASE - release
327
-# os_UPDATE - update
328
-# os_PACKAGE - package type
329
-# os_CODENAME - vendor's codename for release
325
+# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
326
+# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
327
+# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
328
+# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
329
+# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
330
+declare os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
331
+
330 332
 # GetOSVersion
331 333
 function GetOSVersion {
334
+
332 335
     # Figure out which vendor we are
333 336
     if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
334 337
         # OS/X
... ...
@@ -418,6 +427,8 @@ function GetOSVersion {
418 418
 
419 419
 # Translate the OS version values into common nomenclature
420 420
 # Sets global ``DISTRO`` from the ``os_*`` values
421
+declare DISTRO
422
+
421 423
 function GetDistro {
422 424
     GetOSVersion
423 425
     if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
... ...
@@ -452,9 +463,7 @@ function GetDistro {
452 452
 # Utility function for checking machine architecture
453 453
 # is_arch arch-type
454 454
 function is_arch {
455
-    ARCH_TYPE=$1
456
-
457
-    [[ "$(uname -m)" == "$ARCH_TYPE" ]]
455
+    [[ "$(uname -m)" == "$1" ]]
458 456
 }
459 457
 
460 458
 # Determine if current distribution is a Fedora-based distribution
... ...
@@ -662,16 +671,17 @@ function get_default_host_ip {
662 662
     # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
663 663
     if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
664 664
         host_ip=""
665
-        host_ips=`LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/");  print parts[1]}'`
666
-        for IP in $host_ips; do
665
+        local host_ips=$(LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/");  print parts[1]}')
666
+        local ip
667
+        for ip in $host_ips; do
667 668
             # Attempt to filter out IP addresses that are part of the fixed and
668 669
             # floating range. Note that this method only works if the ``netaddr``
669 670
             # python library is installed. If it is not installed, an error
670 671
             # will be printed and the first IP from the interface will be used.
671 672
             # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
672 673
             # address.
673
-            if ! (address_in_net $IP $fixed_range || address_in_net $IP $floating_range); then
674
-                host_ip=$IP
674
+            if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
675
+                host_ip=$ip
675 676
                 break;
676 677
             fi
677 678
         done
... ...
@@ -684,6 +694,7 @@ function get_default_host_ip {
684 684
 # Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
685 685
 # get_field field-number
686 686
 function get_field {
687
+    local data field
687 688
     while read data; do
688 689
         if [ "$1" -lt 0 ]; then
689 690
             field="(\$(NF$1))"
... ...
@@ -726,12 +737,12 @@ function policy_add {
726 726
 # Usage: get_or_create_user <username> <password> <project> [<email>]
727 727
 function get_or_create_user {
728 728
     if [[ ! -z "$4" ]]; then
729
-        local EMAIL="--email=$4"
729
+        local email="--email=$4"
730 730
     else
731
-        local EMAIL=""
731
+        local email=""
732 732
     fi
733 733
     # Gets user id
734
-    USER_ID=$(
734
+    local user_id=$(
735 735
         # Gets user id
736 736
         openstack user show $1 -f value -c id 2>/dev/null ||
737 737
         # Creates new user
... ...
@@ -739,63 +750,63 @@ function get_or_create_user {
739 739
             $1 \
740 740
             --password "$2" \
741 741
             --project $3 \
742
-            $EMAIL \
742
+            $email \
743 743
             -f value -c id
744 744
     )
745
-    echo $USER_ID
745
+    echo $user_id
746 746
 }
747 747
 
748 748
 # Gets or creates project
749 749
 # Usage: get_or_create_project <name>
750 750
 function get_or_create_project {
751 751
     # Gets project id
752
-    PROJECT_ID=$(
752
+    local project_id=$(
753 753
         # Gets project id
754 754
         openstack project show $1 -f value -c id 2>/dev/null ||
755 755
         # Creates new project if not exists
756 756
         openstack project create $1 -f value -c id
757 757
     )
758
-    echo $PROJECT_ID
758
+    echo $project_id
759 759
 }
760 760
 
761 761
 # Gets or creates role
762 762
 # Usage: get_or_create_role <name>
763 763
 function get_or_create_role {
764
-    ROLE_ID=$(
764
+    local role_id=$(
765 765
         # Gets role id
766 766
         openstack role show $1 -f value -c id 2>/dev/null ||
767 767
         # Creates role if not exists
768 768
         openstack role create $1 -f value -c id
769 769
     )
770
-    echo $ROLE_ID
770
+    echo $role_id
771 771
 }
772 772
 
773 773
 # Gets or adds user role
774 774
 # Usage: get_or_add_user_role <role> <user> <project>
775 775
 function get_or_add_user_role {
776 776
     # Gets user role id
777
-    USER_ROLE_ID=$(openstack user role list \
777
+    local user_role_id=$(openstack user role list \
778 778
         $2 \
779 779
         --project $3 \
780 780
         --column "ID" \
781 781
         --column "Name" \
782 782
         | grep " $1 " | get_field 1)
783
-    if [[ -z "$USER_ROLE_ID" ]]; then
783
+    if [[ -z "$user_role_id" ]]; then
784 784
         # Adds role to user
785
-        USER_ROLE_ID=$(openstack role add \
785
+        user_role_id=$(openstack role add \
786 786
             $1 \
787 787
             --user $2 \
788 788
             --project $3 \
789 789
             | grep " id " | get_field 2)
790 790
     fi
791
-    echo $USER_ROLE_ID
791
+    echo $user_role_id
792 792
 }
793 793
 
794 794
 # Gets or creates service
795 795
 # Usage: get_or_create_service <name> <type> <description>
796 796
 function get_or_create_service {
797 797
     # Gets service id
798
-    SERVICE_ID=$(
798
+    local service_id=$(
799 799
         # Gets service id
800 800
         openstack service show $1 -f value -c id 2>/dev/null ||
801 801
         # Creates new service if not exists
... ...
@@ -805,22 +816,22 @@ function get_or_create_service {
805 805
             --description="$3" \
806 806
             -f value -c id
807 807
     )
808
-    echo $SERVICE_ID
808
+    echo $service_id
809 809
 }
810 810
 
811 811
 # Gets or creates endpoint
812 812
 # Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
813 813
 function get_or_create_endpoint {
814 814
     # Gets endpoint id
815
-    ENDPOINT_ID=$(openstack endpoint list \
815
+    local endpoint_id=$(openstack endpoint list \
816 816
         --column "ID" \
817 817
         --column "Region" \
818 818
         --column "Service Name" \
819 819
         | grep " $2 " \
820 820
         | grep " $1 " | get_field 1)
821
-    if [[ -z "$ENDPOINT_ID" ]]; then
821
+    if [[ -z "$endpoint_id" ]]; then
822 822
         # Creates new endpoint
823
-        ENDPOINT_ID=$(openstack endpoint create \
823
+        endpoint_id=$(openstack endpoint create \
824 824
             $1 \
825 825
             --region $2 \
826 826
             --publicurl $3 \
... ...
@@ -828,9 +839,10 @@ function get_or_create_endpoint {
828 828
             --internalurl $5 \
829 829
             | grep " id " | get_field 2)
830 830
     fi
831
-    echo $ENDPOINT_ID
831
+    echo $endpoint_id
832 832
 }
833 833
 
834
+
834 835
 # Package Functions
835 836
 # =================
836 837
 
... ...
@@ -991,6 +1003,7 @@ function get_packages {
991 991
 }
992 992
 
993 993
 # Distro-agnostic package installer
994
+# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
994 995
 # install_package package [package ...]
995 996
 function update_package_repo {
996 997
     if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
... ...
@@ -1091,6 +1104,7 @@ function yum_install {
1091 1091
 }
1092 1092
 
1093 1093
 # zypper wrapper to set arguments correctly
1094
+# Uses globals ``OFFLINE``, ``*_proxy``
1094 1095
 # zypper_install package [package ...]
1095 1096
 function zypper_install {
1096 1097
     [[ "$OFFLINE" = "True" ]] && return
... ...
@@ -1107,7 +1121,8 @@ function zypper_install {
1107 1107
 # _run_process() is designed to be backgrounded by run_process() to simulate a
1108 1108
 # fork.  It includes the dirty work of closing extra filehandles and preparing log
1109 1109
 # files to produce the same logs as screen_it().  The log filename is derived
1110
-# from the service name and global-and-now-misnamed SCREEN_LOGDIR
1110
+# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1111
+# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``
1111 1112
 # _run_process service "command-line"
1112 1113
 function _run_process {
1113 1114
     local service=$1
... ...
@@ -1133,6 +1148,7 @@ function _run_process {
1133 1133
 
1134 1134
 # Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1135 1135
 # This is used for ``service_check`` when all the ``screen_it`` are called finished
1136
+# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
1136 1137
 # init_service_check
1137 1138
 function init_service_check {
1138 1139
     SCREEN_NAME=${SCREEN_NAME:-stack}
... ...
@@ -1150,15 +1166,15 @@ function init_service_check {
1150 1150
 function is_running {
1151 1151
     local name=$1
1152 1152
     ps auxw | grep -v grep | grep ${name} > /dev/null
1153
-    RC=$?
1153
+    local exitcode=$?
1154 1154
     # some times I really hate bash reverse binary logic
1155
-    return $RC
1155
+    return $exitcode
1156 1156
 }
1157 1157
 
1158 1158
 # run_process() launches a child process that closes all file descriptors and
1159 1159
 # then exec's the passed in command.  This is meant to duplicate the semantics
1160 1160
 # of screen_it() without screen.  PIDs are written to
1161
-# $SERVICE_DIR/$SCREEN_NAME/$service.pid
1161
+# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid``
1162 1162
 # run_process service "command-line"
1163 1163
 function run_process {
1164 1164
     local service=$1
... ...
@@ -1170,6 +1186,8 @@ function run_process {
1170 1170
 }
1171 1171
 
1172 1172
 # Helper to launch a service in a named screen
1173
+# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_NAME``, ``SCREEN_LOGDIR``,
1174
+# ``SERVICE_DIR``, ``USE_SCREEN``
1173 1175
 # screen_it service "command-line"
1174 1176
 function screen_it {
1175 1177
     SCREEN_NAME=${SCREEN_NAME:-stack}
... ...
@@ -1212,6 +1230,7 @@ function screen_it {
1212 1212
 }
1213 1213
 
1214 1214
 # Screen rc file builder
1215
+# Uses globals ``SCREEN_NAME``, ``SCREENRC``
1215 1216
 # screen_rc service "command-line"
1216 1217
 function screen_rc {
1217 1218
     SCREEN_NAME=${SCREEN_NAME:-stack}
... ...
@@ -1242,6 +1261,7 @@ function screen_rc {
1242 1242
 # If a PID is available use it, kill the whole process group via TERM
1243 1243
 # If screen is being used kill the screen window; this will catch processes
1244 1244
 # that did not leave a PID behind
1245
+# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
1245 1246
 # screen_stop service
1246 1247
 function screen_stop {
1247 1248
     SCREEN_NAME=${SCREEN_NAME:-stack}
... ...
@@ -1262,6 +1282,7 @@ function screen_stop {
1262 1262
 }
1263 1263
 
1264 1264
 # Helper to get the status of each running service
1265
+# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
1265 1266
 # service_check
1266 1267
 function service_check {
1267 1268
     local service
... ...
@@ -1331,18 +1352,19 @@ function pip_install {
1331 1331
     fi
1332 1332
     if [[ $TRACK_DEPENDS = True ]]; then
1333 1333
         source $DEST/.venv/bin/activate
1334
-        CMD_PIP=$DEST/.venv/bin/pip
1335
-        SUDO_PIP="env"
1334
+        local cmd_pip=$DEST/.venv/bin/pip
1335
+        local sudo_pip="env"
1336 1336
     else
1337
-        SUDO_PIP="sudo"
1338
-        CMD_PIP=$(get_pip_command)
1337
+        local cmd_pip=$(get_pip_command)
1338
+        local sudo_pip="sudo"
1339 1339
     fi
1340 1340
 
1341 1341
     # Mirror option not needed anymore because pypi has CDN available,
1342 1342
     # but it's useful in certain circumstances
1343 1343
     PIP_USE_MIRRORS=${PIP_USE_MIRRORS:-False}
1344
+    local pip_mirror_opt=""
1344 1345
     if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
1345
-        PIP_MIRROR_OPT="--use-mirrors"
1346
+        pip_mirror_opt="--use-mirrors"
1346 1347
     fi
1347 1348
 
1348 1349
     # pip < 1.4 has a bug where it will use an already existing build
... ...
@@ -1355,13 +1377,13 @@ function pip_install {
1355 1355
     local pip_build_tmp=$(mktemp --tmpdir -d pip-build.XXXXX)
1356 1356
 
1357 1357
     $xtrace
1358
-    $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
1358
+    $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
1359 1359
         http_proxy=$http_proxy \
1360 1360
         https_proxy=$https_proxy \
1361 1361
         no_proxy=$no_proxy \
1362
-        $CMD_PIP install --build=${pip_build_tmp} \
1363
-        $PIP_MIRROR_OPT $@ \
1364
-        && $SUDO_PIP rm -rf ${pip_build_tmp}
1362
+        $cmd_pip install --build=${pip_build_tmp} \
1363
+        $pip_mirror_opt $@ \
1364
+        && $sudo_pip rm -rf ${pip_build_tmp}
1365 1365
 }
1366 1366
 
1367 1367
 # this should be used if you want to install globally, all libraries should
... ...
@@ -1396,7 +1418,7 @@ function setup_package_with_req_sync {
1396 1396
 
1397 1397
     if [[ $update_requirements != "changed" ]]; then
1398 1398
         (cd $REQUIREMENTS_DIR; \
1399
-            $SUDO_CMD python update.py $project_dir)
1399
+            python update.py $project_dir)
1400 1400
     fi
1401 1401
 
1402 1402
     setup_package $project_dir $flags
... ...
@@ -1503,6 +1525,7 @@ function disable_service {
1503 1503
 # enable_service service [service ...]
1504 1504
 function enable_service {
1505 1505
     local tmpsvcs="${ENABLED_SERVICES}"
1506
+    local service
1506 1507
     for service in $@; do
1507 1508
         if ! is_service_enabled $service; then
1508 1509
             tmpsvcs+=",$service"
... ...
@@ -1539,7 +1562,8 @@ function is_service_enabled {
1539 1539
     local xtrace=$(set +o | grep xtrace)
1540 1540
     set +o xtrace
1541 1541
     local enabled=1
1542
-    services=$@
1542
+    local services=$@
1543
+    local service
1543 1544
     for service in ${services}; do
1544 1545
         [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
1545 1546
 
... ...
@@ -1575,8 +1599,9 @@ function is_service_enabled {
1575 1575
 function use_exclusive_service {
1576 1576
     local options=${!1}
1577 1577
     local selection=$3
1578
-    out=$2
1578
+    local out=$2
1579 1579
     [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
1580
+    local opt
1580 1581
     for opt in $options;do
1581 1582
         [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1582 1583
     done
... ...
@@ -1600,7 +1625,7 @@ function _safe_permission_operation {
1600 1600
 
1601 1601
     let last="${#args[*]} - 1"
1602 1602
 
1603
-    dir_to_check=${args[$last]}
1603
+    local dir_to_check=${args[$last]}
1604 1604
     if [ ! -d "$dir_to_check" ]; then
1605 1605
         dir_to_check=`dirname "$dir_to_check"`
1606 1606
     fi