Merge ~smoser/cloud-init:cleanup/ds-identify-shellcheck-fixes into cloud-init:master

Proposed by Scott Moser
Status: Merged
Approved by: Scott Moser
Approved revision: c172d647ab5e11b2afc55126705aac1ee17ea911
Merge reported by: Scott Moser
Merged at revision: 4c1af5c7eb8db67f51f35130e13157a735256d2b
Proposed branch: ~smoser/cloud-init:cleanup/ds-identify-shellcheck-fixes
Merge into: cloud-init:master
Diff against target: 216 lines (+32/-18)
1 file modified
tools/ds-identify (+32/-18)
Reviewer Review Type Date Requested Status
Scott Moser Approve
Server Team CI bot continuous-integration Approve
Review via email: mp+344768@code.launchpad.net

Commit message

ds-identify: make shellcheck 0.4.6 happy with ds-identify.

This fixes warnings reported by shellcheck at 0.4.6.
The complaints that we are ignoring globally (top of the file) are:
 2015: Note that A && B || C is not if-then-else. C may run if A is true.
 2039: In POSIX sh, 'local' is undefined.
 2162: read without -r will mangle backslashes.
 2166: Prefer [ p ] && [ q ] as [ p -a q ] is not well defined.

Most of the complaints were just noise, but a few unused variables
were reported and fixed.

Related shellcheck issues opened:
 - https://github.com/koalaman/shellcheck/issues/1191
 - https://github.com/koalaman/shellcheck/issues/1192
 - https://github.com/koalaman/shellcheck/issues/1193
 - https://github.com/koalaman/shellcheck/issues/1194

Description of the change

see commit message

To post a comment you must log in.
Revision history for this message
Scott Moser (smoser) wrote :

In the process of doing this I filed 4 issues (2 bugs, 2 feature requests)

 - https://github.com/koalaman/shellcheck/issues/1191
 - https://github.com/koalaman/shellcheck/issues/1192
 - https://github.com/koalaman/shellcheck/issues/1193
 - https://github.com/koalaman/shellcheck/issues/1194

Things I am not terribly happy about:
 * don't know how to run this from tox or c-i and get a 'shellcheck-tip' and 'shellchec' style targets.
 * I don't love the '# shellcheck disable' noise. perhaps we could strip them out when installing.

Revision history for this message
Server Team CI bot (server-team-bot) wrote :

PASSED: Continuous integration, rev:f99b8c8143b35606895c9354edd7e3fb33134640
https://jenkins.ubuntu.com/server/job/cloud-init-ci/1072/
Executed test runs:
    SUCCESS: Checkout
    SUCCESS: Unit & Style Tests
    SUCCESS: Ubuntu LTS: Build
    SUCCESS: Ubuntu LTS: Integration
    SUCCESS: MAAS Compatability Testing
    IN_PROGRESS: Declarative: Post Actions

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/1072/rebuild

review: Approve (continuous-integration)
Revision history for this message
Ryan Harper (raharper) wrote :

LGTM, some nits inline.

Revision history for this message
Server Team CI bot (server-team-bot) wrote :

PASSED: Continuous integration, rev:c172d647ab5e11b2afc55126705aac1ee17ea911
https://jenkins.ubuntu.com/server/job/cloud-init-ci/1075/
Executed test runs:
    SUCCESS: Checkout
    SUCCESS: Unit & Style Tests
    SUCCESS: Ubuntu LTS: Build
    SUCCESS: Ubuntu LTS: Integration
    SUCCESS: MAAS Compatability Testing
    IN_PROGRESS: Declarative: Post Actions

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/1075/rebuild

review: Approve (continuous-integration)
Revision history for this message
Scott Moser (smoser) wrote :

based on Ryan's lgtm.

review: Approve
Revision history for this message
Scott Moser (smoser) wrote :

An upstream commit landed for this bug.

To view that commit see the following URL:
https://git.launchpad.net/cloud-init/commit/?id=4c1af5c7

