Full best-practices / shellcheck-compliance pass
- Use modern `$()` syntax vs backticks [see [SC2006](https://github.com/koalaman/shellcheck/wiki/SC2006)]
- Always check for, and bail on, failures of `cd`. [see [SC2164](https://github.com/koalaman/shellcheck/wiki/SC2164)]
- Avoid `for foo in $(...)`, per [DontReadLinesWithFor](http://mywiki.wooledge.org/DontReadLinesWithFor)
- Avoid parsing `ls` to find subdirectories when `for d in "$dir"/*/` does the job; see also [ParsingLs](http://mywiki.wooledge.org/ParsingLs)
- `\s` is not specified in POSIX ERE (thus, not guaranteed to be available in bash-native regexes unless local platform's C library's regex implementation extends standard); use `[[:space:]]` instead.
- Avoid unnecessary deviations from POSIX sh specification:
- `echo -e` violates (not just extends) POSIX, doesn't work as expected in bash compiled with `--enable-xpg-echo-default`; [POSIX specification](http://pubs.opengroup.org/onlinepubs/009604599/utilities/echo.html) suggests `printf` as replacement (see APPLICATION USAGE section).
- `function` keyword not specified in POSIX, provides no benefit over standard-compliant syntax
diff --git a/plugins.sh b/plugins.sh
index 027c382..9b08ddb 100755
--- a/plugins.sh
+++ b/plugins.sh
@@ -31,7 +31,7 @@
exit 1
else
JENKINS_INPUT_JOB_LIST=$1
- if [ ! -f $JENKINS_INPUT_JOB_LIST ]
+ if [ ! -f "$JENKINS_INPUT_JOB_LIST" ]
then
echo "ERROR File not found: $JENKINS_INPUT_JOB_LIST"
exit 1
@@ -40,7 +40,7 @@
# the war includes a # of plugins, to make the build efficient filter out
# the plugins so we dont install 2x - there about 17!
-if [ -d $JENKINS_HOME ]
+if [ -d "$JENKINS_HOME" ]
then
TEMP_ALREADY_INSTALLED=$JENKINS_HOME/preinstalled.plugins.$$.txt
else
@@ -49,33 +49,31 @@
fi
JENKINS_PLUGINS_DIR=/var/jenkins_home/plugins
-if [ -d $JENKINS_PLUGINS_DIR ]
+if [ -d "$JENKINS_PLUGINS_DIR" ]
then
echo "Analyzing: $JENKINS_PLUGINS_DIR"
- for i in `ls -pd1 $JENKINS_PLUGINS_DIR/*|egrep '\/$'`
- do
- JENKINS_PLUGIN=`basename $i`
- JENKINS_PLUGIN_VER=`egrep -i Plugin-Version "$i/META-INF/MANIFEST.MF"|cut -d\: -f2|sed 's/ //'`
+ for i in "$JENKINS_PLUGINS_DIR"/*/; do
+ JENKINS_PLUGIN=$(basename "$i")
+ JENKINS_PLUGIN_VER=$(egrep -i Plugin-Version "$i/META-INF/MANIFEST.MF"|cut -d: -f2|sed 's/ //')
echo "$JENKINS_PLUGIN:$JENKINS_PLUGIN_VER"
- done > $TEMP_ALREADY_INSTALLED
+ done >"$TEMP_ALREADY_INSTALLED"
else
JENKINS_WAR=/usr/share/jenkins/jenkins.war
- if [ -f $JENKINS_WAR ]
+ if [ -f "$JENKINS_WAR" ]
then
echo "Analyzing war: $JENKINS_WAR"
TEMP_PLUGIN_DIR=/tmp/plugintemp.$$
- for i in `jar tf $JENKINS_WAR | egrep '[^detached-]plugins.*\..pi' | sort`
- do
- rm -fr $TEMP_PLUGIN_DIR
- mkdir -p $TEMP_PLUGIN_DIR
- PLUGIN=`basename $i|cut -f1 -d'.'`
- (cd $TEMP_PLUGIN_DIR;jar xf $JENKINS_WAR "$i";jar xvf $TEMP_PLUGIN_DIR/$i META-INF/MANIFEST.MF >/dev/null 2>&1)
- VER=`egrep -i Plugin-Version "$TEMP_PLUGIN_DIR/META-INF/MANIFEST.MF"|cut -d\: -f2|sed 's/ //'`
+ while read -r i <&3; do
+ rm -fr "$TEMP_PLUGIN_DIR"
+ mkdir -p "$TEMP_PLUGIN_DIR"
+ PLUGIN=$(basename "$i"|cut -f1 -d'.')
+ (cd "$TEMP_PLUGIN_DIR" || exit; jar xf "$JENKINS_WAR" "$i"; jar xvf "$TEMP_PLUGIN_DIR/$i" META-INF/MANIFEST.MF >/dev/null 2>&1)
+ VER=$(egrep -i Plugin-Version "$TEMP_PLUGIN_DIR/META-INF/MANIFEST.MF"|cut -d: -f2|sed 's/ //')
echo "$PLUGIN:$VER"
- done > $TEMP_ALREADY_INSTALLED
- rm -fr $TEMP_PLUGIN_DIR
+ done 3< <(jar tf "$JENKINS_WAR" | egrep '[^detached-]plugins.*\..pi' | sort) > "$TEMP_ALREADY_INSTALLED"
+ rm -fr "$TEMP_PLUGIN_DIR"
else
- rm -f $TEMP_ALREADY_INSTALLED
+ rm -f "$TEMP_ALREADY_INSTALLED"
echo "ERROR file not found: $JENKINS_WAR"
exit 1
fi
@@ -84,30 +82,30 @@
REF=/usr/share/jenkins/ref/plugins
mkdir -p $REF
COUNT_PLUGINS_INSTALLED=0
-while read spec || [ -n "$spec" ]; do
+while read -r spec || [ -n "$spec" ]; do
plugin=(${spec//:/ });
[[ ${plugin[0]} =~ ^# ]] && continue
- [[ ${plugin[0]} =~ ^\s*$ ]] && continue
+ [[ ${plugin[0]} =~ ^[[:space:]]*$ ]] && continue
[[ -z ${plugin[1]} ]] && plugin[1]="latest"
if [ -z "$JENKINS_UC_DOWNLOAD" ]; then
JENKINS_UC_DOWNLOAD=$JENKINS_UC/download
fi
- if ! grep -q "${plugin[0]}:${plugin[1]}" $TEMP_ALREADY_INSTALLED
+ if ! grep -q "${plugin[0]}:${plugin[1]}" "$TEMP_ALREADY_INSTALLED"
then
echo "Downloading ${plugin[0]}:${plugin[1]}"
- curl --retry 3 --retry-delay 5 -sSL -f ${JENKINS_UC_DOWNLOAD}/plugins/${plugin[0]}/${plugin[1]}/${plugin[0]}.hpi -o $REF/${plugin[0]}.jpi
- unzip -qqt $REF/${plugin[0]}.jpi
- COUNT_PLUGINS_INSTALLED=`expr $COUNT_PLUGINS_INSTALLED + 1`
+ curl --retry 3 --retry-delay 5 -sSL -f "${JENKINS_UC_DOWNLOAD}/plugins/${plugin[0]}/${plugin[1]}/${plugin[0]}.hpi" -o "$REF/${plugin[0]}.jpi"
+ unzip -qqt "$REF/${plugin[0]}.jpi"
+ (( COUNT_PLUGINS_INSTALLED += 1 ))
else
echo " ... skipping already installed: ${plugin[0]}:${plugin[1]}"
fi
-done < $JENKINS_INPUT_JOB_LIST
+done < "$JENKINS_INPUT_JOB_LIST"
echo "---------------------------------------------------"
-if [ $COUNT_PLUGINS_INSTALLED -gt 0 ]
+if (( "$COUNT_PLUGINS_INSTALLED" > 0 ))
then
echo "INFO: Successfully installed $COUNT_PLUGINS_INSTALLED plugins."
@@ -122,5 +120,5 @@
echo "---------------------------------------------------"
#cleanup
-rm $TEMP_ALREADY_INSTALLED
+rm "$TEMP_ALREADY_INSTALLED"
exit 0