]>
Commit | Line | Data |
---|---|---|
1 | #!/bin/sh | |
2 | # Update GeoIP packages to new version provided by MaxMind. | |
3 | # | |
4 | # Author: Elan Ruusamäe <glen@pld-linux.org> | |
5 | # | |
6 | # Changelog: | |
7 | # 2012-07-04 Created initial version | |
8 | # 2014-03-04 Rewritten to be smarter when checking for updates avoiding full download if no changes. | |
9 | # 2014-06-06 Fix finding new versions if multiple previous archives were present | |
10 | # 2015-08-25 Add auto commit support | |
11 | ||
12 | set -e | |
13 | ||
14 | update=false | |
15 | status=false | |
16 | commit=true | |
17 | while [ $# -gt 0 ]; do | |
18 | case "$1" in | |
19 | update|-u|-update|--update) | |
20 | update=true | |
21 | shift | |
22 | ;; | |
23 | status|-s|-status|--status) | |
24 | status=true | |
25 | shift | |
26 | ;; | |
27 | *) | |
28 | break | |
29 | ;; | |
30 | esac | |
31 | done | |
32 | ||
33 | die() { | |
34 | echo >&2 "$0: ERROR: $*" | |
35 | exit 1 | |
36 | } | |
37 | ||
38 | # get file DATE in GMT timezone | |
39 | filedate() { | |
40 | local file="$1" | |
41 | TZ=GMT stat -c '%y' "$file" | awk '{print $1}' | |
42 | } | |
43 | ||
44 | # get package, no sources | |
45 | get_package() { | |
46 | local pkg=$1 out | |
47 | out=$(builder -g -ns $pkg 2>&1) || echo "$out" | |
48 | } | |
49 | ||
50 | get_urls() { | |
51 | local specfile=$1 t url | |
52 | ||
53 | t=$(mktemp) | |
54 | builder -su $specfile 2>/dev/null > $t | |
55 | ||
56 | while read url; do | |
57 | # skip non-archives | |
58 | case "$url" in | |
59 | *.zip|*.gz|*.xz) | |
60 | echo "$url" | |
61 | ;; | |
62 | esac | |
63 | done < $t | |
64 | rm -f $t | |
65 | } | |
66 | ||
67 | update_urls() { | |
68 | local specfile=$1 url fn z | |
69 | ||
70 | for url in "$@"; do | |
71 | # take output filename (anything after last slash) | |
72 | fn=${url##*/} | |
73 | # remove querystring for mtime match to work | |
74 | url=${url%\?*} | |
75 | test -e "$fn" && z= || unset z | |
76 | curl ${z+-z "$fn"} -o "$fn" "$url" -R -s | |
77 | done | |
78 | } | |
79 | ||
80 | # set version to $version in $specfile and build the package | |
81 | update_version() { | |
82 | local specfile="$1" version="$2" out | |
83 | ||
84 | # update version | |
85 | sed -i -e " | |
86 | s/^\(Version:[ \t]\+\)[.0-9]\+\$/\1$version/ | |
87 | s/^\(Release:[ \t]\+\)[.0-9]\+\$/\11/ | |
88 | " $specfile | |
89 | ||
90 | rm *.zip *.gz *.xz | |
91 | # update md5 | |
92 | out=$(md5 -p1 $specfile 2>&1) || echo "$out" | |
93 | ||
94 | # build it | |
95 | out=$(builder -bb $specfile 2>&1) || echo "$out" | |
96 | } | |
97 | ||
98 | commit_vcs() { | |
99 | local specfile="$1" version="$2" | |
100 | ||
101 | git commit -m "updated to $version" $specfile | |
102 | git push || echo push failed | |
103 | } | |
104 | ||
105 | # get version from package files | |
106 | # set $version variable | |
107 | version_from_files() { | |
108 | local pkg=$1 url fn dt d | |
109 | shift | |
110 | ||
111 | for url in "$@"; do | |
112 | # take output filename (anything after last slash) | |
113 | fn=${url##*/} | |
114 | # skip inexistent files | |
115 | test -e "$fn" || continue | |
116 | ||
117 | d=$(filedate "$fn") | |
118 | if [ "$(echo $d | tr -d -)" -gt "$(echo $dt | tr -d -)" ]; then | |
119 | dt=$d | |
120 | fi | |
121 | done | |
122 | ||
123 | case "$pkg" in | |
124 | xtables-geoip) | |
125 | version=$(echo "$dt" | tr -d -) | |
126 | ;; | |
127 | *) | |
128 | version=$(echo "$dt" | tr - .) | |
129 | ;; | |
130 | esac | |
131 | } | |
132 | ||
133 | version_from_attachment() { | |
134 | local url fn dt d t | |
135 | t=$(mktemp) | |
136 | ||
137 | for url in "$@"; do | |
138 | # remove querystring to receive proper headers | |
139 | url=${url%\?*} | |
140 | curl -Is "$url" -o "$t" | |
141 | fn=$(awk 'BEGIN {FS=": "}/^[Cc]ontent-[Dd]isposition/{sub(/.*filename=/, "", $2); print $2}' "$t") | |
142 | fn=${fn#GeoLite2-Country-CSV_} | |
143 | d=$(echo "$fn" | sed -e 's/[^0-9-]//g') | |
144 | ||
145 | if [ "$d" -gt "$dt" ]; then | |
146 | dt=$d | |
147 | fi | |
148 | done | |
149 | ||
150 | test -n "$dt" || die "Failed to find date from $*" | |
151 | ||
152 | rm -f $t | |
153 | version=$dt | |
154 | } | |
155 | ||
156 | dir=$(dirname "$0") | |
157 | APPDIR=$(d=$0; [ -L "$d" ] && d=$(readlink -f "$d"); dirname "$d") | |
158 | PATH=$APPDIR:$PATH | |
159 | cd "$dir" | |
160 | ||
161 | pkgs='GeoIP-db-City GeoIP-db-Country GeoIP-db-IPASNum xtables-geoip' | |
162 | for pkg in ${*:-$pkgs}; do | |
163 | pkg=${pkg%.spec} | |
164 | $status && continue | |
165 | ||
166 | get_package $pkg | |
167 | cd $pkg | |
168 | specfile=*.spec | |
169 | ||
170 | urls=$(get_urls $specfile) | |
171 | if [ "$pkg" = "xtables-geoip" ]; then | |
172 | version_from_attachment $urls | |
173 | else | |
174 | update_urls $urls | |
175 | version_from_files $pkg $urls | |
176 | fi | |
177 | ||
178 | oldvers=$(awk '/^Version:[ ]+/{print $NF}' $specfile) | |
179 | if [ "$oldvers" != "$version" ]; then | |
180 | update_version $specfile $version | |
181 | if $commit; then | |
182 | commit_vcs $specfile $version | |
183 | fi | |
184 | fi | |
185 | cd .. | |
186 | done | |
187 | ||
188 | # report each package git status | |
189 | for pkg in ${*:-$pkgs}; do | |
190 | pkg=${pkg%.spec} | |
191 | cd $pkg | |
192 | out=$( | |
193 | git status --porcelain | |
194 | git status | grep ahead || : | |
195 | ) | |
196 | test -n "$out" && echo "$pkg: $out" | |
197 | cd .. | |
198 | done |