OILS / test / alpine.sh View on Github | oils.pub

347 lines, 142 significant
1#!/usr/bin/env bash
2#
3# Make an Alpine Linux chroot and run Oils within it.
4#
5# Usage:
6# test/alpine.sh <function name>
7#
8# Use Cases:
9# - _chroot/alpine-oils-tar
10# Test if the oil tarball can be configured/compiled/installed inside a
11# minimal Linux distro. This is tested with EACH RELEASE.
12# - _chroot/alpine-oils-spec
13# Test how the spec tests run (gawk and zip deps)
14# - _chroot/alpine-distro-build
15# Test if OSH can run Alpine's own package manager and scripts (not done yet)
16#
17# Examples:
18#
19# 1. To make oils-tar env:
20#
21# $0 download
22# $0 extract-oils-tar
23# $0 setup-dns
24# $0 add-oils-tar-deps
25#
26# One of:
27# devtools/release-native.sh make-tar
28# devtools/release.sh py-tarball
29#
30# $0 copy-tar
31# $0 test-tar
32#
33# 1. To make a spec test env:
34#
35# $0 download
36# $0 extract-oils-spec
37# $0 setup-dns
38# $0 add-oils-spec-deps
39#
40# $0 copy-tar _chroot/alpine-oils-spec # TODO: copy arbitrary tarball
41# TODO: set up non-root user
42#
43# $0 make-oils-spec
44# $0 copy-oils-spec
45#
46# Now enter the chroot:
47#
48# test/alpine.sh interactive _chroot/alpine-oils-spec
49# bash # if you prefer, use bash inside
50#
51# cd src/
52# tar -x -z < oils-ref-$VERSION.tar.gz
53# cd oil-$VERSION/
54# ./configure && make && sudo ./install
55#
56# cd ~/src/oils-spec
57# tar -x < oils-spec.tar
58#
59# test/spec-alpine.sh all
60# test/spec-alpine.sh archive-results
61#
62# TODO: Automate this more with an arbitrary tarball.
63#
64# Now again OUTSIDE:
65#
66# test/alpine.sh copy-wwz
67# test/alpine.sh publish
68
69: ${LIB_OSH=stdlib/osh}
70source $LIB_OSH/bash-strict.sh
71source $LIB_OSH/task-five.sh
72
73readonly ROOTFS_URL='http://dl-cdn.alpinelinux.org/alpine/v3.11/releases/x86_64/alpine-minirootfs-3.11.3-x86_64.tar.gz'
74#readonly ROOTFS_URL='https://dl-cdn.alpinelinux.org/alpine/v3.22/releases/x86_64/alpine-minirootfs-3.22.1-x86_64.tar.gz'
75
76readonly CHROOT_OILS_TAR=_chroot/alpine-oils-tar
77readonly CHROOT_OILS_SPEC=_chroot/alpine-oils-spec
78readonly CHROOT_DISTRO_BUILD=_chroot/alpine-distro-build
79
80download() {
81 wget --no-clobber --directory _tmp $ROOTFS_URL
82}
83
84_extract() {
85 local dest=$1
86
87 local tarball=_tmp/$(basename $ROOTFS_URL)
88
89 mkdir -p $dest
90 # Must be run as root
91 tar --extract --gzip --verbose --directory $dest < $tarball
92
93 du --si -s $dest
94}
95
96extract-oils-tar() {
97 mkdir -p _chroot # should not be owned by root
98 sudo $0 _extract $CHROOT_OILS_TAR
99}
100
101extract-oils-spec() {
102 mkdir -p _chroot # should not be owned by root
103 sudo $0 _extract $CHROOT_OILS_SPEC;
104}
105
106# Without this, you can't 'su myusername'. It won't be able to execute bash.
107chmod-chroot() {
108 local dest=${1:-$CHROOT_OILS_TAR}
109 sudo chmod 755 $dest
110}
111
112# add DNS -- for package manager
113
114_setup-dns() {
115 local chroot_dir=${1:-$CHROOT_OILS_TAR}
116 cat >$chroot_dir/etc/resolv.conf <<EOF
117nameserver 8.8.8.8
118nameserver 8.8.4.4
119EOF
120}
121setup-dns() { sudo $0 _setup-dns "$@"; }
122
123#
124# Deps for different chroots
125#
126
127# 106 MiB as of 7/7/2017.
128add-oils-tar-deps() {
129 local chroot_dir=${1:-$CHROOT_OILS_TAR}
130 sudo chroot $chroot_dir /bin/sh <<EOF
131apk update
132apk add bash make gcc g++ musl-dev
133EOF
134}
135
136# Additions:
137# python2, gawk: to run spec tests
138# zip: for publishing it
139
140# 3/6/2020: 154 MiB
141add-oils-spec-deps() {
142 local chroot_dir=${1:-$CHROOT_OILS_SPEC}
143 sudo chroot $chroot_dir /bin/sh <<EOF
144apk update
145apk add bash make gcc musl-dev python2 gawk zip
146EOF
147}
148
149# alpine-sdk scripts are /bin/sh busybox scripts!
150# Executing busybox-1.26.2-r5.trigger
151# Executing ca-certificates-20161130-r2.trigger
152# OK: 195 MiB in 72 packages
153#
154# Hm they still have triggers...
155# 72 packages. bash/readline are installed!
156
157add-alpine-sdk() {
158 local chroot_dir=${1:-$CHROOT_DISTRO_BUILD}
159 sudo chroot $chroot_dir /bin/sh <<EOF
160apk update
161apk add bash alpine-sdk
162EOF
163}
164
165#
166# Admin
167#
168
169list-packages() {
170 local chroot_dir=${1:-$CHROOT_DISTRO_BUILD}
171 sudo chroot $chroot_dir apk info
172}
173
174destroy-chroot() {
175 local chroot_dir=${1:-$CHROOT_OILS_TAR}
176 sudo rm -r -rf $chroot_dir
177}
178
179# Interactive /bin/sh.
180enter-chroot() {
181 local chroot_dir=${1:-$CHROOT_OILS_TAR}
182 shift
183 sudo chroot $chroot_dir "$@"
184}
185
186interactive() {
187 local chroot_dir=${1:-$CHROOT_OILS_TAR}
188 enter-chroot $chroot_dir /bin/sh
189}
190
191#
192# oils-tar functions
193#
194
195readonly OILS_VERSION=$(head -n 1 oils-version.txt)
196
197_copy-tar() {
198 local chroot_dir=${1:-$CHROOT_OILS_TAR}
199 local name=${2:-oils-for-unix}
200 local version=${3:-$OILS_VERSION}
201
202 local dest=$chroot_dir/src
203 rm -r -f $dest # make sure it's empty
204 mkdir -p $dest
205 cp -v _release/$name-$version.tar.gz $dest
206}
207
208copy-tar() {
209 sudo $0 _copy-tar "$@"
210}
211
212_test-tar() {
213 local chroot_dir=${1:-$CHROOT_OILS_TAR}
214 local name=${2:-oils-for-unix}
215 local version=${3:-$OILS_VERSION}
216 local target=_bin/${name}.ovm
217
218 # TODO:
219 # - Run soil/cpp-tarball.sh build-static
220 # - Then publish this musl build!
221
222 enter-chroot "$chroot_dir" /bin/sh -c '
223set -e
224
225name=$1
226version=$2
227target=$3
228
229cd src
230tar --extract -z < $name-$version.tar.gz
231cd $name-$version
232./configure
233
234# Build the tar
235if test $name = oils-ref; then
236 time make $target
237 $target --version
238else
239 _build/oils.sh --skip-rebuild
240 _bin/cxx-opt-sh/osh --version
241
242 build/static-oils.sh
243 _bin/cxx-opt-sh/osh-static --version
244fi
245
246./install
247echo
248echo "*** Running osh"
249
250osh --version
251echo status=$?
252echo
253
254ldd $(which osh)
255
256echo DONE
257' dummy "$name" "$version" "$target"
258}
259
260test-tar() {
261 sudo $0 _test-tar "$@"
262}
263
264copy-static() {
265 local chroot_dir=${1:-$CHROOT_OILS_TAR}
266 local dir=_tmp/musl-libc
267 mkdir -p $dir
268 cp -v \
269 $CHROOT_OILS_TAR/src/oils-for-unix-$OILS_VERSION/_bin/cxx-opt-sh/oils-for-unix-static* \
270 $dir
271 ls -l --si $dir
272}
273
274build-static-musl() {
275 copy-tar
276 test-tar
277 copy-static
278}
279
280#
281# cpp tarball
282#
283
284copy-cpp-tar() {
285 copy-tar '' oils-for-unix
286}
287
288test-cpp-tar() {
289 test-tar '' oils-for-unix
290}
291
292#
293# oils-spec functions
294#
295
296# Spec tests
297make-oils-spec() {
298 # TODO: maybe get rid of doctools
299 # test/spec.sh is just for reference
300 # web/*.css dir because we want the end user to be able to see it
301 find \
302 benchmarks/time_.py \
303 test/sh_spec.py doctools/{html_head,doc_html,__init__}.py \
304 test/{common,spec-common,spec,spec-alpine,spec-runner}.sh \
305 spec/ \
306 web/*.css \
307 -type f \
308 | xargs tar --create > _tmp/oils-spec.tar
309}
310
311_copy-oils-spec() {
312 local dest=$CHROOT_OILS_SPEC/src/oils-spec
313 mkdir -p $dest
314 cp -v _tmp/oils-spec.tar $dest
315}
316copy-oils-spec() { sudo $0 _copy-oils-spec "$@"; }
317
318
319copy-wwz() {
320 ### Take results out of chroot
321
322 local out=_tmp/spec-results
323 mkdir -p $out
324 cp -v _chroot/alpine-oils-spec/src/oils-spec/*.wwz $out
325 ls -l $out
326}
327
328publish() {
329 ### Publish results to oilshell.org/spec-results
330 # similar to web/publish.sh
331
332 local user=$1
333 local host=$user.org
334
335 local path=$2
336
337 local dest='oilshell.org/spec-results'
338 ssh $user@$host mkdir --verbose -p $dest
339 scp $path $user@$host:$dest
340
341 echo "Visit http://$dest/$(basename $path)/"
342}
343
344name=$(basename $0)
345if test "$name" = 'alpine.sh'; then
346 task-five "$@"
347fi