-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-plugins.sh
executable file
·218 lines (189 loc) · 6.14 KB
/
install-plugins.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/bin/bash -eux
set -o pipefail
export REF_DIR=${JENKINS_PLUGIN_REF:-/var/lib/jenkins/plugin-ref/plugins}
FAILED="$REF_DIR/failed-plugins.txt"
DEPS="$REF_DIR/deps-plugins.txt"
JENKINS_UC="https://updates.jenkins.io"
CMSCOPY="http://muzaffar.web.cern.ch/muzaffar/jenkins-plugins/"
PLUGIN_LIST=""
JENKINS_WAR=$(ps -awx | grep ' \-jar /' | grep '/jenkins.war' | sed 's|.*-jar /|/|;s| .*||' | tail -1)
export JENKINS_WAR
let PARALLEL_JOBS=$(getconf _NPROCESSORS_ONLN)*2
. $(dirname $0)/jenkins-support
getLockFile() {
printf '%s' "$REF_DIR/lock/${1}"
}
getArchiveFilename() {
printf '%s' "$REF_DIR/${1}.jpi"
}
download() {
local plugin version jpi url
plugin="$1"
version="${2:-latest}"
jpi="$(getArchiveFilename "$plugin")"
if ! doDownload "$plugin" "$version"; then
if ! doDownload "$plugin-plugin" "$version"; then
url="${CMSCOPY}/${plugin}.jpi"
if ! downloadURL "$plugin" "$jpi" "$url" ; then
echo "Failed to download plugin: $plugin or ${plugin}-plugin" >&2
echo "Not downloaded: ${plugin}" >> "$FAILED"
rm -f "$jpi"
return 1
fi
fi
fi
if ! checkIntegrity "$plugin"; then
echo "Downloaded file is not a valid ZIP: $(getArchiveFilename "$plugin")" >&2
echo "Download integrity: ${plugin}" >> "$FAILED"
rm -f "$jpi"
return 1
fi
if [ "$version" = "latest" ] ; then touch "$jpi.latest" ; fi
resolveDependencies "$plugin"
}
getPluginVersion() {
local jpi
jpi="$(getArchiveFilename "$1")"
if test -f "$jpi" ; then
echo $(unzip -p "$jpi" META-INF/MANIFEST.MF | tr -d '\r' | grep "^Plugin-Version:" | sed 's|^Plugin-Version: *||')
else
echo ""
fi
}
hasNewerVersion() {
local xv cv
xv=$(echo $2 | sed -e s'|-.*$||')
cv="$(getPluginVersion "$1")"
if [ "$(echo -e "$xv\n$cv" | sort -V | tail -1)" = "$cv" ] ; then
return 0
fi
return 1
}
doDownload() {
local plugin version url jpi cversion
plugin="$1"
version="$2"
if [ "$version" != "latest" ] ; then
if hasNewerVersion "$plugin" "$version" ;then echo "Installed: $plugin $version"; return 0; fi
fi
jpi="$(getArchiveFilename "$plugin")"
rm -f "$jpi"
if [[ "$version" == "latest" && -n "$JENKINS_UC_LATEST" ]]; then
# If version-specific Update Center is available, which is the case for LTS versions,
# use it to resolve latest versions.
url="$JENKINS_UC_LATEST/latest/${plugin}.hpi"
elif [[ "$version" == "experimental" && -n "$JENKINS_UC_EXPERIMENTAL" ]]; then
# Download from the experimental update center
url="$JENKINS_UC_EXPERIMENTAL/latest/${plugin}.hpi"
else
JENKINS_UC_DOWNLOAD=${JENKINS_UC_DOWNLOAD:-"$JENKINS_UC/download"}
url="$JENKINS_UC_DOWNLOAD/plugins/$plugin/$version/${plugin}.hpi"
fi
if ! downloadURL "$plugin" "$jpi" "$url" ; then return 1 ; fi
return 0
}
downloadURL() {
echo "Downloading plugin: $1 from $3"
curl --connect-timeout "${CURL_CONNECTION_TIMEOUT:-20}" --retry "${CURL_RETRY:-5}" --retry-delay "${CURL_RETRY_DELAY:-0}" --retry-max-time "${CURL_RETRY_MAX_TIME:-60}" -s -f -L "$3" -o "$2"
return $?
}
checkIntegrity() {
local jpi
jpi="$(getArchiveFilename "$1")"
unzip -t -qq "$jpi" >/dev/null
return $?
}
resolveDependencies() {
local plugin jpi dependencies cv lock
plugin="$1"
jpi="$(getArchiveFilename "$plugin")"
dependencies="$(unzip -p "$jpi" META-INF/MANIFEST.MF | tr -d '\r' | tr '\n' '|' | sed -e 's#| ##g' | tr '|' '\n' | grep "^Plugin-Dependencies: " | sed -e 's#^Plugin-Dependencies: ##')"
if [[ ! $dependencies ]]; then return ; fi
IFS=',' read -r -a array <<< "$dependencies"
for d in "${array[@]}" ; do
if [[ $d == *"resolution:=optional"* ]]; then
continue
else
plugin="$(cut -d':' -f1 - <<< "$d")"
cv="$(cut -d':' -f2 - <<< "$d")"
lock="$(getLockFile "$plugin")"
if [ ! -d "$lock" ] ; then
echo "$plugin:$cv" >> $DEPS
fi
fi
done
}
versionFromPlugin() {
local plugin=$1
if [[ $plugin =~ .*:.* ]]; then
echo "${plugin##*:}"
else
echo "latest"
fi
}
installedPlugins() {
for f in "$REF_DIR"/*.jpi; do
echo "$(basename "$f" | sed -e 's/\.jpi//'):$(get_plugin_version "$f")"
done
}
main() {
local plugin pluginVersion jenkinsVersion
local plugins=()
mkdir -p "$REF_DIR" || exit 1
rm -f $FAILED
rm -f $DEPS
touch $DEPS
plugins=$(cat $1)
echo "Analyzing war..."
bundledPlugins="$(bundledPlugins)"
# Check if there's a version-specific update center, which is the case for LTS versions
jenkinsVersion="$(jenkinsMajorMinorVersion)"
if curl -fsL -o /dev/null "$JENKINS_UC/$jenkinsVersion"; then
JENKINS_UC_LATEST="$JENKINS_UC/$jenkinsVersion"
echo "Using version-specific update center: $JENKINS_UC_LATEST..."
else
JENKINS_UC_LATEST=
fi
for plugin in $plugins; do
mkdir "$(getLockFile "${plugin%%:*}")"
done
echo "Downloading plugins..."
for plugin in $plugins; do
while [ $(jobs -p | wc -l) -ge ${PARALLEL_JOBS} ] ; do sleep 1; done
pluginVersion=""
if [[ $plugin =~ .*:.* ]]; then
pluginVersion=$(versionFromPlugin "${plugin}")
plugin="${plugin%%:*}"
fi
download "$plugin" "$pluginVersion" &
done
wait
echo
echo "WAR bundled plugins:"
echo "${bundledPlugins}"
if [[ -f $FAILED ]]; then
echo "Some plugins failed to download!" "$(<"$FAILED")" >&2
rm -rf "$REF_DIR/*.lock"
exit 1
fi
}
rm -rf "$REF_DIR/lock"
mkdir -p "$REF_DIR/lock"
PLUGIN_LIST=$1
main "$2"
while [ -s $DEPS ] ; do
rm -f ${DEPS}.uniq
touch ${DEPS}.uniq
for p in $(cat $DEPS | sed -e 's|:.*$||' | sort -u) ; do
list_ver=$(grep "^$p:" $PLUGIN_LIST | sed -e 's|.*:||' | sort -V | tail -1 || true)
if [ "$list_ver" = "" ] ; then
max_ver="latest"
else
max_ver1=$(grep "^$p:" $DEPS | sed -e 's|.*:||' | sort -V | tail -1)
max_ver=$(echo -e "$list_ver\n$max_ver1" | sort -V | tail -1)
fi
echo "$p:$max_ver" >> ${DEPS}.uniq
done
main "${DEPS}.uniq"
done
rm -rf "$REF_DIR/lock"