Carlos Sanchez | 7a54989 | 2016-08-15 10:25:03 +0200 | [diff] [blame] | 1 | #!/bin/bash -eu |
Vincent Latombe | c14af95 | 2016-07-18 10:20:12 +0200 | [diff] [blame] | 2 | |
| 3 | # compare if version1 < version2 |
| 4 | versionLT() { |
Carlos Sanchez | c0f6379 | 2016-08-15 10:30:16 +0200 | [diff] [blame] | 5 | local v1; v1=$(echo "$1" | cut -d '-' -f 1 ) |
| 6 | local q1; q1=$(echo "$1" | cut -s -d '-' -f 2- ) |
| 7 | local v2; v2=$(echo "$2" | cut -d '-' -f 1 ) |
| 8 | local q2; q2=$(echo "$2" | cut -s -d '-' -f 2- ) |
| 9 | if [ "$v1" = "$v2" ]; then |
| 10 | if [ "$q1" = "$q2" ]; then |
| 11 | return 1 |
| 12 | else |
| 13 | if [ -z "$q1" ]; then |
| 14 | return 1 |
| 15 | else |
| 16 | if [ -z "$q2" ]; then |
| 17 | return 0 |
| 18 | else |
| 19 | [ "$q1" = "$(echo -e "$q1\n$q2" | sort -V | head -n1)" ] |
| 20 | fi |
| 21 | fi |
| 22 | fi |
| 23 | else |
| 24 | [ "$v1" = "$(echo -e "$v1\n$v2" | sort -V | head -n1)" ] |
| 25 | fi |
Vincent Latombe | c14af95 | 2016-07-18 10:20:12 +0200 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | # returns a plugin version from a plugin archive |
| 29 | get_plugin_version() { |
Carlos Sanchez | c0f6379 | 2016-08-15 10:30:16 +0200 | [diff] [blame] | 30 | local archive; archive=$1 |
| 31 | local version; version=$(unzip -p "$archive" META-INF/MANIFEST.MF | grep "^Plugin-Version: " | sed -e 's#^Plugin-Version: ##') |
| 32 | version=${version%%[[:space:]]} |
| 33 | echo "$version" |
Vincent Latombe | c14af95 | 2016-07-18 10:20:12 +0200 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | # Copy files from /usr/share/jenkins/ref into $JENKINS_HOME |
| 37 | # So the initial JENKINS-HOME is set with expected content. |
| 38 | # Don't override, as this is just a reference setup, and use from UI |
| 39 | # can then change this, upgrade plugins, etc. |
| 40 | copy_reference_file() { |
Carlos Sanchez | c0f6379 | 2016-08-15 10:30:16 +0200 | [diff] [blame] | 41 | f="${1%/}" |
| 42 | b="${f%.override}" |
| 43 | rel="${b:23}" |
| 44 | version_marker="${rel}.version_from_image" |
| 45 | dir=$(dirname "${b}") |
| 46 | local action; |
| 47 | local reason; |
| 48 | local container_version; |
| 49 | local image_version; |
| 50 | local marker_version; |
| 51 | local log; log=false |
| 52 | if [[ ${rel} == plugins/*.jpi ]]; then |
| 53 | container_version=$(get_plugin_version "$JENKINS_HOME/${rel}") |
Carlos Sanchez | 7a54989 | 2016-08-15 10:25:03 +0200 | [diff] [blame] | 54 | image_version=$(get_plugin_version "${f}") |
Carlos Sanchez | c0f6379 | 2016-08-15 10:30:16 +0200 | [diff] [blame] | 55 | if [[ -e $JENKINS_HOME/${version_marker} ]]; then |
Carlos Sanchez | 7a54989 | 2016-08-15 10:25:03 +0200 | [diff] [blame] | 56 | marker_version=$(cat "$JENKINS_HOME/${version_marker}") |
| 57 | if versionLT "$marker_version" "$container_version"; then |
Vincent Latombe | c14af95 | 2016-07-18 10:20:12 +0200 | [diff] [blame] | 58 | action="SKIPPED" |
| 59 | reason="Installed version ($container_version) has been manually upgraded from initial version ($marker_version)" |
| 60 | log=true |
| 61 | else |
| 62 | if [[ "$image_version" == "$container_version" ]]; then |
| 63 | action="SKIPPED" |
| 64 | reason="Version from image is the same as the installed version $image_version" |
| 65 | else |
Carlos Sanchez | 7a54989 | 2016-08-15 10:25:03 +0200 | [diff] [blame] | 66 | if versionLT "$image_version" "$container_version"; then |
Vincent Latombe | c14af95 | 2016-07-18 10:20:12 +0200 | [diff] [blame] | 67 | action="SKIPPED" |
| 68 | log=true |
| 69 | reason="Image version ($image_version) is older than installed version ($container_version)" |
| 70 | else |
| 71 | action="UPGRADED" |
| 72 | log=true |
| 73 | reason="Image version ($image_version) is newer than installed version ($container_version)" |
| 74 | fi |
| 75 | fi |
| 76 | fi |
| 77 | else |
| 78 | if [[ -n "$TRY_UPGRADE_IF_NO_MARKER" ]]; then |
| 79 | if [[ "$image_version" == "$container_version" ]]; then |
| 80 | action="SKIPPED" |
| 81 | reason="Version from image is the same as the installed version $image_version (no marker found)" |
| 82 | # Add marker for next time |
Carlos Sanchez | 7a54989 | 2016-08-15 10:25:03 +0200 | [diff] [blame] | 83 | echo "$image_version" > "$JENKINS_HOME/${version_marker}" |
Vincent Latombe | c14af95 | 2016-07-18 10:20:12 +0200 | [diff] [blame] | 84 | else |
Carlos Sanchez | 7a54989 | 2016-08-15 10:25:03 +0200 | [diff] [blame] | 85 | if versionLT "$image_version" "$container_version"; then |
Vincent Latombe | c14af95 | 2016-07-18 10:20:12 +0200 | [diff] [blame] | 86 | action="SKIPPED" |
| 87 | log=true |
| 88 | reason="Image version ($image_version) is older than installed version ($container_version) (no marker found)" |
| 89 | else |
| 90 | action="UPGRADED" |
| 91 | log=true |
| 92 | reason="Image version ($image_version) is newer than installed version ($container_version) (no marker found)" |
| 93 | fi |
| 94 | fi |
| 95 | fi |
Carlos Sanchez | c0f6379 | 2016-08-15 10:30:16 +0200 | [diff] [blame] | 96 | fi |
Vincent Latombe | c14af95 | 2016-07-18 10:20:12 +0200 | [diff] [blame] | 97 | if [[ ! -e $JENKINS_HOME/${rel} || "$action" == "UPGRADED" || $f = *.override ]]; then |
| 98 | action=${action:-"INSTALLED"} |
| 99 | log=true |
| 100 | mkdir -p "$JENKINS_HOME/${dir:23}" |
| 101 | cp -r "${f}" "$JENKINS_HOME/${rel}"; |
Carlos Sanchez | c0f6379 | 2016-08-15 10:30:16 +0200 | [diff] [blame] | 102 | # pin plugins on initial copy |
| 103 | touch "$JENKINS_HOME/${rel}.pinned" |
Carlos Sanchez | 7a54989 | 2016-08-15 10:25:03 +0200 | [diff] [blame] | 104 | echo "$image_version" > "$JENKINS_HOME/${version_marker}" |
Vincent Latombe | c14af95 | 2016-07-18 10:20:12 +0200 | [diff] [blame] | 105 | reason=${reason:-$image_version} |
| 106 | else |
| 107 | action=${action:-"SKIPPED"} |
Carlos Sanchez | c0f6379 | 2016-08-15 10:30:16 +0200 | [diff] [blame] | 108 | fi |
Vincent Latombe | c14af95 | 2016-07-18 10:20:12 +0200 | [diff] [blame] | 109 | else |
| 110 | if [[ ! -e $JENKINS_HOME/${rel} || $f = *.override ]] |
| 111 | then |
| 112 | action="INSTALLED" |
| 113 | log=true |
| 114 | mkdir -p "$JENKINS_HOME/${dir:23}" |
| 115 | cp -r "${f}" "$JENKINS_HOME/${rel}"; |
| 116 | else |
| 117 | action="SKIPPED" |
| 118 | fi |
Carlos Sanchez | c0f6379 | 2016-08-15 10:30:16 +0200 | [diff] [blame] | 119 | fi |
| 120 | if [[ -n "$VERBOSE" || "$log" == "true" ]]; then |
Vincent Latombe | c14af95 | 2016-07-18 10:20:12 +0200 | [diff] [blame] | 121 | if [ -z "$reason" ]; then |
| 122 | echo "$action $rel" >> "$COPY_REFERENCE_FILE_LOG" |
| 123 | else |
| 124 | echo "$action $rel : $reason" >> "$COPY_REFERENCE_FILE_LOG" |
| 125 | fi |
Carlos Sanchez | c0f6379 | 2016-08-15 10:30:16 +0200 | [diff] [blame] | 126 | fi |
Vincent Latombe | c14af95 | 2016-07-18 10:20:12 +0200 | [diff] [blame] | 127 | } |