forked from kwindrem/SetupHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packageAutoUpdater
executable file
·268 lines (232 loc) · 9.09 KB
/
packageAutoUpdater
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
#!/bin/bash
# This script keeps Venus modification packages up to date
# It looks on GitHub for package updates
# The timestamp of the current tag on GitHub is compatred to the one that was last installed
# if GitHub is newer, the local copy is updated and the package is reinstalled
#
# This script also checks for updates on a USB stick and gives priority to that location
#
# An automatic update will only occur if the package is installed and the autoUpdate setup option is set
#
# the script runs as a service so any updates that also update this script will restart automatically
# get the full, unambiguous path to this script
scriptDir="$( cd "$(dirname $0)" >/dev/null 2>&1 ; /bin/pwd -P )"
packageame=$(basename "$scriptDir")
shortScriptName=$(basename "$scriptDir")/$(basename "$0")
source "/data/SetupHelper/EssentialResources"
source "/data/SetupHelper/LogHandler"
# this flag is tested by LogHandler to determine if messages should be output to the console
logToConsole=false
source "/data/SetupHelper/UpdateResources"
source "/data/SetupHelper/DbusSettingsResources"
# updates status message on GUI
# $1 is the message identifier (not the actual message
lastMessage=''
lastPackage=''
updateStatus ()
{
updateDbus=false
checkPackage=false
if [[ $1 == 'IDLE' ]] ; then
message="Fast updates: 10 sec/pkg, Normal updates: 10 min/pkg"
elif [[ $1 == 'USB_DISABLED' ]] ; then
message="USB/SD updates disabled auto GitHub updates"
elif [[ $1 == 'CHECKING' ]] ; then
message="checking $package"
checkPackage=true
elif [[ $1 == 'WAIT' ]] ; then
message="waiting to check $package"
checkPackage=true
elif [[ $1 == 'USB_CHECK' ]] ; then
message="Checking USB/SD for updates"
else
message=""
fi
if [[ $1 != $lastMessage ]]; then
updateDbus=true
elif $checkPackage && [[ $package != $lastPackage ]]; then
updateDbus=true
fi
# update GUI status message
if $updateDbus ; then
updateDbusStringSetting "/Settings/PackageVersion/CheckingPackage" "$message"
fi
lastMessage=$1
lastPackage=$package
}
#### main code starts here
# wait until dbus settings are active
while [ $(dbus -y | grep -c "com.victronenergy.settings") == 0 ]; do
logMessage "waiting for dBus settings"
sleep 1
done
logMessage "starting up"
usbCheck=false
mediaDetected=false
lastUpdateTime=0
checkingPackage=false
updateSetupHelper=false
# 10 minutes between GitHub checks to minimize network traffic
gitHubSlowCheckDelay=600
# 10 seconds for first pass
gitHubFastCheckDelay=10
gitHubCheckDelay=0
lastGitHubUpdateSetting=0
# loop forever
while true ; do
rebootNeeded=false
restartFromFirstPackage=false
# skip all processing if package list doesn't exist
# but keep checking
if [ ! -f "$packageListFile" ]; then
sleep 10
continue
fi
# loop through packages from package list
while read -u 9 package gitHubUser gitHubBranch; do
# skip comments
if [[ ${package:0:1} == "#" ]] ; then
continue
# skip blank/incomplete lines
elif [ -z $package ] || [ -z $gitHubUser ] || [ -z $gitHubBranch ] ; then
continue
fi
packageDir="/data/$package"
setupOptionsDir="$setupOptionsRoot"/$package
# skip uninstalled packages
if [ ! -f "$installedFlagPrefix"$package ]; then
continue
# package has been installed, check for updates
else
checkingPackage=true
fi
# this loop permits detection of USB media during the long wait for the next GitHub check
while $checkingPackage ; do
doUpdate=false
# pull Git Hub autoupdate mode from dbus
autoUpdateSetting=$(dbus-send --system --print-reply=literal --dest=com.victronenergy.settings /Settings/PackageVersion/GitHubAutoUpdate\
com.victronenergy.BusItem.GetValue 2> /dev/null | awk '{print $3}')
if [ -z $autoUpdateSetting ]; then
autoUpdateSetting=0
fi
# check for USB / SD media
mediaList=($(ls /media))
# no media
if [ -z $mediaList ] ; then
mediaDetected=false
usbCheck=false
# media first detected, enable USB checks and start loop over
elif ! $mediaDetected ; then
mediaDetected=true
usbCheck=true
updateStatus 'USB_CHECK'
checkingPackage=false
restartFromFirstPackage=true
break
fi
# nothing to do - reset loop and wait
if (( $autoUpdateSetting == 0 )) && ! $usbCheck ; then
checkingPackage=false
restartFromFirstPackage=true
updateStatus 'IDLE'
break
fi
# USB / SD updates
if $usbCheck ; then
for dir in ${mediaList[@]} ; do
getFromUsb $package
if [ $? -eq 1 ]; then
logMessage "found $package on USB"
doUpdate=true
updateStatus 'CHECKING'
break
fi
done
# done checking for this package, time to move on
checkingPackage=false
# Git Hub updates
elif (( $autoUpdateSetting != 0 )); then
# if speeding up the loop, start package scan over
if (( $autoUpdateSetting >= 2 )) && (( $lastGitHubUpdateSetting <= 1 )); then
checkingPackage=false
restartFromFirstPackage=true
lastGitHubUpdateSetting=$autoUpdateSetting
break
fi
# set update delay based on update mode
if (( $autoUpdateSetting >= 2 )) ; then
gitHubCheckDelay=$gitHubFastCheckDelay
else
gitHubCheckDelay=$gitHubSlowCheckDelay
fi
currentTime=$(date '+%s')
# wait between GitHub updates to minimize traffic
if (( $currentTime >= $lastUpdateTime + $gitHubCheckDelay )) ; then
updateStatus 'CHECKING'
lastUpdateTime=$currentTime
getFromGitHub $package
if [ $? -eq 1 ]; then
logMessage "found $package on GitHub"
doUpdate=true
fi
checkingPackage=false
elif (( $autoUpdateSetting != 0 )); then
updateStatus 'WAIT'
fi
fi
if $doUpdate ; then
# do SetupHelper update now since other setup scripts depend on it's resources
# will end this script which will start up again via supervise
if [ $package == "SetupHelper" ]; then
updateSetupHelper=true
break
# reinstall the package without user interaction
else
# update via unattended reinstall only
logMessage "updating $package with $gitHubBranch"
doUpdate $package $gitHubBranch
if $usbCheck ; then
sleep 1
fi
fi
elif (( $autoUpdateSetting == 0 )); then
rm -rf "$packageDir-$gitHubBranch"
fi
# delay for inner loop
if $checkingPackage ; then
if $usbCheck ; then
usleep 200000
else
sleep 1
fi
fi
done # end while checkingPackage
if $restartFromFirstPackage || $updateSetupHelper ; then
break
else
sleep 1
fi
done 9< "$packageListFile" # end while read ...
# if not restarting scan, update automatic update state
if ! $restartFromFirstPackage && ! $updateSetupHelper; then
# single pass
if (( $autoUpdateSetting == 3 )) ; then
setSetting 0 /Settings/PackageVersion/GitHubAutoUpdate
# end of first pass, switch to slow updates
elif (( $autoUpdateSetting == 2 )) ; then
setSetting 1 /Settings/PackageVersion/GitHubAutoUpdate
fi
fi
usbCheck=false
# continue execution in packageAutoUpdateCleanup
# so that this script and the service can exit cleanly
if $updateSetupHelper || $rebootNeeded ; then
logMessage "continuing in cleanup script - $packageName exiting"
nohup "$scriptDir/packageAutoUpdaterCleanup" $gitHubBranch $updateSetupHelper $rebootNeeded 2>&1 | awk '{print "packageAutoUpdaterCleanup " $0}'| tai64n >> /var/log/SetupHelper &
# shutdown the service which runs this script - this will end this script
svc -d "/service/SetupHelper"
# wait here for service to end this script
sleep 10000
fi
sleep 1
done