Skip to content

Instantly share code, notes, and snippets.

@psufoxman
Forked from chuxau/install_jenkins_plugin.sh
Last active May 4, 2017 23:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save psufoxman/89c9b8c23aeaaec005be058d6ad85ce6 to your computer and use it in GitHub Desktop.
Save psufoxman/89c9b8c23aeaaec005be058d6ad85ce6 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
UPDATES_URL="http://updates.jenkins-ci.org/download/plugins/"
if [ $# -lt 2 ]; then
echo "USAGE: $0 plugin-list-file destination-directory"
exit 1
fi
plugin_list=$1
plugin_dir=$2
#file_owner=jenkins.jenkins
mkdir -p $plugin_dir
containsElement () {
local e
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
return 1
}
installPlugin() {
if [[ -e ${plugin_dir}/${1}.hpi || -e ${plugin_dir}/${1}.jpi ]]; then
if [ "$2" == "1" ]; then
echo "Some sh*t"
return 1
fi
echo "Skipped: $1 (already installed)"
return 0
else
echo "Installing: $1-${2}"
curl -L --silent --output ${plugin_dir}/${1}-${2}.hpi ${UPDATES_URL}/${1}/${2}/${1}.hpi
return 0
fi
}
while IFS="|" read plugin version
do
#escape comments
if [[ $plugin =~ ^# ]]; then
echo "here"
continue
fi
#install the plugin
installPlugin $plugin $version
done < $plugin_list
changed=1
maxloops=100
pluginsProcesed=()
while [ "$changed" == "1" ]; do
if [ $maxloops -lt 1 ] ; then
echo "Max loop count reached - probably a bug in this script: $0"
exit 1
fi
((maxloops--))
changed=0
for f in ${plugin_dir}/*.hpi ; do
if [[ ! " ${pluginsProcesed[*]} " == *"${f}"* ]]; then
# get a list of only non-optional dependencies
echo "Checking dependencies for ${f}"
fileType=`file $f`
if [[ ! " $fileType " == *"archive"* ]]; then
echo "$f was not a proper archive file - check the file"
pluginsProcesed+=(${f})
continue
fi
hasDeps=0
manifest=`unzip -p ${f} META-INF/MANIFEST.MF`
if [[ -z $manifest ]]; then
echo "Didn't find a manifest in $f"
pluginsProcesed+=(${f})
continue
fi
pluginDeps=`unzip -p ${f} META-INF/MANIFEST.MF | grep -c 'Plugin-Dependencies' | tr -d '\r\n'`
if [ "$pluginDeps" == "0" ]; then
echo "Didn't find any dependencies for ${f}"
pluginsProcesed+=(${f})
continue
fi
if [[ ! -z $pluginDeps ]]; then
pluginDeps=`unzip -p ${f} META-INF/MANIFEST.MF | tr -d '\r' | sed -e ':a;N;$!ba;s/\n //g' | grep -e 'Plugin-Dependencies' | awk '{print $2}' | tr "," "\n"`
hasDeps=1
else
echo "Didn't find any dependencies for ${f}"
pluginsProcesed+=(${f})
continue
fi
#if deps were found, install them .. then set changed, so we re-loop all over all hpi's
if [ "$hasDeps" == "1" ]; then
tmpDeps=`echo $pluginDeps | tr -d '\r' | sed -e 's/ /,/g'`
echo "Found deps: $tmpDeps"
echo $pluginDeps | tr ' ' '\n' |
while IFS=: read plugin version; do
if [[ " $version " == *"optional"* ]]; then
version=`echo $version | awk -F ";" '{print $1}'`
fi
installPlugin $plugin $version
done
changed=1
else
echo "Didn't find any dependencies for ${f}"
pluginsProcesed+=(${f})
continue
fi
pluginsProcesed+=(${f})
fi
echo "Done installing dependencies for ${f}"
done
done
echo "all done"
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment