blob: 570f1c8568c81e32eb347d39ac65aa765d2e0efe [file] [log] [blame]
Nicolas De Loofd6064382014-10-04 08:17:28 +02001#! /bin/bash
2
Mike Dillon1f9d73c2015-04-06 17:17:40 -07003set -e
4
Vincent Latombebb8f8c92016-07-13 18:18:14 +02005# compare if version1 < version2
6versionLT() {
7 [ "$1" = "$2" ] && return 1 || [ "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ]
8}
9
10# returns a plugin version from a plugin archive
11get_plugin_version() {
12 local archive; archive=$1
13 local version; version=$(unzip -p $archive META-INF/MANIFEST.MF | grep "^Plugin-Version: " | sed -e 's#^Plugin-Version: ##')
14 version=${version%%[[:space:]]}
15 echo $version
16}
17
Dave Head-Rapson595a4682016-02-29 13:41:53 +000018# Copy files from /usr/share/jenkins/ref into $JENKINS_HOME
Bastiaan Bakker45b0f672016-01-11 17:37:53 +010019# So the initial JENKINS-HOME is set with expected content.
20# Don't override, as this is just a reference setup, and use from UI
Nicolas De Loofacb86492014-10-24 13:43:13 +020021# can then change this, upgrade plugins, etc.
22copy_reference_file() {
Kanstantsin Shautsou44096322015-12-11 02:21:20 +030023 f="${1%/}"
Bastiaan Bakker45b0f672016-01-11 17:37:53 +010024 b="${f%.override}"
Bastiaan Bakker45b0f672016-01-11 17:37:53 +010025 rel="${b:23}"
Vincent Latombebb8f8c92016-07-13 18:18:14 +020026 version_marker="${rel}.version_from_image"
Bastiaan Bakker45b0f672016-01-11 17:37:53 +010027 dir=$(dirname "${b}")
Vincent Latombebb8f8c92016-07-13 18:18:14 +020028 local action;
29 local reason;
30 local container_version;
31 local image_version;
32 local marker_version;
33 local log; log=false
34 if [[ ${rel} == plugins/*.jpi ]]; then
35 container_version=$(get_plugin_version $JENKINS_HOME/${rel})
36 image_version=$(get_plugin_version ${f})
37 if [[ -e $JENKINS_HOME/${version_marker} ]]; then
38 marker_version=$(cat $JENKINS_HOME/${version_marker})
39 if versionLT $marker_version $container_version; then
40 action="SKIPPED"
41 reason="Installed version ($container_version) has been manually upgraded from initial version ($marker_version)"
42 log=true
43 else
44 if [[ "$image_version" == "$container_version" ]]; then
45 action="SKIPPED"
46 reason="Version from image is the same as the installed version $image_version"
47 else
48 if versionLT $image_version $container_version; then
49 action="SKIPPED"
50 log=true
51 reason="Image version ($image_version) is older than installed version ($container_version)"
52 else
53 action="UPGRADED"
54 log=true
55 reason="Image version ($image_version) is newer than installed version ($container_version)"
56 fi
57 fi
58 fi
59 else
60 if [[ -n "$TRY_UPGRADE_IF_NO_MARKER" ]]; then
61 if [[ "$image_version" == "$container_version" ]]; then
62 action="SKIPPED"
63 reason="Version from image is the same as the installed version $image_version (no marker found)"
64 # Add marker for next time
65 echo $image_version > $JENKINS_HOME/${version_marker}
66 else
67 if versionLT $image_version $container_version; then
68 action="SKIPPED"
69 log=true
70 reason="Image version ($image_version) is older than installed version ($container_version) (no marker found)"
71 else
72 action="UPGRADED"
73 log=true
74 reason="Image version ($image_version) is newer than installed version ($container_version) (no marker found)"
75 fi
76 fi
77 fi
78 fi
79 if [[ ! -e $JENKINS_HOME/${rel} || "$action" == "UPGRADED" || $f = *.override ]]; then
80 action=${action:-"INSTALLED"}
81 log=true
82 mkdir -p "$JENKINS_HOME/${dir:23}"
83 cp -r "${f}" "$JENKINS_HOME/${rel}";
84 # pin plugins on initial copy
85 touch "$JENKINS_HOME/${rel}.pinned"
86 echo $image_version > $JENKINS_HOME/${version_marker}
87 reason=${reason:-$image_version}
88 else
89 action=${action:-"SKIPPED"}
90 fi
91 else
92 if [[ ! -e $JENKINS_HOME/${rel} || $f = *.override ]]
93 then
94 action="INSTALLED"
95 log=true
96 mkdir -p "$JENKINS_HOME/${dir:23}"
97 cp -r "${f}" "$JENKINS_HOME/${rel}";
98 else
99 action="SKIPPED"
100 fi
101 fi
102 if [[ -n "$VERBOSE" || "$log" == "true" ]]; then
103 if [ -z "$reason" ]; then
104 echo "$action $rel" >> "$COPY_REFERENCE_FILE_LOG"
105 else
106 echo "$action $rel : $reason" >> "$COPY_REFERENCE_FILE_LOG"
107 fi
108 fi
Nicolas De Loofacb86492014-10-24 13:43:13 +0200109}
Dave Head-Rapson595a4682016-02-29 13:41:53 +0000110: ${JENKINS_HOME:="/var/jenkins_home"}
Vincent Latombebb8f8c92016-07-13 18:18:14 +0200111export -f versionLT
112export -f get_plugin_version
Nicolas De Loofacb86492014-10-24 13:43:13 +0200113export -f copy_reference_file
Carlos Sanchezbd2f6c92016-01-14 09:15:47 +0100114touch "${COPY_REFERENCE_FILE_LOG}" || (echo "Can not write to ${COPY_REFERENCE_FILE_LOG}. Wrong volume permissions?" && exit 1)
Kanstantsin Shautsou44096322015-12-11 02:21:20 +0300115echo "--- Copying files at $(date)" >> "$COPY_REFERENCE_FILE_LOG"
Vincent Latombeaa2b3542015-08-04 14:10:24 +0200116find /usr/share/jenkins/ref/ -type f -exec bash -c "copy_reference_file '{}'" \;
Nicolas De Loofacb86492014-10-24 13:43:13 +0200117
Nicolas De Loof07c307d2014-10-07 14:02:42 +0200118# if `docker run` first argument start with `--` the user is passing jenkins launcher arguments
119if [[ $# -lt 1 ]] || [[ "$1" == "--"* ]]; then
Carlos Sancheze7d56fa2016-02-29 13:55:33 +0100120 eval "exec java $JAVA_OPTS -jar /usr/share/jenkins/jenkins.war $JENKINS_OPTS \"\$@\""
Nicolas De Loof07c307d2014-10-07 14:02:42 +0200121fi
122
123# As argument is not jenkins, assume user want to run his own process, for sample a `bash` shell to explore this image
Nicolas De loofb2b442e2014-10-08 08:05:02 +0200124exec "$@"