forked from openhab/openhab-distro
-
Notifications
You must be signed in to change notification settings - Fork 1
/
update
executable file
·326 lines (288 loc) · 11.8 KB
/
update
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/bin/sh
setup(){
## Keep the script general by allowing the user to provide the version number to download.
if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
echo "Usage: ./runtime/bin/update [VersionNumber] [openHAB Dir]"
echo ""
echo " e.g. ./runtime/bin/update << Updates to the next version"
echo " ./runtime/bin/update 2.0.0 << Updates to a specific version"
echo " ./update 2.1.0 /opt/openHAB2 << Updates a specific root folder"
echo " ./runtime/bin/update 2.2.0-SNAPSHOT << Updates to latest SNAPSHOT"
echo ""
echo "Use this script to change openHAB to another version. Specifying the version allows"
echo "you to upgrade or downgrade to that version, or to the latest snapshot. Not specifying"
echo "any parameters will attempt to find the next version for you."
echo ""
echo "You can place this script anywhere, but you should run it from inside the openHAB root folder."
echo "Do not try to run the script from inside the runtime folder."
echo ""
exit 0
fi
## Ask to run as root to prevent us from running sudo in this script.
if [ "$(id -u)" -ne 0 ]; then
echo "Please run this script as root! (e.g. use sudo)" >&2
exit 1
fi
## Second parameter can be the openHAB path, if not assume the script is called from root!
if [ -z "$2" ]; then
if [ -n "$OPENHAB_HOME" ]; then
WorkingDir="$OPENHAB_HOME"
DirError="'OPENHAB_HOME' does not point towards openHAB's root directory."
else
WorkingDir="."
DirError="The script must be called from openHAB's root directory."
fi
else
WorkingDir="$2"
DirError="The specified directory is not openHAB's root directory."
fi
if [ -z "$OPENHAB_CONF" ]; then OPENHAB_CONF="$WorkingDir/conf"; fi
if [ -z "$OPENHAB_USERDATA" ]; then OPENHAB_USERDATA="$WorkingDir/userdata"; fi
## Check two of the standard openHAB folders to make sure we're updating the right thing.
if [ ! -d "$OPENHAB_USERDATA" ] || [ ! -d "$OPENHAB_CONF" ]; then
echo "$DirError" >&2
echo "Either specify a directory or place this update script in and run from openHAB's root folder." >&2
exit 1
fi
## Check to see if processes are running before updating
if [ ! -z "$(ps aux | grep "openhab2.*java" | grep -v grep)" ]; then
echo "openHAB is running! Please stop the process before updating." >&2
exit 1
fi
CurrentVersion="$(awk '/openhab-distro/{print $3}' "$OPENHAB_USERDATA/etc/version.properties")"
OHVersion="$1"
## If no OHVersion is specified, try incrementing the second point.
if [ -z "$OHVersion" ]; then
FirstPart="$(echo "$CurrentVersion" | awk -F'.' '{print $1}')"
SecondPart="$(echo "$CurrentVersion" | awk -F'.' '{print $2}')"
ThirdPart="$(echo "$CurrentVersion" | awk -F'.' '{print $3}')"
if test "${ThirdPart#*-SNAPSHOT}" != "$ThirdPart"; then
OHVersion="$CurrentVersion"
else
OHVersion="$FirstPart.$((SecondPart + 1)).$ThirdPart"
fi
fi
## Choose bintray for releases, cloudbees for snapshot.
if test "${OHVersion#*-SNAPSHOT}" != "$OHVersion"; then
DownloadLocation="https://openhab.ci.cloudbees.com/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab/target/openhab-$OHVersion.zip"
AddonsDownloadLocation="https://openhab.ci.cloudbees.com/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons/target/openhab-addons-$OHVersion.kar"
LegacyAddonsDownloadLocation="https://openhab.ci.cloudbees.com/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons-legacy/target/openhab-addons-legacy-$OHVersion.kar"
elif [ "$OHVersion" = "$CurrentVersion" ]; then
echo "You are already on openHAB $CurrentVersion" >&2
exit 1
else
DownloadLocation="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab%2F$OHVersion%2Fopenhab-$OHVersion.zip"
AddonsDownloadLocation="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons%2F$OHVersion%2Fopenhab-addons-$OHVersion.kar"
LegacyAddonsDownloadLocation="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons-legacy%2F$OHVersion%2Fopenhab-addons-legacy-$OHVersion.kar"
fi
## Set the temporary directories.
TempDir="/tmp/openhab"
OutputFile="$TempDir/openhab-$OHVersion.zip"
## Store anything in temporary folders
echo "Making Temporary Directory"
mkdir -p "$TempDir" || {
echo "Failed to make temporary directory: $TempDir" >&2
exit 1
}
}
## Download the specified version of openHAB and check for an update script.
download(){
## Skip this part if the script was called by an older version of itself.
if [ "$1" != "--skipnew" ]; then
echo "Downloading openHAB $OHVersion..."
curl -Lf# "$DownloadLocation" -o "$OutputFile" || {
echo "Download failed, version $OHVersion does not exist." >&2
echo "If you believe this to be an error, please check the openHAB website. (www.openhab.org)"
exit 1
}
## First check if there's a newer version of this update script
unzip -qp "$OutputFile" runtime/bin/update > "$TempDir/update" 2>/dev/null && {
echo "Update script in .zip archive found, using that instead."
chmod a+x "$TempDir/update"
"$TempDir/update" "$OHVersion" "$(cd "$WorkingDir" && pwd -P)" "--skipnew"; exit 0
}
fi
}
runCommand() {
string="$1"
string="$(echo "$string" | sed "s:\$OPENHAB_USERDATA:${OPENHAB_USERDATA:?}:g")"
string="$(echo "$string" | sed "s:\$OPENHAB_CONF:${OPENHAB_CONF:?}:g")"
string="$(echo "$string" | sed "s:\$OPENHAB_HOME:${WorkingDir:?}:g")"
command="$(echo "$string" | awk -F';' '{print $1}')"
param1="$(echo "$string" | awk -F';' '{print $2}')"
param2="$(echo "$string" | awk -F';' '{print $3}')"
case $command in
'DEFAULT')
# Just rename the file, the update process adds back the new version
echo " Adding '.bak' to $param1"
mv "$param1" "$param1.bak"
;;
'DELETE')
echo " Deleting: $param1"
rm -f "$param1"
;;
'MOVE')
echo " Moving: From $param1 to $param2"
mv "$param1" "$param2"
;;
'NOTE') printf ' \033[32mNote:\033[m %s\n' "$param1";;
'ALERT') printf ' \033[31mWarning:\033[m %s\n' "$param1";;
esac
}
getVersionNumber() {
firstPart="$(echo "$1" | awk -F'.' '{print $1}')"
secondPart="$(echo "$1" | awk -F'.' '{print $2}')"
thirdPart="$(echo "$1" | awk -F'.' '{print $3}')"
thirdPart="${thirdPart%%-*}"
echo $((firstPart*10000+secondPart*100+thirdPart))
}
scanVersioningList() {
Section="$1"
VersionMessage="$2"
InSection=false
InNewVersion=false
## Read the file line by line.
while IFS= read -r Line
do
case $Line in
'')
continue
;;
## Flag to run the relevant [[section]] only.
"[[$Section]]")
InSection=true
;;
## Stop reading the file if another [[section]] starts.
"[["*"]]")
if $InSection; then
break
fi
;;
## Detect the [version] and execute the line if relevant.
'['*'.'*'.'*']')
if $InSection; then
LineVersion="$(echo "$Line" | awk -F'[][]' '{print $2}')"
LineVersionNumber=$(getVersionNumber "$LineVersion")
if [ "$CurrentVersionNumber" -lt "$LineVersionNumber" ]; then
InNewVersion=true
echo ""
echo "$VersionMessage $LineVersion:"
else
InNewVersion=false
fi
fi
;;
*)
if $InSection && $InNewVersion; then
runCommand "$Line"
fi
;;
esac
done < "$TempDir/$transferFile"
}
echo " "
echo "#########################################"
echo " openHAB 2.x.x update script "
echo "#########################################"
echo " "
SpecifiedVersion="$1"
SpecifiedDir="$2"
SkipModifier="$3"
##Run the initialisation functions defined above
setup "$SpecifiedVersion" "$SpecifiedDir"
download "$SkipModifier"
transferFile="update.lst"
CurrentVersionNumber=$(getVersionNumber "$CurrentVersion")
case $CurrentVersion in
*"-"*) CurrentVersionNumber=$((CurrentVersionNumber-1));;
esac
## Go through a list of transitional commands that are stored in the update archive.
echo "New update list required, extracting from zip..."
unzip -qp "$OutputFile" "runtime/bin/$transferFile" > "$TempDir/$transferFile" || {
echo "Additional update commands not found in archive, exiting..."
exit 1
}
## Notify the user of important changes first
echo "The script will attempt to update openHAB to version $OHVersion"
printf 'Please read the following \033[32mnotes\033[m and \033[31mwarnings\033[m:\n'
scanVersioningList "MSG" "Important notes for version"
if [ -z "$OPENHAB_NONINTERACT" ]; then
printf '\nIs this okay? [y/N]: '
read -r answer
case $answer in
[Yy]*)
;;
*)
echo "Cancelling update..."
rm -rf "${TempDir:?}"
exit 0
;;
esac
fi
## Preserve file ownership of old setup.
FileOwner=$(ls -ld "$OPENHAB_USERDATA" | awk '{print $3}')
FileGroup=$(ls -ld "$OPENHAB_USERDATA" | awk '{print $4}')
## Perform version specific pre-update commands
scanVersioningList "PRE" "Performing pre-update tasks for version"
## Remove only the files that are to be replaced.
echo ""
echo "Removing openHAB System Files..."
mkdir -p "$TempDir/runtime"
mkdir -p "$TempDir/userdata/etc"
mv "$WorkingDir/runtime" "$TempDir/runtime/"
## Go through a list of system files that are stored in the update archive.
echo "New system filelist required, extracting from zip..."
unzip -qp "$OutputFile" runtime/bin/userdata_sysfiles.lst > "$TempDir/filelist.lst" || {
echo "System Filelist not found in update, exiting..."
exit 1
}
while IFS= read -r fileName
do
fullPath="$WorkingDir/userdata/etc/$fileName"
if [ -f "$fullPath" ]; then
mv "$fullPath" "$TempDir/userdata/etc/"
fi
done < "$TempDir/filelist.lst"
## Clearing the cache and tmp folders is necessary for upgrade.
echo "Clearing cache..."
rm -rf "${OPENHAB_USERDATA:?}/cache"
rm -rf "${OPENHAB_USERDATA:?}/tmp"
## Unzip the downloaded folder into openHAB's directory WITHOUT replacing any existing files.
echo "Updating openHAB..."
unzip -nq "$OutputFile" -d "$WorkingDir/" || {
echo "Failed to unzip archive, restoring system files..." >&2
## An error has occured so try to restore openHAB to it's previous state.
cp -a "$TempDir/runtime" "$WorkingDir/runtime"
cp -a "$TempDir/userdata/etc/"* "${OPENHAB_USERDATA:?}/etc/"
exit 1
}
## Perform version specific post-update commands
scanVersioningList "POST" "Performing post-update tasks for version"
## If there's an existing addons file, we need to replace it with the correct version.
AddonsFile="$WorkingDir/addons/openhab-addons-$CurrentVersion.kar"
if [ -f "$AddonsFile" ]; then
echo "Found an openHAB addons file, replacing with new version..."
rm -f "${AddonsFile:?}"
curl -Lf# "$AddonsDownloadLocation" -o "$WorkingDir/addons/openhab-addons-$OHVersion.kar" || {
echo "Download of addons file failed, please find it on the openHAB website (www.openhab.org)" >&2
}
fi
## Do the same for the legacy addons file.
LegacyAddonsFile="$WorkingDir/addons/openhab-addons-legacy-$CurrentVersion.kar"
if [ -f "$LegacyAddonsFile" ]; then
echo "Found an openHAB legacy addons file, replacing with new version..."
rm -f "${LegacyAddonsFile:?}"
curl -Lf# "$LegacyAddonsDownloadLocation" -o "$WorkingDir/addons/openhab-addons-legacy-$OHVersion.kar" || {
echo "Download of legacy addons file failed, please find it on the openHAB website (www.openhab.org)" >&2
}
fi
echo ""
## Remove the downloaded zip-file.
echo "Deleting temporary files..."
rm -rf "${TempDir:?}"
## Restore file ownership.
echo "Restoring previous file ownership ($FileOwner:$FileGroup)"
chown -R "$FileOwner:$FileGroup" "$WorkingDir"
echo ""
echo "SUCCESS: openHAB updated from $CurrentVersion to $OHVersion"
echo ""