-
-
Save vitaminmoo/d7ffe123a0adb9c6ee9f30f51e9141f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
TMP=$(mktemp -d) | |
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 | |
} | |
latestVersion() { | |
cat "$TMP/$1" 2>/dev/null || | |
curl -s https://updates.jenkins.io/download/plugins/${1}/ | | |
grep /download/ | head -n1 |sed "s/.*href='//;s/'>.*//;s#.*${1}/##;s#/.*##" | | |
tee "$TMP/$1" | |
} | |
installPlugin() { | |
if [ "$2" == "latest" ]; then | |
version=$(latestVersion "$1") | |
else | |
version=$2 | |
fi | |
if [[ -e ${plugin_dir}/${1}.hpi || -e ${plugin_dir}/${1}.jpi || -e ${plugin_dir}/${1}-${version}.hpi ]]; then | |
if [ "$2" == "1" ]; then | |
echo "Some sh*t" | |
return 1 | |
fi | |
echo "Skipped: $1 (already installed)" | |
return 0 | |
else | |
echo "Installing: $1-${version}" | |
curl -L --silent --output ${plugin_dir}/${1}-${version}.hpi ${UPDATES_URL}/${1}/${version}/${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 latest | |
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