forked from mikkeloscar/arch-travis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arch-travis.sh
executable file
·358 lines (294 loc) · 8.94 KB
/
arch-travis.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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/bin/bash
# Copyright (C) 2016 Mikkel Oscar Lyderik Larsen
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Script for setting up and running a travis-ci build in an up to date
# Arch Linux chroot
ARCH_TRAVIS_MIRROR=${ARCH_TRAVIS_MIRROR:-"https://mirrors.lug.mtu.edu/archlinux"}
ARCH_TRAVIS_ARCH=${ARCH_TRAVIS_ARCH:-"x86_64"}
mirror_entry='Server = '$ARCH_TRAVIS_MIRROR'/\$repo/os/\$arch'
ARCH_TRAVIS_ROOT_ARCHIVE=""
default_root="root.${ARCH_TRAVIS_ARCH}"
ARCH_TRAVIS_CHROOT=${ARCH_TRAVIS_CHROOT:-"$default_root"}
user="travis"
user_home="/home/$user"
user_build_dir=$(pwd)
uid=$UID
gid=$GID
if [ -n "$CC" ]; then
# store travis CC
TRAVIS_CC=$CC
# reset to gcc for building arch packages
CC=gcc
fi
GOROOT=""
# default packages
default_packages=("base-devel" "git")
# pacman.conf repository line
repo_line=70
# try to find the latest iso by iterating through the dates of the current
# month, falling back to the last month if needed.
get_base_archive() {
local months=(
$(date +%Y.%m)
$(date +%Y.%m -d "-1 month")
)
for date in "${months[@]}"; do
echo $date
for i in {1..15}; do
local iso_date="$date"
if [ "$i" -lt 10 ]; then
iso_date="$date.0$i"
else
iso_date="$date.$i"
fi
ARCH_TRAVIS_ROOT_ARCHIVE="archlinux-bootstrap-${iso_date}-${ARCH_TRAVIS_ARCH}.tar.gz"
local url="$ARCH_TRAVIS_MIRROR/iso/$iso_date/$ARCH_TRAVIS_ROOT_ARCHIVE"
if [ -f "$ARCH_TRAVIS_ROOT_ARCHIVE" ]; then
return
fi
echo $url
curl --fail -O $url 2>&1
local ret=$?
if [ $ret -eq 0 ]; then
return
fi
done
done
}
# setup working Arch Linux chroot
setup_chroot() {
arch_msg "Setting up Arch chroot"
# if [ ! -f $archive ]; then
# # get root fs
# curl --fail -O "$ARCH_TRAVIS_MIRROR/iso/$ARCH_TRAVIS_ARCH_ISO/$archive" 2>&1
# local ret=$?
# # if it fails, try arch iso form the previous month
# if [ $ret -gt 0 ]; then
# ARCH_TRAVIS_ARCH_ISO="$(date +%Y.%m -d "-1 month").01"
# archive="archlinux-bootstrap-$ARCH_TRAVIS_ARCH_ISO-${ARCH_TRAVIS_ARCH}.tar.gz"
# as_normal "curl -O $ARCH_TRAVIS_MIRROR/iso/$ARCH_TRAVIS_ARCH_ISO/$archive"
# fi
# fi
get_base_archive
# extract root fs
as_root "tar xf $ARCH_TRAVIS_ROOT_ARCHIVE -C $HOME"
# remove archive if ARCH_TRAVIS_CLEAN_CHROOT is set
if [ -n "$ARCH_TRAVIS_CLEAN_CHROOT" ]; then
as_root "rm $ARCH_TRAVIS_ROOT_ARCHIVE"
fi
if [ "$ARCH_TRAVIS_CHROOT" != "$default_root" ]; then
as_root "mv $HOME/$default_root $HOME/$ARCH_TRAVIS_CHROOT"
fi
ARCH_TRAVIS_CHROOT="$HOME/$ARCH_TRAVIS_CHROOT"
# don't care for signed packages
as_root "sed -i 's|SigLevel = Required DatabaseOptional|SigLevel = Never|' $ARCH_TRAVIS_CHROOT/etc/pacman.conf"
# enable multilib
as_root "sed -i 's|#\[multilib\]|\[multilib\]\nInclude = /etc/pacman.d/mirrorlist|' $ARCH_TRAVIS_CHROOT/etc/pacman.conf"
# add mirror
as_root "echo $mirror_entry >> $ARCH_TRAVIS_CHROOT/etc/pacman.d/mirrorlist"
# add nameserver to resolv.conf
as_root "echo nameserver 8.8.8.8 >> $ARCH_TRAVIS_CHROOT/etc/resolv.conf"
sudo mount $ARCH_TRAVIS_CHROOT $ARCH_TRAVIS_CHROOT --bind
sudo mount --bind /proc $ARCH_TRAVIS_CHROOT/proc
sudo mount --bind /sys $ARCH_TRAVIS_CHROOT/sys
sudo mount --bind /dev $ARCH_TRAVIS_CHROOT/dev
sudo mount --bind /dev/pts $ARCH_TRAVIS_CHROOT/dev/pts
sudo mount --bind /dev/shm $ARCH_TRAVIS_CHROOT/dev/shm
sudo mount --bind /run $ARCH_TRAVIS_CHROOT/run
# update packages
chroot_as_root "pacman -Syy"
chroot_as_root "pacman -Syu ${default_packages[*]} --noconfirm"
# use LANG=en_US.UTF-8 as expected in travis environments
as_root "sed -i 's|#en_US.UTF-8|en_US.UTF-8|' $ARCH_TRAVIS_CHROOT/etc/locale.gen"
chroot_as_root "locale-gen"
# setup non-root user
chroot_as_root "useradd -u $uid -m -s /bin/bash $user"
# disable password for sudo users
as_root "echo \"$user ALL=(ALL) NOPASSWD: ALL\" >> $ARCH_TRAVIS_CHROOT/etc/sudoers.d/$user"
# mount HOME dirs
for d in $HOME/*/; do
if [ "$d" != "$ARCH_TRAVIS_CHROOT/" ]; then
dir="$user_home/$(basename "$d")"
chroot_as_root "mkdir -p $dir && chown $user $dir"
sudo mount --bind "$d" "$ARCH_TRAVIS_CHROOT$dir"
fi
done
# add custom repos
add_repositories
# setup pacaur for AUR packages
setup_pacaur
}
# add custom repositories to pacman.conf
add_repositories() {
if [ ${#CONFIG_REPOS[@]} -gt 0 ]; then
for r in "${CONFIG_REPOS[@]}"; do
local splitarr=(${r//=/ })
((repo_line+=1))
as_root "sed -i '${repo_line}i[${splitarr[0]}]' $ARCH_TRAVIS_CHROOT/etc/pacman.conf"
((repo_line+=1))
as_root "sed -i '${repo_line}iServer = ${splitarr[1]}\n' $ARCH_TRAVIS_CHROOT/etc/pacman.conf"
((repo_line+=1))
done
# update repos
chroot_as_root "pacman -Syy"
fi
}
# a wrapper which can be used to eventually add fakeroot support.
sudo_wrapper() {
sudo "$@"
}
# run command as normal user
as_normal() {
local cmd="$@"
run /bin/bash -c "$cmd"
}
# run command as root
as_root() {
local cmd="$@"
run sudo_wrapper /bin/bash -c "$cmd"
}
# run command in chroot as root
chroot_as_root() {
local cmd="$@"
run sudo_wrapper setarch $ARCH_TRAVIS_ARCH chroot \
$ARCH_TRAVIS_CHROOT /bin/bash -c "$cmd"
}
# execute command in chroot as normal user
_chroot_as_normal() {
local cmd="$@"
sudo_wrapper setarch $ARCH_TRAVIS_ARCH chroot \
--userspec=$uid:$uid $ARCH_TRAVIS_CHROOT /bin/bash \
-c "cd $user_build_dir && $cmd"
}
# run command in chroot as normal user
chroot_as_normal() {
local cmd="$@"
run _chroot_as_normal "$cmd"
}
# run command
run() {
"$@"
local ret=$?
if [ $ret -gt 0 ]; then
takedown_chroot
exit $ret
fi
}
# run build script
run_build_script() {
local cmd="$@"
echo "$ $cmd"
_chroot_as_normal "$cmd"
local ret=$?
if [ $ret -gt 0 ]; then
takedown_chroot
exit $ret
fi
}
# setup pacaur
setup_pacaur() {
# Check if pacaur is available in the added repos
if _chroot_as_normal "pacman -Si pacaur &> /dev/null"; then
chroot_as_root "pacman -S --noconfirm pacaur"
else
local cowerarchive="cower.tar.gz"
local aururl="https://aur.archlinux.org/cgit/aur.git/snapshot/"
# install cower
as_normal "curl -O $aururl/$cowerarchive"
as_normal "tar xf $cowerarchive"
chroot_as_normal "cd cower && makepkg -is --skippgpcheck --noconfirm"
as_root "rm -r cower"
as_normal "rm $cowerarchive"
# install pacaur
chroot_as_normal "cower -dd pacaur"
chroot_as_normal "cd pacaur && makepkg -is --noconfirm"
chroot_as_normal "rm -rf pacaur"
fi
}
# install package through pacaur
_pacaur() {
local pacaur="pacaur -S $@ --noconfirm --noedit"
chroot_as_normal "$pacaur"
}
# takedown chroot
# unmounts anything mounted in the chroot setup
takedown_chroot() {
sudo umount $ARCH_TRAVIS_CHROOT/{run,dev/shm,dev/pts,dev,sys,proc}
# umount HOME dirs
for d in $HOME/*/; do
if [ "$d" != "$ARCH_TRAVIS_CHROOT/" ]; then
sudo umount "$ARCH_TRAVIS_CHROOT$user_home/$(basename "$d")"
fi
done
sudo umount $ARCH_TRAVIS_CHROOT
if [ -n "$ARCH_TRAVIS_CLEAN_CHROOT" ]; then
as_root "rm -rf $ARCH_TRAVIS_CHROOT"
fi
}
# read value from .travis.yml
travis_yml() {
ruby -ryaml -e 'puts ARGV[1..-1].inject(YAML.load(File.read(ARGV[0]))) {|acc, key| acc[key] }' .travis.yml $@
}
read_config() {
local old_ifs=$IFS
IFS=$'\n'
CONFIG_BUILD_SCRIPTS=($(travis_yml arch script))
CONFIG_PACKAGES=($(travis_yml arch packages))
CONFIG_REPOS=($(travis_yml arch repos))
IFS=$old_ifs
}
# run build scripts defined in .travis.yml
build_scripts() {
if [ ${#CONFIG_BUILD_SCRIPTS[@]} -gt 0 ]; then
for script in "${CONFIG_BUILD_SCRIPTS[@]}"; do
run_build_script $script
done
else
echo "No build scripts defined"
takedown_chroot
exit 1
fi
}
# install packages defined in .travis.yml
install_packages() {
_pacaur "${CONFIG_PACKAGES[@]}"
}
# install custom compiler if CC != gcc
install_c_compiler() {
if [ "$TRAVIS_CC" != "gcc" ]; then
_pacaur "$TRAVIS_CC"
fi
}
arch_msg() {
lightblue='\033[1;34m'
reset='\e[0m'
echo -e "${lightblue}$@${reset}"
}
# read .travis.yml
read_config
echo "travis_fold:start:arch_travis"
setup_chroot
install_packages
if [ -n "$CC" ]; then
install_c_compiler
# restore CC
CC=$TRAVIS_CC
fi
echo "travis_fold:end:arch_travis"
echo ""
arch_msg "Running travis build"
build_scripts
takedown_chroot
# vim:set ts=2 sw=2 et: