blob: 9b08ddb98815a09c98a660262026e883b3bd2dbe [file] [log] [blame]
Nicolas De Loofacb86492014-10-24 13:43:13 +02001#! /bin/bash
2
3# Parse a support-core plugin -style txt file as specification for jenkins plugins to be installed
4# in the reference directory, so user can define a derived Docker image with just :
Nikolay Yurin390dc612015-04-20 14:46:29 +03005#
Nicolas De Loofacb86492014-10-24 13:43:13 +02006# FROM jenkins
7# COPY plugins.txt /plugins.txt
Justin Clayton8aec15c2015-01-23 12:19:02 -08008# RUN /usr/local/bin/plugins.sh /plugins.txt
Nikolay Yurin390dc612015-04-20 14:46:29 +03009#
Jim Zucker4e24cac2016-04-05 08:06:38 -070010# Note: Plugins already installed are skipped
11#
Nicolas De Loofacb86492014-10-24 13:43:13 +020012
Jesse Glick9c8ddc42015-06-01 11:47:18 -040013set -e
14
Carlos Sanchez0ab40702016-08-05 10:44:58 +020015echo "WARN: plugins.sh is deprecated, please switch to install-plugins.sh"
16
Jim Zucker4e24cac2016-04-05 08:06:38 -070017if [ -z "$1" ]
18then
Carlos Sanchez44deec32016-06-10 12:21:21 +020019 echo "
20USAGE:
Jim Zucker4e24cac2016-04-05 08:06:38 -070021 Parse a support-core plugin -style txt file as specification for jenkins plugins to be installed
22 in the reference directory, so user can define a derived Docker image with just :
Carlos Sanchez44deec32016-06-10 12:21:21 +020023
Jim Zucker4e24cac2016-04-05 08:06:38 -070024 FROM jenkins
25 COPY plugins.txt /plugins.txt
26 RUN /usr/local/bin/plugins.sh /plugins.txt
Carlos Sanchez44deec32016-06-10 12:21:21 +020027
Jim Zucker4e24cac2016-04-05 08:06:38 -070028 Note: Plugins already installed are skipped
Carlos Sanchez44deec32016-06-10 12:21:21 +020029
Jim Zucker4e24cac2016-04-05 08:06:38 -070030"
Carlos Sanchez44deec32016-06-10 12:21:21 +020031 exit 1
Jim Zucker4e24cac2016-04-05 08:06:38 -070032else
Carlos Sanchez44deec32016-06-10 12:21:21 +020033 JENKINS_INPUT_JOB_LIST=$1
Charles Duffy3eec42c2016-09-20 12:11:33 -050034 if [ ! -f "$JENKINS_INPUT_JOB_LIST" ]
Carlos Sanchez44deec32016-06-10 12:21:21 +020035 then
36 echo "ERROR File not found: $JENKINS_INPUT_JOB_LIST"
37 exit 1
38 fi
Jim Zucker4e24cac2016-04-05 08:06:38 -070039fi
40
Carlos Sanchez44deec32016-06-10 12:21:21 +020041# the war includes a # of plugins, to make the build efficient filter out
Jim Zucker4e24cac2016-04-05 08:06:38 -070042# the plugins so we dont install 2x - there about 17!
Charles Duffy3eec42c2016-09-20 12:11:33 -050043if [ -d "$JENKINS_HOME" ]
Jim Zucker4e24cac2016-04-05 08:06:38 -070044then
Carlos Sanchez44deec32016-06-10 12:21:21 +020045 TEMP_ALREADY_INSTALLED=$JENKINS_HOME/preinstalled.plugins.$$.txt
Jim Zucker4e24cac2016-04-05 08:06:38 -070046else
Carlos Sanchez44deec32016-06-10 12:21:21 +020047 echo "ERROR $JENKINS_HOME not found"
48 exit 1
Jim Zucker4e24cac2016-04-05 08:06:38 -070049fi
50
51JENKINS_PLUGINS_DIR=/var/jenkins_home/plugins
Charles Duffy3eec42c2016-09-20 12:11:33 -050052if [ -d "$JENKINS_PLUGINS_DIR" ]
Jim Zucker4e24cac2016-04-05 08:06:38 -070053then
Carlos Sanchez44deec32016-06-10 12:21:21 +020054 echo "Analyzing: $JENKINS_PLUGINS_DIR"
Charles Duffy3eec42c2016-09-20 12:11:33 -050055 for i in "$JENKINS_PLUGINS_DIR"/*/; do
56 JENKINS_PLUGIN=$(basename "$i")
57 JENKINS_PLUGIN_VER=$(egrep -i Plugin-Version "$i/META-INF/MANIFEST.MF"|cut -d: -f2|sed 's/ //')
Carlos Sanchez44deec32016-06-10 12:21:21 +020058 echo "$JENKINS_PLUGIN:$JENKINS_PLUGIN_VER"
Charles Duffy3eec42c2016-09-20 12:11:33 -050059 done >"$TEMP_ALREADY_INSTALLED"
Jim Zucker4e24cac2016-04-05 08:06:38 -070060else
Carlos Sanchez44deec32016-06-10 12:21:21 +020061 JENKINS_WAR=/usr/share/jenkins/jenkins.war
Charles Duffy3eec42c2016-09-20 12:11:33 -050062 if [ -f "$JENKINS_WAR" ]
Carlos Sanchez44deec32016-06-10 12:21:21 +020063 then
64 echo "Analyzing war: $JENKINS_WAR"
65 TEMP_PLUGIN_DIR=/tmp/plugintemp.$$
Charles Duffy3eec42c2016-09-20 12:11:33 -050066 while read -r i <&3; do
67 rm -fr "$TEMP_PLUGIN_DIR"
68 mkdir -p "$TEMP_PLUGIN_DIR"
69 PLUGIN=$(basename "$i"|cut -f1 -d'.')
70 (cd "$TEMP_PLUGIN_DIR" || exit; jar xf "$JENKINS_WAR" "$i"; jar xvf "$TEMP_PLUGIN_DIR/$i" META-INF/MANIFEST.MF >/dev/null 2>&1)
71 VER=$(egrep -i Plugin-Version "$TEMP_PLUGIN_DIR/META-INF/MANIFEST.MF"|cut -d: -f2|sed 's/ //')
Carlos Sanchez44deec32016-06-10 12:21:21 +020072 echo "$PLUGIN:$VER"
Charles Duffy3eec42c2016-09-20 12:11:33 -050073 done 3< <(jar tf "$JENKINS_WAR" | egrep '[^detached-]plugins.*\..pi' | sort) > "$TEMP_ALREADY_INSTALLED"
74 rm -fr "$TEMP_PLUGIN_DIR"
Carlos Sanchez44deec32016-06-10 12:21:21 +020075 else
Charles Duffy3eec42c2016-09-20 12:11:33 -050076 rm -f "$TEMP_ALREADY_INSTALLED"
Carlos Sanchez44deec32016-06-10 12:21:21 +020077 echo "ERROR file not found: $JENKINS_WAR"
78 exit 1
79 fi
Jim Zucker4e24cac2016-04-05 08:06:38 -070080fi
81
Nicolas De Loofacb86492014-10-24 13:43:13 +020082REF=/usr/share/jenkins/ref/plugins
83mkdir -p $REF
Jim Zucker4e24cac2016-04-05 08:06:38 -070084COUNT_PLUGINS_INSTALLED=0
Charles Duffy3eec42c2016-09-20 12:11:33 -050085while read -r spec || [ -n "$spec" ]; do
Jim Zucker4e24cac2016-04-05 08:06:38 -070086
Nikolay Yurin390dc612015-04-20 14:46:29 +030087 plugin=(${spec//:/ });
Manuel Prinzdddb9652015-02-21 13:03:29 +010088 [[ ${plugin[0]} =~ ^# ]] && continue
Charles Duffy3eec42c2016-09-20 12:11:33 -050089 [[ ${plugin[0]} =~ ^[[:space:]]*$ ]] && continue
Nikolay Yurin390dc612015-04-20 14:46:29 +030090 [[ -z ${plugin[1]} ]] && plugin[1]="latest"
Carlos Sanchezd359b042015-07-02 13:06:14 +020091
92 if [ -z "$JENKINS_UC_DOWNLOAD" ]; then
93 JENKINS_UC_DOWNLOAD=$JENKINS_UC/download
94 fi
Jim Zucker4e24cac2016-04-05 08:06:38 -070095
Charles Duffy3eec42c2016-09-20 12:11:33 -050096 if ! grep -q "${plugin[0]}:${plugin[1]}" "$TEMP_ALREADY_INSTALLED"
Jim Zucker4e24cac2016-04-05 08:06:38 -070097 then
Carlos Sanchez44deec32016-06-10 12:21:21 +020098 echo "Downloading ${plugin[0]}:${plugin[1]}"
Charles Duffy3eec42c2016-09-20 12:11:33 -050099 curl --retry 3 --retry-delay 5 -sSL -f "${JENKINS_UC_DOWNLOAD}/plugins/${plugin[0]}/${plugin[1]}/${plugin[0]}.hpi" -o "$REF/${plugin[0]}.jpi"
100 unzip -qqt "$REF/${plugin[0]}.jpi"
101 (( COUNT_PLUGINS_INSTALLED += 1 ))
Jim Zucker4e24cac2016-04-05 08:06:38 -0700102 else
Carlos Sanchez44deec32016-06-10 12:21:21 +0200103 echo " ... skipping already installed: ${plugin[0]}:${plugin[1]}"
Jim Zucker4e24cac2016-04-05 08:06:38 -0700104 fi
Charles Duffy3eec42c2016-09-20 12:11:33 -0500105done < "$JENKINS_INPUT_JOB_LIST"
Jim Zucker4e24cac2016-04-05 08:06:38 -0700106
107echo "---------------------------------------------------"
Charles Duffy3eec42c2016-09-20 12:11:33 -0500108if (( "$COUNT_PLUGINS_INSTALLED" > 0 ))
Jim Zucker4e24cac2016-04-05 08:06:38 -0700109then
Carlos Sanchez44deec32016-06-10 12:21:21 +0200110 echo "INFO: Successfully installed $COUNT_PLUGINS_INSTALLED plugins."
Jim Zucker4e24cac2016-04-05 08:06:38 -0700111
Carlos Sanchez44deec32016-06-10 12:21:21 +0200112 if [ -d $JENKINS_PLUGINS_DIR ]
113 then
114 echo "INFO: Please restart the container for changes to take effect!"
115 fi
Jim Zucker4e24cac2016-04-05 08:06:38 -0700116else
Carlos Sanchez44deec32016-06-10 12:21:21 +0200117 echo "INFO: No changes, all plugins previously installed."
Jim Zucker4e24cac2016-04-05 08:06:38 -0700118
119fi
120echo "---------------------------------------------------"
121
122#cleanup
Charles Duffy3eec42c2016-09-20 12:11:33 -0500123rm "$TEMP_ALREADY_INSTALLED"
Jim Zucker4e24cac2016-04-05 08:06:38 -0700124exit 0