-
Notifications
You must be signed in to change notification settings - Fork 25
/
update-data.sh
93 lines (74 loc) · 1.56 KB
/
update-data.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
#! /bin/sh
ARCHIVE_NAME=venus-data
ARCHIVE_SUF="tar.gz tgz zip"
do_tar() {
a=$1
d=$2
shift 2
tar xzf "$a" -C "$d" "$@"
}
do_zip() {
a=$1
d=$2
shift 2
unzip -o -d "$d" "$a" "$@"
}
unpack() {
archive=$1
dest=$2
case $archive in
*.tar.gz|*.tgz)
cmd=do_tar
exclude=--exclude
rc=rc
;;
*.zip)
cmd=do_zip
exclude=-x
rc="rc/*"
;;
esac
tmp=$(mktemp -d)
trap 'rm -rf $tmp' EXIT
pre="$tmp/rc/pre-hook.sh"
post="$tmp/rc/post-hook.sh"
$cmd "$archive" "$tmp" "$rc"
if [ -f "$pre" ]; then
sh "$pre" || return
fi
if $cmd "$archive" "$dest" $exclude "$rc"; then
arg="success"
else
arg="extraction-failed"
fi
if [ -f "$post" ]; then
sh "$post" "$arg"
fi
rm -rf $tmp
trap - EXIT
}
update_data() {
found=0
for dir in /media/*; do
for suf in $ARCHIVE_SUF; do
for archive in $(ls $dir/$ARCHIVE_NAME-*.$suf $dir/$ARCHIVE_NAME.$suf 2> /dev/null); do
echo "Updating /data with $archive"
unpack "$archive" /data
found=1
done
done
done
if [ $found -eq 1 ]; then
return 0
fi
# Note: this triggers a delayed check, to support slow mounting media.
return 1
}
delayed_update() {
udevadm settle
mkdir -p $(readlink /media)
inotifywait -q -m -e create,isdir -t 30 /media
update_data
}
update_data && exit 0
delayed_update &