Merge pull request #274 from cbuckleytbi/master

adding retry to plugins.sh for curl
diff --git a/Dockerfile b/Dockerfile
index 4ab9728..3617254 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -62,3 +62,4 @@
 
 # from a derived Dockerfile, can use `RUN plugins.sh active.txt` to setup /usr/share/jenkins/ref/plugins from a support bundle
 COPY plugins.sh /usr/local/bin/plugins.sh
+COPY install-plugins.sh /usr/local/bin/install-plugins.sh
diff --git a/README.md b/README.md
index 99f8027..95cdef3 100644
--- a/README.md
+++ b/README.md
@@ -156,6 +156,13 @@
 RUN /usr/local/bin/plugins.sh /usr/share/jenkins/ref/plugins.txt
 ```
 
+As an alternative, you can rely on the `install-plugins.sh` script to pass a set of plugins to download with their dependencies. Use plugin artifact ID, whithout `-plugin` extension.
+
+```
+FROM jenkins
+RUN install-plugins.sh docker-slaves github-branch-source 
+```
+
 When jenkins container starts, it will check JENKINS_HOME has this reference content, and copy them
 there if required. It will not override such files, so if you upgraded some plugins from UI they won't
 be reverted on next start.
diff --git a/install-plugins.sh b/install-plugins.sh
new file mode 100755
index 0000000..630b8cd
--- /dev/null
+++ b/install-plugins.sh
@@ -0,0 +1,84 @@
+#! /bin/bash
+
+# Resolve dependencies and download plugins given on the command line
+#
+# FROM jenkins
+# RUN install-plugins.sh docker-slaves github-branch-source
+
+set -e
+
+REF=${REF:-/usr/share/jenkins/ref/plugins}
+mkdir -p "$REF"
+
+function download() {
+	local plugin="$1"; shift
+
+	if [[ ! -f "${plugin}.hpi" ]]; then
+
+		local url="${JENKINS_UC}/latest/${plugin}.hpi"
+		echo "download plugin : $plugin from $url"
+
+		if ! curl -s -f -L "$url" -o "${plugin}.hpi" 
+		then
+			# some plugin don't follow the rules about artifact ID
+			# typically: docker-plugin
+			plugin=${plugin}-plugin
+
+			local url="${JENKINS_UC}/latest/${plugin}.hpi"
+			echo "download plugin : $plugin from $url"
+			if ! curl -s -f -L "${url}" -o "${plugin}.hpi"
+			then
+				>&2 echo "failed to download plugin ${plugin}"
+				exit -1
+			fi
+		fi
+	else
+		echo "$plugin is already downloaded."
+	fi	
+
+	if [[ ! -f ${plugin}.resolved ]]; then
+		resolveDependencies "$plugin"
+	fi
+}
+
+function resolveDependencies() {	
+	local plugin="$1"; shift
+
+	local dependencies=`jrunscript -e '\
+	java.lang.System.out.println(\
+		new java.util.jar.JarFile("'${plugin}.hpi'")\
+			.getManifest()\
+			.getMainAttributes()\
+			.getValue("Plugin-Dependencies")\
+	);'`
+
+	if [[ "$dependencies" == "null" ]]; then
+		echo " > plugin has no dependencies"
+		return
+	fi
+
+	echo " > depends on  ${dependencies}"
+
+	IFS=',' read -a array <<< "${dependencies}"
+    for d in "${array[@]}"
+	do
+		local p=$(echo $d | cut -d':' -f1 -)
+		if [[ $d == *"resolution:=optional"* ]] 
+		then	
+			echo "skipping optional dependency $p"
+		else
+			download "$p"
+		fi
+	done
+	touch "${plugin}.resolved"
+}
+
+cd "$REF"
+
+for plugin in "$@"
+do
+    download "$plugin"
+done
+
+# cleanup 'resolved' flag files
+rm *.resolved
diff --git a/weekly.sh b/weekly.sh
new file mode 100755
index 0000000..e615272
--- /dev/null
+++ b/weekly.sh
@@ -0,0 +1,21 @@
+#!/bin/bash
+
+set -e
+set -x
+
+JENKINS_VERSION=`curl -q https://api.github.com/repos/jenkinsci/jenkins/tags | grep '"name":' | grep -o '[0-9]\.[0-9]*'  | uniq | sort | tail -1`
+echo $JENKINS_VERSION
+
+JENKINS_SHA=`curl http://repo.jenkins-ci.org/simple/releases/org/jenkins-ci/main/jenkins-war/${JENKINS_VERSION}/jenkins-war-${JENKINS_VERSION}.war.sha1`
+echo $JENKINS_SHA
+
+docker build --build-arg JENKINS_VERSION=$JENKINS_VERSION \
+             --build-arg JENKINS_SHA=$JENKINS_SHA \
+             --tag jenkinsci/jenkins:$JENKINS_VERSION .
+
+docker tag -f jenkinsci/jenkins:$JENKINS_VERSION jenkinsci/jenkins:latest
+
+docker push jenkinsci/jenkins:$JENKINS_VERSION
+docker push jenkinsci/jenkins:latest
+
+