Skip to content

Commit ada886d

Browse files
committed
Don't mix declaration and set of locals
Ia0957b47187c3dcadd46154b17022c4213781112 proposes to have bashate find instances of setting a local value. The issue is that "local" always returns 0, thus hiding any failure in the commands running to set the variable. This is an automated replacement of such instances Depends-On: I676c805e8f0401f75cc5367eee83b3d880cdef81 Change-Id: I9c8912a8fd596535589b207d7fc553b9d951d3fe
1 parent 433a9b1 commit ada886d

30 files changed

+286
-143
lines changed

exercises/neutron-adv-test.sh

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -122,41 +122,47 @@ function foreach_tenant_net {
122122
}
123123

124124
function get_image_id {
125-
local IMAGE_ID=$(openstack image list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1)
125+
local IMAGE_ID
126+
IMAGE_ID=$(openstack image list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1)
126127
die_if_not_set $LINENO IMAGE_ID "Failure retrieving IMAGE_ID"
127128
echo "$IMAGE_ID"
128129
}
129130

130131
function get_tenant_id {
131132
local TENANT_NAME=$1
132-
local TENANT_ID=`openstack project list | grep " $TENANT_NAME " | head -n 1 | get_field 1`
133+
local TENANT_ID
134+
TENANT_ID=`openstack project list | grep " $TENANT_NAME " | head -n 1 | get_field 1`
133135
die_if_not_set $LINENO TENANT_ID "Failure retrieving TENANT_ID for $TENANT_NAME"
134136
echo "$TENANT_ID"
135137
}
136138

137139
function get_user_id {
138140
local USER_NAME=$1
139-
local USER_ID=`openstack user list | grep $USER_NAME | awk '{print $2}'`
141+
local USER_ID
142+
USER_ID=`openstack user list | grep $USER_NAME | awk '{print $2}'`
140143
die_if_not_set $LINENO USER_ID "Failure retrieving USER_ID for $USER_NAME"
141144
echo "$USER_ID"
142145
}
143146

144147
function get_role_id {
145148
local ROLE_NAME=$1
146-
local ROLE_ID=`openstack role list | grep $ROLE_NAME | awk '{print $2}'`
149+
local ROLE_ID
150+
ROLE_ID=`openstack role list | grep $ROLE_NAME | awk '{print $2}'`
147151
die_if_not_set $LINENO ROLE_ID "Failure retrieving ROLE_ID for $ROLE_NAME"
148152
echo "$ROLE_ID"
149153
}
150154

151155
function get_network_id {
152156
local NETWORK_NAME="$1"
153-
local NETWORK_ID=`neutron net-list -F id -- --name=$NETWORK_NAME | awk "NR==4" | awk '{print $2}'`
157+
local NETWORK_ID
158+
NETWORK_ID=`neutron net-list -F id -- --name=$NETWORK_NAME | awk "NR==4" | awk '{print $2}'`
154159
echo $NETWORK_ID
155160
}
156161