There was an error fetching revisions from git servers. Please try again in a few minutes. If the problem persists, contact Launchpad support.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/tools/ds-identify b/tools/ds-identify
2index 7fff5d1..4e871c0 100755
3--- a/tools/ds-identify
4+++ b/tools/ds-identify
5@@ -1,4 +1,5 @@
6 #!/bin/sh
7+# shellcheck disable=2015,2039,2162,2166
8 #
9 # ds-identify is configured via /etc/cloud/ds-identify.cfg
10 # or on the kernel command line. It takes primarily 2 inputs:
11@@ -125,7 +126,6 @@ DI_ON_NOTFOUND=""
12 DI_EC2_STRICT_ID_DEFAULT="true"
13
14 _IS_IBM_CLOUD=""
15-_IS_IBM_PROVISIONING=""
16
17 error() {
18 set -- "ERROR:" "$@";
19@@ -211,7 +211,9 @@ read_fs_info() {
20 # 'set --' will collapse multiple consecutive entries in IFS for
21 # whitespace characters (\n, tab, " ") so we cannot rely on getting
22 # empty lines in "$@" below.
23- IFS="$CR"; set -- $out; IFS="$oifs"
24+
25+ # shellcheck disable=2086
26+ { IFS="$CR"; set -- $out; IFS="$oifs"; }
27
28 for line in "$@"; do
29 case "${line}" in
30@@ -311,6 +313,7 @@ read_dmi_product_serial() {
31 DI_DMI_PRODUCT_SERIAL="$_RET"
32 }
33
34+# shellcheck disable=2034
35 read_uname_info() {
36 # run uname, and parse output.
37 # uname is tricky to parse as it outputs always in a given order
38@@ -330,6 +333,7 @@ read_uname_info() {
39 return $ret
40 }
41 fi
42+ # shellcheck disable=2086
43 set -- $out
44 DI_UNAME_KERNEL_NAME="$1"
45 DI_UNAME_NODENAME="$2"
46@@ -357,7 +361,8 @@ parse_yaml_array() {
47 # the fix was to quote the open bracket (val=${val#"["}) (LP: #1689648)
48 val=${val#"["}
49 val=${val%"]"}
50- IFS=","; set -- $val; IFS="$oifs"
51+ # shellcheck disable=2086
52+ { IFS=","; set -- $val; IFS="$oifs"; }
53 for tok in "$@"; do
54 trim "$tok"
55 unquote "$_RET"
56@@ -393,7 +398,7 @@ read_datasource_list() {
57 fi
58 if [ -z "$dslist" ]; then
59 dslist=${DI_DSLIST_DEFAULT}
60- debug 1 "no datasource_list found, using default:" $dslist
61+ debug 1 "no datasource_list found, using default: $dslist"
62 fi
63 DI_DSLIST=$dslist
64 return 0
65@@ -404,7 +409,8 @@ read_pid1_product_name() {
66 cached "${DI_PID_1_PRODUCT_NAME}" && return
67 [ -r "${PATH_PROC_1_ENVIRON}" ] || return
68 out=$(tr '\0' '\n' <"${PATH_PROC_1_ENVIRON}")
69- IFS="$CR"; set -- $out; IFS="$oifs"
70+ # shellcheck disable=2086
71+ { IFS="$CR"; set -- $out; IFS="$oifs"; }
72 for tok in "$@"; do
73 key=${tok%%=*}
74 [ "$key" != "$tok" ] || continue
75@@ -471,6 +477,7 @@ nocase_equal() {
76 [ "$1" = "$2" ] && return 0
77
78 local delim="-delim-"
79+ # shellcheck disable=2018,2019
80 out=$(echo "$1${delim}$2" | tr A-Z a-z)
81 [ "${out#*${delim}}" = "${out%${delim}*}" ]
82 }
83@@ -547,11 +554,13 @@ check_config() {
84 else
85 files="$*"
86 fi
87- set +f; set -- $files; set -f;
88+ # shellcheck disable=2086
89+ { set +f; set -- $files; set -f; }
90 if [ "$1" = "$files" -a ! -f "$1" ]; then
91 return 1
92 fi
93 local fname="" line="" ret="" found=0 found_fn=""
94+ # shellcheck disable=2094
95 for fname in "$@"; do
96 [ -f "$fname" ] || continue
97 while read line; do
98@@ -787,7 +796,7 @@ ec2_read_strict_setting() {
99 # 3. look for the key 'strict_id' (datasource/Ec2/strict_id)
100 # only in cloud.cfg or cloud.cfg.d/EC2.cfg (case insensitive)
101 local cfg="${PATH_ETC_CI_CFG}" cfg_d="${PATH_ETC_CI_CFG_D}"
102- if check_config strict_id $cfg "$cfg_d/*[Ee][Cc]2*.cfg"; then
103+ if check_config strict_id "$cfg" "$cfg_d/*[Ee][Cc]2*.cfg"; then
104 debug 2 "${_RET_fname} set strict_id to $_RET"
105 return 0
106 fi
107@@ -994,7 +1003,7 @@ dscheck_Scaleway() {
108 *\ scaleway\ *) return ${DS_FOUND};;
109 esac
110
111- if [ -f ${PATH_ROOT}/var/run/scaleway ]; then
112+ if [ -f "${PATH_ROOT}/var/run/scaleway" ]; then
113 return ${DS_FOUND}
114 fi
115
116@@ -1149,6 +1158,7 @@ found() {
117 }
118
119 trim() {
120+ # shellcheck disable=2048,2086
121 set -- $*
122 _RET="$*"
123 }
124@@ -1169,7 +1179,7 @@ _read_config() {
125 # if no parameters are set, modifies _rc scoped environment vars.
126 # if keyname is provided, then returns found value of that key.
127 local keyname="${1:-_unset}"
128- local line="" hash="#" ckey="" key="" val=""
129+ local line="" hash="#" key="" val=""
130 while read line; do
131 line=${line%%${hash}*}
132 key="${line%%:*}"
133@@ -1247,7 +1257,8 @@ parse_policy() {
134
135 local mode="" report="" found="" maybe="" notfound=""
136 local oifs="$IFS" tok="" val=""
137- IFS=","; set -- $policy; IFS="$oifs"
138+ # shellcheck disable=2086
139+ { IFS=","; set -- $policy; IFS="$oifs"; }
140 for tok in "$@"; do
141 val=${tok#*=}
142 case "$tok" in
143@@ -1314,15 +1325,15 @@ manual_clean_and_existing() {
144 }
145
146 read_uptime() {
147- local up idle
148+ local up _
149 _RET="${UNAVAILABLE}"
150- [ -f "$PATH_PROC_UPTIME" ] &&
151- read up idle < "$PATH_PROC_UPTIME" && _RET="$up"
152+ [ -f "$PATH_PROC_UPTIME" ] && read up _ < "$PATH_PROC_UPTIME" &&
153+ _RET="$up"
154 return
155 }
156
157 _main() {
158- local dscheck="" ret_dis=1 ret_en=0
159+ local dscheck_fn="" ret_dis=1 ret_en=0
160
161 read_uptime
162 debug 1 "[up ${_RET}s]" "ds-identify $*"
163@@ -1357,8 +1368,9 @@ _main() {
164 return
165 fi
166
167- # if there is only a single entry in $DI_DSLIST
168+ # shellcheck disable=2086
169 set -- $DI_DSLIST
170+ # if there is only a single entry in $DI_DSLIST
171 if [ $# -eq 1 ] || [ $# -eq 2 -a "$2" = "None" ] ; then
172 debug 1 "single entry in datasource_list ($DI_DSLIST) use that."
173 found "$@"
174@@ -1391,6 +1403,7 @@ _main() {
175 done
176
177 debug 2 "found=${found# } maybe=${maybe# }"
178+ # shellcheck disable=2086
179 set -- $found
180 if [ $# -ne 0 ]; then
181 if [ $# -eq 1 ]; then
182@@ -1406,6 +1419,7 @@ _main() {
183 return
184 fi
185
186+ # shellcheck disable=2086
187 set -- $maybe
188 if [ $# -ne 0 -a "${DI_ON_MAYBE}" != "none" ]; then
189 debug 1 "$# datasources returned maybe: $*"
190@@ -1434,7 +1448,7 @@ _main() {
191 *) error "Unexpected result";;
192 esac
193 debug 1 "$msg"
194- return $ret
195+ return "$ret"
196 }
197
198 main() {
199@@ -1445,7 +1459,7 @@ main() {
200 if read ret < "$PATH_RUN_DI_RESULT"; then
201 if [ "$ret" = "0" ] || [ "$ret" = "1" ]; then
202 debug 2 "used cached result $ret. pass --force to re-run."
203- return $ret;
204+ return "$ret";
205 fi
206 debug 1 "previous run returned unexpected '$ret'. Re-running."
207 else
208@@ -1457,7 +1471,7 @@ main() {
209 echo "$ret" > "$PATH_RUN_DI_RESULT"
210 read_uptime
211 debug 1 "[up ${_RET}s]" "returning $ret"
212- return $ret
213+ return "$ret"
214 }
215
216 noop() {

Subscribers

People subscribed via source and target branches