Skip to content

Commit c14af95

Browse files
Vlatombecarlossg
authored andcommitted
Make versionsLT support versions with qualifier (jenkinsci#307)
Extracted functions to jenkins-support; included in both jenkins.sh and install-plugin.sh. Added a separate test file to test versionLT function
1 parent f50f960 commit c14af95

File tree

5 files changed

+156
-117
lines changed

5 files changed

+156
-117
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ ENV COPY_REFERENCE_FILE_LOG $JENKINS_HOME/copy_reference_file.log
5858

5959
USER ${user}
6060

61+
COPY jenkins-support /usr/local/bin/jenkins-support
6162
COPY jenkins.sh /usr/local/bin/jenkins.sh
6263
ENTRYPOINT ["/bin/tini", "--", "/usr/local/bin/jenkins.sh"]
6364

install-plugins.sh

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
REF_DIR=${REF:-/usr/share/jenkins/ref/plugins}
99
FAILED="$REF_DIR/failed-plugins.txt"
1010

11+
. /usr/local/bin/jenkins-support
12+
1113
function getLockFile() {
1214
echo -n "$REF_DIR/${1}.lock"
1315
}
@@ -73,11 +75,6 @@ function checkIntegrity() {
7375
return $?
7476
}
7577

76-
# compare if version1 < version2
77-
versionLT() {
78-
[ "$1" = "$2" ] && return 1 || [ "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ]
79-
}
80-
8178
function resolveDependencies() {
8279
local plugin jpi dependencies
8380
plugin="$1"

jenkins-support

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#!/bin/bash -e
2+
3+
# compare if version1 < version2
4+
versionLT() {
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
26+
}
27+
28+
# returns a plugin version from a plugin archive
29+
get_plugin_version() {
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
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() {
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})
54+
image_version=$(get_plugin_version ${f})
55+
if [[ -e $JENKINS_HOME/${version_marker} ]]; then
56+
marker_version=$(cat $JENKINS_HOME/${version_marker})
57+
if versionLT $marker_version $container_version; then
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
66+
if versionLT $image_version $container_version; then
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
83+
echo $image_version > $JENKINS_HOME/${version_marker}
84+
else
85+
if versionLT $image_version $container_version; then
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
96+
fi
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}";
102+
# pin plugins on initial copy
103+
touch "$JENKINS_HOME/${rel}.pinned"
104+
echo $image_version > $JENKINS_HOME/${version_marker}
105+
reason=${reason:-$image_version}
106+
else
107+
action=${action:-"SKIPPED"}
108+
fi
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
119+
fi
120+
if [[ -n "$VERBOSE" || "$log" == "true" ]]; then
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
126+
fi
127+
}

jenkins.sh

Lines changed: 2 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,9 @@
1-
#! /bin/bash
1+
#! /bin/bash -e
22

3-
set -e
4-
5-
# compare if version1 < version2
6-
versionLT() {
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
11-
get_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-
18-
# Copy files from /usr/share/jenkins/ref into $JENKINS_HOME
19-
# 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
21-
# can then change this, upgrade plugins, etc.
22-
copy_reference_file() {
23-
f="${1%/}"
24-
b="${f%.override}"
25-
rel="${b:23}"
26-
version_marker="${rel}.version_from_image"
27-
dir=$(dirname "${b}")
28-
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
109-
}
1103
: ${JENKINS_HOME:="/var/jenkins_home"}
111-
export -f versionLT
112-
export -f get_plugin_version
113-
export -f copy_reference_file
1144
touch "${COPY_REFERENCE_FILE_LOG}" || (echo "Can not write to ${COPY_REFERENCE_FILE_LOG}. Wrong volume permissions?" && exit 1)
1155
echo "--- Copying files at $(date)" >> "$COPY_REFERENCE_FILE_LOG"
116-
find /usr/share/jenkins/ref/ -type f -exec bash -c "copy_reference_file '{}'" \;
6+
find /usr/share/jenkins/ref/ -type f -exec bash -c ". /usr/local/bin/jenkins-support; copy_reference_file '{}'" \;
1177

1188
# if `docker run` first argument start with `--` the user is passing jenkins launcher arguments
1199
if [[ $# -lt 1 ]] || [[ "$1" == "--"* ]]; then

tests/functions.bats

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env bats
2+
3+
load 'test_helper/bats-support/load'
4+
load 'test_helper/bats-assert/load'
5+
load test_helpers
6+
7+
. $BATS_TEST_DIRNAME/../jenkins-support
8+
9+
@test "versionLT" {
10+
run versionLT 1.0 1.0
11+
assert_failure
12+
run versionLT 1.0 1.1
13+
assert_success
14+
run versionLT 1.1 1.0
15+
assert_failure
16+
run versionLT 1.0-beta-1 1.0
17+
assert_success
18+
run versionLT 1.0 1.0-beta-1
19+
assert_failure
20+
run versionLT 1.0-alpha-1 1.0-beta-1
21+
assert_success
22+
run versionLT 1.0-beta-1 1.0-alpha-1
23+
assert_failure
24+
}

0 commit comments

Comments
 (0)