157162
function get_flavor_id {
158163
local INSTANCE_TYPE=$1
159-
local FLAVOR_ID=`nova flavor-list | grep $INSTANCE_TYPE | awk '{print $2}'`
164+
local FLAVOR_ID
165+
FLAVOR_ID=`nova flavor-list | grep $INSTANCE_TYPE | awk '{print $2}'`
160166
die_if_not_set $LINENO FLAVOR_ID "Failure retrieving FLAVOR_ID for $INSTANCE_TYPE"
161167
echo "$FLAVOR_ID"
162168
}
@@ -185,13 +191,15 @@ function add_tenant {
185191

186192
function remove_tenant {
187193
local TENANT=$1
188-
local TENANT_ID=$(get_tenant_id $TENANT)
194+
local TENANT_ID
195+
TENANT_ID=$(get_tenant_id $TENANT)
189196
openstack project delete $TENANT_ID
190197
}
191198

192199
function remove_user {
193200
local USER=$1
194-
local USER_ID=$(get_user_id $USER)
201+
local USER_ID
202+
USER_ID=$(get_user_id $USER)
195203
openstack user delete $USER_ID
196204
}
197205

@@ -221,9 +229,11 @@ function create_network {
221229
local NET_NAME="${TENANT}-net$NUM"
222230
local ROUTER_NAME="${TENANT}-router${NUM}"
223231
source $TOP_DIR/openrc admin admin
224-
local TENANT_ID=$(get_tenant_id $TENANT)
232+
local TENANT_ID
233+
TENANT_ID=$(get_tenant_id $TENANT)
225234
source $TOP_DIR/openrc $TENANT $TENANT
226-
local NET_ID=$(neutron net-create --tenant-id $TENANT_ID $NET_NAME $EXTRA| grep ' id ' | awk '{print $4}' )
235+
local NET_ID
236+
NET_ID=$(neutron net-create --tenant-id $TENANT_ID $NET_NAME $EXTRA| grep ' id ' | awk '{print $4}' )
227237
die_if_not_set $LINENO NET_ID "Failure creating NET_ID for $TENANT_ID $NET_NAME $EXTRA"
228238
neutron subnet-create --ip-version 4 --tenant-id $TENANT_ID --gateway $GATEWAY $NET_ID $CIDR
229239
neutron_debug_admin probe-create --device-owner compute $NET_ID
@@ -251,7 +261,8 @@ function create_vm {
251261
done
252262
#TODO (nati) Add multi-nic test
253263
#TODO (nati) Add public-net test
254-
local VM_UUID=`nova boot --flavor $(get_flavor_id m1.tiny) \
264+
local VM_UUID
265+
VM_UUID=`nova boot --flavor $(get_flavor_id m1.tiny) \
255266
--image $(get_image_id) \
256267
$NIC \
257268
$TENANT-server$NUM | grep ' id ' | cut -d"|" -f3 | sed 's/ //g'`
@@ -309,7 +320,8 @@ function delete_network {
309320
local NUM=$2
310321
local NET_NAME="${TENANT}-net$NUM"
311322
source $TOP_DIR/openrc admin admin
312-
local TENANT_ID=$(get_tenant_id $TENANT)
323+
local TENANT_ID
324+
TENANT_ID=$(get_tenant_id $TENANT)
313325
#TODO(nati) comment out until l3-agent merged
314326
#for res in port subnet net router;do
315327
for net_id in `neutron net-list -c id -c name | grep $NET_NAME | awk '{print $2}'`;do

functions

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ function upload_image {
264264
;;
265265
*.img)
266266
image_name=$(basename "$image" ".img")
267-
local format=$(qemu-img info ${image} | awk '/^file format/ { print $3; exit }')
267+
local format
268+
format=$(qemu-img info ${image} | awk '/^file format/ { print $3; exit }')
268269
if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then
269270
disk_format=$format
270271
else
@@ -405,7 +406,8 @@ function get_instance_ip {
405406
local vm_id=$1
406407
local network_name=$2
407408
local nova_result="$(nova show $vm_id)"
408-
local ip=$(echo "$nova_result" | grep "$network_name" | get_field 2)
409+
local ip
410+
ip=$(echo "$nova_result" | grep "$network_name" | get_field 2)
409411
if [[ $ip = "" ]];then
410412
echo "$nova_result"
411413
die $LINENO "[Fail] Coudn't get ipaddress of VM"
@@ -455,7 +457,8 @@ function check_path_perm_sanity {
455457
# homedir permissions on RHEL and common practice of making DEST in
456458
# the stack user's homedir.
457459

458-
local real_path=$(readlink -f $1)
460+
local real_path
461+
real_path=$(readlink -f $1)
459462
local rebuilt_path=""
460463
for i in $(echo ${real_path} | tr "/" " "); do
461464
rebuilt_path=$rebuilt_path"/"$i

functions-common

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ function isset {
140140
# backtrace level
141141
function backtrace {
142142
local level=$1
143-
local deep=$((${#BASH_SOURCE[@]} - 1))
143+
local deep
144+
deep=$((${#BASH_SOURCE[@]} - 1))
144145
echo "[Call Trace]"
145146
while [ $level -le $deep ]; do
146147
echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
@@ -477,7 +478,8 @@ function git_clone {
477478
local git_remote=$1
478479
local git_dest=$2
479480
local git_ref=$3
480-
local orig_dir=$(pwd)
481+
local orig_dir
482+
orig_dir=$(pwd)
481483
local git_clone_flags=""
482484

483485
RECLONE=$(trueorfalse False RECLONE)
@@ -641,7 +643,8 @@ function get_default_host_ip {
641643
host_ip=""
642644
# Find the interface used for the default route
643645
host_ip_iface=${host_ip_iface:-$(ip -f $af route | awk '/default/ {print $5}' | head -1)}
644-
local host_ips=$(LC_ALL=C ip -f $af addr show ${host_ip_iface} | sed /temporary/d |awk /$af'/ {split($2,parts,"/"); print parts[1]}')
646+
local host_ips
647+
host_ips=$(LC_ALL=C ip -f $af addr show ${host_ip_iface} | sed /temporary/d |awk /$af'/ {split($2,parts,"/"); print parts[1]}')
645648
local ip
646649
for ip in $host_ips; do
647650
# Attempt to filter out IP addresses that are part of the fixed and
@@ -690,7 +693,8 @@ function get_field {
690693
# copy over a default policy.json and policy.d for projects
691694
function install_default_policy {
692695
local project=$1
693-
local project_uc=$(echo $1|tr a-z A-Z)
696+
local project_uc
697+
project_uc=$(echo $1|tr a-z A-Z)
694698
local conf_dir="${project_uc}_CONF_DIR"
695699
# eval conf dir to get the variable
696700
conf_dir="${!conf_dir}"
@@ -723,7 +727,8 @@ function policy_add {
723727

724728
# Add a terminating comma to policy lines without one
725729
# Remove the closing '}' and all lines following to the end-of-file
726-
local tmpfile=$(mktemp)
730+
local tmpfile
731+
tmpfile=$(mktemp)
727732
uniq ${policy_file} | sed -e '
728733
s/]$/],/
729734
/^[}]/,$d
@@ -945,7 +950,8 @@ function get_or_create_endpoint {
945950
# scenarios currently that use the returned id. Ideally this behaviour
946951
# should be pushed out to the service setups and let them create the
947952
# endpoints they need.
948-
local public_id=$(_get_or_create_endpoint_with_interface $1 public $3 $2)
953+
local public_id
954+
public_id=$(_get_or_create_endpoint_with_interface $1 public $3 $2)
949955
_get_or_create_endpoint_with_interface $1 admin $4 $2
950956
_get_or_create_endpoint_with_interface $1 internal $5 $2
951957

@@ -1065,7 +1071,8 @@ function get_packages {
10651071
xtrace=$(set +o | grep xtrace)
10661072
set +o xtrace
10671073
local services=$@
1068-
local package_dir=$(_get_package_dir)
1074+
local package_dir
1075+
package_dir=$(_get_package_dir)
10691076
local file_to_parse=""
10701077
local service=""
10711078

@@ -1980,8 +1987,10 @@ function address_in_net {
19801987
local ip=$1
19811988
local range=$2
19821989
local masklen=${range#*/}
1983-
local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1984-
local subnet=$(maskip $ip $(cidr2netmask $masklen))
1990+
local network
1991+
network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1992+
local subnet
1993+
subnet=$(maskip $ip $(cidr2netmask $masklen))
19851994
[[ $network == $subnet ]]
19861995
}
19871996

@@ -2033,7 +2042,8 @@ function export_proxy_variables {
20332042

20342043
# Returns true if the directory is on a filesystem mounted via NFS.
20352044
function is_nfs_directory {
2036-
local mount_type=`stat -f -L -c %T $1`
2045+
local mount_type
2046+
mount_type=`stat -f -L -c %T $1`
20372047
test "$mount_type" == "nfs"
20382048
}
20392049

@@ -2044,13 +2054,15 @@ function maskip {
20442054
local ip=$1
20452055
local mask=$2
20462056
local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
2047-
local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
2057+
local subnet
2058+
subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
20482059
echo $subnet
20492060
}
20502061

20512062
# Return the current python as "python<major>.<minor>"
20522063
function python_version {
2053-
local python_version=$(python -c 'import sys; print("%s.%s" % sys.version_info[0:2])')
2064+
local python_version
2065+
python_version=$(python -c 'import sys; print("%s.%s" % sys.version_info[0:2])')
20542066
echo "python${python_version}"
20552067
}
20562068

inc/ini-config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ function iniset {
196196
$option = $value
197197
" "$file"
198198
else
199-
local sep=$(echo -ne "\x01")
199+
local sep
200+
sep=$(echo -ne "\x01")
200201
# Replace it
201202
$sudo sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file"
202203
fi

inc/meta-config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ function merge_config_file {
8989
# note, configfile might be a variable (note the iniset, etc
9090
# created in the mega-awk below is "eval"ed too, so we just leave
9191
# it alone.
92-
local real_configfile=$(eval echo $configfile)
92+
local real_configfile
93+
real_configfile=$(eval echo $configfile)
9394
if [ ! -f $real_configfile ]; then
9495
touch $real_configfile
9596
fi

inc/python

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ function get_python_exec_prefix {
6161
# pip_install_gr packagename
6262
function pip_install_gr {
6363
local name=$1
64-
local clean_name=$(get_from_global_requirements $name)
64+
local clean_name
65+
clean_name=$(get_from_global_requirements $name)
6566
pip_install $clean_name
6667
}
6768

@@ -100,7 +101,8 @@ function pip_install {
100101
local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
101102
local sudo_pip="env"
102103
else
103-
local cmd_pip=$(get_pip_command)
104+
local cmd_pip
105+
cmd_pip=$(get_pip_command)
104106
local sudo_pip="sudo -H"
105107
fi
106108
fi
@@ -109,7 +111,8 @@ function pip_install {
109111
# Always apply constraints
110112
cmd_pip="$cmd_pip -c $REQUIREMENTS_DIR/upper-constraints.txt"
111113

112-
local pip_version=$(python -c "import pip; \
114+
local pip_version
115+
pip_version=$(python -c "import pip; \
113116
print(pip.__version__.strip('.')[0])")
114117
if (( pip_version<6 )); then
115118
die $LINENO "Currently installed pip version ${pip_version} does not" \
@@ -143,7 +146,8 @@ function pip_install {
143146
# get_from_global_requirements <package>
144147
function get_from_global_requirements {
145148
local package=$1
146-
local required_pkg=$(grep -i -h ^${package} $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1)
149+
local required_pkg
150+
required_pkg=$(grep -i -h ^${package} $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1)
147151
if [[ $required_pkg == "" ]]; then
148152
die $LINENO "Can't find package $package in requirements"
149153
fi
@@ -222,7 +226,8 @@ function setup_develop {
222226
# practical ways.
223227
function is_in_projects_txt {
224228
local project_dir=$1
225-
local project_name=$(basename $project_dir)
229+
local project_name
230+
project_name=$(basename $project_dir)
226231
grep -q "/$project_name\$" $REQUIREMENTS_DIR/projects.txt
227232
}
228233

@@ -241,7 +246,8 @@ function setup_package_with_constraints_edit {
241246

242247
if [ -n "$REQUIREMENTS_DIR" ]; then
243248
# Constrain this package to this project directory from here on out.
244-
local name=$(awk '/^name.*=/ {print $3}' $project_dir/setup.cfg)
249+
local name
250+
name=$(awk '/^name.*=/ {print $3}' $project_dir/setup.cfg)
245251
$REQUIREMENTS_DIR/.venv/bin/edit-constraints \
246252
$REQUIREMENTS_DIR/upper-constraints.txt -- $name \
247253
"$flags file://$project_dir#egg=$name"

inc/rootwrap

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ function add_sudo_secure_path {
4141
# configure_rootwrap project
4242
function configure_rootwrap {
4343
local project=$1
44-
local project_uc=$(echo $1|tr a-z A-Z)
44+
local project_uc
45+
project_uc=$(echo $1|tr a-z A-Z)
4546
local bin_dir="${project_uc}_BIN_DIR"
4647
bin_dir="${!bin_dir}"
4748
local project_dir="${project_uc}_DIR"
@@ -60,7 +61,8 @@ function configure_rootwrap {
6061
sudo sed -e "s:^filters_path=.*$:filters_path=/etc/${project}/rootwrap.d:" -i /etc/${project}/rootwrap.conf
6162

6263
# Set up the rootwrap sudoers
63-
local tempfile=$(mktemp)
64+
local tempfile
65+
tempfile=$(mktemp)
6466
# Specify rootwrap.conf as first parameter to rootwrap
6567
rootwrap_sudo_cmd="${rootwrap_bin} /etc/${project}/rootwrap.conf *"
6668
echo "$STACK_USER ALL=(root) NOPASSWD: $rootwrap_sudo_cmd" >$tempfile

lib/apache

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,14 @@ function install_apache_wsgi {
7272
# various differences between Apache 2.2 and 2.4 that warrant special handling.
7373
function get_apache_version {
7474
if is_ubuntu; then
75-
local version_str=$(sudo /usr/sbin/apache2ctl -v | awk '/Server version/ {print $3}' | cut -f2 -d/)
75+
local version_str
76+
version_str=$(sudo /usr/sbin/apache2ctl -v | awk '/Server version/ {print $3}' | cut -f2 -d/)
7677
elif is_fedora; then
77-
local version_str=$(rpm -qa --queryformat '%{VERSION}' httpd)
78+
local version_str
79+
version_str=$(rpm -qa --queryformat '%{VERSION}' httpd)
7880
elif is_suse; then
79-
local version_str=$(rpm -qa --queryformat '%{VERSION}' apache2)
81+
local version_str
82+
version_str=$(rpm -qa --queryformat '%{VERSION}' apache2)
8083
else
8184
exit_distro_not_supported "cannot determine apache version"
8285
fi
@@ -115,7 +118,8 @@ function get_apache_version {
115118
function apache_site_config_for {
116119
local site=$@
117120
if is_ubuntu; then
118-
local apache_version=$(get_apache_version)
121+
local apache_version
122+
apache_version=$(get_apache_version)
119123
if [[ "$apache_version" == "2.2" ]]; then
120124
# Ubuntu 12.04 - Apache 2.2
121125
echo $APACHE_CONF_DIR/${site}

0 commit comments

Comments
 (0)