travis: improve log output of download_sdk
[feed/packages.git] / .travis_do.sh
1 #!/bin/bash
2 #
3 # MIT Alexander Couzens <lynxis@fe80.eu>
4
5 set -e
6
7 SDK_HOME="$HOME/sdk"
8 SDK_PATH=https://downloads.lede-project.org/snapshots/targets/ar71xx/generic/
9 SDK=openwrt-sdk-ar71xx-generic_gcc-5.5.0_musl.Linux-x86_64
10 PACKAGES_DIR="$PWD"
11
12 echo_red() { printf "\033[1;31m$*\033[m\n"; }
13 echo_green() { printf "\033[1;32m$*\033[m\n"; }
14 echo_blue() { printf "\033[1;34m$*\033[m\n"; }
15
16 exec_status() {
17 PATTERN="$1"
18 shift
19 while :;do sleep 590;echo "still running (please don't kill me Travis)";done &
20 ("$@" 2>&1) | tee logoutput
21 R=${PIPESTATUS[0]}
22 kill $! && wait $! 2>/dev/null
23 if [ $R -ne 0 ]; then
24 echo_red "=> '$*' failed (return code $R)"
25 return 1
26 fi
27 if grep -qE "$PATTERN" logoutput; then
28 echo_red "=> '$*' failed (log matched '$PATTERN')"
29 return 1
30 fi
31
32 echo_green "=> '$*' successful"
33 return 0
34 }
35
36 # download will run on the `before_script` step
37 # The travis cache will be used (all files under $HOME/sdk/). Meaning
38 # We don't have to download the file again
39 download_sdk() {
40 mkdir -p "$SDK_HOME"
41 cd "$SDK_HOME"
42
43 echo_blue "=== download SDK"
44 wget "$SDK_PATH/sha256sums" -O sha256sums
45 wget "$SDK_PATH/sha256sums.gpg" -O sha256sums.asc
46
47 # LEDE Build System (LEDE GnuPG key for unattended build jobs)
48 gpg --import $PACKAGES_DIR/.travis/626471F1.asc
49 echo '54CC74307A2C6DC9CE618269CD84BCED626471F1:6:' | gpg --import-ownertrust
50 # LEDE Release Builder (17.01 "Reboot" Signing Key)
51 gpg --import $PACKAGES_DIR/.travis/D52BBB6B.asc
52 echo 'B09BE781AE8A0CD4702FDCD3833C6010D52BBB6B:6:' | gpg --import-ownertrust
53
54 echo_blue "=== Verifying sha256sums signature"
55 gpg --verify sha256sums.asc
56 echo_blue "=== Verified sha256sums signature."
57 if ! grep "$SDK" sha256sums > sha256sums.small ; then
58 echo_red "=== Can not find $SDK file in sha256sums."
59 echo_red "=== Is \$SDK out of date?"
60 false
61 fi
62
63 # if missing, outdated or invalid, download again
64 if ! sha256sum -c ./sha256sums.small ; then
65 echo_blue "=== sha256 doesn't match or SDK file wasn't downloaded yet."
66 echo_blue "=== Downloading a fresh version"
67 wget "$SDK_PATH/$SDK.tar.xz" -O "$SDK.tar.xz"
68 fi
69
70 # check again and fail here if the file is still bad
71 echo_blue "Checking sha256sum a second time"
72 if ! sha256sum -c ./sha256sums.small ; then
73 echo_red "=== SDK can not be verified!"
74 false
75 fi
76 echo_blue "=== SDK is up-to-date"
77 }
78
79 # test_package will run on the `script` step.
80 # test_package call make download check for very new/modified package
81 test_packages2() {
82 local commit_range=$TRAVIS_COMMIT_RANGE
83 if [ -z "$TRAVIS_PULL_REQUEST_SHA" ]; then
84 echo_blue "Using only the latest commit, since we're not in a Pull Request"
85 commit_range=HEAD~1
86 fi
87
88 # search for new or modified packages. PKGS will hold a list of package like 'admin/muninlite admin/monit ...'
89 PKGS=$(git diff --diff-filter=d --name-only "$commit_range" | grep 'Makefile$' | grep -v '/files/' | awk -F'/Makefile' '{ print $1 }')
90
91 if [ -z "$PKGS" ] ; then
92 echo_blue "No new or modified packages found!"
93 return 0
94 fi
95
96 echo_blue "=== Found new/modified packages:"
97 for pkg in $PKGS ; do
98 echo "===+ $pkg"
99 done
100
101 echo_blue "=== Setting up SDK"
102 tmp_path=$(mktemp -d)
103 cd "$tmp_path"
104 tar Jxf "$SDK_HOME/$SDK.tar.xz" --strip=1
105
106 # use github mirrors to spare lede servers
107 cat > feeds.conf <<EOF
108 src-git base https://github.com/lede-project/source.git
109 src-link packages $PACKAGES_DIR
110 src-git luci https://github.com/openwrt/luci.git
111 EOF
112
113 # enable BUILD_LOG
114 sed -i '1s/^/config BUILD_LOG\n\tbool\n\tdefault y\n\n/' Config-build.in
115
116 ./scripts/feeds update -a > /dev/null
117 ./scripts/feeds install -a > /dev/null
118 make defconfig > /dev/null
119 echo_blue "=== Setting up SDK done"
120
121 RET=0
122 # E.g: pkg_dir => admin/muninlite
123 # pkg_name => muninlite
124 for pkg_dir in $PKGS ; do
125 pkg_name=$(echo "$pkg_dir" | awk -F/ '{ print $NF }')
126 echo_blue "=== $pkg_name: Starting quick tests"
127
128 exec_status '^ERROR' make "package/$pkg_name/download" V=s || RET=1
129 exec_status '^ERROR' make "package/$pkg_name/check" V=s || RET=1
130
131 echo_blue "=== $pkg_name: quick tests done"
132 done
133
134 [ $RET -ne 0 ] && return $RET
135
136 for pkg_dir in $PKGS ; do
137 pkg_name=$(echo "$pkg_dir" | awk -F/ '{ print $NF }')
138 echo_blue "=== $pkg_name: Starting compile test"
139
140 # we can't enable verbose built else we often hit Travis limits
141 # on log size and the job get killed
142 exec_status '^ERROR' make "package/$pkg_name/compile" -j3 || RET=1
143
144 echo_blue "=== $pkg_name: compile test done"
145
146 echo_blue "=== $pkg_name: begin compile logs"
147 for f in $(find logs/package/feeds/packages/$pkg_name/ -type f); do
148 echo_blue "Printing last 200 lines of $f"
149 tail -n200 "$f"
150 done
151 echo_blue "=== $pkg_name: end compile logs"
152
153 echo_blue "=== $pkg_name: begin packages sizes"
154 du -ba bin/
155 echo_blue "=== $pkg_name: end packages sizes"
156 done
157
158 return $RET
159 }
160
161 test_commits() {
162 RET=0
163 if [ -z "$TRAVIS_PULL_REQUEST_SHA" ]; then
164 echo_blue "Skipping commits tests (not in a Pull Request)"
165 return 0
166 fi
167 for commit in $(git rev-list ${TRAVIS_COMMIT_RANGE/.../..}); do
168 echo_blue "=== Checking commit '$commit'"
169 if git show --format='%P' -s $commit | grep -qF ' '; then
170 echo_red "Pull request should not include merge commits"
171 RET=1
172 fi
173
174 author="$(git show -s --format=%aN $commit)"
175 if echo $author | grep -q '\S\+\s\+\S\+'; then
176 echo_green "Author name ($author) seems ok"
177 else
178 echo_red "Author name ($author) need to be your real name 'firstname lastname'"
179 RET=1
180 fi
181
182 subject="$(git show -s --format=%s $commit)"
183 if echo "$subject" | grep -q -e '^[0-9A-Za-z,/_-]\+: ' -e '^Revert '; then
184 echo_green "Commit subject line seems ok ($subject)"
185 else
186 echo_red "Commit subject line MUST start with '<package name>: ' ($subject)"
187 RET=1
188 fi
189
190 body="$(git show -s --format=%b $commit)"
191 sob="$(git show -s --format='Signed-off-by: %aN <%aE>' $commit)"
192 if echo "$body" | grep -qF "$sob"; then
193 echo_green "Signed-off-by match author"
194 else
195 echo_red "Signed-off-by is missing or doesn't match author (should be '$sob')"
196 RET=1
197 fi
198 done
199
200 return $RET
201 }
202
203 test_packages() {
204 test_commits && test_packages2 || return 1
205 }
206
207 echo_blue "=== Travis ENV"
208 env
209 echo_blue "=== Travis ENV"
210
211 if [ -n "$TRAVIS_PULL_REQUEST_SHA" ]; then
212 while true; do
213 # if clone depth is too small, git rev-list / diff return incorrect or empty results
214 C="$(git rev-list ${TRAVIS_COMMIT_RANGE/.../..} | tail -n1)" 2>/dev/null
215 [ -n "$C" -a "$C" != "a22de9b74cf9579d1ce7e6cf1845b4afa4277b00" ] && break
216 echo_blue "Fetching 50 commits more"
217 git fetch origin --deepen=50
218 done
219 fi
220
221 if [ $# -ne 1 ] ; then
222 cat <<EOF
223 Usage: $0 (download_sdk|test_packages)
224
225 download_sdk - download the SDK to $HOME/sdk.tar.xz
226 test_packages - do a make check on the package
227 EOF
228 exit 1
229 fi
230
231 $@