2014-03-20 01:56:27 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# Copyright 2014 Red Hat, Inc.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
|
|
# implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
# This script checks all files in the "elements" directory for some
|
|
|
|
# common mistakes and exits with a non-zero status if it finds any.
|
|
|
|
|
2014-03-20 02:06:31 +00:00
|
|
|
set -eu
|
|
|
|
set -o pipefail
|
|
|
|
|
2016-09-09 03:11:52 +00:00
|
|
|
ELEMENTS_DIR=diskimage_builder/elements
|
|
|
|
|
2014-04-25 21:44:25 +00:00
|
|
|
parse_exclusions() {
|
2014-07-02 16:15:18 +00:00
|
|
|
# Per-file exclusions
|
|
|
|
# Example: # dib-lint: disable=sete setpipefail
|
2014-04-25 21:44:25 +00:00
|
|
|
local filename=$1
|
|
|
|
local disable_pattern="# dib-lint: disable="
|
|
|
|
local exclusions=$(grep "^$disable_pattern.*$" $filename | sed "s/$disable_pattern//g")
|
2014-07-02 16:15:18 +00:00
|
|
|
|
|
|
|
# Global exclusions read from tox.ini
|
|
|
|
# Example section in tox.ini:
|
|
|
|
# [dib-lint]
|
|
|
|
# ignore = sete setu
|
|
|
|
section="dib-lint"
|
|
|
|
option="ignore"
|
|
|
|
global_exclusions=$(python -c \
|
|
|
|
"import ConfigParser; \
|
|
|
|
conf=ConfigParser.ConfigParser(); \
|
|
|
|
conf.read('tox.ini'); \
|
|
|
|
print conf.get('$section', '$option') if conf.has_option('$section', '$option') else ''"
|
|
|
|
)
|
|
|
|
echo $exclusions $global_exclusions
|
2014-04-25 21:44:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
excluded() {
|
|
|
|
local test_name=$1
|
|
|
|
for e in $exclusions; do
|
|
|
|
if [ "$e" = "$test_name" ]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2014-05-27 18:16:29 +00:00
|
|
|
error() {
|
2016-04-08 05:46:21 +00:00
|
|
|
echo -e "ERROR: $1"
|
2014-05-27 18:16:29 +00:00
|
|
|
rc=1
|
|
|
|
}
|
|
|
|
|
2016-04-22 00:21:51 +00:00
|
|
|
echo "Running dib-lint in $(pwd)"
|
|
|
|
|
2014-03-20 01:56:27 +00:00
|
|
|
rc=0
|
2015-06-18 09:07:05 +00:00
|
|
|
TMPDIR=$(mktemp -d /tmp/tmp.XXXXXXXXXX)
|
2014-03-20 02:05:16 +00:00
|
|
|
trap "rm -rf $TMPDIR" EXIT
|
2016-09-09 03:11:52 +00:00
|
|
|
for i in $(find $ELEMENTS_DIR -type f \
|
2016-01-29 04:49:10 +00:00
|
|
|
-not -name \*~ \
|
|
|
|
-not -name \#\*\# \
|
2015-09-24 10:51:07 +00:00
|
|
|
-not -name \*.orig \
|
2016-01-29 04:49:10 +00:00
|
|
|
-not -name \*.rst \
|
2015-09-15 10:48:23 +00:00
|
|
|
-not -name \*.yaml \
|
2016-01-29 04:49:10 +00:00
|
|
|
-not -name \*.py \
|
2015-05-29 05:39:46 +00:00
|
|
|
-not -name \*.pyc); do
|
2016-01-29 04:49:10 +00:00
|
|
|
|
2016-04-22 00:21:51 +00:00
|
|
|
echo "Checking $i"
|
|
|
|
|
2014-04-25 21:44:25 +00:00
|
|
|
exclusions=("$(parse_exclusions $i)")
|
2016-01-29 04:49:10 +00:00
|
|
|
|
2014-03-20 01:56:27 +00:00
|
|
|
# Check that files starting with a shebang are +x
|
2014-04-18 09:11:29 +00:00
|
|
|
firstline=$(head -n 1 "$i")
|
|
|
|
if [ "${firstline:0:2}" = "#!" ]; then
|
|
|
|
if [ ! -x "$i" ] && ! excluded executable; then
|
2014-05-27 18:16:29 +00:00
|
|
|
error "$i is not executable"
|
2014-04-25 21:44:25 +00:00
|
|
|
fi
|
2014-04-18 09:11:29 +00:00
|
|
|
|
2016-11-23 08:44:50 +00:00
|
|
|
# run flake8 over python files. note our "dib-python"
|
|
|
|
# interpreter can confuse the magic matching being done in
|
|
|
|
# "file" and make it think the file is not python;
|
|
|
|
# special-case it.
|
|
|
|
if [[ "$(file -b -k --mime-type $i)" =~ "text/x-python" ]] || \
|
|
|
|
[[ $firstline =~ "dib-python" ]]; then
|
2016-04-15 15:29:48 +00:00
|
|
|
flake8 $i || error "$i failed flake8"
|
2014-11-17 15:47:41 +00:00
|
|
|
else
|
2016-11-23 08:44:50 +00:00
|
|
|
# Ensure 4 spaces indent are used
|
2014-11-17 15:47:41 +00:00
|
|
|
if ! excluded indent ; then
|
2015-09-24 10:51:07 +00:00
|
|
|
indent_regex='^\( \{4\}\)* \{1,3\}[^ ]'
|
|
|
|
if grep -q "$indent_regex" ${i}; then
|
2014-11-17 15:47:41 +00:00
|
|
|
error "$i should use 4 spaces indent"
|
2015-09-24 10:51:07 +00:00
|
|
|
# outline the failing lines with line number
|
|
|
|
grep -n "$indent_regex" ${i}
|
2014-11-17 15:47:41 +00:00
|
|
|
fi
|
2014-11-12 02:06:25 +00:00
|
|
|
fi
|
2014-04-18 09:11:29 +00:00
|
|
|
fi
|
2014-03-20 01:56:27 +00:00
|
|
|
fi
|
2014-03-20 02:05:16 +00:00
|
|
|
|
|
|
|
# Check alphabetical ordering of element-deps
|
|
|
|
if [ $(basename $i) = "element-deps" ]; then
|
|
|
|
UNSORTED=${TMPDIR}/element-deps.unsorted
|
|
|
|
SORTED=${TMPDIR}/element-deps.sorted
|
2015-10-06 02:48:38 +00:00
|
|
|
grep -v -e '^#' -e '^$' $i > ${UNSORTED}
|
2014-03-20 02:05:16 +00:00
|
|
|
sort ${UNSORTED} > ${SORTED}
|
2014-11-26 20:23:15 +00:00
|
|
|
if [ -n "$(diff -c ${UNSORTED} ${SORTED})" ]; then
|
2014-05-27 18:16:29 +00:00
|
|
|
error "$i is not sorted alphabetically"
|
2015-10-06 02:48:38 +00:00
|
|
|
diff -y ${UNSORTED} ${SORTED}
|
2014-03-20 02:05:16 +00:00
|
|
|
fi
|
|
|
|
fi
|
2014-03-20 02:06:31 +00:00
|
|
|
|
2016-01-29 04:49:10 +00:00
|
|
|
# for consistency, let's just use #!/bin/bash everywhere (not
|
|
|
|
# /usr/bin/env, etc)
|
|
|
|
regex='^#!.*bash'
|
|
|
|
if [[ "$firstline" =~ $regex &&
|
|
|
|
"$firstline" != "#!/bin/bash" ]]; then
|
|
|
|
error "$i : only use #!/bin/bash for scripts"
|
|
|
|
fi
|
|
|
|
|
2014-09-04 04:56:29 +00:00
|
|
|
# Check that all scripts are set -eu -o pipefail and look for
|
|
|
|
# DIB_DEBUG_TRACE
|
2014-03-20 02:06:31 +00:00
|
|
|
# NOTE(bnemec): This doesn't verify that the set call occurs high
|
|
|
|
# enough in the file to be useful, but hopefully nobody will be
|
|
|
|
# sticking set calls at the end of their file to trick us. And if
|
|
|
|
# they are, that's easy enough to catch in reviews.
|
|
|
|
# Also, this is only going to check bash scripts - we've decided to
|
|
|
|
# explicitly require bash for any scripts that don't have a specific
|
|
|
|
# need to run under other shells, and any exceptions to that rule
|
|
|
|
# may not want these checks either.
|
2016-01-29 04:49:10 +00:00
|
|
|
if [[ "$firstline" =~ '#!/bin/bash' ]]; then
|
2014-03-20 02:06:31 +00:00
|
|
|
if ! excluded sete; then
|
|
|
|
if [ -z "$(grep "^set -[^ ]*e" $i)" ]; then
|
2014-05-27 18:16:29 +00:00
|
|
|
error "$i is not set -e"
|
2014-03-20 02:06:31 +00:00
|
|
|
fi
|
|
|
|
fi
|
2014-04-12 00:39:50 +00:00
|
|
|
if ! excluded setu; then
|
|
|
|
if [ -z "$(grep "^set -[^ ]*u" $i)" ]; then
|
2014-05-27 18:16:29 +00:00
|
|
|
error "$i is not set -u"
|
2014-04-12 00:39:50 +00:00
|
|
|
fi
|
|
|
|
fi
|
2014-03-29 03:38:45 +00:00
|
|
|
if ! excluded setpipefail; then
|
|
|
|
if [ -z "$(grep "^set -o pipefail" $i)" ]; then
|
2014-05-27 18:16:29 +00:00
|
|
|
error "$i is not set -o pipefail"
|
2014-03-29 03:38:45 +00:00
|
|
|
fi
|
|
|
|
fi
|
2014-09-04 04:56:29 +00:00
|
|
|
if ! excluded dibdebugtrace; then
|
|
|
|
if [ -z "$(grep "DIB_DEBUG_TRACE" $i)" ]; then
|
|
|
|
error "$i does not follow DIB_DEBUG_TRACE"
|
|
|
|
fi
|
|
|
|
fi
|
2014-03-20 02:06:31 +00:00
|
|
|
fi
|
2016-04-08 05:46:21 +00:00
|
|
|
|
2016-10-17 13:16:55 +00:00
|
|
|
# check that environment files don't "set -x" and they have no executable
|
|
|
|
# bits set
|
2016-10-20 02:51:16 +00:00
|
|
|
if [[ "$i" =~ (environment.d) ]]; then
|
|
|
|
if grep -q "set -x" $i; then
|
|
|
|
error "Environment file $i should not set tracing"
|
|
|
|
fi
|
2016-10-17 13:16:55 +00:00
|
|
|
if [[ -x $i ]]; then
|
|
|
|
error "Environment file $i should not be marked as executable"
|
|
|
|
fi
|
2016-10-20 02:51:16 +00:00
|
|
|
fi
|
2016-04-08 05:46:21 +00:00
|
|
|
|
2016-11-22 07:02:42 +00:00
|
|
|
# check for
|
|
|
|
# export FOO=$(bar)
|
|
|
|
# calls. These are dangerous, because the export hides the return
|
|
|
|
# code of the $(bar) call. Split this into 2 lines and -e will
|
|
|
|
# fail on the assignment
|
|
|
|
if grep -q 'export .*\$(' $i; then
|
|
|
|
error "Split export and assignments in $i"
|
2016-10-20 02:51:16 +00:00
|
|
|
fi
|
2016-04-08 05:46:21 +00:00
|
|
|
|
|
|
|
# check that sudo calls in phases run outside the chroot look
|
|
|
|
# "safe"; meaning that they seem to operate within the chroot
|
|
|
|
# somehow. This is not fool-proof, but catches egregious errors,
|
|
|
|
# and makes you think about it if you're doing something outside
|
|
|
|
# the box.
|
|
|
|
if ! excluded safe_sudo; then
|
|
|
|
if [[ $(dirname $i) =~ (root.d|extra-data.d|block-device.d|finalise.d|cleanup.d) ]]; then
|
|
|
|
while read LINE
|
|
|
|
do
|
|
|
|
if [[ $LINE =~ "sudo " ]]; then
|
|
|
|
# messy regex ahead! Don't match:
|
|
|
|
# - explicitly ignored
|
|
|
|
# - basic comments
|
|
|
|
# - install-packages ... sudo ...
|
|
|
|
# - any of the paths passed into the out-of-chroot elements
|
|
|
|
if [[ $LINE =~ (dib-lint: safe_sudo|^#|install-packages|TARGET_ROOT|IMAGE_BLOCK_DEVICE|TMP_MOUNT_PATH|TMP_IMAGE_PATH) ]]; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
error "$i : potentially unsafe sudo\n -- $LINE"
|
|
|
|
fi
|
|
|
|
done < $i
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2014-03-20 01:56:27 +00:00
|
|
|
done
|
2014-04-18 08:37:06 +00:00
|
|
|
|
2016-04-22 00:21:51 +00:00
|
|
|
echo "Checking indents..."
|
|
|
|
|
2016-09-09 03:11:52 +00:00
|
|
|
for i in $(find $ELEMENTS_DIR -type f -and -name '*.rst' -or -type f -executable); do
|
2014-04-18 08:37:06 +00:00
|
|
|
# Check for tab indentation
|
2014-11-12 02:06:25 +00:00
|
|
|
if ! excluded tabindent; then
|
2014-07-24 17:50:48 +00:00
|
|
|
if grep -q $'^ *\t' ${i}; then
|
|
|
|
error "$i contains tab characters"
|
|
|
|
fi
|
2014-04-18 08:37:06 +00:00
|
|
|
fi
|
2014-05-02 16:15:42 +00:00
|
|
|
|
2014-07-24 17:50:48 +00:00
|
|
|
if ! excluded newline; then
|
|
|
|
if [ "$(tail -c 1 $i)" != "" ]; then
|
|
|
|
error "No newline at end of file: $i"
|
|
|
|
fi
|
2014-05-02 16:15:42 +00:00
|
|
|
fi
|
2014-04-18 08:37:06 +00:00
|
|
|
done
|
2015-04-02 04:21:50 +00:00
|
|
|
|
2016-04-15 15:38:32 +00:00
|
|
|
if ! excluded mddocs; then
|
2016-09-09 03:11:52 +00:00
|
|
|
md_docs=$(find $ELEMENTS_DIR -name '*.md')
|
2016-04-15 15:38:32 +00:00
|
|
|
if [ -n "$md_docs" ]; then
|
|
|
|
error ".md docs found: $md_docs"
|
|
|
|
fi
|
2015-04-02 04:21:50 +00:00
|
|
|
fi
|
2015-09-15 10:48:23 +00:00
|
|
|
|
2016-04-22 01:20:57 +00:00
|
|
|
echo "Checking YAML parsing..."
|
2016-09-09 03:11:52 +00:00
|
|
|
for i in $(find $ELEMENTS_DIR -type f -name '*.yaml'); do
|
2016-04-22 01:20:57 +00:00
|
|
|
echo "Parsing $i"
|
2015-09-15 10:48:23 +00:00
|
|
|
py_check="
|
|
|
|
import yaml
|
|
|
|
import sys
|
|
|
|
try:
|
|
|
|
objs = yaml.load(open('$i'))
|
|
|
|
except yaml.parser.ParserError:
|
2016-04-22 01:20:57 +00:00
|
|
|
sys.exit(1)
|
|
|
|
"
|
|
|
|
if ! python -c "$py_check"; then
|
|
|
|
error "$i is not a valid YAML file"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
echo "Checking pkg-map files..."
|
2016-09-09 03:11:52 +00:00
|
|
|
for i in $(find $ELEMENTS_DIR -type f \
|
2016-04-22 01:20:57 +00:00
|
|
|
-name 'pkg-map' -a \! -executable); do
|
|
|
|
echo "Parsing $i"
|
|
|
|
py_check="
|
|
|
|
import json
|
|
|
|
import sys
|
|
|
|
try:
|
|
|
|
objs = json.load(open('$i'))
|
|
|
|
except ValueError:
|
|
|
|
sys.exit(1)
|
|
|
|
"
|
2015-09-15 10:48:23 +00:00
|
|
|
if ! python -c "$py_check"; then
|
2016-04-22 01:20:57 +00:00
|
|
|
error "$i is not a valid JSON file"
|
2015-09-15 10:48:23 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2016-04-22 00:21:51 +00:00
|
|
|
if [[ $rc == 0 ]]; then
|
|
|
|
echo "PASS"
|
|
|
|
else
|
|
|
|
echo "*** FAIL: Some tests failed!"
|
|
|
|
fi
|
|
|
|
|
2014-03-20 02:06:31 +00:00
|
|
|
exit $rc
|