Skip to content

Instantly share code, notes, and snippets.

@endzyme
Last active March 21, 2016 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save endzyme/8cf3aa887a8fdad5a870 to your computer and use it in GitHub Desktop.
Save endzyme/8cf3aa887a8fdad5a870 to your computer and use it in GitHub Desktop.
A quick script to cycle through exsiting plugins in jenkins and install any new ones defined in the script.
#!/bin/bash
#
# Author: Nick Huanca
#
# Basic assumptions:
# - This script is run from cwd: /var/lib/jenkins/plugins
# - You have wget
#
# Known Issues:
# Any versions with "1.2-SNAPSHOT (private blah blah blah)" appended
# to the version will only take into account the 1.2-SNAPSHOT part.
# This is handled with `ver=` when getting plugin information, you
# could implement fancier regex or some magic here to account for this.
#
# We expect to be successful
exit_code=0
# If anything fails let's log it
trap 'log_and_exit "$BASH_COMMAND" $?' ERR
while getopts "vdh" opt; do
case $opt in
v)
DEBUG_ON=true
;;
d)
# Dry Run
DRY_RUN=true
;;
h)
echo "Usage $0 [-d<dryrun>] [-v<verbose>] [-h<help>]"
exit 0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
# Declare Arrays
declare -a plugin_list
declare -a custom_plugin_list
declare -a existing_plugins
declare -a plugins_to_install
# Add data to arrays
plugin_list=( "${plugin_list[@]}" "git;2.4.0" )
plugin_list=( "${plugin_list[@]}" "script-security;1.13" )
custom_plugin_list=( "${custom_plugin_list[@]}" "my-custom;1.1-SNAPSHOT;http://web-server.mine:8000/what/my/files/are.hpi" )
# Functions
function log_and_exit() {
error "Failed running command '$1', exited with code $2"
error "Will exit with error code 1"
exit_code=1
}
function info () {
echo -e "\e[32m[INFO] $@ \e[39m"
}
function warn () {
echo -e "\e[33m[WARN] $@ \e[39m"
}
function error () {
echo -e "\e[31m[ERROR] $@ \e[39m"
}
function debug () {
if [ ! -z "$DEBUG_ON" ]; then
echo -e "\e[36m[DEBUG] $@ \e[39m"
fi
}
function print_dot () {
if [ -z "$DEBUG_ON" ]; then
echo -ne "\e[32m.\e[39m"
fi
}
function load_jenkins_url (){
egrep -o "http.?://[a-zA-Z0-9\._-\/:]+" ../jenkins.model.JenkinsLocationConfiguration.xml
}
function run_action () {
if [ ! -z "$DRY_RUN" ]; then
warn "Would have $@"
else
debug "Running Action: $@"
$*
fi
}
function grab_existing_plugins () {
plugin_dirs=($(find . -maxdepth 1 -type d | awk -F/ '{print $2}'))
for d in ${plugin_dirs[@]}; do
while read line; do
if [[ $line =~ 'Plugin-Version' ]]; then
ver=$(echo $line | awk -F: '{$1=""; print $0}' | awk '{print $1}' | tr -d '[[:space:]]')
debug "Found plugin $d on ver:$ver installed"
existing_plugins=( "${existing_plugins[@]}" "$d;${ver// /_}" )
fi
done < <(tr -d '\r' < $d/META-INF/MANIFEST.MF)
done
# Remove any matching plugins from install lists
for delete in ${existing_plugins[@]}; do
if [[ "${plugin_list[@]}" =~ "$delete" ]] ; then
debug "Dropping $delete from plugin list array because it's already installed"
plugin_list=( "${plugin_list[@]/$delete}" )
elif [[ "${custom_plugin_list[@]}" =~ "$delete" ]]; then
debug "Dropping $delete from custom plugin list array because it's already installed"
custom_plugin_list=( "${custom_plugin_list[@]/$delete}" )
else
warn "Didn't find ${delete//;/ ver:} in the manual install list, you may want to add it sometime"
fi
done
}
function jenkins_plugin_download() {
download_output=$(run_action curl --fail -s -L -XGET $2 > $1.hpi)
if [ $? -eq 0 ]; then
debug "Download successful adding to installation list"
plugins_to_install=( "${plugins_to_install[@]}" "$1" )
else
error "Download Failed running curl command (curl --fail -s -L -XGET $2)"
run_action rm $1.hpi
exit_code=1
fi
}
function download_plugins () {
for item in $@; do
arrPluginVars=(${item//;/ })
plugin=${arrPluginVars[0]}
version=${arrPluginVars[1]}
src=${arrPluginVars[2]}
if [ -z "$src" ]; then
download_url="http://updates.jenkins-ci.org/download/plugins/${plugin}/${version}/${plugin}.hpi"
else
download_url=$src
fi
debug "Working On Plugin:$plugin // Version: $version // Src: $download_url"
if [ -z "$version" ]; then
# This is to catch all the custom plugin installations that only have the URL left from the delete
# Also we should never proceed if $version doesn't exist
debug "Skipping this array item as there's no version; most likely this was in the custom_plugin_list and the package is already installed: ITEM:(${item[@]})"
continue
else
debug "Running Installtion of $plugin ver:$version"
debug "Found version variable ('$version') was not empty"
debug "Removing ${plugin} directory"
run_action rm -rf ${plugin} ${plugin}.* /var/cache/jenkins/war/WEB-INF/plugins/${plugin}.hpi
debug "Installing Plugin ${plugin} via Jenkins CLI"
print_dot
jenkins_plugin_download ${plugin} ${download_url}
fi
done
}
function install_plugins () {
for item in $@; do
for n in 1 2 3 4 5 6 7 8; do
debug "Working on ${item} (try #$n)"
plugin=${item}
install_output=$(run_action /usr/bin/java -jar /var/cache/jenkins/war/WEB-INF/jenkins-cli.jar -s $jenkins_url install-plugin ${plugin}.hpi 2>&1)
info $install_output
if egrep -iq "error|exception" <(echo "$install_output"); then
if [ $n -eq 8 ]; then
error "Install Had an Error: \n ${install_output}"
exit_code=1
fi
else
print_dot && break
fi
done
done
}
debug "Loading local Jenkins URL"
jenkins_url=$(load_jenkins_url)
info "Found ($jenkins_url) as Jenkins URL"
info "Grabbing exsting plugins and versions"
grab_existing_plugins
info "Downloading Missing Plugins or Upgrading Versions"
download_plugins "${plugin_list[@]}"
download_plugins "${custom_plugin_list[@]}"
echo ""
info "Finished Downloads"
info "Installing Plugins"
debug "${plugins_to_install[@]}"
install_plugins "${plugins_to_install[@]}"
echo ""
info "Finished Installs"
if [ $exit_code -eq 0 ]; then
info "Terminated Run Successfully"
else
error "Run Failed - maybe run with -v for verbose debug to see more"
fi
exit $exit_code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment