-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-plugins.sh
executable file
·192 lines (158 loc) · 5.05 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
#!/bin/bash
# Resolve dependencies and download plugins given on the command line
#
# FROM jenkins
# RUN install-plugins.sh docker-slaves github-branch-source
REF_DIR=${REF:-/usr/share/jenkins/ref/plugins}
FAILED="$REF_DIR/failed-plugins.txt"
function getLockFile() {
echo -n "$REF_DIR/${1}.lock"
}
function getArchiveFilename() {
echo -n "$REF_DIR/${1}.jpi"
}
function download() {
local plugin originalPlugin version lock ignoreLockFile
plugin="$1"
version="${2:-latest}"
ignoreLockFile="$3"
lock="$(getLockFile "$plugin")"
if [[ $ignoreLockFile ]] || mkdir "$lock" &>/dev/null; then
if ! doDownload "$plugin" "$version"; then
# some plugin don't follow the rules about artifact ID
# typically: docker-plugin
originalPlugin="$plugin"
plugin="${plugin}-plugin"
if ! doDownload "$plugin" "$version"; then
echo "Failed to download plugin: $originalPlugin or $plugin" >&2
echo "Not downloaded: ${originalPlugin}" >> "$FAILED"
return 1
fi
fi
if ! checkIntegrity "$plugin"; then
echo "Downloaded file is not a valid ZIP: $(getArchiveFilename "$plugin")" >&2
echo "Download integrity: ${plugin}" >> "$FAILED"
return 1
fi
resolveDependencies "$plugin"
fi
}
function doDownload() {
local plugin version url jpi
plugin="$1"
version="$2"
jpi="$(getArchiveFilename "$plugin")"
if [[ -f $jpi ]]; then
echo "Using provided plugin: $plugin"
return 0
fi
url="$JENKINS_UC/download/plugins/$plugin/$version/${plugin}.hpi"
echo "Downloading plugin: $plugin from $url"
curl --connect-timeout 5 --retry 5 --retry-delay 0 --retry-max-time 60 -s -f -L "$url" -o "$jpi"
return $?
}
function checkIntegrity() {
local plugin jpi
plugin="$1"
jpi="$(getArchiveFilename "$plugin")"
zip -T "$jpi" >/dev/null
return $?
}
# compare if version1 < version2
versionLT() {
[ "$1" = "$2" ] && return 1 || [ "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ]
}
function resolveDependencies() {
local plugin jpi dependencies
plugin="$1"
jpi="$(getArchiveFilename "$plugin")"
# ^M below is a control character, inserted by typing ctrl+v ctrl+m
dependencies="$(unzip -p "$hpi" META-INF/MANIFEST.MF | sed -e 's#
##g' | tr '\n' '|' | sed -e 's#| ##g' | tr '|' '\n' | grep "^Plugin-Dependencies: " | sed -e 's#^Plugin-Dependencies: ##')"
if [[ ! $dependencies ]]; then
echo " > $plugin has no dependencies"
return
fi
echo " > $plugin depends on $dependencies"
IFS=',' read -a array <<< "$dependencies"
for d in "${array[@]}"
do
plugin="$(cut -d':' -f1 - <<< "$d")"
if [[ $d == *"resolution:=optional"* ]]; then
echo "Skipping optional dependency $plugin"
else
pluginInstalled="$(echo "${bundledPlugins}" | grep "^${plugin}:")"
pluginInstalled="${pluginInstalled//[$'\r']}"
if ! [ -z "${pluginInstalled}" ]; then
versionInstalled=$(versionFromPlugin "${pluginInstalled}")
versionToInstall=$(versionFromPlugin "${d}")
if versionLT "${versionInstalled}" "${versionToInstall}"; then
echo "Upgrading bundled dependency $d ($versionToInstall > $versionInstalled)"
download "$plugin" "$versionToInstall" &
else
echo "Skipping already bundled dependency $d ($versionToInstall <= $versionInstalled)"
fi
else
download "$plugin" "$(versionFromPlugin "${d}")" &
fi
fi
done
wait
}
function bundledPlugins() {
local JENKINS_WAR=/usr/share/jenkins/jenkins.war
if [ -f $JENKINS_WAR ]
then
TEMP_PLUGIN_DIR=/tmp/plugintemp.$$
for i in `jar tf $JENKINS_WAR|egrep 'plugins.*\..pi'|egrep -v '\/$'|sort`
do
rm -fr $TEMP_PLUGIN_DIR
mkdir -p $TEMP_PLUGIN_DIR
PLUGIN=`basename $i|cut -f1 -d'.'`
(cd $TEMP_PLUGIN_DIR;jar xf $JENKINS_WAR "$i";jar xvf $TEMP_PLUGIN_DIR/$i META-INF/MANIFEST.MF >/dev/null 2>&1)
VER=`egrep -i Plugin-Version "$TEMP_PLUGIN_DIR/META-INF/MANIFEST.MF"|cut -d\: -f2|sed 's/ //'`
echo "$PLUGIN:$VER"
done
rm -fr $TEMP_PLUGIN_DIR
else
rm -f $TEMP_ALREADY_INSTALLED
echo "ERROR file not found: $JENKINS_WAR"
exit 1
fi
}
function versionFromPlugin() {
local plugin=$1
if [[ $plugin =~ .*:.* ]]; then
echo "${plugin##*:}"
else
echo "latest"
fi
}
main() {
local plugin version
mkdir -p "$REF_DIR" || exit 1
# Create lockfile manually before first run to make sure any explicit version set is used.
echo "Creating initial locks..."
for plugin in "$@"; do
mkdir "$(getLockFile "${plugin%%:*}")"
done
echo -e "\nAnalyzing war..."
bundledPlugins="$(bundledPlugins)"
echo -e "\nDownloading plugins..."
for plugin in "$@"; do
version=""
if [[ $plugin =~ .*:.* ]]; then
version=$(versionFromPlugin "${plugin}")
plugin="${plugin%%:*}"
fi
download "$plugin" "$version" "true" &
done
wait
if [[ -f $FAILED ]]; then
echo -e "\nSome plugins failed to download!\n$(<"$FAILED")" >&2
exit 1
fi
echo -e "\nCleaning up locks"
rm -r "$REF_DIR"/*.lock
}
main "$@"