Browse code

Merge "Clean up local variable usage - misc functions"

Jenkins authored on 2014/08/02 09:36:17
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
... ...
@@ -661,16 +670,17 @@ function get_default_host_ip {
661 661
     # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
662 662
     if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
663 663
         host_ip=""
664
-        host_ips=`LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/");  print parts[1]}'`
665
-        for IP in $host_ips; do
664
+        local host_ips=$(LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/");  print parts[1]}')
665
+        local ip
666
+        for ip in $host_ips; do
666 667
             # Attempt to filter out IP addresses that are part of the fixed and
667 668
             # floating range. Note that this method only works if the ``netaddr``
668 669
             # python library is installed. If it is not installed, an error
669 670
             # will be printed and the first IP from the interface will be used.
670 671
             # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
671 672
             # address.
672
-            if ! (address_in_net $IP $fixed_range || address_in_net $IP $floating_range); then
673
-                host_ip=$IP
673
+            if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
674
+                host_ip=$ip
674 675
                 break;
675 676
             fi
676 677
         done
... ...
@@ -683,6 +693,7 @@ function get_default_host_ip {
683 683
 # Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
684 684
 # get_field field-number
685 685
 function get_field {
686
+    local data field
686 687
     while read data; do
687 688
         if [ "$1" -lt 0 ]; then
688 689
             field="(\$(NF$1))"
... ...
@@ -725,12 +736,12 @@ function policy_add {
725 725
 # Usage: get_or_create_user <username> <password> <project> [<email>]
726 726
 function get_or_create_user {
727 727
     if [[ ! -z "$4" ]]; then
728
-        local EMAIL="--email=$4"
728
+        local email="--email=$4"
729 729
     else
730
-        local EMAIL=""
730
+        local email=""
731 731
     fi
732 732
     # Gets user id
733
-    USER_ID=$(
733
+    local user_id=$(
734 734
         # Gets user id
735 735
         openstack user show $1 -f value -c id 2>/dev/null ||
736 736
         # Creates new user
... ...
@@ -738,63 +749,63 @@ function get_or_create_user {
738 738
             $1 \
739 739
             --password "$2" \
740 740
             --project $3 \
741
-            $EMAIL \
741
+            $email \
742 742
             -f value -c id
743 743
     )
744
-    echo $USER_ID
744
+    echo $user_id
745 745
 }
746 746
 
747 747
 # Gets or creates project
748 748
 # Usage: get_or_create_project <name>
749 749
 function get_or_create_project {
750 750
     # Gets project id
751
-    PROJECT_ID=$(
751
+    local project_id=$(
752 752
         # Gets project id
753 753
         openstack project show $1 -f value -c id 2>/dev/null ||
754 754
         # Creates new project if not exists
755 755
         openstack project create $1 -f value -c id
756 756
     )
757
-    echo $PROJECT_ID
757
+    echo $project_id
758 758
 }
759 759
 
760 760
 # Gets or creates role
761 761
 # Usage: get_or_create_role <name>
762 762
 function get_or_create_role {
763
-    ROLE_ID=$(
763
+    local role_id=$(
764 764
         # Gets role id
765 765
         openstack role show $1 -f value -c id 2>/dev/null ||
766 766
         # Creates role if not exists
767 767
         openstack role create $1 -f value -c id
768 768
     )
769
-    echo $ROLE_ID
769
+    echo $role_id
770 770
 }
771 771
 
772 772
 # Gets or adds user role
773 773
 # Usage: get_or_add_user_role <role> <user> <project>
774 774
 function get_or_add_user_role {
775 775
     # Gets user role id
776
-    USER_ROLE_ID=$(openstack user role list \
776
+    local user_role_id=$(openstack user role list \
777 777
         $2 \
778 778
         --project $3 \
779 779
         --column "ID" \
780 780
         --column "Name" \
781 781
         | grep " $1 " | get_field 1)
782
-    if [[ -z "$USER_ROLE_ID" ]]; then
782
+    if [[ -z "$user_role_id" ]]; then
783 783
         # Adds role to user
784
-        USER_ROLE_ID=$(openstack role add \
784
+        user_role_id=$(openstack role add \
785 785
             $1 \
786 786
             --user $2 \
787 787
             --project $3 \
788 788
             | grep " id " | get_field 2)
789 789
     fi
790
-    echo $USER_ROLE_ID
790
+    echo $user_role_id
791 791
 }
792 792
 
793 793
 # Gets or creates service
794 794
 # Usage: get_or_create_service <name> <type> <description>
795 795
 function get_or_create_service {
796 796
     # Gets service id
797
-    SERVICE_ID=$(
797
+    local service_id=$(
798 798
         # Gets service id
799 799
         openstack service show $1 -f value -c id 2>/dev/null ||
800 800
         # Creates new service if not exists
... ...
@@ -804,22 +815,22 @@ function get_or_create_service {
804 804
             --description="$3" \
805 805
             -f value -c id
806 806
     )
807
-    echo $SERVICE_ID
807
+    echo $service_id
808 808
 }
809 809
 
810 810
 # Gets or creates endpoint
811 811
 # Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
812 812
 function get_or_create_endpoint {
813 813
     # Gets endpoint id
814
-    ENDPOINT_ID=$(openstack endpoint list \
814
+    local endpoint_id=$(openstack endpoint list \
815 815
         --column "ID" \
816 816
         --column "Region" \
817 817
         --column "Service Name" \
818 818
         | grep " $2 " \
819 819
         | grep " $1 " | get_field 1)
820
-    if [[ -z "$ENDPOINT_ID" ]]; then
820
+    if [[ -z "$endpoint_id" ]]; then
821 821
         # Creates new endpoint
822
-        ENDPOINT_ID=$(openstack endpoint create \
822
+        endpoint_id=$(openstack endpoint create \
823 823
             $1 \
824 824
             --region $2 \
825 825
             --publicurl $3 \
... ...
@@ -827,9 +838,10 @@ function get_or_create_endpoint {
827 827
             --internalurl $5 \
828 828
             | grep " id " | get_field 2)
829 829
     fi
830
-    echo $ENDPOINT_ID
830
+    echo $endpoint_id
831 831
 }
832 832
 
833
+
833 834
 # Package Functions
834 835
 # =================
835 836
 
... ...
@@ -990,6 +1002,7 @@ function get_packages {
990 990
 }
991 991
 
992 992
 # Distro-agnostic package installer
993
+# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
993 994
 # install_package package [package ...]
994 995
 function update_package_repo {
995 996
     if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
... ...
@@ -1090,6 +1103,7 @@ function yum_install {
1090 1090
 }
1091 1091
 
1092 1092
 # zypper wrapper to set arguments correctly
1093
+# Uses globals ``OFFLINE``, ``*_proxy``
1093 1094
 # zypper_install package [package ...]
1094 1095
 function zypper_install {
1095 1096
     [[ "$OFFLINE" = "True" ]] && return
... ...
@@ -1106,7 +1120,8 @@ function zypper_install {
1106 1106
 # _run_process() is designed to be backgrounded by run_process() to simulate a
1107 1107
 # fork.  It includes the dirty work of closing extra filehandles and preparing log
1108 1108
 # files to produce the same logs as screen_it().  The log filename is derived
1109
-# from the service name and global-and-now-misnamed SCREEN_LOGDIR
1109
+# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1110
+# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``
1110 1111
 # _run_process service "command-line"
1111 1112
 function _run_process {
1112 1113
     local service=$1
... ...
@@ -1132,6 +1147,7 @@ function _run_process {
1132 1132
 
1133 1133
 # Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1134 1134
 # This is used for ``service_check`` when all the ``screen_it`` are called finished
1135
+# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
1135 1136
 # init_service_check
1136 1137
 function init_service_check {
1137 1138
     SCREEN_NAME=${SCREEN_NAME:-stack}
... ...
@@ -1149,15 +1165,15 @@ function init_service_check {
1149 1149
 function is_running {
1150 1150
     local name=$1
1151 1151
     ps auxw | grep -v grep | grep ${name} > /dev/null
1152
-    RC=$?
1152
+    local exitcode=$?
1153 1153
     # some times I really hate bash reverse binary logic
1154
-    return $RC
1154
+    return $exitcode
1155 1155
 }
1156 1156
 
1157 1157
 # run_process() launches a child process that closes all file descriptors and
1158 1158
 # then exec's the passed in command.  This is meant to duplicate the semantics
1159 1159
 # of screen_it() without screen.  PIDs are written to
1160
-# $SERVICE_DIR/$SCREEN_NAME/$service.pid
1160
+# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid``
1161 1161
 # run_process service "command-line"
1162 1162
 function run_process {
1163 1163
     local service=$1
... ...
@@ -1169,6 +1185,8 @@ function run_process {
1169 1169
 }
1170 1170
 
1171 1171
 # Helper to launch a service in a named screen
1172
+# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_NAME``, ``SCREEN_LOGDIR``,
1173
+# ``SERVICE_DIR``, ``USE_SCREEN``
1172 1174
 # screen_it service "command-line"
1173 1175
 function screen_it {
1174 1176
     SCREEN_NAME=${SCREEN_NAME:-stack}
... ...
@@ -1211,6 +1229,7 @@ function screen_it {
1211 1211
 }
1212 1212
 
1213 1213
 # Screen rc file builder
1214
+# Uses globals ``SCREEN_NAME``, ``SCREENRC``
1214 1215
 # screen_rc service "command-line"
1215 1216
 function screen_rc {
1216 1217
     SCREEN_NAME=${SCREEN_NAME:-stack}
... ...
@@ -1241,6 +1260,7 @@ function screen_rc {
1241 1241
 # If a PID is available use it, kill the whole process group via TERM
1242 1242
 # If screen is being used kill the screen window; this will catch processes
1243 1243
 # that did not leave a PID behind
1244
+# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
1244 1245
 # screen_stop service
1245 1246
 function screen_stop {
1246 1247
     SCREEN_NAME=${SCREEN_NAME:-stack}
... ...
@@ -1261,6 +1281,7 @@ function screen_stop {
1261 1261
 }
1262 1262
 
1263 1263
 # Helper to get the status of each running service
1264
+# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
1264 1265
 # service_check
1265 1266
 function service_check {
1266 1267
     local service
... ...
@@ -1330,18 +1351,19 @@ function pip_install {
1330 1330
     fi
1331 1331
     if [[ $TRACK_DEPENDS = True ]]; then
1332 1332
         source $DEST/.venv/bin/activate
1333
-        CMD_PIP=$DEST/.venv/bin/pip
1334
-        SUDO_PIP="env"
1333
+        local cmd_pip=$DEST/.venv/bin/pip
1334
+        local sudo_pip="env"
1335 1335
     else
1336
-        SUDO_PIP="sudo"
1337
-        CMD_PIP=$(get_pip_command)
1336
+        local cmd_pip=$(get_pip_command)
1337
+        local sudo_pip="sudo"
1338 1338
     fi
1339 1339
 
1340 1340
     # Mirror option not needed anymore because pypi has CDN available,
1341 1341
     # but it's useful in certain circumstances
1342 1342
     PIP_USE_MIRRORS=${PIP_USE_MIRRORS:-False}
1343
+    local pip_mirror_opt=""
1343 1344
     if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
1344
-        PIP_MIRROR_OPT="--use-mirrors"
1345
+        pip_mirror_opt="--use-mirrors"
1345 1346
     fi
1346 1347
 
1347 1348
     # pip < 1.4 has a bug where it will use an already existing build
... ...
@@ -1354,13 +1376,13 @@ function pip_install {
1354 1354
     local pip_build_tmp=$(mktemp --tmpdir -d pip-build.XXXXX)
1355 1355
 
1356 1356
     $xtrace
1357
-    $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
1357
+    $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
1358 1358
         http_proxy=$http_proxy \
1359 1359
         https_proxy=$https_proxy \
1360 1360
         no_proxy=$no_proxy \
1361
-        $CMD_PIP install --build=${pip_build_tmp} \
1362
-        $PIP_MIRROR_OPT $@ \
1363
-        && $SUDO_PIP rm -rf ${pip_build_tmp}
1361
+        $cmd_pip install --build=${pip_build_tmp} \
1362
+        $pip_mirror_opt $@ \
1363
+        && $sudo_pip rm -rf ${pip_build_tmp}
1364 1364
 }
1365 1365
 
1366 1366
 # this should be used if you want to install globally, all libraries should
... ...
@@ -1395,7 +1417,7 @@ function setup_package_with_req_sync {
1395 1395
 
1396 1396
     if [[ $update_requirements != "changed" ]]; then
1397 1397
         (cd $REQUIREMENTS_DIR; \
1398
-            $SUDO_CMD python update.py $project_dir)
1398
+            python update.py $project_dir)
1399 1399
     fi
1400 1400
 
1401 1401
     setup_package $project_dir $flags
... ...
@@ -1502,6 +1524,7 @@ function disable_service {
1502 1502
 # enable_service service [service ...]
1503 1503
 function enable_service {
1504 1504
     local tmpsvcs="${ENABLED_SERVICES}"
1505
+    local service
1505 1506
     for service in $@; do
1506 1507
         if ! is_service_enabled $service; then
1507 1508
             tmpsvcs+=",$service"
... ...
@@ -1538,7 +1561,8 @@ function is_service_enabled {
1538 1538
     local xtrace=$(set +o | grep xtrace)
1539 1539
     set +o xtrace
1540 1540
     local enabled=1
1541
-    services=$@
1541
+    local services=$@
1542
+    local service
1542 1543
     for service in ${services}; do
1543 1544
         [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
1544 1545
 
... ...
@@ -1574,8 +1598,9 @@ function is_service_enabled {
1574 1574
 function use_exclusive_service {
1575 1575
     local options=${!1}
1576 1576
     local selection=$3
1577
-    out=$2
1577
+    local out=$2
1578 1578
     [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
1579
+    local opt
1579 1580
     for opt in $options;do
1580 1581
         [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1581 1582
     done
... ...
@@ -1599,7 +1624,7 @@ function _safe_permission_operation {
1599 1599
 
1600 1600
     let last="${#args[*]} - 1"
1601 1601
 
1602
-    dir_to_check=${args[$last]}
1602
+    local dir_to_check=${args[$last]}
1603 1603
     if [ ! -d "$dir_to_check" ]; then
1604 1604
         dir_to_check=`dirname "$dir_to_check"`
1605 1605
     fi