blob: 726ed86d454857d380978a117d904e56e8f9fb44 [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
Jim Zucker4e24cac2016-04-05 08:06:38 -070015if [ -z "$1" ]
16then
Carlos Sanchez44deec32016-06-10 12:21:21 +020017 echo "
18USAGE:
Jim Zucker4e24cac2016-04-05 08:06:38 -070019 Parse a support-core plugin -style txt file as specification for jenkins plugins to be installed
20 in the reference directory, so user can define a derived Docker image with just :
Carlos Sanchez44deec32016-06-10 12:21:21 +020021
Jim Zucker4e24cac2016-04-05 08:06:38 -070022 FROM jenkins
23 COPY plugins.txt /plugins.txt
24 RUN /usr/local/bin/plugins.sh /plugins.txt
Carlos Sanchez44deec32016-06-10 12:21:21 +020025
Jim Zucker4e24cac2016-04-05 08:06:38 -070026 Note: Plugins already installed are skipped
Carlos Sanchez44deec32016-06-10 12:21:21 +020027
Jim Zucker4e24cac2016-04-05 08:06:38 -070028"
Carlos Sanchez44deec32016-06-10 12:21:21 +020029 exit 1
Jim Zucker4e24cac2016-04-05 08:06:38 -070030else
Carlos Sanchez44deec32016-06-10 12:21:21 +020031 JENKINS_INPUT_JOB_LIST=$1
32 if [ ! -f $JENKINS_INPUT_JOB_LIST ]
33 then
34 echo "ERROR File not found: $JENKINS_INPUT_JOB_LIST"
35 exit 1
36 fi
Jim Zucker4e24cac2016-04-05 08:06:38 -070037fi
38
Carlos Sanchez44deec32016-06-10 12:21:21 +020039# the war includes a # of plugins, to make the build efficient filter out
Jim Zucker4e24cac2016-04-05 08:06:38 -070040# the plugins so we dont install 2x - there about 17!
41if [ -d $JENKINS_HOME ]
42then
Carlos Sanchez44deec32016-06-10 12:21:21 +020043 TEMP_ALREADY_INSTALLED=$JENKINS_HOME/preinstalled.plugins.$$.txt
Jim Zucker4e24cac2016-04-05 08:06:38 -070044else
Carlos Sanchez44deec32016-06-10 12:21:21 +020045 echo "ERROR $JENKINS_HOME not found"
46 exit 1
Jim Zucker4e24cac2016-04-05 08:06:38 -070047fi
48
49JENKINS_PLUGINS_DIR=/var/jenkins_home/plugins
50if [ -d $JENKINS_PLUGINS_DIR ]
51then
Carlos Sanchez44deec32016-06-10 12:21:21 +020052 echo "Analyzing: $JENKINS_PLUGINS_DIR"
53 for i in `ls -pd1 $JENKINS_PLUGINS_DIR/*|egrep '\/$'`
54 do
55 JENKINS_PLUGIN=`basename $i`
56 JENKINS_PLUGIN_VER=`egrep -i Plugin-Version "$i/META-INF/MANIFEST.MF"|cut -d\: -f2|sed 's/ //'`
57 echo "$JENKINS_PLUGIN:$JENKINS_PLUGIN_VER"
58 done > $TEMP_ALREADY_INSTALLED
Jim Zucker4e24cac2016-04-05 08:06:38 -070059else
Carlos Sanchez44deec32016-06-10 12:21:21 +020060 JENKINS_WAR=/usr/share/jenkins/jenkins.war
61 if [ -f $JENKINS_WAR ]
62 then
63 echo "Analyzing war: $JENKINS_WAR"
64 TEMP_PLUGIN_DIR=/tmp/plugintemp.$$
65 for i in `jar tf $JENKINS_WAR|egrep 'plugins'|egrep -v '\/$'|sort`
66 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;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/ //'`
72 echo "$PLUGIN:$VER"
73 done > $TEMP_ALREADY_INSTALLED
74 rm -fr $TEMP_PLUGIN_DIR
75 else
76 rm -f $TEMP_ALREADY_INSTALLED
77 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
Carlos Sanchezc0475fd2015-04-29 11:55:48 +020085while read 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
89 [[ ${plugin[0]} =~ ^\s*$ ]] && 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
Carlos Sanchez44deec32016-06-10 12:21:21 +020096 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]}"
Jim Zucker4e24cac2016-04-05 08:06:38 -070099 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
Carlos Sanchez44deec32016-06-10 12:21:21 +0200101 COUNT_PLUGINS_INSTALLED=`expr $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
105done < $JENKINS_INPUT_JOB_LIST
106
107echo "---------------------------------------------------"
108if [ $COUNT_PLUGINS_INSTALLED -gt 0 ]
109then
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
123rm $TEMP_ALREADY_INSTALLED
124exit 0